Skip to content

perf(FlashKDA): add opt-in SM90 indexed KDA prefill - #5212

Merged
silencelamb merged 7 commits into
InternLM:mainfrom
silencelamb:pr/glm53-flashkda-prefill
Dec 2, 2026
Merged

silencelamb merged 7 commits into
InternLM:mainfrom
silencelamb:pr/glm53-flashkda-prefill

Conversation

@silencelamb

@silencelamb silencelamb commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Change

Add opt-in FlashKDA for SM90 compact indexed KDA prefill. The direct provider reads strided BF16 Q/K/V (head dimension 128, bounded gate), gathers shared input states before writing separate destinations, and exports FP32 or BF16 state. Terminal checkpoints use the final state; interior checkpoints use 64-token boundaries.

CUDA 13 production and development images package two independent providers. Before worker startup, set FLA_FLASH_KDA=1 to use the #5073 wheel for eligible prefills without checkpoint metadata, or LMDEPLOY_FLASH_KDA_LIB=1 for the direct provider. Both default to off. On eligible indexed prefill the direct provider takes precedence and is located automatically in the installed wheel. LMDEPLOY_FLASH_KDA_LIB_PATH allows an external library override.

Incremental AgentX performance

On the MegaMoE + 16K-checkpoint deployment at C90, enabling direct FlashKDA increased output throughput by 9.19%, request throughput by 5.41%, and goodput by 20.91%. The serving window used eaaa56d9 and its direct-provider switch. The independent GSM8K run below uses the current switch on the scored head.

Cumulative configuration C Decode tok/s Requests/s Good req/s TTFT P50 / P95 (ms) TPOT P50 / P95 (ms) Both P50s pass
#5186 + #5211 90 1,429.08 1.6489 0.9902 809.5 / 2,771.4 21.91 / 56.49 Yes
+ direct FlashKDA 90 1,560.46 1.7380 1.1973 726.4 / 2,523.6 19.44 / 48.79 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 a0784181 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_FLASH_KDA_LIB=1 (direct SM90; FLA dispatcher off) 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.

Per-PR counts, repeat runs and scoring evidence.

H200 latency

A (FLA) is the original indexed path. A (Docker) uses the #5073 wheel through FLA. B and C use the same direct-provider binary; B copies Q/K/V inside the timer and C consumes the views. The four arms share the same indexed state layout and inputs.

Scope A (FLA) A (Docker) B (contiguous) C (strided) Docker → C B → C FLA → C
8K single-layer KDA recurrence 2.235 ms 1.725 ms 1.079 ms 0.627 ms 63.64% 41.89% 71.94%
8K layer 20, convolution + recurrence 3.100 ms 2.657 ms 1.934 ms 1.462 ms 44.98% 24.43% 52.85%
8K layer 20, full attention 6.344 ms 5.890 ms 5.218 ms 4.823 ms 18.10% 7.55% 23.97%
8K layer 20, decoder block incl. MoE 14.504 ms 13.932 ms 13.320 ms 12.986 ms 6.79% 2.51% 10.47%
64K whole-model prefill 5.590 s 5.406 s 5.291 s 5.179 s 4.19% 2.11% 7.35%

The 64K measurement uses 8×H200, DP8/EP8/TP1, eight 8K chunks and eight paired measurement blocks. C saves 411 ms versus indexed FLA and 226 ms versus Docker FlashKDA. Removing the three copies accounts for 112 ms. Docker → B changes both the provider revision and calling interface.

Numerical agreement and validation

  • Against A (FLA), C has 8K output relative L2 0.005896, 64K final-position logit relative L2 0.28439, and 10/32 matching first tokens.
  • Against A (Docker), B and C each have output relative L2 2.67e-5 on the synthetic 8K case, logit relative L2 0.4092 on the 32 fixed 64K prompts, and 6/32 matching first tokens. In one traced prompt the first difference occurs at layer 0, token 33. The Docker wheel rounds recurrent state between 16-token tiles; the direct provider accumulates in FP32 within a call. Task accuracy on this build remains unmeasured.
  • B and C have identical tested activations, final states and all 32/32 full-vocabulary final-position logits. The packaged provider also reproduced the reference binary's logits on all 32 prompts. H200 provider/dispatch tests passed 12/12, and seven host tests cover the startup settings.

The fork retains the experimental setup, original three-arm results, pairwise error metrics, FP64 controls and full 45-layer × 8-chunk trace. 完整中文报告 · Detailed measurements and layer trace

@silencelamb silencelamb changed the title [Perf] Add opt-in FlashKDA for SM90 indexed KDA prefill perf(FlashKDA): add opt-in SM90 indexed KDA prefill Nov 30, 2026
@zzwwrr

zzwwrr commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

When every requested checkpoint is at the end of its sequence, _export_states copies from final for every checkpoint. The direct provider still allocates checkpoint = torch.empty_like(initial) for the whole batch. With 32 rows, 64 local heads and head dimension 128, that adds 64 MiB of temporary BF16 state or 128 MiB of FP32 state for this call.

Could we record whether any row needs an interior snapshot when building the prefill metadata on the host? For a terminal-only batch, we could omit both snapshot arguments to the provider and still export the terminal checkpoints from final. A terminal-only test could check both the exported values and that the provider received no snapshot buffer.

Classify interior checkpoints on the host after reservation IDs are bound. Preserve the plan on device transfer, and export terminal destinations from the final state even when both optional native snapshot arguments are absent.

Copy link
Copy Markdown
Collaborator Author

@zzwwrr Thanks for catching this. Fixed in 95160d91. The host now checks the actual reserved checkpoint IDs and offsets and carries that decision to the device. For a terminal-only batch, the direct provider receives None for both checkpoint_state and checkpoint_offsets, and _export_states writes the requested checkpoints from final. A batch with an interior checkpoint still uses the snapshot buffer.

The new BF16 and FP32 regression cases failed on the previous head and passed on H200 after the fix. They inspect both native operator arguments and compare the output and entire state pool element-for-element, including a separate checkpoint destination at length 137. With the 32-row, 64-head, head-dim-128 shape you described, measured peak allocated memory for the call fell by 64 MiB for BF16 and 128 MiB for FP32.

@silencelamb
silencelamb requested a review from hehesnail December 1, 2026 23:11

@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

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

Retested both FlashKDA providers on H200. With terminal-only checkpoints, the updated direct provider produced exactly the same outputs and state pool as the previous revision while reducing peak allocation by about 64 MiB for BF16 and 129 MiB for FP32 in the 32-row, 64-head case. In a mixed batch, the interior checkpoint at token 64 also matched the state from running that prefix independently. LGTM.

@silencelamb
silencelamb merged commit c92c9ea into InternLM:main Dec 2, 2026
14 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