[CUDA] Add QKNormRotaryEmbedding and InverseRotaryRegroup contrib operators - #32125
Open
Tianlei Wu (tianleiwu) wants to merge 2 commits into
Open
[CUDA] Add QKNormRotaryEmbedding and InverseRotaryRegroup contrib operators#32125Tianlei Wu (tianleiwu) wants to merge 2 commits into
Tianlei Wu (tianleiwu) wants to merge 2 commits into
Conversation
…rators QKNormRotaryEmbedding prepares a query and a shared latent KV row for MLA-style attention: per-head RMS norm on the query, weighted RMS norm on the latent row, partial rotary on both, and the optional simulated FP8 round trip the cache expects. InverseRotaryRegroup undoes that rotation on the attention output and regroups the heads for a grouped output projection.
Tianlei Wu (tianleiwu)
requested review from
Akshay Sonawane (apsonawane),
Justin Chu (justinchuby) and
kunal-vaishnavi
August 18, 2026 16:58
| * <a href="#com.microsoft.PagedAttention">com.microsoft.PagedAttention</a> | ||
| * <a href="#com.microsoft.QAttention">com.microsoft.QAttention</a> | ||
| * <a href="#com.microsoft.QGemm">com.microsoft.QGemm</a> | ||
| * <a href="#com.microsoft.QKNormRotaryEmbedding">com.microsoft.QKNormRotaryEmbedding</a> |
Contributor
There was a problem hiding this comment.
Instead of naming it QKNormRotaryEmbedding, could we do something more generic like AttentionQK, QKProjection, or QKSubgraph? The QK subgraph keeps changing per model and this op name would only reflect certain fused ops.
|
|
||
| ### <a name="com.microsoft.InverseRotaryRegroup"></a><a name="com.microsoft.inverserotaryregroup">**com.microsoft.InverseRotaryRegroup**</a> | ||
|
|
||
| Undoes the rotation QKNormRotaryEmbedding applied to the query and regroups the heads for a grouped output projection. |
Contributor
There was a problem hiding this comment.
Can we not fuse this op inside the QK op?
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.
Description
Adds two CUDA contrib operators for the query/KV preparation around MLA-style attention.
QKNormRotaryEmbeddingtakes the raw query and latent KV projections up to the attention kernel — head split, RMS norm, rotary embedding and optional FP8 cache simulation — in one launch.InverseRotaryRegroupundoes that rotation on the attention output and regroups the heads for a grouped output projection.Both are ~10-node primitive subgraphs whose per-node cost is dominated by launch overhead at decode, and the norms have precision contracts (which intermediate is rounded to
T, and where) that are easy to get subtly wrong when the subgraph is rebuilt by hand. The schemas state those contracts explicitly.Summary of Changes
QKNormRotaryEmbeddingonnxruntime/contrib_ops/cuda/math/qk_norm_rotary.{h,cc}OpKernel— attributes, validation, output shapesonnxruntime/contrib_ops/cuda/math/qk_norm_rotary_impl.{h,cu}Tbefore the multiply, and the multiply is done inT, matching the unfused subgraph.T.cos/sinare stored pre-interleaved, so the rotation is the signed swap of each adjacent pair:simulate_fp8, the leadinghead_dim - rope_head_dimchannels of the KV row take a simulated FP8-E4M3 round trip in blocks of 64 with a power-of-two scale — what a quantized attention cache will store.InverseRotaryRegrouponnxruntime/contrib_ops/cuda/math/inverse_rotary_regroup.{h,cc}OpKernelonnxruntime/contrib_ops/cuda/math/inverse_rotary_regroup_impl.{h,cu}(tokens, num_heads * head_dim)into(num_groups, tokens, group_dim). Both views index the same flat channel, so this is only a change of addressing and is folded into the same kernel.Shared
onnxruntime/core/graph/contrib_ops/contrib_defs.cconnxruntime/core/graph/contrib_ops/ms_opset.honnxruntime/contrib_ops/cuda/cuda_contrib_kernels.ccdocs/ContribOperators.md,docs/OperatorKernels.mdTesting
onnxruntime/test/python/transformers/test_qk_norm_rotary.py— PyTorch references for both operators over float32/float16/bfloat16 and several head/dim configurations, with and withoutsimulate_fp8, plus a forward/inverse rotary round-trip test.Additive only — no existing operator, schema or kernel is modified.
Checklist