Skip to content

[CUDA] Add BitwiseAnd, BitwiseOr, and BitwiseNot integer kernels - #33646

Open
Justin Chu (justinchuby) wants to merge 4 commits into
mainfrom
justinchuby-cuda-bitwise-and-or-not
Open

Justin Chu (justinchuby) wants to merge 4 commits into
mainfrom
justinchuby-cuda-bitwise-and-or-not

Conversation

@justinchuby

Copy link
Copy Markdown
Contributor

Description

Add standard ONNX CUDA BitwiseAnd, BitwiseOr, and BitwiseNot kernels for all eight integer types, starting at opset 18. And/Or reuse the existing bitwise functors and binary broadcasting machinery; Not uses the shared unary elementwise launch with ~a. Existing boolean And, Or, and Not registrations and semantics are unchanged.

Add shared, strict CUDA-only regressions (session.disable_cpu_ep_fallback=1) covering integer extrema, full-width 64-bit patterns, scalars, multidirectional broadcasting in both operand orders, empty tensors, and tail sizes 255/256/257/1025 at opsets 18 and 24. Include a strict boolean regression and update only the three CUDA operator-support rows.

Motivation and Context

These integer bitwise operators currently lack CUDA kernels and fall back to CPU. This PR adds native CUDA support using the existing elementwise machinery. It is independent of the BitwiseXor work in #33645 and of BitShift: neither implementation is included or required.

Validation

Verified on NVIDIA H200 (physical GPU 4, sm90), CUDA 13.3, cuDNN 9.10.2, Python 3.12, ORT 1.32.0 Release, from base 812043410eebd29c4bdd46b906447e2e5b77f7b2 plus this isolated patch. All eight committed source/test files match the compiled-source SHA256 manifest after line-ending normalization.

  • Targeted CUDA provider build and wheel build: passed.
  • Targeted C++ regressions: 34/34 passed, no skips, including all eight new integer typed tests and the strict boolean test.
  • Additional ad hoc NumPy exact-value/profile probe: 528/528 passed with CPU fallback disabled; profiles recorded 216 BitwiseAnd, 216 BitwiseOr, and 96 BitwiseNot CUDA executions. Covers all eight types and opsets 18/24/28, full-width random 64-bit values, scalars, multidirectional broadcasts, and empties. The same 528 models profiled on CPU before this change.
  • CUDA operator documentation generation: passed; generated And/Not/Or rows match the added 18+, eight-integer-type rows.
  • git diff 812043410eebd29c4bdd46b906447e2e5b77f7b2 HEAD --check: passed.
  • .venv\Scripts\git-clang-format.exe --binary .venv\Scripts\clang-format.exe --diff 812043410eebd29c4bdd46b906447e2e5b77f7b2 with clang-format 20.1.8: passed, no formatting changes.
Exact GPU build and validation commands

Build from the isolated source checkout:

python tools/ci_build/build.py --config Release --build_dir /tmp/ort-cuda-bitwise-xor-8d05709c/build --update --build --build_wheel --skip_tests --skip_submodule_sync --use_cuda --cuda_home /usr/local/cuda --cudnn_home /usr/lib/python3.12/site-packages/nvidia/cudnn --cmake_generator Ninja --parallel 16 --disable_contrib_ops --disable_ml_ops --cmake_extra_defines CMAKE_CUDA_ARCHITECTURES=90 FETCHCONTENT_FULLY_DISCONNECTED=ON onnxruntime_QUICK_BUILD=ON onnxruntime_USE_FLASH_ATTENTION=OFF onnxruntime_USE_MEMORY_EFFICIENT_ATTENTION=OFF onnxruntime_USE_TRT_FUSED_ATTENTION=OFF onnxruntime_USE_XQA=OFF --targets onnxruntime_provider_test onnxruntime_pybind11_state

Result: passed. The temporary build directory name reflects reuse of the coordinator's build resources, not inclusion of the XOR patch.

From the build output directory (build/Release):

