Skip to content

Kimi-K3.1 KDA prefill: tiled/sliding conv, paged state, FlashKDA schedules, checkpoint split - #3896

Draft
whx-sjtu wants to merge 4 commits into
ROCm:mainfrom
whx-sjtu:up/P3-kda-prefill
Draft

whx-sjtu wants to merge 4 commits into
ROCm:mainfrom
whx-sjtu:up/P3-kda-prefill

Conversation

@whx-sjtu

@whx-sjtu whx-sjtu commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Kimi-K3.1 KDA prefill: tiled/sliding conv, paged state, FlashKDA schedules, checkpoint split

Faster KDA prefill for Kimi-K3.1 on MI355X. Every change here is on by default. No environment variable is added. Each optimization was developed behind a switch, and this PR drops both the switch and the old path.

What changed

causal_conv1d (B, bit-identical)

  • Channel-last prefill without prefix caching used to run the 8-token row kernel. It now runs one program per [16 tokens x 512 channels] tile.
  • At width 4 (Kimi-K3.1) a sliding-window kernel reads each input row once and keeps the last three rows in registers. Before, the tile was re-read for every tap.
  • Both kernels use the row kernel's per-element arithmetic with fp fusion off. The q/k/v outputs and the conv-state pool match the row kernel bit for bit at Kimi's shape (width 4, no bias).
  • How the tiles were chosen:
    • The 16x512, 4-warp, channel-fast tile was the fastest of 72 tile/warp/grid-order configurations measured on MI355X.
    • The sliding kernel uses 256 channels, 1 warp and 2 stages.
  • Other layouts keep the row kernel.

KDA prefill on the paged state (B)

  • FlashKDA reads and writes the recurrent state directly in the slot pool (state_cache). Before, the state was gathered, copied K-first and scattered back.
  • A state fork, or interior checkpoints that are not split (see below), keep the gather path.

FlashKDA schedules

  • gfx950 pass A always takes its published wide tile (BW=64). FlashKDA only uses it above 2 blocks per CU, which H=12 never reaches. Same arithmetic per element (B).
  • gfx950 pass B runs the prefetching scan, which FlashKDA otherwise only uses on gfx1250. It still respects aiter's AITER_FDA_SCAN_PREFETCH. The scan schedule differs, so numerics change (N).
  • A multi-sequence step sizes FlashKDA segments on its total chunks instead of its longest sequence. The length comes from FlashKDA's own _choose_chunks_per_seg, capped at the longest sequence. Multi-sequence numerics change (N).
    • The integration branch read FlashKDA's CHUNK_DELTA_ATTN_FLASH_KDA_SEG override directly with os.getenv in ATOM. That read is removed. The override now applies through FlashKDA's chooser, the same way it does for FlashKDA's own calls.

Prefill checkpoints by cutting the recurrence (N)

  • Before: every interior checkpoint's head was re-run as one more varlen sequence. A 1025-token prefill anchored at 1024 ran 2049 tokens of recurrence.
  • Now: the builder cuts the step at the interior checkpoints (kda_ckpt_split.py). Each block runs FlashKDA on the paged state, and a checkpoint is copied out of the runtime slot right after the block that ends on it.
    • The builder attaches each block's bounds and chunk indices to its cu_seqlens, so FlashKDA does not sync the host per block.
  • Where a cut moves FlashKDA's segments, the rounding changes; the error against fp32 stays of the same order.
  • A resumed block of at most 6 tokens per row (typically the tail after a prompt-end anchor) runs on the fp32 token recurrence used by KDA decode (TAIL_RECURRENT_MAX = 6). Why 6:
    • On MI355X a FlashKDA call costs 22-24 us whatever the length.
    • The token recurrence costs 10.5 us at 1 token and 21.1 us at 8.
    • Its fp32 state is closer to an fp32 reference: tail-block error 4.40e-3 -> 3.36e-3 at 8193 tokens.

Performance (MI355X, Kimi-K3.1 TP8 + DCP8)

  • Tiled conv, paged state and schedules, per KDA layer:
    • 16K prefill: 425 -> 333 us (about -6.4 ms per step).
    • 2K resume: 158 -> 102 us.
    • Four-sequence 16K step: 510 -> 329 us.
    • The bit-identical changes alone save 85.8 us per layer at 16K.
  • Checkpoint split, TTFT median vs head re-run (20 runs, prefix cache hit):
    • 8193 + 64K context: -9.8 ms.
    • 3073 + 128K: -7.6 ms.
    • 2049 + 128K: -5.8 ms.
    • 1025 + 128K: -2.0 ms.
  • Sliding conv plus tail recurrence: 8193 + 64K about -1.9 ms; 16385 about -3.6 ms per step.

Dependencies

Left out (follow-ups)

