Skip to content

[CUDA] Add BFloat16 ArgMax/ArgMin support and extend GatedAdd with projected gate - #33470

Open
Akshay Sonawane (apsonawane) wants to merge 17 commits into
mainfrom
asonawane/main-ops-hyperconnection-tensor
Open

Akshay Sonawane (apsonawane) wants to merge 17 commits into
mainfrom
asonawane/main-ops-hyperconnection-tensor

Conversation

@apsonawane

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

Copy link
Copy Markdown
Contributor

Adds BFloat16 support to standard ai.onnx::ArgMax and ai.onnx::ArgMin (opset 13+) on the CUDA Execution Provider and extends com.microsoft::GatedAdd with an optional projected-gate weight (gate_weight) and sigmoid_in_fp32 attribute, eliminating the need for any new standalone contrib operators (ArgMaxLastAxis, SigmoidGateMul, HyperConnectionPostMix) in Qwen3.8-Flash-Next RC2.

Key Changes

Component Files Description
Standard ArgMax / ArgMin BFloat16 (CUDA) onnxruntime/core/providers/cuda/reduction/reduction_ops.cc, reduction_functions.cu, cuda_execution_provider.cc Registers BFloat16 for ArgMax and ArgMin (opset 13+) and instantiates reduce_lastdim_max_min<BFloat16> for fast last-axis vocabulary argmax without requiring com.microsoft::ArgMaxLastAxis.
com.microsoft::GatedAdd Projected Gate & Vectorized Kernel onnxruntime/core/graph/contrib_ops/bert_defs.cc, onnxruntime/contrib_ops/cuda/bert/gated_add.{h,cc}, gated_add_impl.{h,cu} Adds optional 4th input gate_weight ([K, 1]) and sigmoid_in_fp32 attribute to GatedAdd (output = X + Y * sigmoid(gate @ gate_weight)), plus GatedAddVec8Kernel (uint4 8-element vectorized loads/stores) and ProjectedSigmoidGateMulKernel.
Tests & Docs onnxruntime/test/contrib_ops/gated_add_op_test.cc, docs/ContribOperators.md, docs/OperatorKernels.md Adds unit tests for BFloat16 ArgMax (keepdims=0/1) and GatedAdd projected gate (sigmoid_in_fp32=0/1), and syncs operator schema/kernel tables.

Testing Notes

  • Run unit tests: onnxruntime_provider_test --gtest_filter="ContribOpGatedAddTest.*"
  • Verified end-to-end on qwen_flash_next_int4_int8_rc2_contrib_dflash2 ($S=8$ DFlash2 verification): 27/27 engine_behaviours.py pass, 15.54–15.88 ms/cycle at 2K–32K context, and 673 / 800 (84.125%) on MMLU-Pro (391.26 tok/s median, 444.08 tok/s P90, 474.01 tok/s max).

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

Unresolved memory-safety, dependency-ordering and schema-integration defects block approval.

7 open issues
What changed in this PR

Adds fused CUDA operators for transformer workloads and extends EngramGate to accept packed inputs on CPU and CUDA.

Changes:

  • Adds HyperConnectionLowRankMix and five fused tensor operators.
  • Adds numerical, shape-validation and CUDA graph regression tests.
  • Adds schemas, kernel registrations and partial operator documentation.
File Description
onnxruntime/​test/​python/​transformers/​test_hyper_connection_low_rank_mix.py Adds reference comparisons and graph-capture tests.
onnxruntime/​test/​contrib_ops/​hyper_connection_low_rank_mix_op_test.cc Tests fused and fallback paths.
onnxruntime/​test/​contrib_ops/​fused_tensor_ops_test.cc Tests fused tensor operators.
onnxruntime/​test/​contrib_ops/​engram_ops_test.cc Adds packed EngramGate cases.
onnxruntime/​core/​graph/​contrib_ops/​ms_opset.h Registers new schemas.
onnxruntime/​core/​graph/​contrib_ops/​bert_defs.cc Defines operator contracts and shape inference.
onnxruntime/​contrib_ops/​cuda/​cuda_contrib_kernels.cc Registers new CUDA kernels.
onnxruntime/​contrib_ops/​cuda/​bert/​hyper_connection_low_rank_mix.h Declares the operator class.
onnxruntime/​contrib_ops/​cuda/​bert/​hyper_connection_low_rank_mix.cc Validates inputs and dispatches execution.
onnxruntime/​contrib_ops/​cuda/​bert/​hyper_connection_low_rank_mix_impl.h Defines launch interfaces and configuration.
onnxruntime/​contrib_ops/​cuda/​bert/​hyper_connection_low_rank_mix_impl.cu Implements fused kernels and cuBLAS fallback.
onnxruntime/​contrib_ops/​cuda/​bert/​fused_tensor_ops.h Declares tensor operators and launchers.
onnxruntime/​contrib_ops/​cuda/​bert/​fused_tensor_ops.cu Implements tensor CUDA kernels.
onnxruntime/​contrib_ops/​cuda/​bert/​fused_tensor_ops.cc Adds validation and kernel wrappers.
onnxruntime/​contrib_ops/​cuda/​bert/​engram_gate.cc Accepts packed CUDA inputs.
onnxruntime/​contrib_ops/​cuda/​bert/​engram_gate_impl.h Updates the launcher interface.
onnxruntime/​contrib_ops/​cuda/​bert/​engram_gate_impl.cu Uses flattened token counts.
onnxruntime/​contrib_ops/​cpu/​bert/​engram_gate.cc Accepts packed CPU inputs.
docs/​ContribOperators.md Documents HyperConnectionLowRankMix.

Review effort: Balanced

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment thread docs/ContribOperators.md Outdated
Comment thread docs/OperatorKernels.md Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/test/contrib_ops/engram_ops_test.cc Outdated
Comment thread onnxruntime/test/contrib_ops/fused_tensor_ops_test.cc Outdated
@apsonawane

Copy link
Copy Markdown
Contributor Author

Pushed e587c56 addressing all eight outstanding review threads: checked narrowing of packed gate attributes, precise padded-stride projection bounds, WebGPU-to-CPU packed EngramGate fallback with real session regressions, safe no-exception regression guards, and generated schema ordering/type-constraint corrections.

Validation: full-PR lintrunner -a and check-only lint passed in qwen-flash; official renderer/source-metadata checks verify complete schema ordering and exact CPU/CUDA type sets; projection extent arithmetic was checked across 2,304 layouts. Source whitespace checks pass. Renderer-produced blank-line indentation was preserved in the moved generated documentation.

No local builds, compilation, native tests, or model execution were performed. New C++ regressions are added but unexecuted locally. At inspection, no head checks were failed; several were still running. Fresh post-push CI, including native --gen_doc validate, is pending and is not claimed green.

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

WebGPU adapter builds still assign packed EngramGate inputs to an unsupported kernel instead of falling back to CPU.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Avoid unused scratch allocation for single-partition argmax

onnxruntime/​contrib_ops/​cuda/​bert/​fused_tensor_ops.cc:74

For last-axis widths up to 4096, LaunchTypedArgmax writes directly to the output and never uses either scratch buffer. These allocations still reserve 12 bytes per row: 12 MiB of unused scratch for a [1048576, 1] input. Set the scratch count to zero for a single partition; GetScratchBuffer returns nullptr for zero counts, avoiding both allocations.

Medium severity Apply rank filtering in adapter capability path

onnxruntime/​core/​providers/​webgpu/​webgpu_execution_provider.cc:784

This rank filter is compiled out under ORT_USE_EP_API_ADAPTERS (the enclosing condition starts at line 706). Adapter builds instead use Ep::GetCapabilityImpl in core/providers/webgpu/ep/ep.cc:59–168, which accepts registered EngramGate nodes without checking rank. Packed FP32/FP16 inputs therefore still reach the WebGPU kernel's rank-4 check and fail instead of falling back to CPU. Add the equivalent filter to the plugin capability path and run the new fallback regressions with adapters enabled.

8 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 b679f3b: optional projected SigmoidGateMul, explicit prefill arithmetic control, and shared-expert activation/row-gate preparation. Existing layout/overflow validation and unrelated reviewer changes are preserved. Scoped CUDA host/device and schema translation units compile. Shared-expert tests in companion #33473 compare against these unfused operators; the combined local runtime passed its 38 focused shared-expert native tests. This does not establish independent PR-branch build/link qualification. Companion #33473 depends on these new helper declarations and implementations.

