Skip to content

[CUDA] Add RMSNormRotary contrib operator - #33469

Open
Akshay Sonawane (apsonawane) wants to merge 9 commits into
mainfrom
asonawane/main-ops-normalization-rotary
Open

Akshay Sonawane (apsonawane) wants to merge 9 commits into
mainfrom
asonawane/main-ops-normalization-rotary

Conversation

@apsonawane

@apsonawane Akshay Sonawane (apsonawane) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the com.microsoft::RMSNormRotary (version 1) CUDA contrib operator for Qwen3.8-Flash-Next QSA query/key normalization and partial rotary embedding:

  • com.microsoft::RMSNormRotary (onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.{h,cc,cu}):
    • Fuses ONNX-arithmetic BF16 RMS normalization ([B, S, heads, stride] slice [input_start : input_start + input_width]) with non-interleaved partial RoPE (rotary_embedding_dim) and flattened [B, S, heads * width] output in a single 64-thread 2-warp shuffle kernel (RMSNormRotaryKernel).
    • Supports both decode FP32 rotary arithmetic (use_fp32_arithmetic=1) and prefill arithmetic.
  • Cleanup:
    • Removed unused RotaryEmbedding(ver 2) and BranchwiseRMSNorm(ver 2) changes (BranchwiseRMSNorm(ver 1) on main already supports BFloat16 scales).

Copilot AI 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.

🟡 Changes recommended

Exact arithmetic is not preserved across all optimization and determinism paths, and generated documentation remains inconsistent.

4 open issues
What changed in this PR

Adds opt-in exact arithmetic to ONNX Runtime’s normalization and rotary operators while preserving default dispatch.

Changes:

  • Adds exact RMS arithmetic, widened output, and version-2 packed-input slicing.
  • Adds version-2 CUDA rotary arithmetic and provider restrictions.
  • Adds regression tests and operator documentation.
File Description
orttraining/​orttraining/​core/​graph/​training_op_defs.cc Allows Microsoft opset 2 in training.
onnxruntime/​test/​contrib_ops/​rotary_embedding_op_test.cc Adds rotary parity and validation tests.
onnxruntime/​test/​contrib_ops/​hyper_connection_op_test.cc Adds exact RMS and packed-input tests.
onnxruntime/​core/​session/​environment.cc Allows Microsoft opset 2.
onnxruntime/​core/​providers/​cuda/​reduction/​reduction_functions.h Declares squared-input reduction.
onnxruntime/​core/​providers/​cuda/​reduction/​reduction_functions.cu Implements strided squared-input reduction.
onnxruntime/​core/​graph/​contrib_ops/​ms_opset.h Registers version-2 schemas.
onnxruntime/​core/​graph/​contrib_ops/​bert_defs.cc Defines arithmetic modes and output shapes.
onnxruntime/​contrib_ops/​webgpu/​bert/​branchwise_rms_norm.cc Rejects unsupported exact arithmetic.
onnxruntime/​contrib_ops/​cuda/​cuda_contrib_kernels.cc Registers version-2 CUDA kernels.
onnxruntime/​contrib_ops/​cuda/​bert/​rotary_embedding.h Stores arithmetic mode.
onnxruntime/​contrib_ops/​cuda/​bert/​rotary_embedding.cc Validates and forwards rotary mode.
onnxruntime/​contrib_ops/​cuda/​bert/​rotary_embedding_impl.h Extends rotary launcher interfaces.
onnxruntime/​contrib_ops/​cuda/​bert/​rotary_embedding_impl.cu Implements separately rounded FP32 operations.
onnxruntime/​contrib_ops/​cuda/​bert/​branchwise_rms_norm.h Stores arithmetic and slicing settings.
onnxruntime/​contrib_ops/​cuda/​bert/​branchwise_rms_norm.cc Validates slices and dispatches exact RMS.
onnxruntime/​contrib_ops/​cuda/​bert/​branchwise_rms_norm_impl.h Declares exact RMS launcher.
onnxruntime/​contrib_ops/​cuda/​bert/​branchwise_rms_norm_impl.cu Implements exact RMS and widened output.
onnxruntime/​contrib_ops/​cpu/​bert/​branchwise_rms_norm.h Stores CPU arithmetic mode.
onnxruntime/​contrib_ops/​cpu/​bert/​branchwise_rms_norm.cc Adds explicit division and widened output.
docs/​ContribOperators.md Documents arithmetic modes and version restrictions.

Review effort: Balanced

Comment thread docs/ContribOperators.md Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/branchwise_rms_norm_impl.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/branchwise_rms_norm_impl.cu Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated

Copilot AI 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.

🔵 Needs a closer look

Optional-output handling and CPU rounding need correction, and FP32 rotary rounding needs targeted regression coverage.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Exact RMS accumulation allows FMA contraction

onnxruntime/​contrib_ops/​cpu/​bert/​branchwise_rms_norm.cc:107

The new exact CPU path computes this RMS from sum_sq += value * value at line 107. On builds that permit fused multiply-add contraction, that expression removes the documented FP32 square-rounding boundary. For FP32 input [1.58784556, 2.48118043, 1.39505327], separate rounding sums to 10.623682975769043, while contracted accumulation gives 10.62368392944336 and changes normalized outputs. Force the square to round before accumulation in exact mode, preserve the legacy expression otherwise, and add an FMA-sensitive FP32 regression.

Medium severity Missing output-edge guard rejects valid optional-output nodes

onnxruntime/​core/​graph/​contrib_ops/​bert_defs.cc:5828

A schema-valid node with outputs ["Y", ""] omits Y_fp32, but getNumOutputs() still returns 2. With the default arithmetic mode, this block rejects the model during Graph::Resolve even though no widened output was requested. Gate it with ctx.hasOutput(1), which checks whether the output edge exists, and add a regression using AddOptionalOutputEdge<float>().

Medium severity Exact-mode tests lack FMA-sensitive FP32 coverage

onnxruntime/​test/​contrib_ops/​rotary_embedding_op_test.cc:132

These exact-mode tests cover only FP16/BF16 storage. For their bounded values, multiplication is exact in FP32, so they cannot detect accidental fused multiply-add contraction. Add an FP32 cancellation test with independently specified, bitwise-checked expectations: (1 + 2^-23) * (1 - 2^-23) - 1 rounds to zero with separate operations but to -2^-46 when contracted. Reusing the version-1 float kernel as the reference would not independently verify this boundary.

4 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@apsonawane

Copy link
Copy Markdown
Contributor Author

Pushed 5c02717: dedicated CUDA BF16 RMSNormRotary operator, schema/registrations, exact-parity and contract tests, and optional prefill numerical controls. Preserved the reviewer-added deterministic-compute argument separately from reciprocal-multiply mode. New native translation units, CPU/CUDA paths, schema and tests compile using the existing build configuration; WebGPU is unavailable here and was not compiled. The previously tested combined runtime passed 125 focused native tests (3 WebGPU skips) and 33 optimizer tests, with exact 512-token parity on 44 requests. Those combined-runtime results are not an independent build/link qualification of this PR branch. Paired decode improvement was +0.81% at 2K and +0.77% at 8K; 994 to 946 kernels/cycle. Full accuracy evaluation is starting separately.

@github-actions github-actions 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cuda/bert/branchwise_rms_norm.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rotary_embedding.cc Outdated
Comment thread onnxruntime/test/contrib_ops/rms_norm_rotary_test.cc Outdated
Comment thread onnxruntime/test/contrib_ops/rms_norm_rotary_test.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cc Fixed
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.cu Fixed
Comment thread onnxruntime/contrib_ops/cuda/bert/rms_norm_rotary.h Fixed
Comment thread onnxruntime/test/contrib_ops/rms_norm_rotary_test.cc Fixed
… RMSNormRotary/BranchwiseRMSNorm docs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@github-actions github-actions 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ry fallback from PR #33469

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu Tianlei Wu (tianleiwu) changed the title Add exact normalization and rotary CUDA operator modes [CUDA] Add RMSNormRotary contrib operator Oct 24, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tianlei Wu (tianleiwu) added a commit that referenced this pull request Oct 24, 2026
…2 QMoE, and PR cleanups

- Extend FoldMid2/FoldTail to head_rows<=8 and pack tokens[4..7] into ring record words 1, 2, 3, and 5.
- Add state_update_capacity (0..8) support to CausalConvWithState and enable state_update_capacity > 0 in QwenS2 GatedDeltaNetDecodeWarpKernel.
- Raise kMaxProfiledExpandedRows to 80 in moe_gemv.h and kFusedRouteMaxRows to 8 in qmoe_fused_route.cuh; add fast INT3 and offline SM80 INT2/INT4 prepack QMoE support.
- Eliminate pq[8][ROWS] local register array in HcUpPreKernel and sync PR cleanups (#33469, #33470, #33471, #33472, #33474, #33548, #33569).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants