Skip to content

perf(kernel): fuse adjacent mHC post and pre operations for Hopper decode - #5216

Merged
RunningLeon merged 4 commits into
InternLM:mainfrom
silencelamb:perf/glm53-fused-mhc-decode
Dec 2, 2026
Merged

RunningLeon merged 4 commits into
InternLM:mainfrom
silencelamb:perf/glm53-fused-mhc-decode

Conversation

@silencelamb

@silencelamb silencelamb commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

A matched GLM-5.3-Flash single-request decode trace on H200 recorded 450 mHC kernels per step in LMDeploy (1.230 ms of kernel time) and 181 in vLLM (0.957 ms). Each of the 45 target layers has attention and FFN mHC boundaries. The preceding Post and following Pre can share the updated residual at 89 internal boundaries.

Implementation

  • Add one opt-in environment variable, LMDEPLOY_FUSED_MHC_DECODE=1, for the PyTorch engine. GLM-5.3-Flash and DeepSeek-V4 Flash/Pro dispatch through the shared HcPrePost module. The CUDA implementation supports four residual streams, hidden size 4096 or 7168, and 1–32 BF16 decode tokens per rank on SM90. Prefill, deterministic mode and other shapes retain their existing path.
  • Carry the preceding residual, Post coefficients and combination matrix to the next Pre. Materialize the first Pre and final Post separately.
  • The first TileLang kernel forms the BF16 residual and computes partial projections and squared norms in the same pass. The second reduces eight K splits and reuses the existing Sinkhorn, stream collapse and RMSNorm implementation. Both BF16 rounding points are preserved. The FP32 projection uses a different reduction order.
  • The projection tiling follows vLLM's Hopper TileLang implementation. The implementation runs inside LMDeploy with its existing TileLang dependency.

Incremental AgentX performance

On the three-PR deployment at C100, enabling fused mHC (99add054) increased output throughput by 1.25%, request throughput by 1.58%, and goodput by 4.50%. These are full-service results on GLM-5.3-Flash. The single-boundary and DeepSeek-V4 measurements below retain their own workloads.

Cumulative configuration C Decode tok/s Requests/s Good req/s TTFT P50 / P95 (ms) TPOT P50 / P95 (ms) Both P50s pass
#5186 + #5211 + #5212 100 1,620.37 1.8272 1.1592 737.1 / 2,627.5 20.96 / 49.79 Yes
+ fused mHC 100 1,640.66 1.8560 1.2114 729.6 / 2,553.7 20.64 / 47.81 Yes

8×H200, DP8/EP8/TP1, native MTP3, RDMA Mooncake, Weka 256K, independent 1,800 s scored windows. Five-PR AgentX report with configurations, controls and concurrency sweeps.

Full GSM8K accuracy on this PR

Scored 99add054 on GLM-5.3-Flash FP8 with only the feature in this PR enabled. All 1,319 test questions used the same five train demonstrations, greedy decoding, seed 42, C32 through the router and the historical QA setting max_tokens=1024 (generated tokens per question, with input tokens counted separately). The grader compares the last parsable number after #### with the gold answer and includes all 1,319 questions.

Enabled configuration Correct / total Accuracy HTTP errors Missing numeric answer Outputs at limit
LMDEPLOY_FUSED_MHC_DECODE=1 1,214 / 1,319 92.04% 0 0 0

The historical QA image on the same GLM-5.3-Flash FP8 weights scored 91.74% / 91.58% (1,210 / 1,208 correct out of 1,319). The earlier 91.96% QA result used a different checkpoint.

This full-set score is for GLM. The DeepSeek-V4 Flash and Pro GSM8K smoke checks below retain their 16-question scope.

Per-PR counts, repeat runs and scoring evidence.

GLM-5.3-Flash single-boundary latency on H200

CUDA Graphs cycle through all 89 internal sublayer weights from the GLM-5.3-Flash checkpoint. Inputs are synthetic and identical across paths. Each value is the median of 15 alternating-order rounds, eight graph replays per round, in microseconds per boundary. This comparison uses the LMDeploy implementation in this PR for both modes.

Tokens per rank Default Fused Latency reduction
1 12.878 8.392 34.8%
8 13.684 8.814 35.6%
16 15.661 9.093 41.9%
32 20.099 12.333 38.6%

Validation

  • All 61 targeted H200 tests passed: test_dsv4_hc_pre_fused.py, test_dsv4_hc_prepost.py and test_routed_experts.py. Coverage includes both BF16 rounding points with changed inputs across graph replays and the intra-/inter-layer handoff in a two-layer hybrid decoder.
  • CPU routed-expert tests: 15 passed, 1 CUDA-only skip. Repository pre-commit hooks passed.
  • Across the 89 measured weights at one token per rank, 4 of 364,544 final BF16 activation elements differed from the default path, with maximum absolute difference 0.001953125.

GLM-5.3-Flash full-checkpoint decode

Paired runs used the same GLM-5.3-Flash checkpoint (3f1971b7), 8×H200, DP8/EP8/TP1, one active request, a 1,024-token prompt, and the same 64 teacher-forced decode inputs. Both runs captured one CUDA graph per rank. On active rank 0:

