Skip to content

[Feature] Add GLM-5.3-Flash F4: mHC four-stream residual - #2107

Merged
jayhenry merged 5 commits into
feat/glm53flash-f3-kdafrom
feat/glm53flash-f4-mhc
Oct 15, 2026
Merged

jayhenry merged 5 commits into
feat/glm53flash-f3-kdafrom
feat/glm53flash-f4-mhc

Conversation

@jayhenry

@jayhenry jayhenry commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Stack #2179 (bottom to top):

  1. [Fix] Preserve DeepEP layout events in synchronous dispatch #2177 fix/deepep-layout-event → main
  2. [Feature] Add GLM-5.3-Flash F0: 25B cropped reference checkpoint builder #2105 feat/glm53flash-materialize-full-f0 → fix/deepep-layout-event
  3. [Feature] Add GLM-5.3-Flash F3: Kimi Delta Attention (KDA) #2106 feat/glm53flash-f3-kda → feat/glm53flash-materialize-full-f0
  4. [Feature] Add GLM-5.3-Flash F4: mHC four-stream residual #2107 feat/glm53flash-f4-mhc → feat/glm53flash-f3-kda ← you are here
  5. [Feature] Add GLM-5.3-Flash F5: NoPE DSA + KPool indexer + clamped SwiGLU #2108 feat/glm53flash-f5-nope-dsa → feat/glm53flash-f4-mhc
  6. [Feature] Add GLM-5.3-Flash F1: VL data preprocessing pipeline #2109 feat/glm53flash-f1-vl-data → feat/glm53flash-f5-nope-dsa
  7. [Feature] Add GLM-5.3-Flash F2: vision tower + projector (eager) #2110 feat/glm53flash-f2-vision-tower → feat/glm53flash-f1-vl-data
  8. [Fix] Correct generic MTP future embeddings and empty-shard loss #2178 fix/mtp-local-future-embeddings → feat/glm53flash-f2-vision-tower
  9. [Feature] Add GLM-5.3-Flash F6 core: text model + MTP + compose model #2111 feat/glm53flash-f6-text-moe → fix/mtp-local-future-embeddings

Base is #2106's branch (layer 3). Review only this PR's own diff.


Summary

Stack layer 4/9 of GLM-5.3-Flash support (base: layer 3, F3 KDA).

The mHC projection precision, GLM53 decoder wiring and generic MoE router projection changes previously in F6 are now included in this layer.

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). The core math is ported from xtuner's dsv4 branch (DeepSeek-V4's Hyper-Connections) into a shared model-agnostic location, dropping V4's XTUNER_V4_HF_PARITY global toggle and the unported 721-line TileKernels backend (left as a documented NotImplementedError gap rather than a blind, unvalidated port).

Adds xtuner/v1/model/moe/glm53/decoder_layer.py: Glm53DenseDecoderLayer/Glm53MoEDecoderLayer, which only override the pre/post-forward seams so the existing EP/dispatcher/domino-micro-batch pipeline stays untouched — the mHC residual rides through as an opaque _MHCResidual payload in place of a plain Tensor. mhc_cfg=None degrades both layers to ordinary residual math unchanged, for reuse by the MTP layer later in the stack.

Also fixes a real bug surfaced while testing: KDA's dt_bias/A_log were left torch.empty-uninitialized; now initialized to match HF's Glm5NextTextForgetGate._init_weights policy.

Test Plan

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).

assert state == ForwardState.TRAINING, "mHC-wrapped GLM-5.3-Flash decoder layers only support SFT training"

# ---- attention site: hidden_states is [B, S, hc_mult, hidden_size]
fn, scale, base = _unshard_hc_site(self, "attn")

@jayhenry jayhenry Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

直接用 self.hc_attn_fn 等属性,不要这么隐晦地做

jayhenry and others added 5 commits October 15, 2026 04:35
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>
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%.
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.
…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>
Co-authored-by: Kuang <128125671+Knlife@users.noreply.github.com>
Co-authored-by: Tao Ji <taoji@fudan.edu.cn>
@jayhenry
jayhenry force-pushed the feat/glm53flash-f4-mhc branch from 677460a to b436ea3 Compare October 15, 2026 04:37
@jayhenry
jayhenry merged commit d97a289 into main Oct 15, 2026
9 of 14 checks passed
@jayhenry
jayhenry deleted the feat/glm53flash-f4-mhc branch October 15, 2026 05:28
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.

3 participants