|
7 | 7 | # pyre-unsafe
|
8 | 8 |
|
9 | 9 | import unittest
|
| 10 | +from typing import Dict, Tuple, Union |
10 | 11 |
|
11 | 12 | import torch
|
| 13 | + |
| 14 | +from executorch.devtools.inspector._inspector_utils import ( |
| 15 | + DebugHandle, |
| 16 | + propagate_back_debug_handle, |
| 17 | +) |
12 | 18 | from executorch.devtools.inspector._intermediate_output_capturer import (
|
13 | 19 | IntermediateOutputCapturer,
|
14 | 20 | )
|
15 | 21 | from executorch.devtools.inspector.tests.inspector_test_utils import (
|
16 |
| - check_if_final_outputs_match, |
| 22 | + check_if_intermediate_outputs_match, |
17 | 23 | model_registry,
|
18 | 24 | )
|
| 25 | + |
19 | 26 | from executorch.exir import EdgeCompileConfig, EdgeProgramManager, to_edge
|
20 | 27 | from torch.export import export, ExportedProgram
|
21 |
| -from torch.fx import GraphModule |
22 | 28 |
|
23 | 29 |
|
24 | 30 | class TestIntermediateOutputCapturer(unittest.TestCase):
|
25 |
| - def _set_up_model(self, model_name): |
26 |
| - model = model_registry[model_name]() |
27 |
| - input_tensor = model.get_input() |
28 |
| - aten_model: ExportedProgram = export(model, (input_tensor,), strict=True) |
29 |
| - edge_program_manager: EdgeProgramManager = to_edge( |
30 |
| - aten_model, compile_config=EdgeCompileConfig(_check_ir_validity=True) |
| 31 | + def _capture_intermediate_outputs_and_check( |
| 32 | + self, |
| 33 | + inputs: Tuple[torch.Tensor], |
| 34 | + ep: ExportedProgram, |
| 35 | + expected_intermediate_outputs: Dict[ |
| 36 | + DebugHandle, Union[torch.Tensor, Tuple[torch.Tensor]] |
| 37 | + ], |
| 38 | + ): |
| 39 | + captured_intermediate_outputs = IntermediateOutputCapturer( |
| 40 | + ep.module() |
| 41 | + ).run_and_capture(inputs) |
| 42 | + |
| 43 | + # Test keying with debug handle tuple |
| 44 | + for key in captured_intermediate_outputs.keys(): |
| 45 | + self.assertIsInstance(key, tuple) |
| 46 | + |
| 47 | + # Test tensor cloning and detaching |
| 48 | + for output in captured_intermediate_outputs.values(): |
| 49 | + if isinstance(output, torch.Tensor): |
| 50 | + self.assertFalse(output.requires_grad) |
| 51 | + self.assertTrue(output.is_leaf) |
| 52 | + |
| 53 | + # Test placeholder nodes are skipped |
| 54 | + for node in ep.graph.nodes: |
| 55 | + if node.op == "placeholder": |
| 56 | + self.assertNotIn(node.meta.get("debug_handle"), node.meta) |
| 57 | + |
| 58 | + # Test multiple outputs capture |
| 59 | + for inter_output in captured_intermediate_outputs.values(): |
| 60 | + if isinstance(inter_output, tuple): |
| 61 | + for part in output: |
| 62 | + self.assertIsInstance(part, torch.Tensor) |
| 63 | + |
| 64 | + # Test capture correct outputs |
| 65 | + self.assertTrue( |
| 66 | + check_if_intermediate_outputs_match( |
| 67 | + captured_intermediate_outputs, expected_intermediate_outputs |
| 68 | + ) |
31 | 69 | )
|
32 |
| - graph_module: GraphModule = edge_program_manager._edge_programs[ |
33 |
| - "forward" |
34 |
| - ].module() |
35 |
| - capturer = IntermediateOutputCapturer(graph_module) |
36 |
| - intermediate_outputs = capturer.run_and_capture(input_tensor) |
37 |
| - return input_tensor, graph_module, capturer, intermediate_outputs |
38 | 70 |
|
39 | 71 | def test_models(self):
|
40 | 72 | available_models = list(model_registry.keys())
|
41 | 73 | for model_name in available_models:
|
42 | 74 | with self.subTest(model=model_name):
|
43 |
| - input_tensor, graph_module, capturer, intermediate_outputs = ( |
44 |
| - self._set_up_model(model_name) |
| 75 | + model = model_registry[model_name]() |
| 76 | + input_tensor = model.get_input() |
| 77 | + aten_model: ExportedProgram = export(model, (input_tensor,)) |
| 78 | + aten_model_graph_id = id(aten_model.graph) |
| 79 | + |
| 80 | + edge_program_manager: EdgeProgramManager = to_edge( |
| 81 | + aten_model, |
| 82 | + compile_config=EdgeCompileConfig(_check_ir_validity=True), |
45 | 83 | )
|
46 | 84 |
|
47 |
| - # Test keying with debug handle tuple |
48 |
| - for key in intermediate_outputs.keys(): |
49 |
| - self.assertIsInstance(key, tuple) |
50 |
| - |
51 |
| - # Test tensor cloning and detaching |
52 |
| - for output in intermediate_outputs.values(): |
53 |
| - if isinstance(output, torch.Tensor): |
54 |
| - self.assertFalse(output.requires_grad) |
55 |
| - self.assertTrue(output.is_leaf) |
56 |
| - |
57 |
| - # Test placeholder nodes are skipped |
58 |
| - for node in graph_module.graph.nodes: |
59 |
| - if node.op == "placeholder": |
60 |
| - self.assertNotIn(node.meta.get("debug_handle"), node.meta) |
61 |
| - |
62 |
| - # Test multiple outputs capture |
63 |
| - outputs = capturer.run_and_capture(input_tensor) |
64 |
| - for output in outputs.values(): |
65 |
| - if isinstance(output, tuple): |
66 |
| - self.assertEqual(len(output), 2) |
67 |
| - for part in output: |
68 |
| - self.assertIsInstance(part, torch.Tensor) |
69 |
| - |
70 |
| - # Test capture correct outputs |
71 |
| - self.assertTrue( |
72 |
| - check_if_final_outputs_match(model_name, intermediate_outputs) |
| 85 | + ret = propagate_back_debug_handle( |
| 86 | + aten_model, |
| 87 | + aten_model_graph_id, |
| 88 | + edge_program_manager.exported_program(), |
| 89 | + ) |
| 90 | + assert ret is True |
| 91 | + |
| 92 | + self._capture_intermediate_outputs_and_check( |
| 93 | + input_tensor, |
| 94 | + aten_model, |
| 95 | + model.get_exported_program_expected_intermediate_outputs(), |
| 96 | + ) |
| 97 | + self._capture_intermediate_outputs_and_check( |
| 98 | + input_tensor, |
| 99 | + edge_program_manager.exported_program(), |
| 100 | + model.get_edge_dialect_expected_intermediate_outputs(), |
73 | 101 | )
|
0 commit comments