|
85 | 85 |
|
86 | 86 | from torch import nn |
87 | 87 | from torch._prims_common import ELEMENTWISE_TYPE_PROMOTION_KIND |
| 88 | + |
| 89 | +# Import passes |
| 90 | +from torch._subclasses.fake_tensor import FakeTensorMode |
88 | 91 | from torch.export import export |
89 | 92 | from torch.export.graph_signature import InputKind, InputSpec, TensorArgument |
90 | 93 | from torch.fx import GraphModule, subgraph_rewriter |
@@ -539,28 +542,40 @@ class NullPass(ExportPass): |
539 | 542 | self.assertEqual(new_node.target, old_node.target) |
540 | 543 |
|
541 | 544 | 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 |
545 | 554 |
|
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) |
547 | 561 |
|
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) |
552 | 564 |
|
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: |
561 | 573 | 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)) |
564 | 579 |
|
565 | 580 | def test_remove_mixed_types_symfloats(self) -> None: |
566 | 581 | class Foo(torch.nn.Module): |
|
0 commit comments