|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +import torch.nn as nn |
| 11 | +from executorch.exir import to_edge |
| 12 | +from executorch.exir.capture._config import ExecutorchBackendConfig |
| 13 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 14 | +from executorch.exir.passes import MemoryPlanningPass |
| 15 | + |
| 16 | + |
| 17 | +class TestCat(nn.Module): |
| 18 | + def forward(self, x, y, z): |
| 19 | + empty = torch.empty((0, 6)) |
| 20 | + return torch.cat([empty, x, empty, y, z, empty]) |
| 21 | + |
| 22 | + def get_example_inputs(self): |
| 23 | + return (torch.rand(5, 6), torch.rand(5, 6), torch.rand(5, 6)) |
| 24 | + |
| 25 | + |
| 26 | +class TestPruneEmptyTensors(unittest.TestCase): |
| 27 | + def test_empty_tensor_removed_from_cat(self) -> None: |
| 28 | + model = TestCat() |
| 29 | + model.eval() |
| 30 | + example_inputs = model.get_example_inputs() |
| 31 | + ep = torch.export.export(model, example_inputs, strict=True) |
| 32 | + etpm = to_edge(ep).to_executorch( |
| 33 | + config=ExecutorchBackendConfig( |
| 34 | + remove_view_copy=False, |
| 35 | + memory_planning_pass=MemoryPlanningPass(alloc_graph_input=False), |
| 36 | + ), |
| 37 | + ) |
| 38 | + |
| 39 | + for node in etpm.exported_program().graph_module.graph.nodes: |
| 40 | + if node.target in [ |
| 41 | + exir_ops.edge.aten.cat.default, |
| 42 | + torch.ops.aten.cat.default, |
| 43 | + ]: |
| 44 | + self.assertTrue(len(node.all_input_nodes) == 3) |
| 45 | + for input_arg in node.all_input_nodes: |
| 46 | + tensor_val = input_arg.meta["val"] |
| 47 | + self.assertTrue(tensor_val.numel() != 0) |
| 48 | + |
| 49 | + actual = etpm.exported_program().module()(*example_inputs) |
| 50 | + |
| 51 | + reference = model(*example_inputs) |
| 52 | + |
| 53 | + self.assertTrue(torch.allclose(actual, reference)) |
| 54 | + |
| 55 | + def test_cat_removed_all_empty(self) -> None: |
| 56 | + model = TestCat() |
| 57 | + model.eval() |
| 58 | + example_inputs = (torch.empty((0, 6)), torch.empty((0, 6)), torch.empty((0, 6))) |
| 59 | + ep = torch.export.export(model, example_inputs, strict=True) |
| 60 | + etpm = to_edge(ep).to_executorch( |
| 61 | + config=ExecutorchBackendConfig( |
| 62 | + remove_view_copy=False, |
| 63 | + memory_planning_pass=MemoryPlanningPass(alloc_graph_input=False), |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + for node in etpm.exported_program().graph_module.graph.nodes: |
| 68 | + self.assertFalse( |
| 69 | + node.target |
| 70 | + in [exir_ops.edge.aten.cat.default, torch.ops.aten.cat.default] |
| 71 | + ) |
| 72 | + |
| 73 | + actual = etpm.exported_program().module()(*example_inputs) |
| 74 | + |
| 75 | + reference = model(*example_inputs) |
| 76 | + |
| 77 | + self.assertTrue(torch.allclose(actual, reference)) |
0 commit comments