Metric Default Fused
mHC kernels in one profiled decode step 450 183
Sum of mHC kernel durations in that step 1.238 ms 0.816 ms
Median CUDA event duration per decode step, second 64-step replay 11.798 ms 11.353 ms
Mean next-token NLL on this fixed token trajectory (nats) 0.6483 0.6621

The fused trace contains 89 Post/projection kernels. The default and fused runs agree on the prefill argmax and all 97,417 sampled prefill cache/state values on rank 0. Decode top-1 agreed at 60/64 matched positions across the 154,880-way vocabulary. Mean per-position relative L2 logit difference was 0.1117 (maximum 0.2398). Replaying the same path within each process reproduced all 64 argmax tokens. This single trajectory measures numerical drift and latency. Task-level quality and MTP acceptance remain unmeasured.

A separate fresh-process control with the flag disabled reproduced all 64 × 154,880 reference logit values exactly. The same four top-1 differences reappeared when comparing that control with the fused run. In an additional eager diagnostic on the first real decode input, both kernels received identical inputs at each of the 89 boundaries. Their normalized BF16 activations differed in 54 of 364,544 elements (maximum absolute difference 0.001953125). The operator-level agreement should be interpreted separately from the measured full-model logit drift.

DeepSeek-V4

The shared module owns Post deferral, consumption by the next Pre, and the deterministic-mode opt-out. The CUDA backend checks the input shape and device. DeepSeek-V4 retains its standalone RMSNorm in the default path and materializes the final four-stream residual before its learned hyper-head. For Pro, the reduction indexes all 14 segments per thread at width 7168. Width 4096 retains the original reduction order.

Each row uses all eligible attention/FFN checkpoint weights (85 in Flash and 121 in Pro) with identical synthetic activations on a single H200. The comparison uses each model's production Post, Pre and standalone RMSNorm as the default. CUDA-event medians are in microseconds per boundary.

Model Local tokens Default Fused Latency reduction
Flash 1 16.685 8.494 49.1%
Flash 8 25.857 8.787 66.0%
Flash 16 26.791 9.164 65.8%
Flash 32 28.199 12.335 56.3%
Pro 1 19.633 10.442 46.8%
Pro 8 32.333 10.774 66.7%
Pro 16 34.175 11.355 66.8%
Pro 32 37.341 16.728 55.2%

Full-checkpoint validation on H200

Each model used a 512-token prompt and 64 decode inputs from its default greedy trajectory. We compared every vocabulary logit and timed the same inputs with the flag set to 0 and 1. The step measurement is the median rank-0 CUDA-event time across two warm 64-step graph replays (target forward and LM head). Prefill logits matched exactly in both pairs.

Model GPUs / TP Fused boundaries per step (trace) Default step Fused step Reduction Decode top-1 agreement Mean KL(default ‖ fused), nats
Flash (6e763230) 4 85 16.814 ms 15.979 ms 4.97% 64/64 0.00677
Pro (0366e4e0) 8 121 34.716 ms 33.513 ms 3.46% 64/64 0.00207

Mean per-position relative logit L2 differences were Flash 0.08191, Pro 0.07817. Mean next-token NLL on the fixed default greedy continuation (nats, default → fused) was Flash 0.27052 → 0.28090, Pro 0.07070 → 0.07263.

Each model also generated its own greedy answers to the first 16 GSM8K test questions, with the first five training examples as demonstrations, a 160-token generation limit and numeric exact match after ####. Missing answers at the limit count as incorrect. This is a small paired smoke test.

Model Default Fused Wrong → correct Correct → wrong
Flash 13/16 14/16 1 0
Pro 14/16 13/16 0 1

Pro changed from 60 (correct) to 55 on the 15th question. On the separate fixed decode trajectory Pro kept all 64 top-1 tokens, demonstrating why we also tested free-running answers.

Shared-path regression

  • GLM-5.3-Flash reproduced all 9,912,320 previously recorded fused logits bit for bit after the refactor. Its rank-0 trace contained 89 fused boundaries.
  • The H200 regression suite covers both widths, graph replays with changed inputs, RMSNorm energy confined to the tail, model-to-model reuse of the option, and preservation of standalone Pre/Post at the ends of the stack.
  • All 95 targeted tests passed in a fresh run. Repository pre-commit hooks passed.
  • Warm-cache control: the initial benchmark-then-tests run had 15 failures in existing bitwise FP32 scale/projection reference assertions. Using the same prewarmed TorchInductor/Triton cache, the same 14 cases failed on both pre-refactor commit 2965d40f and this revision. One additional two-token scale assertion varied with warmup order. The strict tolerances were retained.

@silencelamb
silencelamb marked this pull request as ready for review November 30, 2026 06:02
@silencelamb silencelamb changed the title perf(kernel): fuse mHC post and pre for GLM-5.3-Flash decode on Hopper perf(kernel): fuse adjacent mHC post and pre operations for Hopper decode Nov 30, 2026

@zzwwrr zzwwrr 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.

LGTM

@RunningLeon RunningLeon 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.

LGTM

@RunningLeon
RunningLeon merged commit 00dbc41 into InternLM:main Dec 2, 2026
4 of 5 checks passed
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