Retain both the PR tensor-op registrations and main's ConvStateReplay
declaration and schema registration.

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

Copy link
Copy Markdown
Contributor Author

Resolved conflicts against main (1f4b965) and pushed through dbd8b83. Both schema-registration conflicts retain the PR tensor operators and main’s ConvStateReplay declaration/registration.

Validation before pushing:

  • Ran lintrunner -a, reviewed/applied formatting-only changes, then reran lintrunner -a and lintrunner: no lint issues.
  • git diff --check against main passes; removed trailing whitespace in the PR operator documentation.
  • Compiled merged environment.cc, bert_defs.cc, contrib_defs.cc, fused_tensor_ops.cc and fused_tensor_ops.cu using the exact isolated PR headers and main-compatible ONNX protobuf headers. All five compilation checks passed.

This is targeted merged-source compilation, not a full clean standalone build, link, or runtime-test qualification. Existing integration worktree, active model package, and LiveCodeBench snapshot were untouched.

…LowRankMix post-gate

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…Weights, EmbeddingRMSNorm, RowSelect, and EngramGate changes from PR #33470

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/contrib_ops/cuda/bert/fused_tensor_ops.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.cu Fixed
Comment thread onnxruntime/contrib_ops/cuda/bert/fused_tensor_ops.h Fixed
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu Tianlei Wu (tianleiwu) changed the title Add fused hyper-connection and tensor CUDA operators [CUDA] Add SigmoidGateMul and ArgMaxLastAxis contrib operators Oct 24, 2026
…ribOperators.md

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tianlei Wu (tianleiwu) added a commit that referenced this pull request Oct 24, 2026
#33473 (S<=8 decode & fused validation)

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>
@tianleiwu
Tianlei Wu (tianleiwu) marked this pull request as draft October 24, 2026 23:41
auto-merge was automatically disabled October 24, 2026 23:41

Pull request was converted to draft

…d with projected gate

- Remove com.microsoft::ArgMaxLastAxis and instead register BFloat16 (opset 13+)
  on standard ai.onnx::ArgMax and ai.onnx::ArgMin in CUDA (reduction_ops.cc,
  reduction_functions.cu, cuda_execution_provider.cc).
- Extend com.microsoft::GatedAdd with optional gate_weight [K, 1] projection,
  sigmoid_in_fp32 attribute, and 16-byte uint4 vectorization (GatedAddVec8Kernel),
  and vectorize ScaledSiLUKernel with uint4 (ScaledSiLUVec8Kernel).
- Update fused_tensor_ops_test.cc, ContribOperators.md, and OperatorKernels.md.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu Tianlei Wu (tianleiwu) changed the title [CUDA] Add SigmoidGateMul and ArgMaxLastAxis contrib operators [CUDA] Add BFloat16 ArgMax/ArgMin support, extend GatedAdd, and add SigmoidGateMul Oct 25, 2026
Remove the three leftover explicit template instantiations of
ArgMaxLastAxis in fused_tensor_ops.cc after ArgMaxLastAxis was replaced
with BFloat16 support on ai.onnx::ArgMax and ai.onnx::ArgMin.

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

- Move LaunchProjectedSigmoidGateMul directly into gated_add_impl.{h,cu} for GatedAdd's optional gate_weight input.
- Remove unused com.microsoft::SigmoidGateMul schema, kernel registration, fused_tensor_ops.{h,cc,cu}, and docs.
- Add BFloat16 projected-gate GatedAdd and ArgMax unit tests to gated_add_op_test.cc.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu Tianlei Wu (tianleiwu) changed the title [CUDA] Add BFloat16 ArgMax/ArgMin support, extend GatedAdd, and add SigmoidGateMul [CUDA] Add BFloat16 ArgMax/ArgMin support and extend GatedAdd with projected gate Oct 25, 2026
@tianleiwu
Tianlei Wu (tianleiwu) marked this pull request as ready for review October 25, 2026 05:36

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