|
| 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 copy |
| 8 | +import unittest |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +from executorch.exir import memory, to_edge |
| 13 | +from executorch.exir.capture._config import ExecutorchBackendConfig |
| 14 | +from executorch.exir.passes import MemoryPlanningPass |
| 15 | + |
| 16 | + |
| 17 | +class TestModel1(nn.Module): |
| 18 | + def __init__(self): |
| 19 | + super().__init__() |
| 20 | + self.parameter = nn.Parameter(torch.rand(5, 6)) |
| 21 | + self.parameter.requires_grad = False |
| 22 | + |
| 23 | + def forward(self, x): |
| 24 | + v1 = self.parameter.view( |
| 25 | + 6, 5 |
| 26 | + ) # removed, lifetime of parameter will be extended |
| 27 | + v2 = x.view(6, 5) # not removed |
| 28 | + v3 = torch.ops.aten.mul.Tensor(v1, v2).view( |
| 29 | + 30 |
| 30 | + ) # removed, lifetime of mul.Tensor will be extended |
| 31 | + return v3 |
| 32 | + |
| 33 | + def get_example_inputs(self): |
| 34 | + return (torch.rand(5, 6),) |
| 35 | + |
| 36 | + |
| 37 | +class TestTryRemoveViewCopy(unittest.TestCase): |
| 38 | + def test_disable(self) -> None: |
| 39 | + model = TestModel1() |
| 40 | + model.eval() |
| 41 | + example_inputs = model.get_example_inputs() |
| 42 | + ep = torch.export.export(model, example_inputs) |
| 43 | + etpm = to_edge(ep).to_executorch( |
| 44 | + config=ExecutorchBackendConfig( |
| 45 | + try_remove_view_copy=False, |
| 46 | + memory_planning_pass=MemoryPlanningPass( |
| 47 | + "greedy", alloc_graph_input=False |
| 48 | + ), |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + for node in etpm.exported_program().graph_module.graph.nodes: |
| 53 | + assert node.target != memory.view |
| 54 | + |
| 55 | + def test_output_matches(self) -> None: |
| 56 | + model = TestModel1() |
| 57 | + model.eval() |
| 58 | + example_inputs = model.get_example_inputs() |
| 59 | + ep = torch.export.export(model, example_inputs) |
| 60 | + |
| 61 | + epm_remove = to_edge(ep) |
| 62 | + epm_no_remove = copy.deepcopy( |
| 63 | + epm_remove |
| 64 | + ) # to_executorch modifies the edge_program, so we make a copy |
| 65 | + |
| 66 | + # Run pass with no removal |
| 67 | + etpm_remove = epm_remove.to_executorch( |
| 68 | + config=ExecutorchBackendConfig( |
| 69 | + try_remove_view_copy=True, |
| 70 | + memory_planning_pass=MemoryPlanningPass( |
| 71 | + "greedy", alloc_graph_input=False |
| 72 | + ), |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + # Run pass with removal |
| 77 | + etpm_no_remove = epm_no_remove.to_executorch( |
| 78 | + config=ExecutorchBackendConfig( |
| 79 | + try_remove_view_copy=True, |
| 80 | + memory_planning_pass=MemoryPlanningPass( |
| 81 | + "greedy", alloc_graph_input=False |
| 82 | + ), |
| 83 | + ), |
| 84 | + ) |
| 85 | + |
| 86 | + out_remove = etpm_remove.exported_program().module()(*example_inputs) |
| 87 | + out_no_remove = etpm_no_remove.exported_program().module()(*example_inputs) |
| 88 | + |
| 89 | + self.assertTrue(torch.allclose(out_remove, out_no_remove)) |
| 90 | + |
| 91 | + def test_spec(self) -> None: |
| 92 | + model = TestModel1() |
| 93 | + model.eval() |
| 94 | + example_inputs = model.get_example_inputs() |
| 95 | + ep = torch.export.export(model, example_inputs) |
| 96 | + |
| 97 | + etpm = to_edge(ep).to_executorch( |
| 98 | + config=ExecutorchBackendConfig( |
| 99 | + try_remove_view_copy=True, |
| 100 | + memory_planning_pass=MemoryPlanningPass( |
| 101 | + "greedy", alloc_graph_input=False |
| 102 | + ), |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + # etpm.exported_program().graph.print_tabular() |
| 107 | + |
| 108 | + # idx opcode name target args kwargs |
| 109 | + # --- ------------- ------------------------ ---------------------------------- -------------------------------------------------- ---------------- |
| 110 | + # 0 placeholder arg0_1 arg0_1 () {} |
| 111 | + # 1 placeholder arg1_1 arg1_1 () {} |
| 112 | + # 2 call_function aten_view_copy_default <function view at 0x7f10a6dfeb00> (arg0_1, [6, 5]) {} |
| 113 | + # 3 call_function alloc <function alloc at 0x7f10a6dfe9e0> (((6, 5), torch.float32),) {} |
| 114 | + # 4 call_function aten_view_copy_default_1 aten.view_copy.out (arg1_1, [6, 5]) {'out': alloc} |
| 115 | + # 5 call_function alloc_1 <function alloc at 0x7f10a6dfe9e0> (((6, 5), torch.float32),) {} |
| 116 | + # 6 call_function aten_mul_tensor aten.mul.out (aten_view_copy_default, aten_view_copy_default_1) {'out': alloc_1} |
| 117 | + # 7 call_function aten_view_copy_default_2 <function view at 0x7f10a6dfeb00> (aten_mul_tensor, [30]) {} |
| 118 | + # 8 output output_1 output ((aten_view_copy_default_2,),) {} |
| 119 | + |
| 120 | + # arg0_1 is the parameter |
| 121 | + # arg1_1 is the user input |
| 122 | + |
| 123 | + for node in etpm.exported_program().graph.nodes: |
| 124 | + if node.name == "arg0_1": |
| 125 | + # arg0_1's lifetime is extended through aten_view_copy_default (memory.view) to idx 6 |
| 126 | + self.assertEqual(node.meta["spec"].lifetime, [0, 6]) |
| 127 | + elif node.name == "aten_view_copy_default": |
| 128 | + # aten_view_copy_default is a memory.view of arg0_1. |
| 129 | + # arg0_1 is a constant with storage, so we check that the view's storage matches the base |
| 130 | + |
| 131 | + # assert base is arg0_1 |
| 132 | + self.assertEqual(node.args[0].name, "arg0_1") |
| 133 | + |
| 134 | + # assert base is const with storage |
| 135 | + self.assertTrue(node.args[0].meta["spec"].const) |
| 136 | + self.assertTrue(node.args[0].meta["spec"].storage is not None) |
| 137 | + self.assertTrue(node.args[0].meta["spec"].mem_id is None) |
| 138 | + self.assertTrue(node.args[0].meta["spec"].mem_offset is None) |
| 139 | + |
| 140 | + # assert self is const with storage |
| 141 | + self.assertTrue(node.meta["spec"].const) |
| 142 | + self.assertTrue(node.meta["spec"].storage is not None) |
| 143 | + self.assertTrue(node.meta["spec"].mem_id is None) |
| 144 | + self.assertTrue(node.meta["spec"].mem_offset is None) |
| 145 | + |
| 146 | + # assert storage matches |
| 147 | + self.assertEqual( |
| 148 | + node.meta["spec"].storage, node.args[0].meta["spec"].storage |
| 149 | + ) |
| 150 | + |
| 151 | + # assert lifetime matches |
| 152 | + self.assertEqual( |
| 153 | + node.meta["spec"].lifetime, node.args[0].meta["spec"].lifetime |
| 154 | + ) |
| 155 | + elif node.name == "aten_mul_tensor": |
| 156 | + # aten_mul_tensor's lifetime is extended through aten_view_copy_default_2 (memory.view) to idx 8 |
| 157 | + self.assertEqual(node.meta["spec"].lifetime, [5, 8]) |
| 158 | + elif node.name == "aten_view_copy_default_2": |
| 159 | + # aten_view_copy_default_2 is a memory.view of aten_mul_tensor |
| 160 | + |
| 161 | + # assert base is aten_mul_tensor |
| 162 | + self.assertEqual(node.args[0].name, "aten_mul_tensor") |
| 163 | + |
| 164 | + # assert base and self are not const, do not have storage, |
| 165 | + # but do have mem_id and mem_offset |
| 166 | + self.assertFalse(node.args[0].meta["spec"].const) |
| 167 | + self.assertTrue(node.args[0].meta["spec"].storage is None) |
| 168 | + self.assertTrue(node.args[0].meta["spec"].mem_id is not None) |
| 169 | + self.assertTrue(node.args[0].meta["spec"].mem_offset is not None) |
| 170 | + |
| 171 | + self.assertFalse(node.meta["spec"].const) |
| 172 | + self.assertTrue(node.meta["spec"].storage is None) |
| 173 | + self.assertTrue(node.meta["spec"].mem_id is not None) |
| 174 | + self.assertTrue(node.meta["spec"].mem_offset is not None) |
| 175 | + |
| 176 | + # assert self and base mem_id, mem_offset, and lifetime matches |
| 177 | + self.assertEqual( |
| 178 | + node.meta["spec"].mem_id, node.args[0].meta["spec"].mem_id |
| 179 | + ) |
| 180 | + self.assertEqual( |
| 181 | + node.meta["spec"].mem_offset, node.args[0].meta["spec"].mem_offset |
| 182 | + ) |
| 183 | + self.assertEqual( |
| 184 | + node.meta["spec"].lifetime, node.args[0].meta["spec"].lifetime |
| 185 | + ) |
| 186 | + |
| 187 | + # Test evalues in execution plan |
| 188 | + evalues = etpm.executorch_program.execution_plan[0].values |
| 189 | + |
| 190 | + # evalue 0 is the parameter arg0_1 and evalue 2 is view aten_view_copy_default |
| 191 | + # assert their sizes are as expected and constant_buffer_idx != 0 |
| 192 | + self.assertEqual(evalues[0].val.sizes, [5, 6]) # pyre-ignore |
| 193 | + self.assertNotEqual(evalues[0].val.constant_buffer_idx, 0) # pyre-ignore |
| 194 | + self.assertEqual(evalues[2].val.sizes, [6, 5]) # pyre-ignore |
| 195 | + self.assertNotEqual(evalues[2].val.constant_buffer_idx, 0) # pyre-ignore |
| 196 | + |
| 197 | + # assert they have the same constant_buffer_idx |
| 198 | + self.assertEqual(evalues[0].val.constant_buffer_idx, evalues[2].val.constant_buffer_idx) # pyre-ignore |
| 199 | + |
| 200 | + # evalue 7 is alloc_1 (aten_mul_tensor) and evalue 8 is aten_view_copy_default_2 |
| 201 | + # assert their sizes are as expected and constant_buffer_idx == 0 |
| 202 | + self.assertEqual(evalues[7].val.sizes, [6, 5]) # pyre-ignore |
| 203 | + self.assertEqual(evalues[7].val.constant_buffer_idx, 0) # pyre-ignore |
| 204 | + self.assertEqual(evalues[8].val.sizes, [30]) # pyre-ignore |
| 205 | + self.assertEqual(evalues[8].val.constant_buffer_idx, 0) # pyre-ignore |
| 206 | + |
| 207 | + # assert they have the same mem_id and mem_offset low and high |
| 208 | + self.assertEqual(evalues[7].val.allocation_info.memory_id, evalues[8].val.allocation_info.memory_id) # pyre-ignore |
| 209 | + self.assertEqual(evalues[7].val.allocation_info.memory_offset_low, evalues[8].val.allocation_info.memory_offset_low) # pyre-ignore |
| 210 | + self.assertEqual(evalues[7].val.allocation_info.memory_offset_high, evalues[8].val.allocation_info.memory_offset_high) # pyre-ignore |
0 commit comments