Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions backends/xnnpack/test/ops/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,30 @@ def test_fp32_addmm(self):
uses_bias=True,
)

def test_fp32_addmm_with_alpha_or_beta(self):
class ScaledAddMMModule(AddMMModule):
def __init__(self, alpha, beta):
super().__init__(8, 4)
self.alpha, self.beta = alpha, beta

def forward(self, x):
return torch.addmm(
self.bias, x, self.mat, alpha=self.alpha, beta=self.beta
)

# XNNPACK has no alpha or beta, so each of these addmm stays on portable.
for alpha, beta in [(2, 1), (1, 3), (1, 0)]:
with self.subTest(alpha=alpha, beta=beta):
(
Tester(ScaledAddMMModule(alpha, beta).eval(), (torch.randn(2, 8),))
.export()
.to_edge_transform_and_lower()
.check(["executorch_exir_dialects_edge__ops_aten_addmm_default"])
.to_executorch()
.serialize()
.run_method_and_compare_outputs()
)

def test_fp32_linear_view_weight(self):
class ViewWeightLinear(torch.nn.Module):
def __init__(self):
Expand Down
32 changes: 32 additions & 0 deletions backends/xnnpack/test/passes/test_convert_to_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,35 @@ def forward(self, x):
)
.run_method_and_compare_outputs(inputs=(x,))
)

def test_fp32_addmm_with_alpha_or_beta(self):
class TransposedWeightAddmm(torch.nn.Module):
def __init__(self, alpha, beta):
super().__init__()
self.weight = torch.nn.Parameter(torch.randn(4, 8))
self.bias = torch.nn.Parameter(torch.randn(4))
self.alpha, self.beta = alpha, beta

def forward(self, x):
return torch.addmm(
self.bias, x, self.weight.t(), alpha=self.alpha, beta=self.beta
)

# linear has no alpha or beta, so only the default addmm may become one.
for alpha, beta, op in [
(1, 1, "linear"),
(2, 1, "addmm"),
(1, 3, "addmm"),
(1, 0, "addmm"),
]:
with self.subTest(alpha=alpha, beta=beta):
(
Tester(TransposedWeightAddmm(alpha, beta), (torch.randn(2, 8),))
.export()
.to_edge()
.run_passes(self.PassStage)
.check_count(
{f"executorch_exir_dialects_edge__ops_aten_{op}_default": 1}
)
.run_method_and_compare_outputs()
)