Skip to content

[MLA v4 nm] Split plan helper for callers and cost-model split count - #5890

Closed
yhl-amd wants to merge 3 commits into
ROCm:mainfrom
yhl-amd:fix/mla-v4-nm-split-planner
Closed

yhl-amd wants to merge 3 commits into
ROCm:mainfrom
yhl-amd:fix/mla-v4-nm-split-planner

Conversation

@yhl-amd

@yhl-amd yhl-amd commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • get_mla_v4_nm_split_plan(num_seqs, num_heads, kv_len, *, split_indptr=None) -> MlaV4NmSplitPlan. New helper that builds the KV split plan for mla_decode_fwd_v4_nm. MlaV4NmSplitPlan(num_kv_splits, split_indptr) is a NamedTuple whose field names are the wrapper's kwargs, so callers pass it on as mla_decode_fwd_v4_nm(..., **plan._asdict()).
    • The split count minimizes a per-arch latency model (workgroup waves x per-wave KV time, plus the partial-write/stage-2 cost of splitting). Coefficients live in a per-arch table; gfx950 is fitted. Other archs keep the wrapper's occupancy-only pick.
    • split_indptr is the uniform [0, s, 2s, ...]. Given a caller-owned persistent buffer it is filled in place (one device launch, no host sync), so CUDA-graph callers keep the buffer alive across replays and rebuild the plan before each replay.
    • The plan depends only on num_seqs, num_heads and the caller's kv_len, never on kv_page_indices, whose length is meaningless for capacity-sized graph buffers.
  • Wrapper unchanged when no plan is passed. The auto pick inside mla_decode_fwd_v4_nm is the same as main.
  • Stage-2 fix. With a caller-supplied split_indptr that gives every seq one split on a grid > 1, the merge took the in-place BF16 copy (keyed on split_indptr[-1] == num_seqs) even though the kernel wrote FP32 partials, returning NaN. The v4 nm merge now always reduces the FP32 partials.

Why planned callers should not rely on the old auto pick: it maximizes CU occupancy only, so for gqa=128 single-token decode it picks 13-16 splits (several workgroup waves) for batches whose KV already fits one wave, adding partial writes and stage-2 work without adding parallelism.

Consumer: DeepSeek-V4 FP8 decode in ATOM (ROCm/ATOM#2411).

Test plan

  • op_tests/test_mla_v4_nm.py on gfx950 (MI355X): all pass
    • new: test_v4_nm_split_planner_picks, test_v4_nm_split_plan_fills_buffer_in_place, test_v4_nm_split_plan_accuracy, test_v4_nm_split_plan_cudagraph_replay (plan captured once, replayed with different KV lengths), test_v4_nm_one_split_per_seq_on_wide_grid
  • ruff / black on changed files

🤖 Generated with Claude Code

The gfx950 v4 nm auto split pick (ignore_total_kv=1) maximizes CU
occupancy only, so it prefers whole multiples of cu_num: for gqa=128
single-token decode it picks 13-16 splits (several workgroup waves) for
batches whose KV already fits one wave, paying extra partial writes and a
wider stage-2 merge for no added parallelism.

- On gfx950, pick num_kv_splits by minimizing a fitted latency model:
  workgroup waves x per-wave KV time, plus the partial-write/stage-2 cost
  of splitting. Other archs keep the existing pick.
- Add kv_len_hint: callers that pass capacity-sized kv_page_indices (e.g.
  CUDA-graph decode) cannot be planned from total_kv / num_seqs, so they
  pass the per-seq KV length to plan for. The pick depends only on shapes
  and this int, so it stays fixed across graph capture and replay.
- Fix the stage-2 merge for a caller-supplied split_indptr with one split
  per seq on a grid > 1: it took the in-place BF16 copy (keyed on
  split_indptr[-1] == num_seqs) although the kernel wrote FP32 partials,
  returning NaN.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: yihonglie <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 5890 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@yhl-amd
yhl-amd marked this pull request as ready for review September 28, 2026 04:37
@yhl-amd
yhl-amd requested a review from a team September 28, 2026 04:37
Planning inside mla_decode_fwd_v4_nm needs a per-seq KV length, and the
only one the wrapper can see, kv_page_indices.shape[0] / num_seqs, is
meaningless for CUDA-graph callers whose index buffers are capacity-sized.

- Add get_mla_v4_nm_split_plan(num_seqs, num_heads, kv_len, split_indptr=):
  returns the split count (cost model on gfx950, occupancy pick elsewhere)
  and fills a caller-owned persistent split_indptr in place. Callers build
  it in their metadata step and pass both to the wrapper.
- Drop kv_len_hint; the wrapper's own auto pick is unchanged from main.
- Tests: plan fill, accuracy through the wrapper, and CUDA-graph replay with
  different KV lengths under a captured plan. _run_varlen_point now returns
  its checkAllclose error so the new tests gate on it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: yihonglie <[email protected]>
@yhl-amd yhl-amd changed the title [MLA v4 nm] Cost-model num_kv_splits auto pick and kv_len_hint [MLA v4 nm] Split plan helper for callers and cost-model split count Sep 28, 2026
- get_mla_v4_nm_split_plan returns MlaV4NmSplitPlan(num_kv_splits,
  split_indptr). The field names are the wrapper's kwargs, so callers pass
  a plan on as mla_decode_fwd_v4_nm(..., **plan._asdict()).
- Latency-model coefficients live in a per-arch table; archs without an
  entry keep the occupancy-only pick. Only gfx950 is fitted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: yihonglie <[email protected]>
@yhl-amd

yhl-amd commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #6126, which now carries these commits (split plan helper, cost-model split count, stage-2 fix) together with the persistent V4-NM decode kernel, merged with current main. Closing in favour of that PR.

@yhl-amd yhl-amd closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants