Skip to content

Commit a02062d

Browse files
authored
Move positional alpha to kwargs in ReplaceScalarWithTensorArgPass (pytorch#24176)
Summary: `add.Scalar(Tensor self, Scalar other, Scalar alpha=1)` takes `alpha` positionally, but `add.Tensor(Tensor self, Tensor other, *, Scalar alpha=1)` takes it as keyword-only, and the same holds for `sub`. When `alpha` was the third positional argument, `get_replacement` passed it through `*args[2:]`, and building the `.Tensor` node raised `RuntimeError: aten::add() takes 2 positional argument(s) but 3 was/were given`. The 3-argument path now binds the call by name with `torch.fx.operator_schemas.normalize_function` (the part of `get_arg` that doesn't need a `Node`; `call_operator` gets no node), passes `self` (`input` after normalization) and `other` positionally, and `alpha` as a kwarg. `get_replacement` always gets 2 args and no longer forwards extra ones. The Arm `ReplaceScalarWithTensorByProfilePass` subclass routes through the same base `call_operator`, so it gets the fix too for edge `add/sub.Scalar`. `torch.ops.aten.add/sub.Scalar` with a positional `alpha` still pass through unreplaced, as before. The changed path previously always raised, so no graph that worked before is affected. Authored with Claude Code. Reviewed By: DrJessop Differential Revision: D127746753 Pull Request resolved: pytorch#24176
1 parent 0db7a88 commit a02062d

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,33 @@ def test_sub_replace_scalar_with_tensor_arg(
326326
0,
327327
)
328328

329+
@expand(
330+
[
331+
["add", exir_ops.edge.aten.add.Scalar, exir_ops.edge.aten.add.Tensor],
332+
["sub", exir_ops.edge.aten.sub.Scalar, exir_ops.edge.aten.sub.Tensor],
333+
]
334+
)
335+
@torch.no_grad()
336+
def test_replace_scalar_with_tensor_arg_positional_alpha(
337+
self,
338+
_,
339+
scalar_op: torch._ops.OpOverload,
340+
tensor_op: torch._ops.OpOverload,
341+
) -> None:
342+
x = torch.randn(7, 5)
343+
original_gm = single_op_builder(
344+
placeholders=(x,),
345+
op=scalar_op,
346+
args=(x, 1.5, 2),
347+
)
348+
gm_before = copy.deepcopy(original_gm)
349+
p = ReplaceScalarWithTensorArgPass()
350+
graph_after_passes = cast(PassResult, p(original_gm)).graph_module
351+
352+
validate(gm_before, graph_after_passes, [x], "ReplaceScalarWithTensorArgPass")
353+
self.assertEqual(count_node(graph_after_passes, tensor_op), 1)
354+
self.assertEqual(count_node(graph_after_passes, scalar_op), 0)
355+
329356
@expand(
330357
[
331358
["3d", (7, 5, 6), 1.23],

‎backends/transforms/replace_scalar_with_tensor.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from executorch.exir.dialects.edge._ops import EdgeOpOverload
1313
from executorch.exir.pass_base import ExportPass
1414
from torch._ops import OpOverload
15+
from torch.fx.operator_schemas import normalize_function
1516

1617

1718
class ReplaceScalarWithTensorArgPass(ExportPass):
@@ -71,8 +72,6 @@ def get_replacement(self, op, args, kwargs, meta):
7172
},
7273
meta=meta,
7374
),
74-
# Other args.
75-
*args[2:],
7675
),
7776
kwargs=kwargs,
7877
meta=meta,
@@ -96,4 +95,10 @@ def call_operator(self, op, args, kwargs, meta):
9695
):
9796
return super().call_operator(op, args, kwargs, meta)
9897

99-
return self.get_replacement(op, args, kwargs, meta)
98+
# alpha is positional in add/sub.Scalar but keyword-only in .Tensor.
99+
bound = normalize_function(
100+
op, args, kwargs, normalize_to_only_use_kwargs=True
101+
).kwargs
102+
return self.get_replacement(
103+
op, (bound["input"], bound["other"]), {"alpha": bound["alpha"]}, meta
104+
)

0 commit comments

Comments
 (0)