Cortex-M: quantize aten.matmul by rewriting it to bmm before annotation#21028
Open
rascani wants to merge 1 commit into
Open
Cortex-M: quantize aten.matmul by rewriting it to bmm before annotation#21028rascani wants to merge 1 commit into
rascani wants to merge 1 commit into
Conversation
…annotation The cortex_m quantizer annotates only aten.bmm.default, but torch.matmul / the @ operator is captured as aten.matmul.default and does not decompose to bmm until to_edge -- after quantization runs -- so matmuls (for example every attention score and context product) never received qparams and stayed in fp32 on portable kernels. Add a pre-annotation MatmulToBmmPass that rewrites aten.matmul to aten.bmm (rank-3 directly; higher ranks by folding the leading batch dims to 3D and reshaping back; rank-2 and broadcasting matmuls left unchanged), so the existing CortexMBmmCheck annotation and quantized_batch_matmul lowering handle it. No new kernel is needed -- the batch-matmul kernel already exists; this is purely an annotation-timing fix. Verified test-first (RED->GREEN) with rank-3 and rank-4 dialect tests and no regression on the existing bmm suite; confirmed it flips all 15 SAM mask-decoder attention matmuls from fp32 to quantizable. Authored with Claude Code. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21028
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Unclassified FailureAs of commit 21bdfd3 with merge base 7013c8d ( UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The cortex_m quantizer annotates only aten.bmm.default, but torch.matmul / the @ operator is captured as aten.matmul.default and does not decompose to bmm until to_edge -- after quantization runs -- so matmuls (for example every attention score and context product) never received qparams and stayed in fp32 on portable kernels.
Add a pre-annotation MatmulToBmmPass that rewrites aten.matmul to aten.bmm (rank-3 directly; higher ranks by folding the leading batch dims to 3D and reshaping back; rank-2 and broadcasting matmuls left unchanged), so the existing CortexMBmmCheck annotation and quantized_batch_matmul lowering handle it. No new kernel is needed -- the batch-matmul kernel already exists; this is purely an annotation-timing fix.
Test plan
Verified test-first (RED->GREEN) with rank-3 and rank-4 dialect tests and no regression on the existing bmm suite; confirmed it flips all 15 SAM mask-decoder attention matmuls from fp32 to quantizable.
Authored with Claude Code.