Skip to content

Add CudaQuantizer to onnxruntime.quantization - #29509

Merged
tianleiwu merged 7 commits into
mainfrom
tlwu/qmoe_quantizer
Jul 6, 2026
Merged

Add CudaQuantizer to onnxruntime.quantization#29509
tianleiwu merged 7 commits into
mainfrom
tlwu/qmoe_quantizer

Conversation

@tianleiwu

@tianleiwu tianleiwu commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

Adds a CUDA-focused CudaQuantizer helper to ONNX Runtime's Python quantization tools and exports it from onnxruntime.quantization. The helper centralizes the CUDA quantization paths needed by ONNX Runtime tests and onnxruntime-genai model building, covering both per-channel QMoE weight packing and MatMulNBits blockwise formats without duplicating that logic in test code.

This also updates the CUDA MoE/QMoE transformer tests to consume the shared helper, including full-range symmetric offset storage and CUTLASS mixed-GEMM prepacked layouts.

Summary of Changes

Quantization API

File Change
onnxruntime/python/tools/quantization/cuda_quantizer.py Adds CudaQuantizer with per-channel INT4/INT8 quantization, CUDA mixed-GEMM prepacking, MatMulNBits blockwise quantization, CUTLASS prepacked blockwise quantization, and a pure-PyTorch symmetric blockwise reference path.
onnxruntime/python/tools/quantization/__init__.py Exports CudaQuantizer from onnxruntime.quantization.
onnxruntime/python/tools/quantization/qmoe_quantizer.py Replaced by the more general CUDA quantizer implementation.

Test Updates

File Change
onnxruntime/test/python/transformers/test_moe_cuda.py Replaces duplicated blockwise quantization/prepacking logic with CudaQuantizer calls.
onnxruntime/test/python/transformers/test_qmoe_cuda.py Uses CudaQuantizer for per-channel and blockwise QMoE test setup, including full-range unsigned offset storage coverage and default prepacked layout smoke tests.

Testing

  • Built the CUDA Release configuration.
  • pytest transformers/test_qmoe_cuda.py::TestQMoEIntPrePackSmoke - 5 passed, 2 subtests passed.
  • pytest transformers/test_qmoe_cuda.py - 97 passed, 12 skipped, 2 subtests passed.
  • Validated without CUDA that importing the quantization module does not require torch, and that torch-backed methods raise a clear error only when used.
  • python -m py_compile onnxruntime/python/tools/quantization/cuda_quantizer.py

Checklist

  • Tests added/updated
  • No breaking changes to released APIs
  • CI passes

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.

Pull request overview

This PR brings a CUDA-only QMoE weight quantizer into ONNX Runtime’s Python quantization tooling and exposes it as onnxruntime.quantization.MoeCudaQuantizer, replacing an out-of-repo helper previously loaded by absolute path in the QMoE CUDA tests.

Changes:

  • Added MoeCudaQuantizer implementation with per-channel symmetric INT4/INT8 storage and optional CUDA mixed-GEMM prepacking.
  • Exported MoeCudaQuantizer via onnxruntime.quantization package API.
  • Updated test_qmoe_cuda.py to use MoeCudaQuantizer for per-channel quantization/prepack and added smoke coverage for storage contract and default-prepacked execution.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/test/python/transformers/test_qmoe_cuda.py Switches per-channel quantization/prepack to MoeCudaQuantizer and adds additional smoke tests (including default-prepacked layout).
onnxruntime/python/tools/quantization/qmoe_quantizer.py Introduces the MoeCudaQuantizer implementation with lazy torch import and optional CUDA prepacking via _pybind_state.
onnxruntime/python/tools/quantization/init.py Exports MoeCudaQuantizer from the public quantization package.

Comment thread onnxruntime/python/tools/quantization/qmoe_quantizer.py Outdated
Comment thread onnxruntime/test/python/transformers/test_qmoe_cuda.py
@tianleiwu tianleiwu changed the title Add MoeCudaQuantizer to onnxruntime.quantization Add CudaQuantizer to onnxruntime.quantization Jul 5, 2026
@tianleiwu
tianleiwu force-pushed the tlwu/qmoe_quantizer branch from 439fc78 to 2414fa4 Compare July 5, 2026 22:57

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Outdated
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Outdated
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Outdated
Comment thread onnxruntime/test/python/transformers/test_moe_cuda.py Outdated
Comment thread onnxruntime/test/python/transformers/test_qmoe_cuda.py Outdated
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Fixed
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Fixed
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Fixed
Comment thread onnxruntime/python/tools/quantization/cuda_quantizer.py Fixed
tianleiwu added 2 commits July 6, 2026 18:44
Restructure symmetric_per_channel_quantize so qmin/qmax/scale_divisor/
zero_point are unconditionally initialized (if bits==4 / else), matching
the earlier bits validation. Silences CodeQL 'potentially uninitialized
local variable' alerts on cuda_quantizer.py.

@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/test/python/transformers/test_moe_cuda.py Outdated
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread onnxruntime/test/python/transformers/test_moe_cuda.py

@kunal-vaishnavi kunal-vaishnavi 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.

Thanks for adding this support! Can we also update the model builder to use this quantizer?

@tianleiwu
tianleiwu enabled auto-merge (squash) July 6, 2026 21:29
@tianleiwu

Copy link
Copy Markdown
Contributor Author

Thanks for adding this support! Can we also update the model builder to use this quantizer?

I'm working on that.

@tianleiwu
tianleiwu merged commit 1990bd5 into main Jul 6, 2026
88 checks passed
@tianleiwu
tianleiwu deleted the tlwu/qmoe_quantizer branch July 6, 2026 22:20
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.

4 participants