Conversation
Signed-off-by: xiaogang.zhou <xiaogang.zhou@bytedance.com>
Signed-off-by: xiaogang.zhou <xiaogang.zhou@bytedance.com>
Signed-off-by: xiaogang.zhou <xiaogang.zhou@bytedance.com>
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
📝 WalkthroughWalkthroughSM120 NVFP4 attention now supports grouped-query attention by tracking query and key/value head counts separately through preprocessing, validation, quantization, CUDA parameter packing, KV tile loading, and accuracy tests. ChangesGrouped-query attention support
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant QKV as QKV tensors
participant Python as nvfp4_attention_sm120_quantize_qkv
participant Binding as nvfp4_attention_sm120_fwd
participant Mainloop as CollectiveMainloopFwd
participant KVLoaders as KLoader and VLoader
QKV->>Python: q, k, v with separate head counts
Python->>Binding: quantized tensors, scales, qk_correction
Binding->>Mainloop: h, h_k, h_h_k_ratio
Mainloop->>KVLoaders: load KV tiles using bidkv
KVLoaders-->>Binding: attention output and lse
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/attention/test_nvfp4_attention_sm120.py (1)
194-202: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winGQA test config doesn't exercise multi-KV-head remapping.
With
num_kv_heads=1,h_h_k_ratio = num_q_heads // num_kv_heads = 4, sobidkv = bidh // h_h_k_ratiois0for every validbidh(0..3). This effectively tests MQA (single shared KV head), not general GQA — the newbidkvindexing logic inmainloop.cuh/load_k.cuh/load_v.cuhforbidkv > 0(multiple distinct KV head groups) is left unverified by this test.✅ Suggested config change to exercise multiple KV-head groups
- batch, num_q_heads, num_kv_heads, seq_len, head_dim = 1, 4, 1, 256, 128 + batch, num_q_heads, num_kv_heads, seq_len, head_dim = 1, 6, 2, 256, 128🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/attention/test_nvfp4_attention_sm120.py` around lines 194 - 202, Update the test configuration around the manual seed and tensor setup so num_kv_heads is greater than 1 and still divides num_q_heads, such as selecting two KV heads for four query heads. Preserve the existing tensor shapes and dtypes while ensuring valid query heads map to both bidkv 0 and bidkv 1, exercising multi-KV-head GQA remapping.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@flashinfer/nvfp4_attention_sm120.py`:
- Around line 377-379: Clarify the docstring describing q_fp4 and k_fp4 layouts
so q_fp4 uses num_q_heads while k_fp4 uses num_kv_heads, without grouping them
under a shared num_q_heads shape. Preserve the existing v_fp4_t and v_scale_t
transposed layout description.
---
Nitpick comments:
In `@tests/attention/test_nvfp4_attention_sm120.py`:
- Around line 194-202: Update the test configuration around the manual seed and
tensor setup so num_kv_heads is greater than 1 and still divides num_q_heads,
such as selecting two KV heads for four query heads. Preserve the existing
tensor shapes and dtypes while ensuring valid query heads map to both bidkv 0
and bidkv 1, exercising multi-KV-head GQA remapping.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 496e80b1-db38-4a5f-aa77-27edc0699601
📒 Files selected for processing (7)
csrc/nvfp4_attention_sm120/nvfp4_attention_sm120_binding.cuflashinfer/nvfp4_attention_sm120.pyinclude/flashinfer/attention/sm120/nvfp4_attention_sm120/api/launcher.hinclude/flashinfer/attention/sm120/nvfp4_attention_sm120/compute/mainloop.cuhinclude/flashinfer/attention/sm120/nvfp4_attention_sm120/compute/producer/load_k.cuhinclude/flashinfer/attention/sm120/nvfp4_attention_sm120/compute/producer/load_v.cuhtests/attention/test_nvfp4_attention_sm120.py
| ``[batch, num_q_heads, seq_len, head_dim / 2]``; ``k_fp4`` uses | ||
| ``num_kv_heads``. ``v_fp4_t`` and ``v_scale_t`` are stored transposed as | ||
| ``[batch, num_kv_heads, head_dim, packed_seq_len]``. |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Confusing docstring wording for q_fp4/k_fp4 layout.
The sentence says "q_fp4 and k_fp4 use layout [batch, num_q_heads, ...]; k_fp4 uses num_kv_heads" — it groups k_fp4 with the num_q_heads layout and then immediately contradicts that in the next clause.
✏️ Suggested docstring fix
The packed tensors should be produced by
- :func:`nvfp4_attention_sm120_quantize_qkv`. ``q_fp4`` and ``k_fp4`` use layout
- ``[batch, num_q_heads, seq_len, head_dim / 2]``; ``k_fp4`` uses
- ``num_kv_heads``. ``v_fp4_t`` and ``v_scale_t`` are stored transposed as
+ :func:`nvfp4_attention_sm120_quantize_qkv`. ``q_fp4`` uses layout
+ ``[batch, num_q_heads, seq_len, head_dim / 2]``; ``k_fp4`` uses the same
+ layout with ``num_kv_heads`` in place of ``num_q_heads``. ``v_fp4_t`` and
+ ``v_scale_t`` are stored transposed as
``[batch, num_kv_heads, head_dim, packed_seq_len]``.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ``[batch, num_q_heads, seq_len, head_dim / 2]``; ``k_fp4`` uses | |
| ``num_kv_heads``. ``v_fp4_t`` and ``v_scale_t`` are stored transposed as | |
| ``[batch, num_kv_heads, head_dim, packed_seq_len]``. | |
| The packed tensors should be produced by | |
| :func:`nvfp4_attention_sm120_quantize_qkv`. ``q_fp4`` uses layout | |
| ``[batch, num_q_heads, seq_len, head_dim / 2]``; ``k_fp4`` uses the same | |
| layout with ``num_kv_heads`` in place of ``num_q_heads``. ``v_fp4_t`` and | |
| ``v_scale_t`` are stored transposed as | |
| ``[batch, num_kv_heads, head_dim, packed_seq_len]``. |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@flashinfer/nvfp4_attention_sm120.py` around lines 377 - 379, Clarify the
docstring describing q_fp4 and k_fp4 layouts so q_fp4 uses num_q_heads while
k_fp4 uses num_kv_heads, without grouping them under a shared num_q_heads shape.
Preserve the existing v_fp4_t and v_scale_t transposed layout description.
📌 Description
Add GQA support to the SM120/SM121 NVFP4 attention path.
This PR allows
qto usenum_q_headswhilek/vusenum_kv_heads, withnum_q_heads % num_kv_heads == 0. The kernel now maps each Q head to its shared KV head usingq_head / h_h_k_ratio, while keeping outputs, LSE, Q scale, andqk_correctionaligned with Q heads.🔍 Related Issues
Related to #3809
🚀 Pull Request Checklist
Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.
✅ Pre-commit Checks
pre-commitby runningpip install pre-commit(or used your preferred method).pre-commit install.pre-commit run --all-filesand fixed any reported issues.🧪 Tests
unittest, etc.).Added a GQA coverage case for
nvfp4_attention_sm120.Reviewer Notes
The main logic to review is the Q-head to KV-head mapping:
q,q_scale,qk_correction,out, andlsestay aligned with Q heads;k,v,k_scale, andv_scaleare aligned with KV heads.Summary by CodeRabbit
New Features
Bug Fixes
Tests