Skip to content

Commit 4f3f329

Browse files
authored
Update ScalarToTensorPass to correctly track the modified bit
Differential Revision: D89085647 Pull Request resolved: pytorch#16231
1 parent b48ffef commit 4f3f329

2 files changed

Lines changed: 52 additions & 25 deletions

File tree

‎exir/passes/scalar_to_tensor_pass.py‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
# pyre-strict
88

99
import torch
10-
from executorch.exir.pass_base import ExportPass, map_args
10+
from executorch.exir.pass_base import ExportPass, map_args, PassResult
1111

1212

1313
class ScalarToTensorPass(ExportPass):
14+
def __init__(self) -> None:
15+
super().__init__()
16+
self._modified: bool = False
17+
1418
# pyre-ignore
1519
def call_operator(self, op, args, kwargs, meta):
1620
# pyre-ignore
@@ -21,12 +25,20 @@ def try_coerce(value, arg):
2125
# get a constant tensor with torch.tensor() call but instead
2226
# a fake tensor is created.
2327
with torch.utils._python_dispatch._disable_current_modes():
24-
return (
25-
torch.tensor(value)
26-
if isinstance(value, (float, int, bool))
27-
and isinstance(arg.type, torch.TensorType)
28-
else value
29-
)
28+
if isinstance(value, (float, int, bool)) and isinstance(
29+
arg.type, torch.TensorType
30+
):
31+
self._modified = True
32+
return torch.tensor(value)
33+
34+
return value
3035

3136
args, kwargs = map_args(op, try_coerce, args, kwargs)
3237
return super().call_operator(op, args, kwargs, meta)
38+
39+
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
40+
self._modified = False
41+
graph_module = super().call(graph_module).graph_module
42+
modified = self._modified
43+
self._modified = False
44+
return PassResult(graph_module, modified)

‎exir/tests/test_passes.py‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585

8686
from torch import nn
8787
from torch._prims_common import ELEMENTWISE_TYPE_PROMOTION_KIND
88+
89+
# Import passes
90+
from torch._subclasses.fake_tensor import FakeTensorMode
8891
from torch.export import export
8992
from torch.export.graph_signature import InputKind, InputSpec, TensorArgument
9093
from torch.fx import GraphModule, subgraph_rewriter
@@ -539,28 +542,40 @@ class NullPass(ExportPass):
539542
self.assertEqual(new_node.target, old_node.target)
540543

541544
def test_export_scalar_to_tensor_pass(self) -> None:
542-
class Mul(torch.nn.Module):
543-
def forward(self, x: torch.Tensor) -> torch.Tensor:
544-
return x * 3.14
545+
# Build a graph with a scalar argument where schema expects tensor
546+
graph = torch.fx.Graph()
547+
test_input = torch.randn(
548+
1,
549+
)
550+
with FakeTensorMode() as fake_mode:
551+
fake_input = fake_mode.from_tensor(test_input)
552+
x = graph.placeholder("x")
553+
x.meta["val"] = fake_input
545554

546-
mul = Mul()
555+
# Pass 3.14 as scalar - this should be converted to tensor by the pass
556+
mul_node = graph.call_function(
557+
torch.ops.aten.mul.Tensor,
558+
args=(x, 3.14),
559+
)
560+
graph.output(mul_node)
547561

548-
expo_prog = to_edge(export(mul, (torch.ones(1),), strict=True))
549-
new_prog = expo_prog.transform([ScalarToTensorPass()])
550-
self.assertIsNotNone(new_prog.exported_program().graph_module)
551-
new_graph_module = new_prog.exported_program().graph_module
562+
gm = torch.fx.GraphModule(torch.nn.Module(), graph)
563+
original_output = gm(test_input)
552564

553-
inp = torch.zeros(1)
554-
self.assertTrue(
555-
torch.allclose(
556-
expo_prog.exported_program().module()(inp),
557-
new_prog.exported_program().module()(inp),
558-
)
559-
)
560-
for node in new_graph_module.graph.nodes:
565+
result = ScalarToTensorPass()(gm)
566+
new_gm = result.graph_module
567+
self.assertTrue(result.modified)
568+
# All scalars should be tensors by this point, so running a second time should not modify
569+
self.assertFalse(ScalarToTensorPass()(new_gm).modified)
570+
571+
# All scalars should be converted into nodes
572+
for node in new_gm.graph.nodes:
561573
if node.op == "call_function":
562-
for arg in node.args + tuple(node.kwargs.values()):
563-
self.assertFalse(isinstance(arg, float))
574+
for arg in node.args:
575+
self.assertTrue(isinstance(arg, torch.fx.Node))
576+
577+
new_output = new_gm(test_input)
578+
self.assertTrue(torch.equal(original_output, new_output))
564579

565580
def test_remove_mixed_types_symfloats(self) -> None:
566581
class Foo(torch.nn.Module):

0 commit comments

Comments
 (0)