|
9 | 9 | import ctypes |
10 | 10 | import random |
11 | 11 | import unittest |
12 | | -from typing import List |
| 12 | +from types import SimpleNamespace |
| 13 | +from typing import List, Tuple |
13 | 14 |
|
| 15 | +import executorch.backends.vulkan.custom_ops_lib # noqa: F401 |
14 | 16 | import torch |
| 17 | +from executorch.backends.vulkan.serialization import ( |
| 18 | + vulkan_graph_builder as graph_builder_module, |
| 19 | +) |
15 | 20 |
|
16 | 21 | from executorch.backends.vulkan.serialization.vulkan_graph_schema import ( |
17 | 22 | IntList, |
|
30 | 35 |
|
31 | 36 |
|
32 | 37 | class TestSerialization(unittest.TestCase): |
| 38 | + def _build_mutation_program( |
| 39 | + self, prepack: bool, shared_user_output: bool = False |
| 40 | + ) -> Tuple[SimpleNamespace, torch.fx.Node, torch.fx.Node, torch.fx.Node]: |
| 41 | + graph = torch.fx.Graph() |
| 42 | + state = graph.placeholder("state") |
| 43 | + user_input = graph.placeholder("user_input") |
| 44 | + state.meta["spec"] = graph_builder_module.TensorSpec.from_tensor(torch.zeros(4)) |
| 45 | + user_input.meta["spec"] = graph_builder_module.TensorSpec.from_tensor( |
| 46 | + torch.ones(4) |
| 47 | + ) |
| 48 | + |
| 49 | + state_value = state |
| 50 | + if prepack: |
| 51 | + state_value = graph.call_function( |
| 52 | + graph_builder_module.exir_ops.edge.et_vk.prepack.default, |
| 53 | + (state,), |
| 54 | + ) |
| 55 | + state_value.meta["spec"] = graph_builder_module.TensorSpec.from_tensor( |
| 56 | + torch.zeros(4) |
| 57 | + ) |
| 58 | + |
| 59 | + mutation = graph.call_function( |
| 60 | + torch.ops.aten.add.Tensor, (state_value, user_input) |
| 61 | + ) |
| 62 | + mutation.meta["spec"] = graph_builder_module.TensorSpec.from_tensor( |
| 63 | + torch.ones(4) |
| 64 | + ) |
| 65 | + user_output = mutation |
| 66 | + if not shared_user_output: |
| 67 | + user_output = graph.call_function( |
| 68 | + torch.ops.aten.mul.Tensor, (user_input, 2.0) |
| 69 | + ) |
| 70 | + user_output.meta["spec"] = graph_builder_module.TensorSpec.from_tensor( |
| 71 | + torch.ones(4) |
| 72 | + ) |
| 73 | + graph.output((mutation, user_output)) |
| 74 | + |
| 75 | + graph_module = torch.fx.GraphModule({}, graph) |
| 76 | + signature = SimpleNamespace( |
| 77 | + buffers_to_mutate={mutation.name: "state"}, |
| 78 | + inputs_to_buffers={state.name: "state"}, |
| 79 | + inputs_to_lifted_tensor_constants={}, |
| 80 | + inputs_to_parameters={}, |
| 81 | + non_persistent_buffers=set(), |
| 82 | + user_outputs=(user_output.name,), |
| 83 | + ) |
| 84 | + program = SimpleNamespace( |
| 85 | + constants={}, |
| 86 | + graph_module=graph_module, |
| 87 | + graph_signature=signature, |
| 88 | + state_dict={"state": torch.zeros(4)}, |
| 89 | + ) |
| 90 | + return program, state_value, mutation, user_output |
| 91 | + |
| 92 | + def test_alias_buffer_mutations_is_opt_in(self) -> None: |
| 93 | + for prepack in (False, True): |
| 94 | + with self.subTest(prepack=prepack): |
| 95 | + program, state_value, mutation, user_output = ( |
| 96 | + self._build_mutation_program(prepack) |
| 97 | + ) |
| 98 | + |
| 99 | + default_builder = graph_builder_module.VkGraphBuilder( |
| 100 | + program, |
| 101 | + graph_builder_module.DelegateMappingBuilder( |
| 102 | + generated_identifiers=True |
| 103 | + ), |
| 104 | + ) |
| 105 | + default_graph = default_builder.build_graph() |
| 106 | + self.assertNotEqual( |
| 107 | + default_builder.node_to_value_ids[mutation], |
| 108 | + default_builder.node_to_value_ids[state_value], |
| 109 | + ) |
| 110 | + self.assertEqual( |
| 111 | + default_graph.output_ids, |
| 112 | + [ |
| 113 | + default_builder.node_to_value_ids[mutation], |
| 114 | + default_builder.node_to_value_ids[user_output], |
| 115 | + ], |
| 116 | + ) |
| 117 | + |
| 118 | + explicit_false_builder = graph_builder_module.VkGraphBuilder( |
| 119 | + program, |
| 120 | + graph_builder_module.DelegateMappingBuilder( |
| 121 | + generated_identifiers=True |
| 122 | + ), |
| 123 | + alias_buffer_mutations=False, |
| 124 | + ) |
| 125 | + self.assertEqual(default_graph, explicit_false_builder.build_graph()) |
| 126 | + |
| 127 | + aliasing_builder = graph_builder_module.VkGraphBuilder( |
| 128 | + program, |
| 129 | + graph_builder_module.DelegateMappingBuilder( |
| 130 | + generated_identifiers=True |
| 131 | + ), |
| 132 | + alias_buffer_mutations=True, |
| 133 | + ) |
| 134 | + aliasing_graph = aliasing_builder.build_graph() |
| 135 | + self.assertEqual( |
| 136 | + aliasing_builder.node_to_value_ids[mutation], |
| 137 | + aliasing_builder.node_to_value_ids[state_value], |
| 138 | + ) |
| 139 | + self.assertEqual( |
| 140 | + aliasing_graph.output_ids, |
| 141 | + [aliasing_builder.node_to_value_ids[user_output]], |
| 142 | + ) |
| 143 | + |
| 144 | + def test_alias_buffer_mutations_preserves_shared_user_output(self) -> None: |
| 145 | + for prepack in (False, True): |
| 146 | + with self.subTest(prepack=prepack): |
| 147 | + program, state_value, mutation, _ = self._build_mutation_program( |
| 148 | + prepack, shared_user_output=True |
| 149 | + ) |
| 150 | + builder = graph_builder_module.VkGraphBuilder( |
| 151 | + program, |
| 152 | + graph_builder_module.DelegateMappingBuilder( |
| 153 | + generated_identifiers=True |
| 154 | + ), |
| 155 | + alias_buffer_mutations=True, |
| 156 | + ) |
| 157 | + |
| 158 | + graph = builder.build_graph() |
| 159 | + |
| 160 | + self.assertEqual( |
| 161 | + builder.node_to_value_ids[mutation], |
| 162 | + builder.node_to_value_ids[state_value], |
| 163 | + ) |
| 164 | + self.assertEqual( |
| 165 | + graph.output_ids, |
| 166 | + [builder.node_to_value_ids[mutation]], |
| 167 | + ) |
| 168 | + |
33 | 169 | def _generate_random_const_tensors(self, num_tensors: int) -> List[torch.Tensor]: |
34 | 170 | """ |
35 | 171 | Helper function to generate `num_tensor` buffers of random sizes and random contents, |
|
0 commit comments