Skip to content

[Perf] [AMD] kimi-k3: Run KDA target verify through AITER's fused FlyDSL kernel - #48701

Closed
yuychang wants to merge 33 commits into
sgl-project:mainfrom
yuychang:k3/kda-fused-verify-pr
Closed

yuychang wants to merge 33 commits into
sgl-project:mainfrom
yuychang:k3/kda-fused-verify-pr

Conversation

@yuychang

@yuychang yuychang commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Depends on ROCm/aiter#7529 (the fused verify kernel); no pinned AITER carries it yet.

Motivation

Modifications

flowchart TD
  A["KDA _forward_target_verify"] --> B{"layer.kda_fused_verify installed?"}
  B -->|"no: verify backend not aiter, or hook not armed"| T["Triton chain verify"]
  B -->|"yes"| C["KimiK3HipFusedVerify.forward"]
  C --> D{"batch covered: no ragged, tree or request ring; dtypes, shapes, draft count"}
  D -->|"yes"| E["AITER flydsl_kda_verify_with_f_b: f_b, conv1d, verify, gated norm in one launch"]
  D -->|"no: warn once"| F["unfused_gate projects deferred f_b"]
  F --> T
Loading
File Delta Change
python/sglang/kernels/ops/attention/__init__.py +9 / -0 Registers attention.kda_fused_verify_aiter_hip
python/sglang/kernels/ops/attention/kda_fused_verify_aiter_hip.py +346 / -0 Fail-closed adapter: available, covered, run, warmup
python/sglang/srt/arg_groups/attention_hook.py +14 / -9 KDA aiter verify allowed with ReplaySSM spec and ragged ring writes
python/sglang/srt/arg_groups/fields/exec_.py +1 / -1 CLI help of --linear-attn-verify-backend
python/sglang/srt/layers/attention/linear/kda_backend.py +107 / -2 KDAFusedVerify protocol; verify calls the hook
python/sglang/srt/layers/radix_linear_attention.py +3 / -0 kda_fused_verify = None field
python/sglang/srt/models/kimi_k3.py +8 / -68 HIP arming moved out; target verify defers f_b with the hook
python/sglang/srt/models/kimi_k3_amd/kda.py +289 / -0 KimiK3HipFusedVerify, build_fused_verify_hip, prepare_fused_kda_hip
test/registered/... (5 files) +773 / -22 Fused vs unfused parity, covered(), dispatcher, arming and fallback
  • No new env var; --linear-attn-verify-backend aiter now accepted for KDA on ROCm (GDN aiter stays rejected).
  • Batches outside the kernel contract (ragged, tree, request-indexed ring, other draft count) run the Triton chain.
  • Not armed (one warning) without the AITER op, on non-BF16 activations, or a state dtype/device it does not cover.
  • Unarmed aiter differs from main's gfx950 triton path: it also skips the HIP chain-verify kernel.

Accuracy Tests

State mode Fused verify off: GSM8K / accept len Fused verify on: GSM8K / accept len
ReplaySSM ring 0.980 / 4.11 0.980 / 4.04
Snapshot 0.985 / 4.05 0.980 / 4.05

Speed Tests and Profiling

  • InferenceX AgentX, Kimi-K3, 8x MI355X, pinned acceptance, 3600 s per arm, one round; off then on, same build.
    • Run on the pre-review env var revision; not rerun on 4c70b17841. Hour-to-hour spread up to ~4%.
    • Same kernel, but aiter now also bypasses the HIP chain-verify fallback, so the numbers may not carry over.
Point Off tok/s/GPU On tok/s/GPU Delta Off P90 ITL ms On P90 ITL ms Delta
c4 2846.05 2832.75 -0.5% 8.85 8.82 -0.3%
c16 8671.71 8616.17 -0.6% 18.81 17.97 -4.5%
c56 16342.68 16921.71 +3.5% 57.42 53.51 -6.8%
  • Per KDA layer (TP8, MI355X, cold L2/MALL): 26.6 -> 25.2 us at c1, 33.3 -> 23.2 us at c16, 62.2 -> 50.9 us at c56.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #45570926009
Latest PR Test (Extra): ⊘ Skipped -- add the run-ci-nv-extra label to opt in.
Latest PR Test (AMD ROCm 10): ❌ Run #45570928598

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

…e pool's state dtype

- Load flydsl_kda_decode_with_f_b from aiter.ops.flydsl.linear_attention_kernels when the FlyDSL source is aiter.
- Fail closed with a one-time warning when AITER lacks that kernel or the FlyDSL import fails.
- Take the recurrent-state dtypes the bound kernel advertises; the vendored kernel stays FP32-only.
- available() and warmup() take the mamba pool's state dtype; a kernel that cannot take it is not armed and warns once.
- Arm the gfx950 kernel only for an FP32 pool, so a BF16 pool no longer arms it.
- Mirror AITER's cache-slot and stride checks in covered(), so an unsupported layout falls back instead of raising.
- Warm only the pool's state dtype before graph capture and log the armed kernel and dtype.
- Test the bindings, BF16 state, SGLang's cache pool layouts, warmup, arming, and vendored-vs-AITER agreement.
- End test_kimi_k3_kda_decode.py with the __main__ block test/README.md prescribes.
- Document the state-dtype arming rule, the backend default, and the aiter source in the Kimi-K3 cookbook.
- The fused KDA decode takes a dense BF16 f_b with no weight scale; Quark stores f_b as per-channel FP8.
- Dequantize it once to a BF16 copy for the fused kernel only; the FP8 layer keeps serving the unfused path.
- The helper lives in the ROCm-only kimi_k3_rocm_quant.py and runs only once a fused backend is available.
- ShardedStateLoader and RemoteModelLoader transpose the FP8 weight before post_load_weights.
- Skip the dequant for such a weight and warn once; the layer keeps the unfused path instead of failing the load.
- Add CPU tests for the dequant, the BF16 pass-through, and the post-processed weight.
- Note the BF16 dequant in the Kimi-K3 cookbook.
- Define SGLANG_USE_HIP_K3_KDA_FUSED_BACKEND and SGLANG_USE_HIP_K3_FLYDSL_SOURCE in Envs.
- Keep SGLANG_ROCM_K3_KDA_FUSED_BACKEND and SGLANG_K3_FLYDSL_SOURCE as deprecated aliases.
- Read both through envs in the two fused-decode adapters, the FlyDSL source loader and kimi_k3.py.
- Switch the tests to envs overrides and cover the two aliases.
- Document both in the AMD & ROCm table; the cookbook keeps the old names for its pinned image.
# Conflicts:
#	python/sglang/kernels/ops/attention/kda_fused_decode_gfx950.py
…fused KDA decode checks

- Materialize the deferred f_b whenever it is deferred; the ReplaySSM ring path skipped it and got the raw f_a.
- Split the gfx950 arch check from available(), so covered() no longer logs a "not armed" warning per step.
- Restore the unset-default case in test_fused_kda_backend_is_opt_in.
- Clarify in the env-var docs that a BF16 state needs SGLANG_USE_HIP_K3_FLYDSL_SOURCE=aiter.
- Say in the cookbook which init failures log the one-time warning.
…fused decode arming

- SGLANG_USE_HIP_K3_FLYDSL_SOURCE=auto still prefers the vendored kernel, but binds AITER's when only it takes the state
- Cache the kernel binding per (source, state dtype); covered() and run() no longer re-import or probe the device
- Warn separately when AITER lacks flydsl_kda_decode_with_f_b and when the op lacks the state_dtypes tag
- Reset the HIP fused decode arming before re-arming, so a re-run that fails cannot keep a stale f_b in use
- Take the merged BF16 f_b (block-FP8 and BF16 checkpoints) first; warn once when no BF16 f_b can be produced
- Make the decode adapters' available() keyword-only and fix the gfx950 docstring that described an unused device
- Re-wrap the new environ.py comments at clause boundaries and restore the decode test's -v main call
- Tests: auto binds AITER for a BF16 pool, both AITER failure messages, failed re-arm, merged block-FP8 f_b
- Tests: set the deprecated env names through temp_set_env(allow_sglang=True), as they have no descriptor
- Docs: describe the dtype-aware auto source and the BF16 f_b requirement in the cookbook and env var reference

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added documentation Improvements or additions to documentation quant LLM Quantization amd jit-kernel labels Nov 5, 2026
- Document that host-only request-indexed ReplaySSM rings do not arm fused KDA verify.
- Document that fused-accept verify batches keep the unfused path.
- Skip HIP fused decode preparation and warmup when ReplaySSM is enabled without fused verify.
- Preserve shared static inputs and fused verify initialization without arming ring decode.
- Add regression coverage for ReplaySSM gating and verify preservation, and update environment-variable docs.
- Main squash-merged the base PR 41794, so the branch diff now carries only the fused KDA verify
- Resolve environ.py and the env doc to main's alias-free SGLANG_USE_HIP_K3_* selectors; keep this PR's verify entry
- Drop test_pre_rename_names_still_select with the aliases main removed
…fy-backend aiter through a model-owned hook

- Drop SGLANG_OPT_HIP_K3_KDA_FLYDSL_VERIFY; --linear-attn-verify-backend aiter selects the fused verify (C3)
- KDA's aiter verify maps to the Triton chain for batches and models no fused hook covers; non-ROCm raises
- Allow KDA's aiter verify with --enable-linear-replayssm-spec (it writes the ring); GDN's aiter stays rejected
- KDAAttnBackend calls one declared KDAFusedVerify hook (RadixLinearAttention.kda_fused_verify) instead of K3 branches
- Move the K3 layer unpacking and the deferred f_b projection into models/kimi_k3_rocm_kda_verify.py
- Remove the seven _k3_* handoff fields this PR added to RadixLinearAttention.__init__
- Keep the kernel adapter tensor-only, register it as attention.kda_fused_verify_aiter_hip
- Warm the wave counts AITER's verify op declares; drop the fallbacks for untagged pre-release ops
- Drop the fused-accept rejection: fused accept needs the flashinfer verify, so it never meets the hook
- Split the ROCm arming out of KimiK3DeltaAttention._prepare_fused_decode into _prepare_fused_decode_hip
@yuychang

yuychang commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@HaiShaw this is ready for review.

…and gate KDA aiter verify on its kernel's devices

- move the PR's ROCm arming (_prepare_fused_decode_hip, _prepare_fused_verify_hip) to kimi_k3_amd/kda.py functions
- models/kimi_k3_rocm_kda_verify.py becomes kimi_k3_amd/kda.py (KimiK3HipFusedVerify, build/prepare_fused_*_hip)
- kimi_k3.py keeps main's `if _is_hip:` and calls prepare_fused_kda_hip; forward defers f_b when the hook is installed
- drop _kda_hip_fused_verify_ready: layer.kda_fused_verify is not None says the same
- KDAKernelDispatcher asks the registered kda_fused_verify KernelSpec's declared devices instead of is_hip()
- attention_hook names the KDA-model aiter verify flag kda_chain_verify (was kda_aiter_verify)
- tests: the dispatcher accepts aiter verify on a declared device only; arming tests call the moved functions
- Pick up main's ratchet fix (sgl-project#51311) so CI runs the PR against a main whose CPU suite passes
- The ring_writing condition accepts aiter for KDA chain verify, but the ValueError text listed only triton or nv_cutedsl
yuychang added a commit to yuychang/sglang that referenced this pull request Nov 20, 2026
- The KDA fused backend and FlyDSL source vars pick an implementation, so Rule 4 keeps USE_
- Restore the origin/main EnvStr definitions, REMOVED_ENVS hints, readers, tests and docs for both
- Drop the two rows from the rename alias table in test_environ.py
- Decouples this PR from sgl-project#48701, which reads the selector from models/kimi_k3_amd/kda.py
- The kernel and the hook's unfused f_b GEMM take BF16 f_a, so --dtype float16 batches were rejected then hit a dtype error
- build_fused_verify_hip takes activation_dtype and returns None with a one-time warning when it is not BF16
- Cover the dtype axis in the arming table and add a prepare_fused_kda_hip case with FP16 activations
…t its metadata

- An early flydsl_kda_verify_with_f_b exports the op without num_warps; the adapter read it and failed at warmup
- _op now checks state_dtypes, max_tokens and num_warps, warns once naming the missing ones, and returns None
- Add a CPU test with stubbed aiter modules for the missing and the complete op
…thout the AITER kernel

- Drop the AITER skipif from the covered() fallback test and stub the op's max_tokens; it uses meta tensors only
- Add a hook-level case: an unsupported batch never reaches covered() or run() and the unfused gate matches F.linear
- Leave the parity and warmup cases skipped; they need the real kernel, which no CI pin has yet
…ts coverage

- Decide verify arming from verify_wanted, so a decode kernel that rejects the state dtype no longer keeps it off
- Drop the decode buffers when neither decode nor verify armed from them
- Reject unsupported state dtypes and intermediate-state shapes in covered()
- Skip the real-kernel tests through the adapter's own availability check
- Move the meta-only covered() cases to a CPU unit test and cover ragged aiter verify for DSPARK and EAGLE
@bingxche

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@bingxche

Copy link
Copy Markdown
Collaborator

/approve-ci

@yuychang

Copy link
Copy Markdown
Contributor Author

Closing: with sgl-amd-mono serving every verify step, only KDA layer 0 (wide steps) and layers 0-1 (one-request steps) of 69 still reach this path, measured with call counters on a Kimi-K3 c14 server; that is about 0.05% of the original gain. The K3 recipe no longer enables it. ROCm/aiter#7529, its kernel, is closed with it.

@yuychang yuychang closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd documentation Improvements or additions to documentation jit-kernel quant LLM Quantization run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants