|
| 1 | +# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | + |
| 4 | +import torch |
| 5 | +import executorch.backends.cadence.aot.ops_registrations # noqa |
| 6 | +from executorch.backends.cadence.aot.graph_builder import ( |
| 7 | + GraphBuilder, |
| 8 | + single_op_builder, |
| 9 | +) |
| 10 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 11 | +from executorch.exir.pass_base import ExportPass |
| 12 | +from later.unittest import TestCase |
| 13 | +from executorch.backends.cadence.aot.pass_utils import count_node |
| 14 | + |
| 15 | +class TestGraphBuilder(TestCase): |
| 16 | + def test_graph_with_single_im2row(self) -> None: |
| 17 | + # Create a graph with a single im2row node. |
| 18 | + builder = GraphBuilder() |
| 19 | + x = builder.placeholder("x", torch.randn(1, 3, 224, 224)) |
| 20 | + pad_value = builder.placeholder("pad", torch.randn(1)) |
| 21 | + channels_last = False |
| 22 | + im2row = builder.call_operator( |
| 23 | + exir_ops.edge.cadence.im2row.default, |
| 24 | + # pyre-ignore |
| 25 | + ( |
| 26 | + x, |
| 27 | + (2, 2), |
| 28 | + (1, 1), |
| 29 | + (0, 0), |
| 30 | + (1, 1), |
| 31 | + pad_value, |
| 32 | + channels_last, |
| 33 | + ), |
| 34 | + ) |
| 35 | + builder.output([im2row]) |
| 36 | + gm = builder.get_graph_module() |
| 37 | + # Check if graph module is valid by running exportpass on it. |
| 38 | + gm = ExportPass().call(gm).graph_module |
| 39 | + |
| 40 | + # Check graph has a single im2row node. |
| 41 | + self.assertEqual(len([gm.graph.nodes]), 1) |
| 42 | + self.assertEqual(count_node(gm, exir_ops.edge.cadence.im2row.default), 1) |
| 43 | + |
| 44 | + |
| 45 | +class TestSingleOpBuilderUtility(TestCase): |
| 46 | + def test_graph_with_single_im2row(self) -> None: |
| 47 | + # Create a graph with a single im2row node. |
| 48 | + x = torch.randn(1, 3, 224, 224) |
| 49 | + pad_value = torch.randn(1) |
| 50 | + channels_last = False |
| 51 | + gm = single_op_builder( |
| 52 | + (x, pad_value), |
| 53 | + exir_ops.edge.cadence.im2row.default, |
| 54 | + ( |
| 55 | + x, |
| 56 | + (2, 2), |
| 57 | + (1, 1), |
| 58 | + (0, 0), |
| 59 | + (1, 1), |
| 60 | + pad_value, |
| 61 | + channels_last, |
| 62 | + ), |
| 63 | + ) |
| 64 | + # Check if graph module is valid by running exportpass on it. |
| 65 | + gm = ExportPass().call(gm).graph_module |
| 66 | + |
| 67 | + # Check graph has a single im2row node. |
| 68 | + self.assertEqual(len([gm.graph.nodes]), 1) |
| 69 | + self.assertEqual(count_node(gm, exir_ops.edge.cadence.im2row.default), 1) |
0 commit comments