Skip to content

Commit 61682ab

Browse files
linmuchuiyangcuichenxsajadnmeatybobby
authored
fix(model): keep GLM-5 export max_position_embeddings from the reference config (#5996)
Signed-off-by: linmuchuiyang <6169320+linmuchuiyang@users.noreply.github.com> Signed-off-by: Chen Cui <chcui@nvidia.com> Co-authored-by: linmuchuiyang <6169320+linmuchuiyang@users.noreply.github.com> Co-authored-by: Chen Cui <chcui@nvidia.com> Co-authored-by: Sajad Norouzi <snorouzi@nvidia.com> Co-authored-by: meatybobby <meatybobby@gmail.com>
1 parent 726463e commit 61682ab

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/megatron/bridge/models/glm_moe_dsa/glm5_bridge.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ def provider_bridge(self, hf_pretrained: PreTrainedCausalLM) -> MLAModelProvider
186186

187187
return provider
188188

189+
@classmethod
190+
def megatron_to_hf_config(cls, provider: MLAModelProvider) -> dict:
191+
"""Convert a GLM-5 provider to an HF config dict without leaking training-only context length.
192+
193+
The generic ``CONFIG_MAPPING`` maps HF ``max_position_embeddings`` from Megatron ``seq_length``. For a
194+
fine-tuned checkpoint that is the *training* sequence length (e.g. 8192), not the model's context
195+
capability (1048576 for ``zai-org/GLM-5.2``), and ``conform_config_to_reference`` keeps the
196+
Megatron-derived value because the key exists in the reference config. Drop it here so the reference
197+
HF config supplies it; when no reference config is available the key is simply absent and transformers
198+
falls back to the ``GlmMoeDsaConfig`` default.
199+
"""
200+
hf_config = super().megatron_to_hf_config(provider)
201+
hf_config.pop("max_position_embeddings", None)
202+
return hf_config
203+
189204
def generate_pipeline_layout(self, num_layers: int, pp: int, mtp_layers: int = 0) -> list[list[str]] | None:
190205
"""Generate a conversion pipeline layout that keeps DSA top-k sharing groups on one stage.
191206

‎tests/unit_tests/models/glm_moe_dsa/test_glm5_bridge.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ def test_megatron_config_export_keeps_generic_moe_aliases() -> None:
156156
assert mapped_config["n_routed_experts"] == 8
157157

158158

159+
def test_megatron_config_export_does_not_emit_training_seq_length_as_max_position_embeddings() -> None:
160+
"""``seq_length`` is the fine-tuning context, not the model's context capability; leave it to the reference."""
161+
mapped_config = GLM5Bridge.megatron_to_hf_config(SimpleNamespace(num_moe_experts=8, seq_length=8192))
162+
163+
assert "max_position_embeddings" not in mapped_config
164+
assert mapped_config["num_experts"] == 8
165+
166+
167+
def test_megatron_config_export_reference_supplies_max_position_embeddings() -> None:
168+
"""With a reference config, the exported value must be the reference one (1048576 for GLM-5.2), not seq_length."""
169+
from megatron.bridge.models.conversion.utils import conform_config_to_reference
170+
171+
mapped_config = GLM5Bridge.megatron_to_hf_config(SimpleNamespace(num_moe_experts=256, seq_length=8192))
172+
reference = {"max_position_embeddings": 1048576, "n_routed_experts": 256}
173+
conformed = conform_config_to_reference(mapped_config, reference)
174+
175+
assert conformed["max_position_embeddings"] == 1048576
176+
177+
159178
@pytest.mark.parametrize(
160179
("config_overrides", "expected_topk_freq", "expected_skip_topk_offset"),
161180
[

0 commit comments

Comments
 (0)