feat: relax heads_per_group constraint to support 1~8 for BF16 and FP8 decode attention - #58
Open
Religious-J wants to merge 1 commit into
Open
feat: relax heads_per_group constraint to support 1~8 for BF16 and FP8 decode attention#58Religious-J wants to merge 1 commit into
Religious-J wants to merge 1 commit into
Conversation
…8 decode attention
Previously heads_per_group was restricted to {4, 8}. This change extends
support to all integer values in [1, 8] with zero kernel changes, relying
on SM90 TMA hardware behavior: OOB loads are zero-filled and OOB stores
are silently discarded by the hardware tensor-box clamp.
Changes:
- entry.cc: relax TORCH_CHECK from {4,8} to [1,8] for BF16 and FP8 paths
- smallm_bf16_dim128_static.cu: add upper-bound guard (>8 returns false)
- 4 FP8 launch files (static/dynamic × 2 quant types): same guard update
- 3 test files: extend kv_head_q_head to cover hpg ∈ {1,2,3,4,5,6,7,8}
Verified: 720 BF16 + 1728 FP8 tests pass. Benchmark confirms hpg=1~8
produce identical static latency (< 1% variance)
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Relax the
heads_per_group(=num_head_q / num_head_kv) restriction from{4, 8}to[1, 8]for BF16 and FP8 decode attention kernels, enabling models with arbitrary GQA ratios (including MHA where hpg=1).Background
The constraint existed because
kHeadsPerGroup=8is hardcoded in the SM90 kernel launch path as the TMA Q/Y tile size. Forhpg < 8, the tile over-extends the actual tensor — but this is safe due to SM90 TMA hardware guarantees:The zero-filled Q rows (Q=0) produce garbage attention outputs for those positions, but those outputs are always discarded by the TMA Y store clamp.
Softmax correctness is preserved because
gMax/gSumare maintained independently per M-row — the ghost rows'max=0cannot contaminate valid rows.Changes
src/attention/entry.cc: relax twoTORCH_CHECKguards fromhpg == 4 || hpg == 8tohpg >= 1 && hpg <= 8(one for BF16, one for FP8)smallm_bf16_dim128_static.cu: addhpg > 8 → return falseguard in the launch function to prevent silent mis-computation!= 8 && != 4to< 1 || > 8kv_head_q_headto cover hpg ∈ {1,2,3,4,5,6,7,8}Test Results
Benchmark
Static splitk,
qpertoken_perhead_kvpertensor, KV=4 fixed, 100 iterations:hpg means
heads_per_group = num_head_q / num_head_kv,latency in µs (lower is better)