Skip to content

[Relax][Frontend][ONNX] Add QLinearConv operator converter - #20953

Open
Chaitanya140904 wants to merge 6 commits into
apache:mainfrom
Chaitanya140904:relax-onnx-qlinearconv
Open

Chaitanya140904 wants to merge 6 commits into
apache:mainfrom
Chaitanya140904:relax-onnx-qlinearconv

Conversation

@Chaitanya140904

@Chaitanya140904 Chaitanya140904 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds support for the ONNX QLinearConv operator in the Relax ONNX frontend, resolving #20959.

Key Changes

  • In python/tvm/relax/frontend/onnx/onnx_frontend.py:
    • Implemented QLinearConv op converter supporting opset 10.
    • Supports 1D, 2D, and 3D convolutions with dequantization of input activations and weights.
    • Supports per-channel and per-tensor scales and zero-points with channel broadcasting ([1, C, 1, 1] and [O, 1, 1, 1]).
    • Supports auto_pad modes (SAME_UPPER, SAME_LOWER, VALID, NOTSET), strides, dilations, and groups.
    • Handles quantized bias scaling (B * x_scale * w_scale).
    • Requantizes and clamps results to the output integer type range.
    • Registered "QLinearConv": QLinearConv in _get_convert_map().
  • In tests/python/relax/test_frontend_onnx.py:
    • Added test_qlinearconv covering 2D convolutions with and without bias, and multi-channel convolutions with custom padding, validated against ONNX Runtime.

Closes #20959.

@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

Hi @mshr-h, @yzhliu, @tqchen, @akaashrp

Could you please take a look at this PR when you get a chance?

@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

@tlopex
Could you please take a look at this PR when you get a chance?

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

SAME_UPPER and SAME_LOWER imports fail because autopad receives an unnormalized expression, and float32 convolution and bias handling can lose integer precision that the current tests may miss.

4 open issues
What changed in this PR

Adds ONNX QLinearConv conversion to the Relax frontend, extending quantized model import and addressing #20959.

Changes:

  • Registers an opset 10 converter with lowering paths for 1D, 2D and 3D convolution.
  • Implements quantization-parameter broadcasting, convolution attributes, bias scaling and output requantization.
  • Adds ONNX Runtime comparisons for 2D convolutions with and without bias, including multi-channel inputs with padding.
File Description
tests/​python/​relax/​test_frontend_onnx.py Adds numerical comparisons for three QLinearConv cases.
python/​tvm/​relax/​frontend/​onnx/​onnx_frontend.py Implements and registers the QLinearConv converter.

Review effort: Max

@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

Hi @tlopex, @mshr-h, @yzhliu, @tqchen, @akaashrp,

I have updated the PR to address all the recommended review changes:

  1. Normalize Before Autopadding: Ensured input expressions are normalized with bb.normalize(x_centered) before calling autopad() so SAME_UPPER and SAME_LOWER can inspect shape and rank.
  2. Exact int32 Centered Convolution & Bias: Maintained centered convolution and bias addition strictly in int32 before scaling and requantizing, avoiding float32 precision loss during large-channel cancellation.
  3. In-Range Arithmetic Checks: Calibrated test inputs close to zero-points and used appropriate output scales to ensure outputs remain away from clipping limits.
  4. Extended Test Suite: Added tests for 1D, 2D, 3D convolutions, SAME_UPPER/SAME_LOWER/VALID, non-unit strides/dilations, grouped convolutions, signed int8, per-channel parameters, and the precision cancellation regression case.

Could you please take another look when you have a moment? Thank you!

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

Omitting kernel_shape raises a TypeError, and float32 scale multiplication can overflow and produce incorrect requantized outputs.

2 open issues
4 resolved since last review

Review effort: Max

…oat64 scale product

Two fixes:

1. kernel_shape inference: weight_norm.ty.shape[2:] raises TypeError because
   ShapeExpr.__getitem__ does not accept slice arguments. Use
   weight_norm.ty.shape.values[2:] (which returns a list/tuple that can be
   sliced) so symbolic kernel dimensions remain supported.  Adds a regression
   test that omits kernel_shape from the ONNX node and verifies correct output.

2. Scale overflow: x_scale * w_scale can overflow float32 even when all three
   scales and the final requantized result are finite (e.g. x_scale=w_scale=2^65,
   y_scale=2^124 gives a correct output of 64 but float32 product is inf, clipping
   to 255). Compute x_scale * w_scale / y_scale in float64, then cast to float32
   before multiplying with the conv output.  Adds a regression asserting that the
   canonical overflow case produces 64 rather than 255.

Addresses GitHub Copilot review comments:
apache#20953 (comment)
apache#20953 (comment)
@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

