|
| 1 | +import unittest |
| 2 | +from typing import Sequence |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 7 | +from executorch.exir import to_edge_transform_and_lower |
| 8 | +from executorch.exir.passes import remove_unused_parameters_pass |
| 9 | +from executorch.runtime import Runtime |
| 10 | +from torch.export import ExportedProgram |
| 11 | + |
| 12 | + |
| 13 | +class TestRemoveUnusedParametersPass(unittest.TestCase): |
| 14 | + class ModelWithUnusedParameters(torch.nn.Module): |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + self.linear1 = torch.nn.Linear(16, 16) |
| 18 | + self.unused_linear = torch.nn.Linear(1024, 1024) |
| 19 | + |
| 20 | + def forward(self, x): |
| 21 | + return self.linear1(x) |
| 22 | + |
| 23 | + def _test_pass( |
| 24 | + self, |
| 25 | + ep: ExportedProgram, |
| 26 | + unused_param_names_and_args: dict[str, str], |
| 27 | + example_inputs: Sequence[torch.Tensor], |
| 28 | + expected_outputs: torch.Tensor, |
| 29 | + ): |
| 30 | + # Verify EP state before running the pass. |
| 31 | + placeholders = set( |
| 32 | + n.target for n in ep.graph_module.graph.nodes if n.op == "placeholder" |
| 33 | + ) |
| 34 | + for param_name, param_arg in unused_param_names_and_args.items(): |
| 35 | + self.assertIn(param_name, ep.state_dict.keys()) |
| 36 | + self.assertIn(param_name, ep.graph_signature.parameters) |
| 37 | + self.assertIn(param_arg, placeholders) |
| 38 | + |
| 39 | + new_ep = remove_unused_parameters_pass(ep) |
| 40 | + |
| 41 | + # Verify that the unused params are not in the state dict, |
| 42 | + # graph signature, or graph. |
| 43 | + new_placeholders = set( |
| 44 | + n.target for n in new_ep.graph_module.graph.nodes if n.op == "placeholder" |
| 45 | + ) |
| 46 | + for param_name, param_arg in unused_param_names_and_args.items(): |
| 47 | + self.assertNotIn(param_name, new_ep.state_dict.keys()) |
| 48 | + self.assertNotIn(param_name, new_ep.graph_signature.parameters) |
| 49 | + self.assertNotIn(param_arg, new_placeholders) |
| 50 | + |
| 51 | + # Verify that the outputs are unchanged. |
| 52 | + new_outputs = new_ep.module()(*example_inputs) |
| 53 | + self.assertTrue(torch.allclose(new_outputs, expected_outputs)) |
| 54 | + |
| 55 | + def test_remove_unused_parameters_simple(self): |
| 56 | + model = self.ModelWithUnusedParameters() |
| 57 | + model.eval() |
| 58 | + example_inputs = (torch.randn(1, 16),) |
| 59 | + eager_outputs = model(*example_inputs) |
| 60 | + ep = torch.export.export(model, example_inputs, strict=False) |
| 61 | + |
| 62 | + unused_param_names_and_args = { |
| 63 | + "unused_linear.weight": "p_unused_linear_weight", |
| 64 | + "unused_linear.bias": "p_unused_linear_bias", |
| 65 | + } |
| 66 | + |
| 67 | + self._test_pass(ep, unused_param_names_and_args, example_inputs, eager_outputs) |
| 68 | + |
| 69 | + def test_remove_unused_parameters_simple_edge_dialect(self): |
| 70 | + model = self.ModelWithUnusedParameters() |
| 71 | + model.eval() |
| 72 | + example_inputs = (torch.randn(1, 16),) |
| 73 | + eager_outputs = model(*example_inputs) |
| 74 | + |
| 75 | + unused_param_names_and_args = { |
| 76 | + "unused_linear.weight": "p_unused_linear_weight", |
| 77 | + "unused_linear.bias": "p_unused_linear_bias", |
| 78 | + } |
| 79 | + |
| 80 | + for delegated in [False, True]: |
| 81 | + lowered = to_edge_transform_and_lower( |
| 82 | + torch.export.export(model, example_inputs, strict=False), |
| 83 | + partitioner=[XnnpackPartitioner()] if delegated else [], |
| 84 | + ) |
| 85 | + |
| 86 | + self._test_pass( |
| 87 | + lowered.exported_program(), |
| 88 | + unused_param_names_and_args, |
| 89 | + example_inputs, |
| 90 | + eager_outputs, |
| 91 | + ) |
| 92 | + |
| 93 | + def test_remove_unused_parameters_serialized_e2e(self): |
| 94 | + model = self.ModelWithUnusedParameters() |
| 95 | + model.eval() |
| 96 | + example_inputs = (torch.randn(1, 16),) |
| 97 | + eager_outputs = model(*example_inputs) |
| 98 | + |
| 99 | + # Pass is expected to run as part of to_executorch(). |
| 100 | + lowered = to_edge_transform_and_lower( |
| 101 | + torch.export.export(model, example_inputs, strict=False), |
| 102 | + ).to_executorch() |
| 103 | + |
| 104 | + # There are approximately 1M unused fp32 parameters - ~4Mb. |
| 105 | + # Without the unused params, the expected size is ~2.5Kb. |
| 106 | + self.assertLess(len(lowered.buffer), 10000) |
| 107 | + |
| 108 | + # Make sure we can load and run the serialized .pte. |
| 109 | + runtime = Runtime.get() |
| 110 | + program = runtime.load_program(lowered.buffer) |
| 111 | + method = program.load_method("forward") |
| 112 | + runtime_outputs = method.execute([*example_inputs]) |
| 113 | + |
| 114 | + self.assertEqual(1, len(runtime_outputs)) |
| 115 | + self.assertTrue(torch.allclose(runtime_outputs[0], eager_outputs)) |
0 commit comments