Skip to content

perf(deepseek/v4): qkv_proj_rope tiling + fused rms+rope (decode -56%)#578

Merged
zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
Hzfengsy:perf/dsv4-qkv-proj-rope-tiling-fusion
Jun 23, 2026
Merged

perf(deepseek/v4): qkv_proj_rope tiling + fused rms+rope (decode -56%)#578
zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
Hzfengsy:perf/dsv4-qkv-proj-rope-tiling-fusion

Conversation

@Hzfengsy

Copy link
Copy Markdown
Member

Summary

  • Retile the DeepSeek-V4 qkv_proj_rope projection matmuls to the 512B L2 cache line and fuse RMSNorm with RoPE. Decode end-to-end −56% (a2a3 L2 swimlane, 5-rep median: 936µs → 407µs); golden green on decode and prefill.
  • qr_proj / kv_proj: split-K (zero-seed + atomic-add) with N-tile 32 → 256, so each wq_a/wkv row-read fills a full 512B cache line instead of a 64B sub-line (was 8× weight over-fetch). Kernel occupancy −84% / −75%.
  • qproj_matmul: decouple the matmul N-tile from the dequant N-tile and bump matmul TN 128 → 256 (256B/row), capped by the L0C Acc limit (TM*TN*4 ≤ 128KB). TN=512 needs an M-split (TM=64) and measured no faster end-to-end on device.
  • Fuse per-head RMSNorm + NOPE + RoPE into q_head_rms_nope_rope, and KV RMSNorm + RoPE into kv_rms_norm_rope: inv_rms stays in registers (no GM round-trip via the old q_head_inv_rms_all / kv_inv_rms_tensor), collapsing each pair of dispatches into one. RoPE keeps the interleaved (CANN A3) swap-gather layout.

Related Issues

## What

Cut DeepSeek-V4 qkv_proj_rope decode latency ~56% (a2a3 L2 swimlane, 5-rep
median: 936us -> 407us) by retiling the projection matmuls to the L2 cache
line and fusing per-head/KV RMSNorm with RoPE. Golden green (decode + prefill).

## Changes

- qr_proj / kv_proj: split-K (zero-seed + atomic-add) with N-tile 32 -> 256, so
  each wq_a/wkv row-read fills a 512B L2 line instead of a 64B sub-line (was 8x
  weight over-fetch). qr_proj / kv_proj occupancy -84% / -75%.
- qproj_matmul: decouple the matmul N-tile from the dequant N-tile; bump matmul
  TN 128 -> 256 (256B/row), capped by the L0C Acc limit (TM*TN*4 <= 128KB). TN=512
  needs M-split (TM=64) and measured no better end-to-end on device.
- Fuse per-head RMSNorm + NOPE + RoPE into q_head_rms_nope_rope, and KV RMSNorm +
  RoPE into kv_rms_norm_rope: inv_rms stays in registers (no GM round-trip via the
  old q_head_inv_rms_all / kv_inv_rms_tensor) and each pair collapses to one
  dispatch. RoPE keeps the interleaved (CANN A3) swap-gather layout.
- decode_indexer{,_compressor}: point the rope-pattern cross-reference comments at
  the fused kernel names.
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 251e106f-e9c0-4599-946c-6920d1231176

📥 Commits

Reviewing files that changed from the base of the PR and between 4f0d9b8 and 0b2ba6a.

📒 Files selected for processing (3)
  • models/deepseek/v4/decode_indexer.py
  • models/deepseek/v4/decode_indexer_compressor.py
  • models/deepseek/v4/qkv_proj_rope.py

📝 Walkthrough

Walkthrough

The DeepSeek-V4 QKV projection kernel is rewritten to use split-K atomic-add accumulation for both qr_proj and kv_proj, replacing the prior tiled loop approach. Q-head inv-RMS is fused directly into NOPE and RoPE writebacks, eliminating the intermediate buffer round-trip. KV RMSNorm and RoPE are similarly fused in-kernel with gamma folded before the interleaved swap. Two comment-only lines in the decode indexer files are updated to reference the new kv_rms_norm_rope naming.

Changes

DeepSeek-V4 QKV split-K + fused RMSNorm/RoPE kernel rewrite

Layer / File(s) Summary
Tiling and split-K parameter setup
models/deepseek/v4/qkv_proj_rope.py
Replaces D_TILE, QPROJ_T_TILE, KV_ROPE_T_TILE with new matmul/dequant tile parameters (QPROJ_MM_N_TILE, QPROJ_MM_GROUP, QPROJ_M_TILE, DEQUANT_T_TILE) and adds explicit split-K factors and slice sizes (QR_OK, QR_K_SLICE, KV_OK, KV_K_SLICE) with updated assertions.
split-K qr_proj accumulation
models/deepseek/v4/qkv_proj_rope.py
Replaces the prior qr_fp32 accumulation loop with a core-group-scoped zero-seed step followed by split-K matmuls assembled into qr_fp32 via pl.assemble with atomic=Add.
Q-proj matmul/dequant and fused Q head inv-RMS
models/deepseek/v4/qkv_proj_rope.py
Switches qproj_matmul to QPROJ_MM_N_TILE/QPROJ_MM_GROUP head-group tiling, changes dequantization to DEQUANT_T_TILE token blocks, fuses per-head inv-RMS directly into NOPE/RoPE writeback, and removes the intermediate inv-RMS buffer writeback.
split-K kv_proj + fused KV RMSNorm/RoPE writeback and comment updates
models/deepseek/v4/qkv_proj_rope.py, models/deepseek/v4/decode_indexer.py, models/deepseek/v4/decode_indexer_compressor.py
Rewrites kv_proj to split-K atomic-add into kv_fp32, computes inv-RMS locally per tile, folds inv-RMS and gamma before the in-kernel interleaved swap-gather/sign-indexed RoPE rotation, casts results to BF16 in kv_view, and updates two comments to reference kv_rms_norm_rope instead of kv_rope_fused.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • hw-native-sys/pypto-lib#441: Removes intermediate Q rope buffers and restructures the fused KV RoPE loop/parallelization in the same qkv_proj_rope path modified here.
  • hw-native-sys/pypto-lib#480: Changes the interleaved swap/index/sign and cosine/sine application logic in models/deepseek/v4/qkv_proj_rope.py, directly overlapping with this PR's in-kernel RoPE writeback changes.
  • hw-native-sys/pypto-lib#532: Makes substantial rewrites to models/deepseek/v4/qkv_proj_rope.py shared QKV+RoPE projection/kernel logic, the same file being significantly reworked here.

Suggested labels

enhancement

🐇 Split-K seeds the zeros with care,
Atomic adds float through the air,
Inv-RMS folded in, gamma too,
One pass for NOPE and RoPE — it's true!
No buffers remain, just kernels that zoom 🚀

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. 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 directly reflects the main optimization: qkv_proj_rope retiling and RMSNorm+RoPE fusion with the achieved 56% decode latency improvement.
Description check ✅ Passed The description provides a detailed summary of the changes including matmul retiling, split-K implementation, RMSNorm+RoPE fusion, and performance improvements, all of which align with the changeset.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@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 optimizes the DeepSeek v4 decode kernels by decoupling projection matmul tile sizes, implementing split-K parallelization for qr_proj and kv_proj, and fusing RMSNorm, NOPE writeback, and interleaved RoPE rotations to eliminate global memory round-trips. The review feedback suggests peeling the first iteration out of the pipelined loops in both qr_proj_matmul and kv_proj_matmul to avoid conditional checks inside pl.pipeline, which can hinder software pipelining or cause compilation issues on CANN/Ascend hardware.

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/qkv_proj_rope.py
Comment thread models/deepseek/v4/qkv_proj_rope.py
@zhangqi-chen zhangqi-chen merged commit 777675c into hw-native-sys:main Jun 23, 2026
5 of 7 checks passed
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.

2 participants