Hi @tlopex, @mshr-h, @yzhliu, @tqchen, @akaashrp,

I have updated the PR to address the latest review feedback:

  1. Kernel Shape Slicing: Sliced weight_norm.ty.shape.values[2:] instead of the ShapeExpr object directly to avoid TypeError when the kernel_shape attribute is omitted, keeping support for symbolic kernel dimensions intact.
  2. Combined Scale Overflow Safety: Computed the combined scale multiplier (x_scale * w_scale) / y_scale using float64 intermediates before casting to float32, preventing overflow when large individual scale factors (such as ^{65}$) are multiplied together.
  3. Regression Tests: Added test_qlinearconv_no_kernel_shape_attr and test_qlinearconv_scale_overflow covering both omitted kernel shape inference and the float64 scale product overflow case.

Could you please take another look when you get a chance? Thank you!

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

Finite-scale multiplier overflow can turn zero accumulators into NaN, while regression tests fail to distinguish per-channel scales, SAME padding modes, and incorrect output dtypes.

No open issues 🎉

4 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve zero accumulators when the multiplier overflows

python/​tvm/​relax/​frontend/​onnx/​onnx_frontend.py:2392

Narrowing the multiplier to float32 still overflows for finite scales such as x_scale = w_scale = 2**65 and y_scale = 1. With a zero accumulator, this produces 0 * inf = NaN rather than the required y_zero_point (for example, 128). Preserve zero accumulators when applying the multiplier and add this case to the overflow regression.

Medium severity Use distinct scales to test per-channel requantization

tests/​python/​relax/​test_frontend_onnx.py:14622

The per-output-channel test receives identical weight scales [0.25, 0.25, 0.25, 0.25], despite being labeled as testing distinct scales. Consequently, a converter that reuses the first scale for every output channel would still pass. Generate different scales per output channel so the existing bias-enabled case actually validates per-channel requantization.

Medium severity Check output dtypes in quantization comparisons

tests/​python/​relax/​test_frontend_onnx.py:14678

These comparisons check values but not the output dtype, so returning float32 instead of the required uint8 or int8 would still pass. Set check_dtypes=True, matching the nearby quantization tests at lines 14182 and 14201; the calls at lines 14764 and 14815 need the same check.

Medium severity Use an even kernel to distinguish SAME_LOWER padding

tests/​python/​relax/​test_frontend_onnx.py:14695

Both SAME-padding cases use a 3×3 kernel with unit strides, yielding one padding cell on each side. SAME_UPPER and SAME_LOWER are therefore indistinguishable, so always using SAME_UPPER would still pass this test. Please use an even-sized kernel here, such as 2×2, to create an odd total padding and verify that SAME_LOWER puts the extra cell at the beginning.

2 resolved since last review

Review effort: Max

@mshr-h mshr-h 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.

lgtm. lets merge after ci passed.

@mshr-h

mshr-h commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

please fix errors on github actions. @Chaitanya140904

@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

Hi @mshr-h, thank you for the approval and the note! 🙏

I have fixed the GitHub Actions lint errors. The failures were caused by Ruff rule UP038 — the linter requires using union type syntax X | Y instead of tuples (X, Y) in isinstance() calls (Python 3.10+ style). All affected lines in both onnx_frontend.py and test_frontend_onnx.py have been updated and verified clean with pre-commit locally.

The fix has been pushed — CI should now pass. Ready to merge once CI is green! ✅

@mshr-h

mshr-h commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

please resolve conflict @Chaitanya140904

@Chaitanya140904

Copy link
Copy Markdown
Contributor Author

Hi @mshr-h,

I have resolved the merge conflicts with main and pushed the updated branch:

  1. Re-synced with main: Merged the latest main (incorporating PR [Relax][Frontend][ONNX] Add QLinearMatMul operator converter #20951 and PR [Relax][Frontend][ONNX] Add DFT operator converter #20952).
  2. Preserve Zero Accumulators on Scale Overflow: Kept the scale multiplication in float64 (scaled_f64 = conv_f64 * scale_mult_f64) before casting to float32, ensuring zero accumulators (0.0 * inf) evaluate cleanly to 0.0 instead of NaN when scales are exceptionally large.
  3. Distinct Per-Channel Weight Scales & Output Dtype Checks: Generated linearly spaced distinct scales per channel for multi-channel tests, used asymmetric padding (5x5 input with 2x2 kernel) to clearly distinguish SAME_LOWER from SAME_UPPER, and enabled check_dtypes=True on test comparisons.
  4. Lint & Pre-commit: All pre-commit hooks passed cleanly.

The PR is now conflict-free and ready for merge. Thank you! 🙏

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.

[Feature][Relax][ONNX] Support QLinearConv in Relax ONNX frontend

3 participants