Skip to content

[AMD] Let the GLM DSA NextN draft declare its own shared-expert fusion architecture - #41258

Merged
BBuf merged 5 commits into
sgl-project:mainfrom
xiaofei-zheng:feature/xiaofei/glm-nextn-shared-expert-fusion
Oct 9, 2026
Merged

BBuf merged 5 commits into
sgl-project:mainfrom
xiaofei-zheng:feature/xiaofei/glm-nextn-shared-expert-fusion

Conversation

@xiaofei-zheng

@xiaofei-zheng xiaofei-zheng commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

ModelConfig rewrites a GLM-5 draft's architecture to GlmMoeDsaForCausalLMNextN. The fusion gate compares that name with fused_shared_experts_architecture, which the class inherited as "DeepseekV3ForCausalLMNextN", so the draft was rejected even though it has the same 256 routed + 1 shared expert layout as the target model.

Modifications

Declare fused_shared_experts_architecture = "GlmMoeDsaForCausalLMNextN" on the draft class.

Keep fusion disabled whenever SGLANG_GLM_NEXTN_MOE_PTPC=1 casts the routed experts to per-channel FP8: the cast skips the shared expert, so its weight (BF16 or quantized) would land in the FP8 slot without a matching scale. On GLM-5.2-MXFP4's real layer-78 shared expert that zeroes 7-8% of the weights. GLM-5.3 checkpoints do not exclude MTP routed experts, so PTPC never applies there and the draft fuses.

Tests cover the architecture name, the loader-style gate call, the PTPC guard with the shared expert excluded or quantized, and both routed/shared checkpoint names.

Accuracy Tests

Initial GLM-5.3 MXFP4 validation of the architecture override loaded the draft weights and measured MTP accept length 3.56 versus 3.58 on base, with GSM8K unchanged. The final PTPC guard intentionally leaves the unsupported BF16-shared configuration unfused; a real checkpoint load was not rerun after adding that guard.

Local verification:

python3 -m pytest -n 4 \
  test/registered/unit/models/test_glm_nextn_moe_ptpc.py

python3 -m pytest -n 4 \
  test/registered/unit/models/test_shared_experts_fusion_gates.py

pre-commit run --files \
  python/sglang/srt/models/glm4_moe.py \
  test/registered/unit/models/test_glm_nextn_moe_ptpc.py \
  test/registered/unit/models/test_shared_experts_fusion_gates.py

Result: 58 tests passed. Two broader-suite failures reproduce on current main: the ROCm EP gate expectation and a local Transformers/Pixtral import mismatch.

Speed Tests and Profiling

Not isolated. Supported drafts now use the same fused shared-expert path as the target model; the PTPC + BF16-shared case remains unfused for correctness.

Checklist

  • Format code with pre-commit.
  • Add gate and PTPC regression tests.
  • Document the unsupported PTPC interaction in code.
  • Report validation and limitations.

CI States

Latest PR Test (Base): ❌ Run #37629578284
Latest PR Test (Extra): ❌ Run #37629578084
Latest PR Test (AMD ROCm 10): ❌ Run #37629578206

@xiaofei-zheng

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@BBuf BBuf added the run-ci CI: run the baseline test suite on this PR label Sep 30, 2026

@BBuf BBuf left a comment

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.

Reviewed 290787a616. The architecture override fixes the intended gate, but the newly enabled fusion exposes an interaction with SGLANG_GLM_NEXTN_MOE_PTPC=1; details are inline. Local validation: 41 fusion-gate tests and 2 subtests passed; the remaining Pixtral test failed on an unrelated Transformers import in my local environment. The 15 related PTPC/scaling tests passed, but do not cover the fused shared slot.

Added run-ci and reran Base + AMD CI. The Base rerun is now blocked by the required-base check, not the missing label: the branch needs to include 8854857a991e06b3ef9c4caeae34c94fc3588336. Please rebase onto current main and push again; see CI maintenance #21065. The AMD label gate passed; GPU results are still pending.

@@ -1459,6 +1459,9 @@ class GlmMoeDsaForCausalLM(DeepseekV2ForCausalLM):


class GlmMoeDsaForCausalLMNextN(DeepseekV3ForCausalLMNextN):
# ModelConfig rewrites a GLM draft's architecture to this class's own name,
# so the inherited DeepSeek name would never match what the gate compares.
fused_shared_experts_architecture = "GlmMoeDsaForCausalLMNextN"

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.

Could we handle SGLANG_GLM_NEXTN_MOE_PTPC=1 before enabling fusion for this draft? For a Quark checkpoint whose MTP routed/shared experts are excluded (BF16), the inherited gate now permits fusion, and _resolve_nextn_quant_config selects PTPC-FP8 for model.decoder.mlp.experts. However, _maybe_quant_glm_nextn_moe_to_ptpc only matches mlp.experts.<id>.*: mlp.shared_experts.* passes through as BF16 without weight scales. The inherited loader subsequently renames it to expert 256 and copies it into FP8 storage, leaving that slot's scales at their initial value of 1.

