Skip to content

[GlobalOptimization] Rewrite QDQ contractions into integer math - #24902

Merged
Manewing merged 4 commits into
mainfrom
users/ziereis/qdq-integer-contractions
Sep 22, 2026
Merged

Manewing merged 4 commits into
mainfrom
users/ziereis/qdq-integer-contractions

Conversation

@ziereis

@ziereis ziereis commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Rewrites contractions over iree_linalg_ext.dequantize_affine operands into integer contractions, integer zero-point corrections, and floating-point scaling.

The transformation uses indexing maps and iterator types to handle named and generic contractions, including convolutions. It reduces in integer arithmetic over dimensions where quantization parameters are invariant. For blockwise quantization, it computes integer partial results, applies each block's correction and scales, then accumulates across blocks in floating point.

Zero-point correction terms are built as separate computations so constant terms can be evaluated independently. Corrections are applied before conversion to floating point. Scaling and accumulation use at least f32, preserving wider scale or output types and narrowing only the final result.

The rewrite requires a plain multiply-add body, zero initialization, and positive static integer reduction extents whose products and corrections fit in signed i32. Unsupported cases retain the original computation. The pass is enabled by default; reassociation changes floating-point rounding. It can be disabled with --iree-global-opt-enable-qdq-to-integer-math=false.

Builds on the affine QDQ operations PR #24901 and implements the contraction rewrite portion of Linalg(_ext) native QDQ #24862.


Stack created with GitHub Stacks CLI • Give Feedback 💬

@ziereis
ziereis force-pushed the users/ziereis/qdq-integer-contractions branch from 448e464 to b341281 Compare September 11, 2026 06:37
Base automatically changed from users/ziereis/qdq-linalg-ext-ops to main September 15, 2026 08:37
Rewrite contractions over affine-dequantized operands as integer
contractions with zero-point corrections and a scaling epilogue:

  C = sA*sB*(D - zB*RA - zA*RB + N*zA*zB)

Compose storage and parameter maps into contraction coordinates. Reduce
in integer arithmetic where parameters are invariant; retain the other
reduction dimensions as partial results for the floating-point epilogue.
Windowed operand sums use the same access expressions as the
contraction.

Match only plain multiply-add bodies with zero initialization. Require
positive static integer reduction extents and prove that all integer
intermediates fit in signed i32 using the separate storage and zero-
point ranges. Unsupported cases retain the original computation.

Apply corrections before converting to float. Evaluate scaling and
blockwise accumulation in at least f32, preserving wider scale and
output types, and narrow only the final result. Reassociation changes
rounding. Enable the rewrite by default; it can be disabled with
--iree-global-opt-enable-qdq-to-integer-math=false.

Cover the rewrite with three compiler regression files: correction terms
and widening, indexing maps and result shapes, and the contractions the
rewrite declines. Add CPU execution checks for blockwise and convolution
zero-point corrections alongside the original QDQ path.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
@Manewing
Manewing force-pushed the users/ziereis/qdq-integer-contractions branch from b341281 to 886ffac Compare September 15, 2026 08:38

@egebeysel egebeysel 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 the patch! Overall, the implementation looks good to me, with the exception of all the index mapping being quite hard to follow.

Besides that, I guess it would also make sense to think about how we want all of these operations to be partitioned into dispatches. According to that, some decisions on generating independent generic ops for some calculations or melting them into others might be more sensible, e.g. for zB * RA. I'm not certain there would be fusion patterns on the whole-op-level that would achieve the same result for us.

Also, I do like the split between the algebra|maps|negative and it does make some stuff easier to understand, though sometimes, when some IR is emitted due to being checked on other tests, it might get a bit hard to follow what the tests are checking. Refining those with comments would make up for a more pleasant reading experience 😄

All in all, LGTM, so if you do not need my feedback again after addressing the comments, take this as a non-blocking request for changes :)

@egebeysel

egebeysel commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Also, I'm wondering how this would work with data-tiling, e.g. we currently usually get mmt4d -> elemwise -> unpack (-> pack) dispatches (or a bit more for the upcoming inner_tiled stuff!), where the elemwise is usually some sort of bias add or activation. I wonder if we could also have similar dispatch formation and avoid having too much memory traffic 🤔

Added two small helper libraries with dedicated test case coverage.
Improved documentation of the conversion pattern.
Added test cases for mathematical corner cases causing instability and
incorrect results that need to be fixed.

Signed-off-by: Florian Walbroel <[email protected]>
…er math

Handle scaling under and overflow by reordering scaling multiplaction

Document remaining numerical instabilities and corner cases from the
conversion

More accurately compute bound for reduction extent to allow
conversion for unsigned x unsigned cases with larger extent

Signed-off-by: Florian Walbroel <[email protected]>
conv/depthwise conv properly

Add additional test case coverage

Signed-off-by: Florian Walbroel <[email protected]>

@AGindinson AGindinson 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.

Not a full-depth review, however the comments from Ege (who's OOO right now) are largely addressed, thanks.

Regarding the MMT4D tiling question from Ege: in the context of the whole RFC, I would be interested in a performance summary and to learn about the trade-offs in the fusion, but this PR doesn't introduce new behavior for end-to-end models until we get them in QDQ form out of Torch-MLIR et al. So it's fine to proceed as far as I'm concerned.

Comment thread tests/e2e/quantization/CMakeLists.txt Resolved
Comment on lines +455 to +456
// Only the arithmetic consequence is checked here; the resulting maps and
// iterator types belong to the maps file.

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.

Seconding this as a nit, "maps file" is unclear. Actually a small markdown in the directory, very briefly describing this LIT suite could clear this up and replace the header comments.

(That said, I also really like the split)

@Manewing
Manewing merged commit 4c5f6cb into main Sep 22, 2026
57 of 60 checks passed
@Manewing
Manewing deleted the users/ziereis/qdq-integer-contractions branch September 22, 2026 13:43
Manewing pushed a commit that referenced this pull request Sep 23, 2026
Canonicalizes `iree_linalg_ext.quantize_affine` and
`iree_linalg_ext.dequantize_affine` by removing a zero-point operand
when its constant value is zero in every element. The operations then
expose symmetric quantization directly, allowing decomposition to omit
zero-point arithmetic and the integer-contraction rewrite to skip the
corresponding correction terms.

Handles scalar constants, tensor splats, dense tensors, and
resource-backed tensor constants. Removing the operand also removes its
indexing map and `zp_unsigned` attribute while preserving the remaining
operation attributes. Tests cover these constant representations,
nonzero values that must remain, and preservation of the operation's
other parameters.

Builds on #24902 in the affine QDQ stack. Part of #24862.

Signed-off-by: Thomas Ziereis <[email protected]>
Signed-off-by: default <[email protected]>
ziereis added a commit that referenced this pull request Oct 12, 2026
…24906)

The integer contraction rewrite from #24902 needs dequantize_affine to
be the direct producer of a contraction operand. This PR adds
PropagateAffineQuantization, which moves the tensor ops in between onto
the quantized side, where they also operate on narrower data. It runs
right before each integer QDQ rewrite.

It perform the following transformations:

```
quantize(collapse_shape(x))   ->  collapse_shape(quantize(x))
expand_shape(dequantize(q))   ->  dequantize(expand_shape(q))
pad(dequantize(q), 0.0)       ->  dequantize(pad(q, zp))
transpose(dequantize(q))      ->  dequantize(q) with its output map permuted
```

Part of #24862

Assisted By: Claude Code

---------

Signed-off-by: Thomas Ziereis <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@devtbi devtbi mentioned this pull request Oct 19, 2026
7 of 8 tasks
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