Skip to content

Commit 2c560b1

Browse files
committed
[XNNPACK] Reject addmm with non-default alpha or beta
torch.addmm(input, mat1, mat2, beta=b, alpha=a) computes b * input + a * (mat1 @ mat2), but the XNNPACK lowering of addmm ignores alpha and beta. AddmmConfig accepted such an addmm whenever its weight and bias were static params, so the delegate silently computed mat1 @ mat2 + input. It now rejects them, and the addmm runs on the portable kernels. This change was authored with Claude Code.
1 parent 4628b2b commit 2c560b1

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎backends/xnnpack/partition/config/gemm_configs.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,12 @@ def __init__(self, **kwargs):
479479
self.src_partitions = None
480480
self.linear_modules = [torch.nn.functional.linear, torch.nn.Linear]
481481

482+
def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
483+
if node.kwargs.get("alpha", 1) != 1 or node.kwargs.get("beta", 1) != 1:
484+
why(node, "XNNPACK does not support addmm with alpha or beta")
485+
return False
486+
return super().check_constraints(node, ep)
487+
482488
def _get_weight_deps(
483489
self, node: torch.fx.Node, ep: ExportedProgram, precision: ConfigPrecisionType
484490
) -> Tuple[bool, List[torch.fx.Node]]:

‎backends/xnnpack/test/ops/test_linear.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,22 @@ def test_fp32_addmm(self):
782782
uses_bias=True,
783783
)
784784

785+
def test_fp32_addmm_with_alpha_and_beta(self):
786+
class ScaledAddMMModule(AddMMModule):
787+
def forward(self, x):
788+
return torch.addmm(self.bias, x, self.mat, alpha=2, beta=3)
789+
790+
# XNNPACK has no alpha or beta, so the addmm stays on portable.
791+
(
792+
Tester(ScaledAddMMModule(8, 4).eval(), (torch.randn(2, 8),))
793+
.export()
794+
.to_edge_transform_and_lower()
795+
.check(["executorch_exir_dialects_edge__ops_aten_addmm_default"])
796+
.to_executorch()
797+
.serialize()
798+
.run_method_and_compare_outputs()
799+
)
800+
785801
def test_fp32_linear_view_weight(self):
786802
class ViewWeightLinear(torch.nn.Module):
787803
def __init__(self):

0 commit comments

Comments
 (0)