Skip to content

feat(model): add GLM-5.3-Flash support - #6044

Draft
HollowMan6 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
HollowMan6:glm53flash
Draft

HollowMan6 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
HollowMan6:glm53flash

Conversation

@HollowMan6

Copy link
Copy Markdown
Contributor

Pending on:

What does this PR do ?

Add HF ↔ Megatron support for GLM-5.3-Flash (model_type glm5_next, Glm5NextForConditionalGeneration): a unified bridge, provider, and VLM wrapper for its hybrid KDA/DSA + MoE + mHC language model with a replicated HF vision tower, plus the shared conversion, PEFT, and packed-sequence pieces it needs.

Changelog

GLM-5.3-Flash (src/megatron/bridge/models/glm5next/)

  • Add GLM53FlashBridge, GLM53FlashModelProvider, and GLM53FlashModel, registered for Glm5NextForConditionalGeneration with model_type="glm5_next" and exported from megatron.bridge.models.
  • Build hybrid_layer_pattern from the HF layer_types / mlp_layer_types: each HF decoder layer becomes two physical layers (KDA K or DSA D attention at 2N, dense - or MoE E FFN at 2N+1), with moe_layer_freq derived per physical module and num_hidden_layers recovered from the pattern on export.
  • Configure MLA NoPE geometry (qk_pos_emb_head_dim=0, kv_channels=v_head_dim), the kpool DSA indexer (FP8 kpool; index_kpool / index_kpool_always_select_tail read from the HF config), KDA two-stage low-rank gates with gate_lower_bound, mHC multi-stream hyper-connections with fp32 mappings, sigmoid routing with expert bias and an always-on shared expert (moe_shared_expert_gate=False), grouped GEMM with the HybridEP flex dispatcher, and a one-layer DE MTP using the repeated-layer spec.
  • Weight mappings: fused KDA in_proj / conv1d built from separate HF Q/K/V tensors with section-wise TP sharding (_ColumnParallelConcatMapping), MLA projections and indexer weights, dense and MoE FFN (grouped-GEMM and sequential expert layouts), mHC hc_{attn,ffn}_{fn,base,scale}, and the MTP eh_proj split into e_proj / h_proj (_EhProjSplitMapping, import only).
  • Dequantize blockwise FP8 checkpoint weights on import; keep Megatron init for the MTP inner-layer hyper-connection params, which have no HF source.
  • VLM wrapper: replicated HF Glm5NextVisionModel on pre-process ranks, image/video features scattered into the embeddings before THD packing and the sequence-parallel scatter, text-only fallback when no pixel inputs are given, and freeze_{language_model,vision_model,vision_projection} options.

Hybrid providers

  • Add HybridMLAModelProvider (HybridModelProvider + MLATransformerConfig). With pipeline_model_parallel_size > 1 and DSA layers in the pattern, it inserts balanced | separators only before full-indexer DSA layers so IndexShare groups never cross a stage, validates user-provided separators, and rejects num_layers_in_first/last_pipeline_stage.
  • Add scatter_embedding_sequence_parallel to HybridModelProvider (default True, matching HybridModel) and pass it through; the VLM provider sets it to False so vision features are injected before the SP scatter.

Shared conversion infrastructure

  • Add HCAlphaMapping, mapping an HF [3] hc_*_scale tensor to Megatron's alpha_pre / alpha_post / alpha_res (index 0 exports all three), and migrate DeepSeekV4Bridge from its private _HCAlphaMapping / _HCAlphaSecondaryMapping to it with equivalent import/export behavior.
  • Add InitOnlyMapping for Megatron params with no HF counterpart (import keeps init, export is a no-op).
  • Add MegatronModelBridge._prefix_mapping_registry to re-key a text-model registry under a VLM prefix while keeping nested _tp_mapping names in sync.

PEFT

  • Split fused KDA in_proj LoRA linear_out weights by Q/K/V section sizes when kda_two_stage_gates is set, instead of the GQA-style split_qkv_weights path, so KDA LoRA adapters export correctly.

Packed sequences

  • Move preprocess_packed_seqs from models/qwen_vl/modelling_qwen3_vl/utils.py to training/utils/packed_seq_utils.py (shared by Qwen3-VL and GLM-5.3-Flash) and add use_fp8_padding, which aligns each sequence to lcm(16, align) and the packed total to align * 128.

Tests

  • tests/unit_tests/models/glm5next/test_glm53flash_mapping.py: provider precision contract and multimodal config copy, language_model. prefixing of text mappings, HCAlphaMapping import/export and wildcard resolution, section-wise KDA TP sharding round-trips (TP 1 to 16).
  • tests/unit_tests/models/qwen_vl/modelling_qwen3_vl/test_utils.py: packed-sequence alignment and gradient flow with and without FP8 padding.
  • tests/unit_tests/models/test_model_bridge_lora.py: KDA Q/K/V LoRA linear_out split.

GitHub Actions CI

See the CI section in the Contributing doc for how to trigger the CI. A Nvidia developer will need to approve and trigger the CI for external contributors.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation? (no model page yet; see follow-ups below)
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

Glm5NextForConditionalGeneration is imported behind a try/except that falls back to the class name, Glm5NextModel / Glm5NextVisionModel are imported lazily inside GLM53FlashModel.__init__, and the new unit tests use pytest.importorskip on the GLM-5.3-Flash config module.

Additional Information

  • No linked issue.
  • Dependency: transformers. The glm5_next model family ships in transformers v5.17.0; pyproject.toml currently pins transformers<=5.15.0, so on the pinned version the bridge only resolves by class name, the VLM wrapper cannot be instantiated, and the new unit tests are skipped. The pin bump should go in a separate build: PR per the contributing guide.
  • preprocess_packed_seqs moved without a compatibility re-export from its old Qwen3-VL module.
  • Follow-ups not in this PR: docs/models/glm/ page and README / docs model tables, recipes and examples, verification card, and a functional HF ↔ Megatron round-trip test.

Signed-off-by: Hollow Man <hollowman@opensuse.org>
@copy-pr-bot

copy-pr-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant