Skip to content

[Fix] GLM-5.3-Flash F4.1: Correct mHC precision and replica gradient reduction - #2156

Merged
jayhenry merged 3 commits into
InternLM:feat/glm53flash-f4-mhcfrom
bingshao333:fix/glm53flash-f4-mhc-correctness-f41
Oct 12, 2026
Merged

jayhenry merged 3 commits into
InternLM:feat/glm53flash-f4-mhcfrom
bingshao333:fix/glm53flash-f4-mhc-correctness-f41

Conversation

@bingshao333

Copy link
Copy Markdown
Contributor

This F4.1 follow-up aligns mHC numerical behavior with Hugging Face and fixes gradient synchronization for FSDP-ignored non-expert parameters when EP > 1 or expert TP (ETP) > 1.

Target: feat/glm53flash-f4-mhc. Related: #2107 and #2136.

Changes and impact

  • mHC numerical correctness: Keep normalized streams and the hc_fn projection in FP32, avoiding the precision loss from BF16 projection, and return the collapsed stream in the original activation dtype. Add HF forward/backward comparisons with FP32 and BF16 projection weights, plus configured RMS normalization epsilon coverage at all four Dense/MoE attention/FFN call sites. The epsilon wiring is already fixed in the target branch; this PR adds its regression coverage.
  • Replica gradient synchronization: FSDP-ignored parameters can retain an EP/ETP Replicate submesh that omits their FSDP replicas. For fully replicated non-expert parameters whose mesh is smaller than the model mesh, average gradients over the complete replica group. This shared MoE fix also covers affected ignored parameters beyond mHC. Add regression coverage for replica-mean gradients and two SGD updates across EP/SP token partitions, preserving ordinary sharded and expert gradient behavior.

Validation

  • Earlier GPU checks: configured epsilon coverage passed 12/12 cases across the four Dense/MoE attention/FFN sites; FP32 forward/backward comparisons against Automodel using checkpoint mHC weights passed 20/20 cases.
  • 8×H200 reproduction and control: With randomly initialized GLM Dense+MoE layers, FSDP2 × EP4, and three SGD steps, the original reduction produced a maximum replica parameter difference of 5.19216e-4. A world-replicate control reduced it to 0, with gradients matching the global mean. That control changed parameter meshes; this PR changes the reduction group.
  • 10 CPU tests passed, using PyTorch 2.9.1 and Transformers 5.17.0. Both new CPU precision regressions reproduce the F4-base mismatch and pass with this fix.
  • Ruff lint/format, diff checks, and changed-file pre-commit hooks passed, including mypy.

Copilot AI balanced review requested due to automatic review settings October 10, 2026 14:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@jayhenry
jayhenry force-pushed the feat/glm53flash-f4-mhc branch from 6841448 to b245bb8 Compare October 10, 2026 19:01
Select the F4 mHC primitive and regression-test changes from
JT-Ushio/xtuner-muonsplit-verify commit
a8d798a.

Preserve the original author and add type annotations to the new test.
Select the decoder regression from JT-Ushio/xtuner-muonsplit-verify
commit 7665f35.

The production epsilon wiring is already present in F4 commit
6841448.
@bingshao333
bingshao333 force-pushed the fix/glm53flash-f4-mhc-correctness-f41 branch from dea0246 to 6f3b85a Compare October 12, 2026 03:35
# process group covers every rank across those dimensions, allowing
# a single all_reduce regardless of how many Replicate dims exist.
if len(replicate_dim_names) > 1:
replica_mesh = self.world_mesh

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更好地修复方法见 https://github.com/InternLM/xtuner/pull/2157/changes#diff-d67fb488dc8f8ed52b7ee18b4f1e52f870d1cdff28d38a406b2bea0b4c70bb97R683
这里可以做判断,如果出现第一个分支则报错

@jayhenry
jayhenry merged commit f795260 into InternLM:feat/glm53flash-f4-mhc Oct 12, 2026
0 of 2 checks passed
JT-Ushio added a commit to JT-Ushio/xtuner-muonsplit-verify that referenced this pull request Oct 13, 2026
…reduction (InternLM#2156)

* [Fix] Match HF precision for GLM5.3 mHC pre-mixing

Select the F4 mHC primitive and regression-test changes from
JT-Ushio/xtuner-muonsplit-verify commit
a8d798a.

Preserve the original author and add type annotations to the new test.

* [Fix] Reduce FSDP-ignored gradients across all model replicas

(cherry picked from commit 46e207b)

* [Test] Cover GLM5.3 mHC normalization epsilon

Select the decoder regression from JT-Ushio/xtuner-muonsplit-verify
commit 7665f35.

The production epsilon wiring is already present in F4 commit
6841448.

---------

Co-authored-by: Tao Ji <taoji@fudan.edu.cn>
ShilohYu pushed a commit to ShilohYu/xtuner that referenced this pull request Oct 15, 2026
…reduction (InternLM#2156)

* [Fix] Match HF precision for GLM5.3 mHC pre-mixing

Select the F4 mHC primitive and regression-test changes from
JT-Ushio/xtuner-muonsplit-verify commit
a8d798a.

Preserve the original author and add type annotations to the new test.

* [Fix] Reduce FSDP-ignored gradients across all model replicas

(cherry picked from commit 46e207b)

* [Test] Cover GLM5.3 mHC normalization epsilon

Select the decoder regression from JT-Ushio/xtuner-muonsplit-verify
commit 7665f35.

The production epsilon wiring is already present in F4 commit
6841448.

---------

Co-authored-by: Tao Ji <taoji@fudan.edu.cn>
jayhenry added a commit that referenced this pull request Oct 15, 2026
* [Feature] Add GLM-5.3-Flash F4: mHC four-stream residual

Adds xtuner/v1/module/decoder_layer/mhc.py (MHCConfig / hc_split_sinkhorn /
hc_pre / hc_post / unshard_hc_params) and xtuner/v1/ops/hc_post.py (fused
Triton hc_post_fused), following doc/xtuner_glm5p3flash_design.md F4. The
core math is ported from xtuner's dsv4 branch (DeepSeek-V4's Hyper-
Connections, commit 01c31a8, not merged into this branch) into the
model-agnostic public location the design doc calls for, dropping V4's
XTUNER_V4_HF_PARITY global toggle (the default bf16-fast path already
degrades to HF-exact math under fp32 inputs, which the tests use as the
parity anchor) and the unported 721-line TileKernels backend (left as a
documented NotImplementedError gap rather than a blind port with no
hardware to validate it against).

Adds xtuner/v1/model/moe/glm53/decoder_layer.py: Glm53DenseDecoderLayer
(overrides DenseDecoderLayer._forward) and Glm53MoEDecoderLayer, which only
overrides MoEDecoderLayer's _pre_moe_forward/_post_moe_forward seams so the
~400-line EP/dispatcher/domino-micro-batch pipeline stays untouched; the
mHC residual rides through those methods as an opaque _MHCResidual payload
in place of the base class's plain Tensor. mhc_cfg=None degrades both
layers to the base class's ordinary residual math unchanged, for reuse by
the MTP layer in F6.

Also fixes a real bug surfaced while testing: KDA's dt_bias/A_log were left
torch.empty-uninitialized in xtuner/v1/module/attention/kda.py; now
initialized to match HF's Glm5NextTextForgetGate _init_weights policy
(A_log zeroed when a safe gate lower bound is set, dt_bias log-uniform).

Verified with 26 tests across tests/model/test_glm53_mhc.py (Sinkhorn
math + exact HF parity in fp32, bf16 parity within ULP tolerance),
tests/ops/test_hc_post.py (fused kernel vs eager reference, forward/
backward/compile), and tests/model/test_glm53_decoder_layer.py (both
decoder layers, mhc_cfg=None passthrough equivalence, gradient flow).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* [Fix] Pass the text RMS epsilon into mHC hc_pre

HF normalizes the flattened mHC streams with the text-config RMS epsilon,
not the hyper-connection eps. The hc_pre norm_eps parameter existed but all
four call sites relied on the 1e-6 default, so checkpoint gradients drifted
from HF. Pass input/post-attention layernorm variance_epsilon at each site;
the five-layer crop gradient oracle now agrees with HF within 5%.

* [Test] Keep F4 decoder checks within supported compile scope

The build-only MoE test compiled shared decoder class methods globally and made later GLM-5.3 cases fail by test order. Disable compilation for that constructor check. Move the dynamic cu_seqlens compile case to F6, where the KDA custom-op boundary is implemented.

* [Fix] GLM-5.3-Flash F4.1: Correct mHC precision and replica gradient reduction (#2156)

* [Fix] Match HF precision for GLM5.3 mHC pre-mixing

Select the F4 mHC primitive and regression-test changes from
JT-Ushio/xtuner-muonsplit-verify commit
a8d798a.

Preserve the original author and add type annotations to the new test.

* [Fix] Reduce FSDP-ignored gradients across all model replicas

(cherry picked from commit 46e207b)

* [Test] Cover GLM5.3 mHC normalization epsilon

Select the decoder regression from JT-Ushio/xtuner-muonsplit-verify
commit 7665f35.

The production epsilon wiring is already present in F4 commit
6841448.

---------

Co-authored-by: Tao Ji <taoji@fudan.edu.cn>

* [Fix] Move mHC and projection precision fixes into F4

Co-authored-by: Kuang <128125671+Knlife@users.noreply.github.com>
Co-authored-by: Tao Ji <taoji@fudan.edu.cn>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Bing Shao <binggshao@outlook.com>
Co-authored-by: Tao Ji <taoji@fudan.edu.cn>
Co-authored-by: Kuang <128125671+Knlife@users.noreply.github.com>
ShilohYu pushed a commit to ShilohYu/xtuner that referenced this pull request Oct 15, 2026
…reduction (InternLM#2156)

* [Fix] Match HF precision for GLM5.3 mHC pre-mixing

Select the F4 mHC primitive and regression-test changes from
JT-Ushio/xtuner-muonsplit-verify commit
a8d798a.

Preserve the original author and add type annotations to the new test.

* [Fix] Reduce FSDP-ignored gradients across all model replicas

(cherry picked from commit 46e207b)

* [Test] Cover GLM5.3 mHC normalization epsilon

Select the decoder regression from JT-Ushio/xtuner-muonsplit-verify
commit 7665f35.

The production epsilon wiring is already present in F4 commit
6841448.

---------

Co-authored-by: Tao Ji <taoji@fudan.edu.cn>
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.

4 participants