Cortex-M: decompose SDPA with score scaling after matmul - #22892
amacharla15 wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22892
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 2 PendingAs of commit 6864d71 with merge base ed72896 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: none" |
|
Hi @AdrianLundell, CI looks good on this PR. Could you give a final review? |
AdrianLundell
left a comment
There was a problem hiding this comment.
Thanks for the update, this is getting closer now! The new pass is targeting the decomposition from the Arm DecomposeSDPAWithRegularSoftmaxPass and then rewrites it, I see two things wrong with this:
- As currently written the pass will never do anything since DecomposeSDPAWithRegularSoftmaxPass is not in the Cortex-M pass pipeline.
- Rewriting the decomposition of one pass with another one is a bit convoluted, it would make more sense to me to reimplement the SDPA decomposition in a way which suits the Cortex-M backend better directly instead.
Please also add a full SDPA operator test with these changes to make sure the full flow makes sense, if there are more changes required to make them pass fully that is fine, just make sure that the intended change is happening (muls are annotated correctly for folding) and then xfail the tests with a comment about remaining work needed.
6c88f64 to
05766a2
Compare
|
Thanks for the review @AdrianLundell. I updated the PR to match the points you raised.
One remaining issue: |
| } | ||
|
|
||
|
|
||
| def _run_with_known_failure( |
There was a problem hiding this comment.
Please re-use the existing xfail mechanism in the parametrize decorator instead.
AdrianLundell
left a comment
There was a problem hiding this comment.
Looks good overall to me now, just fix the tests to use the regular xfailing mechanism instead of a special function and it is an approve from my side. @rascani Do you have any input also?
The default SDPA decomposition splits the attention scale over query and key, where the quantizer cannot give the scale muls proportional qparams. Decompose SDPA before annotation with a single scale on the scores instead, so the scale mul gets qparams that allow folding it.
05766a2 to
6864d71
Compare
|
Thanks @AdrianLundell , switched to the xfails mechanism in parametrize. |
|
This LGTM, I think I'd just ask that we follow-up with a fix for the Thanks for this! I really appreciate your contribution. |
|
Thanks @rascani, really appreciate the review! Yes, I’d like to take the nn.MultiheadAttention(need_weights=False) follow-up. @AdrianLundell, on #22796 you mentioned an upcoming prototype for changing how folded Q/DQ parameters are handled in the graph. Is that work available now, and should I base this follow-up on it rather than adding a separate Cortex-M-specific workaround? |
Summary
Decompose eligible
aten.scaled_dot_product_attentiondirectly in theCortex-M
transform_for_annotationpipeline.The Cortex-M decomposition is:
The existing
MatmulToBmmPassthen converts the matmuls to BMMs.This replaces the previous approach that rewrote the output of
DecomposeSDPAWithRegularSoftmaxPass. That Arm decomposition is not part ofthe Cortex-M pipeline, so the previous pass did not run in the real flow.
Keeping the attention scale as a single MUL on the score tensor allows the
stock
CortexMQuantizerto produce proportional qparams around that MUL,making it suitable for later folding without adding Cortex-M-specific qspec
logic to the quantizer.
The decomposition is intentionally limited to fp32, unmasked, non-causal,
zero-dropout, non-GQA SDPA with static, non-empty shapes of rank 3 or higher
and matching leading dimensions. Unsupported forms are left unchanged.
Addresses the scale/decomposition part of #21943.
Tests
Added a full SDPA operator test starting from
torch.nn.functional.scaled_dot_product_attention, with no manual SDPAdecomposition.
The tests verify that:
matches eager SDPA;
out_scale ~= in_scale * scaleand equal zero points, so it can be foldedlater;
The SDPA dialect cases are xfailed because no lowering pass folds the scale
MUL yet, using the existing
xfailsmechanism ofparametrizewith itsdefault strict behavior.
nn.MultiheadAttention(need_weights=False)regresses with this change. Onmain it lowers with the attention left in fp32. With this change its
attention BMMs are quantized, and a BMM then gets an int8 input together with
the int32 output of a retraced folded linear, the separate issue from #21943.
That case is also xfailed.
Local verification:
5 deselected;
commit.
FVP implementation tests were not run locally and are covered by the
Cortex-M CI job.