Tests

  • New CPU tests:
    • tests/test_kda_ckpt_split.py: planner, builder transfer, stand-in recurrence equal to the head re-run.
    • tests/test_flash_kda_tuning.py.
  • New GPU tests: tests/test_causal_conv1d_tiled.py and tests/test_causal_conv1d_slide.py, both bit-exact against the row kernel / the tiled kernel.
  • CPU (no GPU), 114 test files that touch KDA, causal_conv1d, the Kimi model or its builder, plus the new ones. Main: 5290 passed, 62 failing ids. This branch: 5354 passed, the same 62 failing ids (environment-related plugin and DSpark tests). No new failure.
  • GPU bitwise/numerics checks will be posted later.

🤖 Generated with Claude Code

whx-sjtu and others added 3 commits November 26, 2026 11:18
Channel-last prefill without prefix caching (Kimi-K3.1 KDA) ran the 8-token
row kernel. It now runs one program per [16 tokens x 512 channels] tile, and
at width 4 a sliding-window kernel that reads each input row once and keeps
the last three in registers instead of re-reading the tile per tap. Other
layouts keep the row kernel.

Both use the row kernel per-element arithmetic with fp fusion off, so the
outputs and the conv-state pool are bit-identical at Kimi-K3.1 shape (width
4, no bias). The 16x512, 4-warp channel-fast tile was the fastest of 72
tile/warp/grid-order configurations swept on MI355X; the sliding kernel uses
256 channels, 1 warp, 2 stages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nt split

- The KDA prefill reads and writes the recurrent state in the slot pool
  (FlashKDA paged state_cache) instead of gathering it, copying it K-first
  and scattering the final state back. Steps with a state fork, or with
  interior checkpoints that are not split, keep the gather path.
  Bit-identical.
- gfx950 FlashKDA pass A always takes its published wide tile (BW=64);
  bit-identical. Pass B runs the prefetching scan (subject to aiter
  AITER_FDA_SCAN_PREFETCH); changes numerics.
- A multi-sequence step sizes FlashKDA segments on its total chunks, by
  FlashKDA own chooser, rather than on its longest sequence; changes
  numerics of multi-sequence steps. FlashKDA CHUNK_DELTA_ATTN_FLASH_KDA_SEG
  override still applies, through that chooser.
- Interior checkpoints: the builder cuts the step at them and each block
  runs FlashKDA on the paged state; a checkpoint is copied out of the
  runtime slot after the block that ends on it. This replaces re-running
  every checkpoint head as one more sequence (a 1025-token prefill anchored
  at 1024 ran 2049 tokens of recurrence). A resumed block of at most 6
  tokens per row runs on the fp32 token recurrence instead (TAIL_RECURRENT_MAX,
  measured crossover). Changes numerics where cuts move FlashKDA segments
  and in the tail blocks (closer to fp32).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The builder tests of the head re-run layout plan no split; a stand-in
FlashKDA pins balanced_chunks_per_seg to FlashKDA chooser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every eligible PR before approval:

  • ✅ Pre Checkin: Black, Ruff, catalog schema validation, non-GPU unit tests

Heavy model tests:

  • ✅ Gate checks are published on every PR so required checks do not stay missing
  • ✅ Expensive jobs run only after the PR is approved, or when a matching ci:* label is present
  • ✅ Pre Checkin must pass before approved heavy jobs continue
  • ✅ Without a matching label, expensive jobs also require a relevant path
  • ✅ A matching ci:* label is an explicit request and can open the gate even when paths would otherwise skip it

Path scope:

  • Native ATOM uses an ignore list: docs/markdown and plugin or Mesh paths are skipped
  • vLLM and SGLang use include lists: their own plugin code under atom/plugin/, plugin CI scripts, model list and release images. A core-only change does not run them; add ci:vllm / ci:sglang to ask for them
  • ATOMesh uses an include list: Mesh code, the ATOMesh accuracy workflow and its 1P1D launcher, plus the shared models_accuracy.json and accuracy_to_dashboard.py. ATOMesh benchmarks run after merge or on dispatch, not on PRs
  • Relevant path alone does not start expensive jobs; approval still opens the automatic path-scoped gate
Label Tests
ci:full Run all heavy PR model tests: native ATOM, vLLM, SGLang, and ATOMesh
ci:atom Run native ATOM model accuracy tests
ci:vllm Run ATOM vLLM OOT model accuracy tests
ci:sglang Run ATOM SGLang model accuracy tests
ci:mesh Run the ATOMesh accuracy tests, standalone and 1P1D, for Mesh-related changes

Without a matching ci:* label, heavy jobs are skipped when the path is not relevant or the PR is not approved.
Add labels via the sidebar or gh pr edit 3896 --add-label <label>

atom.model_ops.kimi_k3 binds AITER kernels at import, so the CPU unit-test
job (no AITER) failed to collect test_flash_kda_tuning.py. Load the module
with aiter_stub.atom_without_aiter like the other CPU tests do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant