Skip to content

Add fused DeepSeek-V3.2 indexer cache kernel - #3185

Merged
valarLip merged 5 commits into
ROCm:mainfrom
XiaobingSuper:zxb/indexer-fused-cache
May 15, 2026
Merged

valarLip merged 5 commits into
ROCm:mainfrom
XiaobingSuper:zxb/indexer-fused-cache

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add indexer_qk_rope_quant_and_cache, a fused DeepSeek-V3.2 indexer helper that applies Q/K RoPE, K RMSNorm, Q FP8 per-1x128 quantization, indexer weight scaling, and K cache insertion in one kernel path.
  • Expose the new kernel through the Python cache ops API.
  • Add runtime validation for the fused path's supported layout and shape constraints so unsupported model shapes fail at the kernel boundary instead of silently producing wrong results.

Validation

  • Built and JIT-loaded through ATOM DeepSeek-V3.2 serving runs.
  • Re-validated with the companion ATOM integration on MI355, TP=4, DeepSeek-V3.2, ISL=1000, OSL=100.
  • ATOM fused-vs-fallback performance re-validation showed consistent Total Token Throughput gains: CON=4 +5.6%, CON=8 +5.7%, CON=16 +3.5%.
  • ATOM fused path accuracy on gsm8k, 3-shot: flexible-extract 0.9462, strict-match 0.9469.

Notes

Made with Cursor

@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:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
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 3185 --add-label <label>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a fused DeepSeek-V3.2 indexer kernel that combines Q/K RoPE, K RMSNorm, Q FP8 per-1x128 quantization, indexer weight scaling, and K-cache insertion in a single HIP kernel, replacing a multi-step path used by the companion ATOM integration.

Changes:

  • Add indexer_qk_rope_quant_and_cache_kernel and its host wrapper (with shape/layout/device validation) in csrc/kernels/cache_kernels.cu, plus a dispatch macro for the FP8 e4m3 cache type.
  • Declare the new op in csrc/include/cache.h and bind it via pybind in csrc/include/rocm_ops.hpp.
  • Expose the new op in the Python cache ops API via aiter/ops/cache.py.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
csrc/kernels/cache_kernels.cu New fused indexer kernel, dispatch macro, and host entry with validation.
csrc/include/cache.h Function declaration for the new op.
csrc/include/rocm_ops.hpp Pybind binding exposing the op to Python.
aiter/ops/cache.py Python-side compile_ops declaration matching the new C++ signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread csrc/kernels/cache_kernels.cu Resolved
q_amax = block_reduce<float, decltype(max_func), HEAD_DIM, true>(q_amax, max_func);

const float q_fp8_max = static_cast<float>(opus::finfo<cache_t>::max());
const float q_scale = fmaxf(q_amax, 1e-10f) / q_fp8_max;
Comment thread csrc/kernels/cache_kernels.cu Resolved
@valarLip
valarLip merged commit e0bbd84 into ROCm:main May 15, 2026
27 of 30 checks passed
frida-andersson added a commit to frida-andersson/aiter that referenced this pull request Jun 1, 2026
…kernel

