Repository navigation
Add mHC precision controls, mean output contract, and full activation recompute with mHC - #7521
Conversation
f91dd5b to
47d11c6
Compare
Phlip79
left a comment
There was a problem hiding this comment.
Can you add some tests with/without mhc_learned_output_contract?
Thank you @Phlip79 ! I added coverage with mhc_learned_output_contract enabled and disabled, including outputs, gradients, checkpoint state, and full recomputation. Please let me know if there are any other comments, thanks! |
ea8718f to
9004813
Compare
|
/ok to test 9004813 |
|
@Phlip79 Your last commit introduced some merge conflicts, would you mind retriggering the CI again? Thanks! |
|
/ok to test d35a58d |
Sorry about that, thanks for your quick commits! |
|
/ok to test 106db94 |
kevalmorabia97
left a comment
There was a problem hiding this comment.
LGTM from the Model Optimizer side. Tested 106db944 (with #7054 merged in) and NVIDIA-NeMo/Megatron-Bridge#6044 in a NeMo 26.10 container on GB300: ModelOpt GPTQ PTQ, distillation with save + resume, and quantized HF export on a 4-layer GLM-5.3-Flash, plus full-size GLM-5.3-Flash QAD on 32 nodes (EP32 x CP4, packed SFT) with distributed-optimizer save + resume, matching the loss and grad norms of our earlier full PTQ + QAD + vLLM eval study on older heads of this stack.
NVIDIA-NeMo/Megatron-Bridge#6044 and our GLM-5.3-Flash support (NVIDIA/Model-Optimizer#2539) are waiting on this PR, so we'd appreciate a merge when possible. Thanks @HollowMan6!
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40308355574 |
|
The merge-queue run failed because of this PR's changes, not flaky tests. The numbers changed in the way the PR intends, but the golden values weren't updated (the PR doesn't touch any Why it's this PR: the merge-group commit is
Suggested fix: update |
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com> Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Philip Petrakian <ppetrakian@nvidia.com>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
|
Thank you @kevalmorabia97 ! I've updated the golden values, would you help with retriggering the CI and the merge queue? Thanks! cc: @Phlip79 @guihong-nv @dimapihtar |
|
/ok to test 0741315 |
kevalmorabia97
left a comment
There was a problem hiding this comment.
Checked 0741315: the three updated golden files match the values from merge-queue run 40308355574 exactly, and the other 15 commits are unchanged from 106db944 after the rebase. LGTM, ready to requeue.
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/40601244978 |
What does this PR do?
Add the mHC (Manifold-Constrained Hyper-Connections) numerics used by GLM-5.3-Flash as opt-in
TransformerConfigflags, and support full activation recompute with mHC-wrapped hybrid layers.Split out of #7054 (GLM-5.3-Flash support) as discussed there; #7054 now carries only the KDA layers. Sibling PRs: #7522 (KPool DSA indexer) and #7523 (blockwise FP8 wgrad guard). All are independent of each other and based on
main.Changes
mhc_norm_eps_inside_sqrt: compute the mapping norm asrsqrt(mean(x²) + layernorm_epsilon)instead of‖x‖ / √K + 1e-6.mhc_keep_mappings_in_fp32: keeph_pre/h_post/h_resand the residual-stream mixing in FP32 until the final cast to the activation dtype.mhc_learned_output_contract(defaultTrue, the current behaviour): withFalse, the residual streams are contracted by their mean instead of the learnedhc_head_*weights, in both theHybridStackpost-process and the MTP layer (GLM-5.3-Flash has no learned head).HyperConnectionHybridLayersetssupports_hybrid_recompute_kwargs,recompute.pyforwards the matching kwargs (includingpacked_sequence_cp_metadata) without importinghybrid_block, and theNotImplementedErrorinTransformerConfig.__post_init__is removed.use_fused_mhcraisesValueErrortogether with either new precision flag (the fused kernels do not implement them).All new flags default to the current behaviour. GLM-5.3-Flash uses mHC with 4 residual streams and 20 Sinkhorn iterations and has no learned output head, i.e.
mhc_norm_eps_inside_sqrt=True,mhc_keep_mappings_in_fp32=True,mhc_learned_output_contract=False.Motivation
GLM-5.3-Flash's reference implementation computes the mHC mapping norm with the epsilon inside the square root, keeps the mappings and the stream mixing in FP32, and contracts the residual streams by their mean. Matching this is needed for the Megatron model to reproduce the released checkpoint's numerics. Full recompute with mHC removes the
NotImplementedErrorthat previously blocked full activation recompute for mHC models.Testing
tests/unit_tests/transformer/test_mhc_precision.py(new): mappings, aggregation and mixing against a pure-torch reference in both precision modes; fused mHC rejects the new flags.tests/unit_tests/models/test_hybrid_mhc.py: full recompute (uniform / block × fp32 / bf16 + FP32 mixing / bf16 fused) matches forward and backward without recompute; a wrapped residual layer does not double-count its input.tests/unit_tests/transformer/test_hyper_connection_recompute.py: drop the obsolete "mHC + full recompute is rejected" case.pytest tests/unit_tests/transformer/test_mhc_precision.py \ tests/unit_tests/models/test_hybrid_mhc.py \ tests/unit_tests/transformer/test_hyper_connection_recompute.pyIssue tracking
For PRs from open-source community contributors:
Linked issue: Related to #6392
Contribution process
Pre-checks
jit_fuser/torch.compile, CUDA extension, TE or external-library dispatch, or a scatter/index accumulation), I have added or updated its bit-exact determinism test and registered it intests/unit_tests/determinism/kernels/manifest.py(guide)Code review
Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.