Repository navigation
[Triton/Gluon] Support Muse QK norm scale in fused RoPE cache op - #6290
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
Caller-provided outputs remain unimplemented, and the hardcoded tuning and coverage gaps should be addressed.
4 open findings
What changed in this PR
Extends the fused QKV norm/RoPE/cache Triton path with Muse-compatible query scaling and BF16 rounding.
Changes:
- Adds
q_scaleand gfx950 token-tile selection. - Implements scaled Q and rounded Q/K RoPE arithmetic.
- Adds focused Muse correctness tests.
| File | Description |
|---|---|
aiter/ops/triton/rope/fused_qkv_split_qk_norm_rope_cache.py |
Adds scaling API and tile selection. |
aiter/ops/triton/_triton_kernels/rope/fused_qkv_split_qk_norm_rope_cache.py |
Implements scaling and rounded RoPE products. |
op_tests/triton_tests/rope/test_fused_qkv_split_qk_rope.py |
Tests Muse BF16 behavior and cache writes. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
azaidy
left a comment
There was a problem hiding this comment.
Can you add a benchmark as well?
- _check_inputs raises ValueError instead of assert, so python -O can't let a short l4 through to the kernel - the fwd and bwd kernel repr now carry W1/W12/W4/SLOPE next to BLOCK_SIZE_N (SLOPE needs the float handling from ROCm#6290) - the bench counts the grad_l12 write in the fwd_bwd bytes (3*W12 + 2*W4) - the empty-input test runs the backward too Signed-off-by: adlashab <[email protected]>
* add block_cat_fused fused op Adds torch.ops.aiter.block_cat_fused, which packs cat([leaky_relu(l12[..., :W1], slope), l12[..., W1:] * l4], dim=-1) into one custom op. Forward runs a single fused Triton kernel on the bf16/fp16 GPU path with an eager torch fallback for CPU/fp32. The kernels store their result in the input dtype (via the output pointer's element type) so an fp16 input stays fp16 with no silent bf16 downcast. The registered backward is a second fused Triton kernel that produces grad_l12 and grad_l4 together, so AOTAutograd dispatches one fused backward target instead of decomposing into per-half pointwise grads. Op-level forward/backward correctness tests cover the CPU fp32 fallback and the GPU bf16 and fp16 Triton paths against an eager reference. Signed-off-by: adlashab <[email protected]> * block_cat_fused: fix backward stride corruption, add random-grad test, int64 index guard Three fixes on the block_cat_fused op: - backward allocated grad_l12/grad_l4 with torch.empty_like, which keeps a non-contiguous (e.g. transposed) input's strides while the kernel writes flat-contiguous. A permuted l12/l4 then got the flat writes scattered under the wrong strides and silently wrong grads. Allocate the grad outputs with contiguous_format so they match what the kernel writes. - the flat index math (pid * XBLOCK, row * W4 + col) is int32 and wraps into an out-of-bounds fault once M * W12 >= 2^31. Promote the offset to int64 in both the forward and backward kernels. - the backward tests only used out.sum().backward(), i.e. an all-ones upstream grad, so a kernel that drops the grad_cat factor still passed. Add a backward test with a random upstream grad and a non-contiguous-input backward test, both compared against eager autograd. Signed-off-by: adlashab <[email protected]> * block_cat_fused: move into the triton fusions layout Kernels go to aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.py, wrapper and custom ops to aiter/ops/triton/fusions/block_cat_fused.py, test to op_tests/triton_tests/fusions/, plus bench_block_cat_fused.py, same as the other fusion ops. The xnumel/XBLOCK builders are replaced by module level kernels with make_kernel_repr. Kernel math is unchanged, launch configs moved to configs/<arch>/triton/fusions/block_cat_fused/DEFAULT.json with the same values as before. The gfx1250 DEFAULT.json is a byte-identical copy of gfx950. Review fixes on top: - l4 has to match l12's device and dtype on every path, so mixed bf16/fp32 asserts instead of silently differing from eager type promotion - fake impls return contiguous tensors like the real fwd and bwd do - the fwd/bwd custom ops register through torch_compile_guard and autograd goes through a torch.autograd.Function; block_cat_fused is exported from aiter.ops.triton.fusions Signed-off-by: adlashab <[email protected]> * block_cat_fused: raise on bad inputs, full kernel repr, bwd byte count - _check_inputs raises ValueError instead of assert, so python -O can't let a short l4 through to the kernel - the fwd and bwd kernel repr now carry W1/W12/W4/SLOPE next to BLOCK_SIZE_N (SLOPE needs the float handling from #6290) - the bench counts the grad_l12 write in the fwd_bwd bytes (3*W12 + 2*W4) - the empty-input test runs the backward too Signed-off-by: adlashab <[email protected]> --------- Signed-off-by: adlashab <[email protected]>
* add block_cat_fused fused op Adds torch.ops.aiter.block_cat_fused, which packs cat([leaky_relu(l12[..., :W1], slope), l12[..., W1:] * l4], dim=-1) into one custom op. Forward runs a single fused Triton kernel on the bf16/fp16 GPU path with an eager torch fallback for CPU/fp32. The kernels store their result in the input dtype (via the output pointer's element type) so an fp16 input stays fp16 with no silent bf16 downcast. The registered backward is a second fused Triton kernel that produces grad_l12 and grad_l4 together, so AOTAutograd dispatches one fused backward target instead of decomposing into per-half pointwise grads. Op-level forward/backward correctness tests cover the CPU fp32 fallback and the GPU bf16 and fp16 Triton paths against an eager reference. Signed-off-by: adlashab <[email protected]> * block_cat_fused: fix backward stride corruption, add random-grad test, int64 index guard Three fixes on the block_cat_fused op: - backward allocated grad_l12/grad_l4 with torch.empty_like, which keeps a non-contiguous (e.g. transposed) input's strides while the kernel writes flat-contiguous. A permuted l12/l4 then got the flat writes scattered under the wrong strides and silently wrong grads. Allocate the grad outputs with contiguous_format so they match what the kernel writes. - the flat index math (pid * XBLOCK, row * W4 + col) is int32 and wraps into an out-of-bounds fault once M * W12 >= 2^31. Promote the offset to int64 in both the forward and backward kernels. - the backward tests only used out.sum().backward(), i.e. an all-ones upstream grad, so a kernel that drops the grad_cat factor still passed. Add a backward test with a random upstream grad and a non-contiguous-input backward test, both compared against eager autograd. Signed-off-by: adlashab <[email protected]> * block_cat_fused: move into the triton fusions layout Kernels go to aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.py, wrapper and custom ops to aiter/ops/triton/fusions/block_cat_fused.py, test to op_tests/triton_tests/fusions/, plus bench_block_cat_fused.py, same as the other fusion ops. The xnumel/XBLOCK builders are replaced by module level kernels with make_kernel_repr. Kernel math is unchanged, launch configs moved to configs/<arch>/triton/fusions/block_cat_fused/DEFAULT.json with the same values as before. The gfx1250 DEFAULT.json is a byte-identical copy of gfx950. Review fixes on top: - l4 has to match l12's device and dtype on every path, so mixed bf16/fp32 asserts instead of silently differing from eager type promotion - fake impls return contiguous tensors like the real fwd and bwd do - the fwd/bwd custom ops register through torch_compile_guard and autograd goes through a torch.autograd.Function; block_cat_fused is exported from aiter.ops.triton.fusions Signed-off-by: adlashab <[email protected]> * block_cat_fused: raise on bad inputs, full kernel repr, bwd byte count - _check_inputs raises ValueError instead of assert, so python -O can't let a short l4 through to the kernel - the fwd and bwd kernel repr now carry W1/W12/W4/SLOPE next to BLOCK_SIZE_N (SLOPE needs the float handling from #6290) - the bench counts the grad_l12 write in the fwd_bwd bytes (3*W12 + 2*W4) - the empty-input test runs the backward too Signed-off-by: adlashab <[email protected]> --------- Signed-off-by: adlashab <[email protected]>

Summary
Muse Glimmer scales BF16-rounded, weightless RMSNorm queries before RoPE. The existing fused QKV norm/RoPE/cache operator did not reproduce that arithmetic, so Muse could not use the fusion.
This adds an optional
q_scalewhile keepingq_scale=1on the existing arithmetic path. The wrapper allocates and returns Q/K/V outputs internally and writes K/V into the supplied paged caches; it does not accept caller-provided Q/K/V output buffers.Changes
q_scale, round again, then use BF16-rounded RoPE products for the scaled path.block_tavailable for an explicit override.Q_SCALEin the kernel trace name and sanitize non-integral float values into valid Triton identifiers.T > 128tile.Validation
pytest -q op_tests/triton_tests/rope/test_fused_qkv_split_qk_rope.py -k with_cacheon gfx950: 771 passed, 256 skipped. The three scaled-Q cases passed.py_compile, andgit diff --checkpassed for the changed files.At 8192 tokens, the maximum absolute difference from the compiled BF16 baseline was 0.25 for Q and 0.03125 for K/cache; V and cached V matched exactly. Model-level lm-eval and a vLLM A/B isolating this fusion have not been run. vLLM integration is separate from this library PR.