Repository navigation
Fix addmm and mm lowering for scales, bias shapes and identity permutes - #24658
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24658
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 4db2d9e with merge base d1f597e ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude /pr-review |
|
Claude finished @shoumikhin's task in 19m 21s —— View job Request changes: Full reviewPR Review: #24658SummaryThe PR stops scaled There are 2 major problems:
There are also 4 minor problems. All 4 changed files were reviewed. Nothing was run, so every finding comes from reading the code. Code Quality
Testing
Backward Compatibility
RecommendationRequest Changes The first Code Quality item and the Samsung item block approval, and both fixes are small:
Review steps
Not checked:
|
6de0bb4 to
274e6e8
Compare
|
Both major items are fixed. 1. Bias that is not 1-D. I reproduced it: after I kept the view rule for a bias with more than 1 dim, since a 2. Samsung. The partitioner now takes an Minor items:
|
|
@claude /pr-review |
|
Claude finished @shoumikhin's task in 21m 20s —— View job Needs discussion: Samsung still accepts Full reviewPR Review: #24658SummaryThe PR keeps an There is 1 major problem, which predates the PR, and there are 2 minor ones. None of them was confirmed to be a regression. All 5 changed files were reviewed. Nothing was run, so every finding comes from reading the code. Code Quality
RecommendationNeeds Discussion
Review steps
Not checked:
|
274e6e8 to
2198156
Compare
|
1. Samsung. The comment now says ENN takes only an 2. Vulkan. I ran a Vulkan build on SwiftShader. On main, 3. |
|
@claude /pr-review |
|
Claude finished @shoumikhin's task in 20m 52s —— View job Approve: the reviewers found no blockers and no major problems, and the 1 minor finding, that Full reviewPR Review: #24658SummaryThe PR stops an
Code Quality
RecommendationApprove Nothing blocks approval. The finding above predates the PR, so it can be fixed here or in a separate PR. Review steps
Not checked:
|
AddmmToLinearTransform turns addmm(bias, x, w.t()) into linear(x, w, bias). But addmm also takes beta and alpha, and computes beta * bias + alpha * (x @ w.t()). The transform dropped both, so torch.addmm(b, x, w.t(), beta=2.0) returned x @ w.t() + b instead of x @ w.t() + 2 * b. A linear cannot scale its bias or its product, so an addmm whose beta or alpha is not 1 now stays an addmm. A new helper, addmm_can_be_linear, holds the whole rule, including the existing check that the weight is a t_copy or permute_copy. Vulkan's addmm applies beta and alpha. A new Vulkan test lowers a scaled addmm and compares it with eager. On SwiftShader it passes at this commit, and before it the output was off by up to 3.5. CI does not run this test. ENN has no addmm. The Samsung partitioner took every addmm, because preprocess runs this transform. But preprocess failed the whole lowering with "aten.addmm.default is not supported in ENN Delegate" for any addmm the transform left alone. The partitioner now takes an addmm of constants, which preprocess folds away. Otherwise it takes an addmm only when addmm_can_be_linear allows it and its weight is not constant. A constant weight is left out because ConstantPropPass runs before the transform and folds its transpose. The rest runs on the CPU. The partitioner needs the ENN SDK to import. So it was checked only with the native wrapper modules stubbed. A scaled addmm, a standalone addmm(b, x, w) and an addmm of a transposed constant weight are now left out. An addmm of a transposed runtime weight and an addmm of constants are still taken. Running the transform before ConstantPropPass would let ENN take the constant weight case. That needs the ENN SDK to verify, so it is left to the Samsung maintainers.
addmm takes any bias that broadcasts to the output, for example [1, N], [M, N], [M, 1] or a 0-D tensor. AddmmToLinearTransform turned such an addmm into a linear with the same bias, but ExecuTorch's linear kernel takes only a 1-D bias. So a torch.addmm that stays outside a delegate and then goes through this transform, as the LLM export does with ConvertToLinearPass, failed at run time with "Bias has wrong dimensionality". addmm_can_be_linear now also requires a 1-D bias, so such an addmm stays an addmm and runs with the addmm kernel, and the Samsung partitioner leaves it to the CPU. Vulkan's linear already handled [1, N], [M, N] and [M, 1] biases. There such an addmm now runs as an addmm, which transposes the weight on every run instead of packing it once. The Vulkan addmm test gains an [M, N] bias case, which matches eager on SwiftShader before and after this change. An F.linear with such a bias is still turned into a linear by ConvertToLinearPass's own source- partition step, which this change does not touch.
When a linear reads a dequantized tensor through views, AddmmToLinearTransform can point the linear at the dequantized tensor so that the view after the linear goes. A linear kept from export, for example one a partitioner keeps whole, can have a bias of more than 1 dim that follows the rows of the flattened input, such as [M, N] or [M, 1]. Such a bias does not broadcast to the dequantized shape, so with a [6, 16] bias the program failed to run. The transform now leaves a linear with such a bias reading its flattened input, and keeps the view after it. This includes the flattened input used as the bias. That case gave right results before, but only because replace_input_with replaced the bias too. It now keeps its views like any other bias of more than 1 dim.
When a linear reads a dequantized tensor through views, AddmmToLinearTransform can point the linear at the dequantized tensor so that the view after the linear goes. It did that with replace_input_with, which replaces every argument that is the input node. For flat @ flat.t() the weight is the same node as the input, so the weight also became the 3-D dequantized tensor, and the program failed to run. The rewire now replaces only the input argument.
The docstring of replace_linear_view_copy_input_output still described only the original pattern. It now says when the view after a linear goes, when the linear reads the dequantized tensor instead of its flattened input, and that with dynamic shapes the linear's input and the view need symbolic shapes in meta["val"]. A new test case, for coverage only, checks that a linear whose output has another reader besides the view keeps its input and its view.
addmm computes beta * bias + alpha * (x @ w). XNNPACK lowers it to a fully connected op, which has no such scales, and the lowering dropped them. So torch.addmm(b, x, w, beta=2.0) ran as x @ w + b inside the XNNPACK delegate, with wrong values. The XNNPACK partitioner now leaves such an addmm out of the delegate, as it already does for an add whose alpha is not 1, so the addmm runs with the portable kernels and gives the right values.
addmm adds a bias that can be any tensor that broadcasts to the output, for example a full [M, N] matrix, or a single value when N > 1. XNNPACK lowers addmm to a fully connected op, which reads exactly 1 bias value per output channel. So an addmm with such a bias ran inside the XNNPACK delegate with wrong values. This also happened for a bias that is a program input, which force_non_static_weights_for_f32_linear lets in. The XNNPACK partitioner now takes an addmm only when its bias holds exactly N values, 1 per output channel: [N] or [1, N], or a single value when N is 1. A bias whose size is not known when lowering is kept out as well, and checking it does not fail the lowering. Any other addmm stays out of the delegate and runs with the portable kernels. The check skips mm, which inherits this config but has no bias argument; a new mm test checks that mm is still delegated. Another test covers an F.linear that to_edge decomposes into addmm(bias, x, weight.t()), which XNNPACK takes through the linear source partition.
AddmmToLinearTransform turns addmm(b, x, w.t()) and mm(x, w.t()) into a linear of w. It took any permute_copy as the transpose, but permute_copy(w, [0, 1]) leaves w as it is. So torch.addmm(b, x, w.permute(0, 1)) and x @ w.permute(0, 1) became linears of w, which multiply by w.t() instead of w. With a square w the values were wrong. With any other w the transform failed. XNNPACK's ConvertToLinearPass, Samsung and Vulkan all use this transform, and the LLM export runs ConvertToLinearPass too. On Vulkan with SwiftShader, the addmm and the mm were off by up to 14.4, on main and on the parent commit. A new helper, is_transpose, accepts t_copy and a permute_copy whose dims are [1, 0], also when written as negative dims. addmm_can_be_linear and the mm branch both use it, so any other permute stays as it is. The Samsung partitioner uses addmm_can_be_linear, so it now also leaves such an addmm to the CPU when its weight is not a constant. On Vulkan the same programs now match eager. Vulkan's partitioner still treats such a permute as part of a linear and skips its checks for it. The permute then runs as its own op. With a check there, the graph and the outputs stay the same.
2198156 to
4db2d9e
Compare
|
The identity permute is fixed in this push. A new helper, A new test runs On Vulkan with SwiftShader, the About the bias drop from the previous review (an |
|
@claude /pr-review |
|
Claude finished @shoumikhin's task in 20m 18s —— View job Approve: the reviewers found no blocker or major problem; the 1 minor finding is that the new Vulkan test does not cover the Full reviewPR Review: #24658SummaryThe PR makes an
Testing
RecommendationApprove Nothing blocks approval. The Testing item is small, so it can go in this PR or a later one. Review steps
Not checked:
|
|
Thanks. That is right: The |
pytorch#24658 made the XNNPACK partitioner reject an addmm whose alpha or beta is not 1, and made the addmm-to-linear transform keep such an addmm. These tests also cover ConvertToLinearPass, which the LLM export builder runs on the whole program, and beta=0, which makes addmm ignore its input. ConvertToLinearPass keeps an addmm with a transposed weight and alpha or beta, and still turns the default one into a linear. The partitioner leaves an addmm with only alpha, only beta, or beta=0 on portable. This change was authored with Claude Code.
torch.addmm(bias, x, w, beta=..., alpha=...)computesbeta * bias + alpha * (x @ w), with any broadcastable bias. Some lowering steps miss this, so some programs give wrong values or fail.What is wrong today
betaandalphaare dropped.AddmmToLinearTransformturnsaddmm(b, x, w.t(), beta=2.0)intolinear(x, w, b)without the scale. XNNPACK delegates a scaledaddmmto its fully connected op, which has no scales.linearwith any bias, but ExecuTorch'slinearkernel takes only a 1-D bias. The LLM export then fails at run time. XNNPACK misreads any bias that is not 1 value per output channel.[0, 1]permute counts as a transpose, soaddmmandmmgive wrong values with a square weight and fail with any other.[M, N]bias and forx @ x.t(), because the transform points the linear at the dequantized tensor.What this change does
addmmunless its weight is transposed, its scales are 1 and its bias is 1-D.addmm_can_be_linearholds the rule. Onlytor a[1, 0]permute counts as a transpose, also formm.addmm, and one whose bias is not 1 value per output channel.Vulkan runs the same transform, and applies the scales itself for float weights. A 2-D bias was already correct there, but now its weight is transposed each run.
Testing
addmm, bias shapes, program-input biases, a decomposedF.linear, a standaloneaddmm, a[0, 1]permute, and the input as bias or weight, each failing without its fix.ConvertToLinearPass, anaddmmwith a non-1-D bias now runs. AnF.linearwith one still fails, and XNNPACK misreads it.