The fused `indexer_qk_rope_quant_and_cache_kernel` (added in ROCm#3185) diverged
numerically from the unfused vLLM path it replaces, causing a catastrophic
GSM8K accuracy regression on DeepSeek-V3.2 (0.95 -> 0.49 strict-match,
20-shot) when fusion was enabled. Two independent bugs in this kernel:

1. Q quant skipped UE8M0 rounding and used direct division by a constexpr
   fp8_max. The unfused path is `per_token_group_quant_fp8` in
   vllm/.../fp8_utils.py which (a) multiplies by the reciprocal of fp8_max
   and (b) applies UE8M0 (`scale = 2^ceil(log2(scale))`) when the caller
   passes `scale_fmt = "ue8m0"`. DSv3.2's indexer always passes ue8m0, so
   the fused kernel disagreed with the unfused one on ~94% of Q values
   per call.

   The K-quant block in the same kernel already applied UE8M0 correctly
   (lines ~1429); only the Q block was missing it. Direct division also
   triggers GPU fast-division which adds 1-ULP noise that flips the
   rounded fp8 value at representable boundaries.

2. `norm_weight` and `norm_bias` were typed `scalar_t*`, which in
   production is bf16. vLLM stores `LayerNorm.weight`/`bias` as fp32 and
   `F.layer_norm` consumes them as fp32 in the unfused path. The implicit
   fp32 -> bf16 cast at the call site lost ~16 mantissa bits per element,
   causing K to drift from the unfused reference by 1+ fp8 ULP on ~3% of
   values per call.

Both drifts compounded across the 58 indexer layers in DSv3.2 and broke
long-context accuracy. With both fixes applied (and a separate cache-layout
fix on the vLLM call site), GSM8K strict-match recovered to 0.95.

Changes:

- Apply UE8M0 + multiply-by-reciprocal to the Q scale, mirroring the
  unfused Triton `per_token_group_quant_fp8`.
- Hard-type `norm_weight` and `norm_bias` as `float*` in the kernel
  signature and the launch macro, drop the `static_cast<float>` on each
  load, and assert `dtype == fp32` on the host so the bf16 footgun cannot
  be reintroduced.

ABI note: this tightens the dtype contract for `norm_weight`/`norm_bias`
from "match q.dtype" to "must be fp32". vLLM is the only known caller of
this kernel today and already has fp32 LN params natively, so the change
is benign for it. Any future caller passing bf16 will get a loud host
check failure rather than silent ~3% K drift.
valarLip pushed a commit that referenced this pull request Jul 28, 2026
…kernel (#3451)

* Fix Q UE8M0 quant and require fp32 LN params in fused DSv3.2 indexer kernel

The fused `indexer_qk_rope_quant_and_cache_kernel` (added in #3185) diverged
numerically from the unfused vLLM path it replaces, causing a catastrophic
GSM8K accuracy regression on DeepSeek-V3.2 (0.95 -> 0.49 strict-match,
20-shot) when fusion was enabled. Two independent bugs in this kernel:

1. Q quant skipped UE8M0 rounding and used direct division by a constexpr
   fp8_max. The unfused path is `per_token_group_quant_fp8` in
   vllm/.../fp8_utils.py which (a) multiplies by the reciprocal of fp8_max
   and (b) applies UE8M0 (`scale = 2^ceil(log2(scale))`) when the caller
   passes `scale_fmt = "ue8m0"`. DSv3.2's indexer always passes ue8m0, so
   the fused kernel disagreed with the unfused one on ~94% of Q values
   per call.

   The K-quant block in the same kernel already applied UE8M0 correctly
   (lines ~1429); only the Q block was missing it. Direct division also
   triggers GPU fast-division which adds 1-ULP noise that flips the
   rounded fp8 value at representable boundaries.

2. `norm_weight` and `norm_bias` were typed `scalar_t*`, which in
   production is bf16. vLLM stores `LayerNorm.weight`/`bias` as fp32 and
   `F.layer_norm` consumes them as fp32 in the unfused path. The implicit
   fp32 -> bf16 cast at the call site lost ~16 mantissa bits per element,
   causing K to drift from the unfused reference by 1+ fp8 ULP on ~3% of
   values per call.

Both drifts compounded across the 58 indexer layers in DSv3.2 and broke
long-context accuracy. With both fixes applied (and a separate cache-layout
fix on the vLLM call site), GSM8K strict-match recovered to 0.95.

Changes:

- Apply UE8M0 + multiply-by-reciprocal to the Q scale, mirroring the
  unfused Triton `per_token_group_quant_fp8`.
- Hard-type `norm_weight` and `norm_bias` as `float*` in the kernel
  signature and the launch macro, drop the `static_cast<float>` on each
  load, and assert `dtype == fp32` on the host so the bf16 footgun cannot
  be reintroduced.

ABI note: this tightens the dtype contract for `norm_weight`/`norm_bias`
from "match q.dtype" to "must be fp32". vLLM is the only known caller of
this kernel today and already has fp32 LN params natively, so the change
is benign for it. Any future caller passing bf16 will get a loud host
check failure rather than silent ~3% K drift.

* Add dim/contiguity guards for norm_weight and norm_bias

The kernel indexes norm_weight[dim] / norm_bias[dim] as a flat array;
a non-1D or non-contiguous view would silently produce wrong results.
Add explicit dim() == 1 and is_contiguous() checks before launch so a
bad input is caught immediately rather than corrupting K values.
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