I reproduced this on CPU using the actual Quark gate, GLM weight iterator, Quark FP8 weight allocation and _load_w13 helper, with the device capability mocked to gfx95: the old architecture returns "Config does not support...", the new one returns None; the routed weight gets FP8 + a scale, while the shared weight stays BF16 with no scale. Shared weights of 1e-4 then become zero when loaded into the fused slot.

Please either keep fusion disabled for this PTPC path or apply the same per-channel conversion and scale emission to the shared expert, with a regression covering both the routed and shared checkpoint names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Handled in ba7f238ffb. I took the conservative option: when PTPC applies to the NextN routed experts and shared_experts is excluded (BF16), the fusion gate now returns a PTPC-specific disable reason. Routed PTPC conversion remains unchanged and the shared weight stays in its separate BF16 module. Added coverage for the gate plus both routed and shared checkpoint names.

@xiaofei-zheng
xiaofei-zheng force-pushed the feature/xiaofei/glm-nextn-shared-expert-fusion branch from 290787a to ba7f238 Compare October 1, 2026 04:00
@xiaofei-zheng

Copy link
Copy Markdown
Contributor Author

@BBuf thanks for finding this. Rebased onto current main; head is ba7f238ffb. The focused PTPC suite passes (11 tests), the broader fusion suites have 58 passes with the same two unrelated environment-specific failures noted in the description, and pre-commit passes. CI is rerunning now.

@xiaofei-zheng
xiaofei-zheng force-pushed the feature/xiaofei/glm-nextn-shared-expert-fusion branch from 8f84c90 to f324118 Compare October 1, 2026 14:30
@xiaofei-zheng

Copy link
Copy Markdown
Contributor Author

@BBuf could you please re-review the current head f324118b2c? The PTPC issue is fixed: fusion is disabled whenever the routed-expert PTPC cast applies, including when the shared expert is not excluded. Focused tests and real GLM checkpoint validation pass. The broad run has 109 passing checks; remaining failures are shared NPU/AMD infrastructure jobs.

@xiaofei-zheng

Copy link
Copy Markdown
Contributor Author

@BBuf gentle ping for re-review of e5786c68dd (latest main merged; your earlier comments are addressed above). CI has no PR-related failures: the Base jobs stopped at the health check on a GitHub API HttpError, before any test ran. The AMD stage-b failures match the latest scheduled main AMD run (https://github.com/sgl-project/sglang/actions/runs/37620899585). Could you rerun the failed Base jobs when you review? Thank you.

@BBuf BBuf left a comment

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.

The PTPC guard addresses my earlier comment: the shared expert stays out of the fused FP8 slot on that path. The PTPC regression tests pass locally.

@xiaofei-zheng

Copy link
Copy Markdown
Contributor Author

@BBuf thank you for the approval. Could you merge this when you have a moment? The remaining red checks are unrelated: the Base jobs stopped at a GitHub API HttpError in the health check, and the AMD stage-b failures match the scheduled main run (https://github.com/sgl-project/sglang/actions/runs/37620899585).

@BBuf
BBuf merged commit db32673 into sgl-project:main Oct 9, 2026
147 of 172 checks passed
tanguofu pushed a commit to tanguofu/sglang that referenced this pull request Oct 12, 2026
…fusion

Two upstream fixes that our decode path provably hits:

1. sgl-project#35872 (790551c) - skip the full-vocab softmax in the EAGLE topk==1
   draft on ROCm. Live decode runs speculative_algorithm=EAGLE with
   eagle_topk=1 and steps=2 on EAGLEWorkerV2, so every draft step went
   through renorm_draft_probs + fast_topk over the whole vocabulary. The
   decode profile measures that softmax at 3.4% and the argmax reduce at
   2.9% of decode kernel time.

   ROCm previously fell through to the generic topk path because of sgl-project#26358
   (torch.argmax tie-break on FP8 logits). This uses the triton
   draft_topk1_postprocess kernel instead of torch.argmax, so the tie-break
   concern does not apply. A scratch positions buffer absorbs the position
   advance that draft-extend has already consumed.

2. sgl-project#41258 (db32673) - let the GLM DSA NextN draft declare its own
   shared-expert fusion architecture. ModelConfig rewrites a GLM draft's
   architecture to GlmMoeDsaForCausalLMNextN, so the inherited DeepSeek
   name never matched the gate and the draft's shared expert was never
   fused into fMoE, costing an extra GEMM per draft step. Measured at
   1.1% of decode kernel time (_fused_append_shared_experts_kernel).

   Only the architecture declaration is ported. The upstream commit's
   second hunk depends on should_apply_glm_nextn_moe_ptpc, which this tree
   does not have.

Deliberately not included: sgl-project#42982 (FP8 KV saturation). It has no throughput
benefit, it changes the values written into the KV cache, and therefore
forces the whole rollout into the "changes KV encoding" risk class. It
should ship on its own with its own correctness run.

Assisted-by: Hy4 preview via CodeBuddy Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants