Skip to content

perf(dsv4 decode swa): deferred attention RMSNorm and dep cleanup#791

Open
zhangqi-chen wants to merge 4 commits into
hw-native-sys:mainfrom
zhangqi-chen:swa-perf
Open

perf(dsv4 decode swa): deferred attention RMSNorm and dep cleanup#791
zhangqi-chen wants to merge 4 commits into
hw-native-sys:mainfrom
zhangqi-chen:swa-perf

Conversation

@zhangqi-chen

Copy link
Copy Markdown
Collaborator

Summary

  • Localize decode QKV projection kernels into decode_attention_swa so the SWA decode path is self-contained.
  • Deferred attention RMSNorm (attn_norm_xgamma): the pre-qkv inv_rms is a per-token positive scalar, and qkv_proj_rope re-RMSNorms BOTH the qr and kv projections downstream, so inv_rms cancels exactly in every qkv output (q / kv / qr int8 / qr_scale). SWA's normalized input feeds only qkv, so inv_rms is dropped entirely and just xg = x*gamma is stored — removing the sq_sum reduction, the rsqrt, and the whole normalize pass. Being pure elementwise (no reduction), it needs no physical-row tile-floor workaround, so it is parallelized to one token per core with a wide D-tile. The now-unused local rms_norm is removed.
    • Not applied to HCA/CSA: there the normalized input also feeds the compressor softmax gate, which is scale-sensitive and needs inv_rms.
  • Defer inv_rms in qr_rms_norm_quant (same transform as the gate ffn_norm): symmetric int8 quant of qr*gamma cancels inv_rms exactly, so the quant uses amax(qr*gamma) directly and inv_rms rides only qr_scale (dequant). int8 output unchanged.
  • Dependency-chain cleanup (guided by dep-gen deps.json):
    • Drop allow_early_resolve from q_rope_prepare (not on the qkv critical chain).
    • Remove explicit deps that duplicate tensormap (data) edges, which already order the tasks: qk_pv deps=[gather_tids[0]] and merge_norm deps=[qk_tid, rope_tid]. deps.json confirms only the tensormap edges remain.

Validated on real a2a3 (ptoas 0.48): x_out and kv_cache PASS. The attention-norm task drops from ~10.8us (rms_norm) to ~2.6us (attn_norm_xgamma, 1 token/core). At start_pos=8k the hc_post finish is ~281us and stable (10 runs, 276.6-285.1us).

Related Issues

None

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5850ad8f-6dbc-45cd-8aff-87876b8cb6b0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

CSA and SWA decode paths now inline projection and normalization kernels with local Torch references. Sparse SWA task stages no longer use explicit dependency tokens for gather, RoPE, and merge operations.

Changes

Decode kernel updates

Layer / File(s) Summary
CSA inline kernels and references
models/deepseek/v4/decode_attention_csa.py
Adds local RMSNorm and fused QKV/RoPE kernels with quantized split-K projections, plus matching Torch golden helpers.
SWA inline kernels and normalization
models/deepseek/v4/decode_attention_swa.py
Adds local projection and deferred normalization kernels, updates the normalization barrier, and uses local Torch golden helpers.
Sparse SWA dependency wiring
models/deepseek/v4/decode_sparse_attn_swa.py
Removes explicit gather, RoPE, and merge-stage dependency token plumbing while retaining early resolution settings.

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

Possibly related PRs

Poem

I’m a rabbit hopping through kernels bright,
Folding RoPE and norms into flight.
Quantized Qs and KV streams align,
While dependency tokens vanish in line.
Thump-thump, the decode paths run—
A tidy hare’s work, neatly done!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: deferred attention RMSNorm plus dependency cleanup in SWA decode.
Description check ✅ Passed The description is detailed and directly matches the localized kernels, deferred RMSNorm, and dependency cleanup changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request inlines the rms_norm, attn_norm_xgamma, and qkv_proj_rope functions directly into the CSA and SWA attention decode files, adds PyTorch golden reference implementations for testing, and cleans up unused task IDs and dependencies in SWA sparse attention. The review feedback suggests optimizing qr_rms_norm_quant in the CSA path by deferring inv_rms and avoiding expensive vector reciprocal instructions, as well as removing allow_early_resolve=True from q_rope_prepare for consistency and reduced overhead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread models/deepseek/v4/decode_attention_csa.py Outdated
Comment thread models/deepseek/v4/decode_attention_csa.py Outdated
- Copy RMSNorm and QKV projection/RoPE kernels into SWA and CSA decode modules
- Specialize the copied signatures for the fixed decode token count
- Keep Torch golden helpers local to remove shared module dependencies

Standalone SWA and CSA tests pass with PTOAS 0.48. The combined decode
layer still reports duplicate local kernel names and is intentionally held.
Port of the qkv-only deferred attention RMSNorm to swa-perf's self-contained
decode_attention_swa. The pre-qkv inv_rms is a per-token positive scalar, and
qkv_proj_rope re-RMSNorms BOTH qr and kv, so inv_rms cancels exactly in q/kv/qr
/qr_scale. SWA's x_normed feeds only qkv, so we drop inv_rms entirely: new
attn_norm_xgamma stores just xg = x*gamma, replacing rms_norm (which is removed
-- it had no remaining caller).

Being pure elementwise (no reduction), it needs no physical-8 tile-floor trick --
each core writes its own [1, XG_D_TILE] rows. Parallelized to one token per core
(pl.spmd(t_dim)) with a wide XG_D_TILE=1024.

attn_norm_xgamma Exec ~2.8us (vs rms_norm ~10.8us). decode_attention_swa a2a3
PASS (x_out, kv_cache), ptoas 0.48. hc_post finish ~272us.
Dependency-chain cleanup guided by deps.json (dep-gen):

- decode_attention_swa: drop allow_early_resolve from q_rope_prepare; it is not
  on the qkv critical chain, so it need not be early-dispatched.

- decode_sparse_attn_swa: remove explicit deps that duplicate tensormap (data)
  edges -- the scheduler already orders these through the tensor dependency:
  * qk_pv       deps=[gather_tids[0]]  (reads swa_kv_flat; affine writes tracked)
  * merge_norm  deps=[qk_tid, rope_tid] (reads qk_pv sparse_blk_* + rope_cs rope_*)
  Dead tid captures (gather_tid/qk_tid/rope_tid) removed; merge_tid kept (proj_a_mm).

deps.json confirms the explicit edges are gone (tensormap edges remain).
decode_attention_swa a2a3 PASS (x_out, kv_cache) 5/5, ptoas 0.48.
Symmetric int8 quant of qr*gamma cancels the per-token inv_rms exactly, so the
quant pass can use amax(qr*gamma) directly and inv_rms rides only qr_scale
(dequant) -- same transform as gate ffn_norm 37d5dee. Drops the redundant
row_expand_mul(inv_rms) from the quant pipeline; int8 output unchanged.

Marginal (qr_rms_norm_quant ~5.1->~5.0us; within hc_post noise) but strictly
less work. decode_attention_swa a2a3 PASS (x_out, kv_cache), ptoas 0.48.
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