Skip to content

fix(cake_kimi_k3_mla): bound the lazy E4M3 reference drop on the row-tile kernels (Inf/NaN rows with attention-sink keys); regenerate SM100 / SM103 - #6126

Merged
yyihuang merged 1 commit into
flashinfer-ai:mainfrom
yyihuang:averyh/cake-1004-kimi-k3-mla-sink
Oct 6, 2026
Merged

yyihuang merged 1 commit into
flashinfer-ai:mainfrom
yyihuang:averyh/cake-1004-kimi-k3-mla-sink

Conversation

@yyihuang

@yyihuang yyihuang commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Developed by the CAKE team.

Summary

Fixes an Inf/NaN defect in the backend="cake" Kimi-K3 MLA FP8 paged attention decode route (#5552, #5602, #5871) and regenerates the programs of both architectures from the fixed producer. The host side (flashinfer/mla/cake_kimi_k3_mla.py, planner, grids, kernel arguments) is unchanged; this PR moves only the generated program sources, the jit registry and the test file.

Defect. Serving Kimi-K3 with FP8 KV cache produced rows of inf / nan output for some requests in a batch while the same requests alone, or with a different batch composition, were fine. Real Kimi-K3 KV caches contain an attention-sink key (token 0) whose score sits ~115-125 octaves above every other key for most heads. The swapped-AB row-tile kernels (RT 16/32/48/64/96, the H12 decode and MTP routes) keep one lazy E4M3 reference per softmax warpgroup and re-reference every column of the warpgroup to the exceeding column's own tile maximum. A column without a sink (a head whose query does not attend the sink, or a padding column that reads the next request's query rows inside the batch) exceeds on a later tile, the whole warpgroup moves its reference down by ~118 octaves, the FP32 rescale alpha = 2^118 overflows the running sum and the O^T accumulator, and the merged row becomes Inf/NaN. This is why the failure depended on the batch and reproduced with num_split=1.

Fix (kernel only). Each softmax column keeps its running maximum in SMEM (the slot previously used for a dead column-sum write, so the SMEM carve-up and ring depths are unchanged) and the reference may never drop more than 32 octaves below the column's running maximum. Moves below 32 octaves are bit-identical to the previous programs; a sink column therefore keeps its reference near the sink and the non-sink columns of its warpgroup are only clamped where their mass is already below 2^-32 of the row. The wide two-CTA route (H96 decode, prefill) uses a monotone running maximum and is not affected; its programs are regenerated from the same producer and are byte-identical apart from the shared headers.

Tests

  • New test_decode_sink_key_rows in tests/mla/test_cake_kimi_k3_mla.py: sink key (+2.0 on all 576 channels at token 0) with sink-attending query heads at +1.25 (~150 octaves above the random keys), H12 batch 1 and the 8-request 11-14-page band that reproduced the failure, H96 two-request and long-request cases, checked against an FP32 reference at FP8-class tolerance (atol/rtol 0.1) plus relative L2 <= 0.05. The previous programs fail these cases with Inf/NaN rows; the regenerated programs pass.
  • tests/mla/test_cake_kimi_k3_mla.py: 38 passed (34 existing + the 4 new sink cases) on B200, 38 passed on B300 on the generated programs of this PR.
  • tests/attention/test_trtllm_gen_mla.py -k trtllm_mla_blackwell (dispatch regression): 27 passed on B200; 27 passed on B300.
  • Export receipts (source-vs-generated host-plan and bitwise output parity plus ABBA CUPTI timing gates; 27 perf rows + 2 coverage rows per architecture): 27 / 29 rows pass after a closed-prior retry round (source/export geomean 0.9997; 28 timed-correct) on B200; 28 / 29 rows pass (source/export geomean 1.0020) on B300. The refusals are measurement-validity gates, not parity or correctness failures: prefill_h96_b1_q2048_kv131072 on both GPUs is the exporter's loaded-SM-clock gate under the sustained power cap (1245 / 1965 MHz on B200, 1170 / 2032 MHz on B300, below the 0.70 fraction; the same row and gate as every previous head of this family), reproduced by a closed-prior retry round on B300 (1192 / 2032 MHz) and on B200 (1207 / 1965 MHz); on B200 mirror_h96_b1_q1_kv76800 (the shortest wide row, ~24 us) fails the paired ABBA directional_disagreement gate in both the first run and the closed-prior retry while bitwise parity holds (source/export 1.0053, then 0.9816 with the sign flipped), the arm-position offset of the paired harness on short rows already disclosed for this row in perf(cake_mla): kimi_k3 shape, regenerate both architectures for the r52 kernel (deferred O rescale, wider long-request threshold) #5602; mirror_h96_b2_q1_kv77699 hit the same gate in the first run (1.0096) and passed in the retry. The B200 and B300 exporter runs delivered byte-identical files (the two delivered patches have the same SHA-256; 21 program sources + README + jit registry). The wide decode kernel's two architecture lowerings now fold into one source under __CUDA_ARCH__ guards like every other kind, so the registry carries one main_wide program instead of two.
  • Producer-side evidence for the fix: the 8 layer-91 decode reproducer dumps (batch 64, H12) are finite with the fix and match an FP32 reference at FP8-class tolerance (previously 2 of 8 had Inf/NaN rows); the 10 pre-existing contract correctness rows are unchanged (6 bitwise identical, 4 peaked rows differ only in elements below 1.3e-17); paired perf A/B on the fixed vs previous kernel over the H12 decode / MTP rows: geomean 1.0005 (within noise).

Performance

No host or schedule change; the bound costs one max, one compare and one select per column per tile on the softmax warps and never fires on the benchmark rows. Export receipts above show source and generated programs within 0.2 % on every measured row.

Unsupported (rejected explicitly)

Unchanged from #5552 / #5602.

Export receipts (29 rows per architecture, source vs generated: same-session ABBA CUPTI cold-L2 medians, CUDA-graph replay; B200 column from the closed-prior retry round, B300 column from the first run whose single refusal the retry reproduced)
row route sm_100a source us sm_100a export us sm_100a src/exp sm_100a verdict sm_103a source us sm_103a export us sm_103a src/exp sm_103a verdict
mtp_h12_fixed_q5 main_rt64_reduce_w2 271.0 270.6 1.0014 PASS 262.7 262.7 1.0001 PASS
mtp_h96_fixed_q8_kv8000 main_rt96_reduce_w1 141.8 141.9 0.9995 PASS 125.6 125.1 1.0038 PASS
trace_h12_b1_q1_kv76800 main_rt16_reduce_cta 22.0 21.3 1.0285 PASS 21.8 20.8 1.0476 PASS
trace_h12_b2_q1_kv77699 main_rt16_reduce_cta 29.3 29.3 1.0000 PASS 29.0 29.0 1.0022 PASS
trace_h12_b3_q1_kv261120 main_rt16_reduce_cta 89.9 90.0 0.9993 PASS 90.7 90.8 0.9989 PASS
trace_h12_b4_q1_kv268800 main_rt16_reduce_cta 111.8 111.7 1.0006 PASS 113.3 113.2 1.0011 PASS
trace_h12_b5_q1_kv276480 main_rt16_reduce_w4 137.5 137.8 0.9977 PASS 139.9 140.2 0.9977 PASS
trace_h12_b6_q1_kv467751 main_rt16_reduce_w4 249.8 249.9 0.9997 PASS 256.1 255.7 1.0013 PASS
trace_h12_b7_q1_kv299613 main_rt16_reduce_w4 194.4 194.1 1.0013 PASS 197.4 197.2 1.0015 PASS
trace_h12_b8_q1_kv342305 main_rt16_reduce_w4 244.7 244.9 0.9995 PASS 250.6 250.7 0.9996 PASS
trace_h12_b9_q1_kv348705 main_rt16_reduce_w4 274.4 274.8 0.9986 PASS 279.9 280.2 0.9989 PASS
trace_h12_b10_q1_kv474166 main_rt16_reduce_w4 405.0 405.0 1.0001 PASS 413.8 413.7 1.0003 PASS
trace_h12_b11_q1_kv474405 main_rt16_reduce_w4 439.6 439.5 1.0001 PASS 449.8 449.7 1.0001 PASS
mirror_h96_b1_q1_kv76800 main_wide_reduce_cta — — — FAIL: failed gates: directional_disagreement 23.0 22.9 1.0028 PASS
mirror_h96_b2_q1_kv77699 main_wide_reduce_cta 33.0 33.6 0.9819 PASS 33.3 33.0 1.0087 PASS
mirror_h96_b6_q1_kv467751 main_wide_reduce_w2 290.3 290.0 1.0011 PASS 285.0 285.2 0.9992 PASS
mirror_h96_b7_q1_kv299613 main_wide_reduce_w2 220.2 220.0 1.0012 PASS 218.4 218.7 0.9984 PASS
mirror_h96_b8_q1_kv342305 main_wide_reduce_w2 281.4 281.2 1.0010 PASS 277.8 278.1 0.9991 PASS
mtp_h12_b8_q2_kv342305 main_rt32_reduce_w4 249.3 249.6 0.9987 PASS 252.5 252.8 0.9991 PASS
mtp_h12_b8_q4_kv342305 main_rt48_reduce_w2 262.0 262.1 0.9995 PASS 259.1 259.2 0.9996 PASS
mtp_h12_b8_q8_kv342305 main_wide_reduce_w2 282.3 282.0 1.0008 PASS 277.3 277.7 0.9986 PASS
mtp_h96_b8_q2_kv342305 main_wide_reduce_w1 549.6 549.3 1.0006 PASS 507.0 506.9 1.0003 PASS
mtp_h96_b8_q4_kv342305 main_wide_reduce_w1 833.4 833.0 1.0005 PASS 769.3 769.3 1.0000 PASS
mtp_h96_b8_q8_kv342305 main_wide_reduce_w1 1613.4 1612.8 1.0003 PASS 1593.6 1593.8 0.9999 PASS
prefill_h12_b1_q2048_kv131072 main_wide_reduce_w1 2378.9 2378.9 1.0000 PASS 2351.6 2351.7 1.0000 PASS
prefill_h12_b2_q512_kv32768 main_wide_reduce_w1 300.5 300.9 0.9988 PASS 278.8 279.3 0.9980 PASS
prefill_h12_b4_q1024_kv8192 main_wide_reduce_w1 309.2 308.8 1.0013 PASS 285.2 285.2 1.0001 PASS
prefill_h96_b1_q2048_kv131072 main_wide_reduce_w1 — — — FAIL: InterleavedTimingError: reportable loaded SM clock 1207/1965 MHz is below the 0.700000 fraction gate — — — FAIL: InterleavedTimingError: reportable loaded SM clock 1192/2032 MHz is below the 0.700000 fraction gate
prefill_h96_b2_q512_kv32768 main_wide_reduce_w1 2402.8 2402.7 1.0000 PASS 2380.4 2379.8 1.0002 PASS

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements

    • Improved numerical stability for FP8 paged attention, including cases with dominant sink scores.
    • Reduced repeated CUDA device setup during kernel execution and updated launch handling.
    • Unified wide-kernel selection across SM100 and SM103 architectures.
  • Tests

    • Added decode coverage across multiple head counts and KV lengths, with checks against an FP32 reference.

References

The shared reference corpus for CAKE kernel development includes the following projects, documentation, and existing CAKE work:

These are corpus-level references. PR-specific implementation details, changes, and benchmark references are documented above.

…tile kernels (sink keys overflowed the FP32 rescale into Inf/NaN rows); regenerate both architectures

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@yyihuang

yyihuang commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@yyihuang

yyihuang commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@flashinfer-bot run tests/mla/test_cake_kimi_k3_mla.py

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4889b27a-4a51-4df6-9ac1-6b608b5b7f46
📥 Commits

Reviewing files that changed from the base of the PR and between ea8135c and af94dfe.

📒 Files selected for processing (25)
  • csrc/cake_kimi_k3_mla/README.md
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_07d8fd7941e0ab779c08_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_07d8fd7941e0ab779c08_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_29b047f732fcfdfeb85b_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_29b047f732fcfdfeb85b_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_2b9895b6bdfdcc6ff476_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_2e4e7903daf6e75c836c_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_2e4e7903daf6e75c836c_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_582ec28a9ab46c704912_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_582ec28a9ab46c704912_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_5d067c89074fe8216a6c_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_9c450b8e82598779f1b2_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_9c450b8e82598779f1b2_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_9f53b9e3e16d64b967c6_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_9f53b9e3e16d64b967c6_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_ab133c42d484f9421945_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_ab133c42d484f9421945_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_b0d0c47e81edc26da19e_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_b0d0c47e81edc26da19e_kernel.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_ea17187e76fd47ab2653_binding.cu
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_ea17187e76fd47ab2653_kernel.cu
  • flashinfer/jit/cake_kimi_k3_mla.py
  • tests/mla/test_cake_kimi_k3_mla.py
💤 Files with no reviewable changes (1)
  • csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_5d067c89074fe8216a6c_binding.cu

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The pull request updates Kimi K3 MLA program mappings and generated CUDA kernels. It changes paged-attention softmax state handling, adds architecture-specific paths to the wide kernel, updates launch bindings, and adds sink-key-row decode tests.

Changes

Kimi K3 MLA

Layer / File(s) Summary
Program mappings and source identifiers
csrc/cake_kimi_k3_mla/README.md, flashinfer/jit/cake_kimi_k3_mla.py
Generated source and program identifiers change. The wide program now maps both sm_100a and sm_103a to one program, with m_tiles and n_full_items in its argument plan.
Paged-attention softmax state and validation
csrc/cake_kimi_k3_mla/*_kernel.cu, tests/mla/test_cake_kimi_k3_mla.py
Five paged-attention kernels track a running maximum and cap the normalization statistic for valid tiles. A new decode test checks sink-key-row cases against the FP32 reference.
Reduction kernel symbols and operations
csrc/cake_kimi_k3_mla/*_kernel.cu, csrc/cake_kimi_k3_mla/*_binding.cu
Reduction kernel and shim identifiers change. Several packed-value conversions and warp shuffles use CUDA intrinsics or C++ operations instead of inline PTX. One generated binding is deleted.
Wide-kernel architecture paths
csrc/cake_kimi_k3_mla/cake_kimi_k3_mla_fp8_paged_attention_ea17187e76fd47ab2653_kernel.cu
The wide kernel adds SM100 score-loading, tail-masking, and maximum-reduction paths. Output scaling uses paired or scalar operations based on architecture.
Paged-attention launch bindings
csrc/cake_kimi_k3_mla/*_binding.cu
Bindings use updated kernel and shim identifiers. Several launchers cache device selection per thread and use cudaLaunchKernelExC with explicit launch configuration.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant KERNELS
  participant Run
  participant CudaKernel
  KERNELS->>Run: select generated program
  Run->>CudaKernel: launch with configured grid, block, stream, and shared memory
  CudaKernel-->>Run: return kernel result
Loading

Suggested reviewers: aleozlx

Merge Risk: ⚪ Minimal · up to af94d

The sink-key fix has no identified merge-blocking issue. The reported GPU test results remain author-reported rather than independently verified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 22 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the kernel fix for lazy E4M3 reference drops and the SM100/SM103 regeneration. It is longer than preferred but remains specific and relevant.
Description check ✅ Passed The description explains the defect, fix, affected routes, tests, performance, and export results in detail. It does not fill out the template’s pre-commit checklist or experimental-track section, but…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 22 functions across 22 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@flashinfer-bot

Copy link
Copy Markdown
Collaborator

GitLab MR !2012 has been created, and the CI pipeline #71909589 is currently running. I'll report back once the pipeline job completes.

@yyihuang
yyihuang merged commit 50a180e into flashinfer-ai:main Oct 6, 2026
23 of 24 checks passed
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.

4 participants