CUDA_VISIBLE_DEVICES=4 LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/lib/python3.12/site-packages/nvidia/cudnn/lib ./onnxruntime_provider_test --gtest_filter='CudaBitwiseOpTest/*.*:CudaBitwiseLogicalTest.*:MathOpTest.BitwiseAnd*:MathOpTest.BitwiseOr*:MathOpTest.BitwiseNot*:MathOpTest.And:MathOpTest.Or:MathOpTest.Not'

Result: 34 tests passed, zero skips.

Ad hoc probe from /tmp/ort-cuda-bitwise-xor-8d05709c/validated-logic (probe script is an external validation artifact, not a repository test):

CUDA_VISIBLE_DEVICES=4 LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/lib/python3.12/site-packages/nvidia/cudnn/lib ../.venv/bin/python ../probe-remaining-bitwise.py --group logic

Result: 528/528 bit-identical models, all operator profiles on CUDA, CPU fallback disabled.

Documentation generation from /tmp/ort-cuda-bitwise-xor-8d05709c, using the preserved validated wheel:

PYTHONPATH=/tmp/ort-cuda-bitwise-xor-8d05709c/validated-logic/wheel-runtime LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/lib/python3.12/site-packages/nvidia/cudnn/lib .venv/bin/python src/tools/python/gen_opkernel_doc.py --providers cuda --output_path validated-logic/OperatorKernels.generated.md

Result: passed; the three generated support rows match this PR.

Validated wheel SHA256: a0c6581f642591cdf1eba0f15b8667721b326490b446495a112d62ab1377ab23.

Limits: targeted operator validation only, not the full suite. QUICK_BUILD enabled, contrib/ML ops disabled, and flash/MEA/TRT-fused/XQA attention disabled. No full-model performance, non-sm90 GPU, or cross-platform runtime claims.

Reuse the elementwise broadcast/unary CUDA machinery for all eight integer types. Add strict CUDA-only extrema, broadcast, empty and tail regressions while preserving logical boolean kernels.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Avoid instantiating the tensor vector overloads with the non-contiguous std::vector<bool> specialization.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 27, 2026 18:22

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.

🟢 Approval recommended

The focused additions consistently reuse established elementwise infrastructure, include strict CUDA regression coverage, and have no unresolved findings.

No open issues 🎉

What changed in this PR

Adds native CUDA support for ONNX BitwiseAnd, BitwiseOr, and BitwiseNot across all eight integer types at opset 18+, avoiding CPU fallback while preserving existing boolean operators.

Changes:

  • Reuses existing binary broadcasting and unary elementwise infrastructure.
  • Adds strict CUDA-only regressions for integer boundaries, broadcasting, scalars, empty tensors, tail sizes, and boolean behavior.
  • Updates the three CUDA operator-support documentation rows.
File Description
onnxruntime/​test/​providers/​cuda/​bitwise_ops_test.cc Adds integer and boolean regressions with CPU fallback disabled.
onnxruntime/​core/​providers/​cuda/​math/​unary_elementwise_ops.h Declares the BitwiseNot kernel class.
onnxruntime/​core/​providers/​cuda/​math/​unary_elementwise_ops.cc Registers integer BitwiseNot kernels.
onnxruntime/​core/​providers/​cuda/​math/​unary_elementwise_ops_impl.h Defines the bitwise complement expression.
onnxruntime/​core/​providers/​cuda/​math/​unary_elementwise_ops_impl.cu Instantiates integer BitwiseNot device implementations.
onnxruntime/​core/​providers/​cuda/​math/​binary_elementwise_ops.cc Registers integer BitwiseAnd and BitwiseOr kernels.
onnxruntime/​core/​providers/​cuda/​math/​binary_elementwise_ops_impl.cu Instantiates integer And/Or device implementations.
onnxruntime/​core/​providers/​cuda/​cuda_execution_provider.cc Adds the new kernels to the CUDA registry.
docs/​OperatorKernels.md Documents CUDA support, types, and opset ranges.

Review effort: Balanced

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants