Skip to content

Commit daf93a1

Browse files
authored
Fix memory planning error
Differential Revision: D88770975 Pull Request resolved: pytorch#16165
1 parent 5a79e18 commit daf93a1

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
import math
1616
import operator
1717
from operator import neg
18-
from typing import cast, Dict, Iterable, Optional, Sequence
18+
from typing import cast, Dict, Optional, Sequence
1919

2020
import torch
2121
import torch.fx
2222
from executorch.backends.cadence.aot.compiler_utils import (
2323
get_zero_point,
24-
is_node_with_op,
2524
quantize_tensor_multiplier,
2625
)
2726
from executorch.backends.cadence.aot.fuse_ops import (
@@ -52,17 +51,6 @@
5251
}
5352

5453

55-
def contains_placeholder_or_param(nodes: Iterable[torch.fx.Node]) -> bool:
56-
"""
57-
Return true if any of the node in the incoming nodes list is a placeholder
58-
or parameter
59-
"""
60-
return any(
61-
is_node_with_op(node, "placeholder") or is_node_with_op(node, "get_attr")
62-
for node in nodes
63-
)
64-
65-
6654
@register_cadence_pass(CadencePassAttribute(opt_level=0))
6755
class ReplaceLogicalNotBooleanWhereWithWherePass(RemoveOrReplacePassInterface):
6856
"""
@@ -367,6 +355,9 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
367355

368356
graph = node.graph
369357

358+
fit_bias = beta == 1.0
359+
fit_mat2 = False
360+
370361
# Handle transpose: if mat2 is a transpose op, extract the original tensor
371362
transposed_mat2 = False
372363
if (
@@ -376,6 +367,10 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
376367
# mat2 is already transposed, so we use the input to the transpose
377368
mat2 = cast(torch.fx.Node, mat2.args[0])
378369
transposed_mat2 = True
370+
fit_mat2 = alpha == 1.0
371+
372+
if not (fit_bias and fit_mat2):
373+
return False
379374

380375
# Multiply bias by beta if needed
381376
if beta != 1.0:

‎backends/cadence/aot/tests/test_replace_ops_passes.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,10 @@ def test_replace_linear_with_fully_connected(self) -> None:
10071007
0,
10081008
)
10091009

1010-
@expand([[1.0, 1.0], [2.0, 3.0]])
10111010
@torch.no_grad()
1012-
def test_replace_addmm_with_linear(self, alpha: float, beta: float) -> None:
1011+
def test_replace_addmm_with_linear(self) -> None:
1012+
alpha = 1.0
1013+
beta = 1.0
10131014
M, K, N = 14, 12, 10
10141015
builder = GraphBuilder()
10151016
x_input = torch.randn(N, dtype=torch.float32)
@@ -1054,6 +1055,37 @@ def test_replace_addmm_with_linear(self, alpha: float, beta: float) -> None:
10541055
0,
10551056
)
10561057

1058+
@torch.no_grad()
1059+
def test_no_replace_addmm_with_linear(self) -> None:
1060+
alpha = 2.0
1061+
beta = 3.0
1062+
M, K, N = 14, 12, 10
1063+
builder = GraphBuilder()
1064+
x_input = torch.randn(N, dtype=torch.float32)
1065+
y_input = torch.randn([M, K], dtype=torch.float32)
1066+
z_input = torch.randn([N, K], dtype=torch.float32)
1067+
x = builder.placeholder("x", x_input)
1068+
y = builder.placeholder("y", y_input)
1069+
z = builder.placeholder("z", z_input)
1070+
permute_copy = builder.call_operator(
1071+
op=exir_ops.edge.aten.permute_copy.default,
1072+
args=(z, [1, 0]),
1073+
)
1074+
addmm = builder.call_operator(
1075+
op=exir_ops.edge.aten.addmm.default,
1076+
args=(x, y, permute_copy),
1077+
kwargs={"beta": beta, "alpha": alpha},
1078+
)
1079+
builder.output([addmm])
1080+
original_gm = builder.get_graph_module()
1081+
1082+
gm = cast(
1083+
PassResult, ReplacePermuteWithTransposePass()(original_gm)
1084+
).graph_module
1085+
1086+
pass_result = cast(PassResult, ReplaceAddMMWithLinearPass()(gm))
1087+
self.assertFalse(pass_result.modified)
1088+
10571089
@torch.no_grad()
10581090
def test_replace_mm_with_addmm(self) -> None:
10591091
M, K, N = 14, 48, 24

0 commit comments

Comments
 (0)