Repository navigation
[GlobalOptimization] Rewrite QDQ contractions into integer math - #24902
Conversation
da37d31 to
1f9f4aa
Compare
4197dd1 to
448e464
Compare
448e464 to
b341281
Compare
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]>
b341281 to
886ffac
Compare
egebeysel
left a comment
There was a problem hiding this comment.
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 :)
|
Also, I'm wondering how this would work with data-tiling, e.g. we currently usually get |
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]>
There was a problem hiding this comment.
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.
| // Only the arithmetic consequence is checked here; the resulting maps and | ||
| // iterator types belong to the maps file. |
There was a problem hiding this comment.
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)
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]>
…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]>
Rewrites contractions over
iree_linalg_ext.dequantize_affineoperands 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 💬