|
15 | 15 | from torch.export import export
|
16 | 16 |
|
17 | 17 |
|
18 |
| -def make_test( # noqa: C901 |
19 |
| - tester: unittest.TestCase, |
20 |
| - runtime: ModuleType, |
21 |
| -) -> Callable[[unittest.TestCase], None]: |
22 |
| - """ |
23 |
| - Returns a function that operates as a test case within a unittest.TestCase class. |
| 18 | +class ModuleAdd(torch.nn.Module): |
| 19 | + """The module to serialize and execute.""" |
24 | 20 |
|
25 |
| - Used to allow the test code for pybindings to be shared across different pybinding libs |
26 |
| - which will all have different load functions. In this case each individual test case is a |
27 |
| - subfunction of wrapper. |
28 |
| - """ |
29 |
| - load_fn: Callable = runtime._load_for_executorch_from_buffer |
| 21 | + def __init__(self): |
| 22 | + super(ModuleAdd, self).__init__() |
30 | 23 |
|
31 |
| - def wrapper(tester: unittest.TestCase) -> None: |
32 |
| - class ModuleAdd(torch.nn.Module): |
33 |
| - """The module to serialize and execute.""" |
| 24 | + def forward(self, x, y): |
| 25 | + return x + y |
34 | 26 |
|
35 |
| - def __init__(self): |
36 |
| - super(ModuleAdd, self).__init__() |
| 27 | + def get_methods_to_export(self): |
| 28 | + return ("forward",) |
37 | 29 |
|
38 |
| - def forward(self, x, y): |
39 |
| - return x + y |
| 30 | + def get_inputs(self): |
| 31 | + return (torch.ones(2, 2), torch.ones(2, 2)) |
40 | 32 |
|
41 |
| - def get_methods_to_export(self): |
42 |
| - return ("forward",) |
43 | 33 |
|
44 |
| - def get_inputs(self): |
45 |
| - return (torch.ones(2, 2), torch.ones(2, 2)) |
| 34 | +class ModuleMulti(torch.nn.Module): |
| 35 | + """The module to serialize and execute.""" |
46 | 36 |
|
47 |
| - class ModuleMulti(torch.nn.Module): |
48 |
| - """The module to serialize and execute.""" |
| 37 | + def __init__(self): |
| 38 | + super(ModuleMulti, self).__init__() |
49 | 39 |
|
50 |
| - def __init__(self): |
51 |
| - super(ModuleMulti, self).__init__() |
| 40 | + def forward(self, x, y): |
| 41 | + return x + y |
52 | 42 |
|
53 |
| - def forward(self, x, y): |
54 |
| - return x + y |
| 43 | + def forward2(self, x, y): |
| 44 | + return x + y + 1 |
55 | 45 |
|
56 |
| - def forward2(self, x, y): |
57 |
| - return x + y + 1 |
| 46 | + def get_methods_to_export(self): |
| 47 | + return ("forward", "forward2") |
58 | 48 |
|
59 |
| - def get_methods_to_export(self): |
60 |
| - return ("forward", "forward2") |
| 49 | + def get_inputs(self): |
| 50 | + return (torch.ones(2, 2), torch.ones(2, 2)) |
61 | 51 |
|
62 |
| - def get_inputs(self): |
63 |
| - return (torch.ones(2, 2), torch.ones(2, 2)) |
64 | 52 |
|
65 |
| - class ModuleAddSingleInput(torch.nn.Module): |
66 |
| - """The module to serialize and execute.""" |
| 53 | +class ModuleAddSingleInput(torch.nn.Module): |
| 54 | + """The module to serialize and execute.""" |
67 | 55 |
|
68 |
| - def __init__(self): |
69 |
| - super(ModuleAddSingleInput, self).__init__() |
| 56 | + def __init__(self): |
| 57 | + super(ModuleAddSingleInput, self).__init__() |
70 | 58 |
|
71 |
| - def forward(self, x): |
72 |
| - return x + x |
| 59 | + def forward(self, x): |
| 60 | + return x + x |
73 | 61 |
|
74 |
| - def get_methods_to_export(self): |
75 |
| - return ("forward",) |
| 62 | + def get_methods_to_export(self): |
| 63 | + return ("forward",) |
76 | 64 |
|
77 |
| - def get_inputs(self): |
78 |
| - return (torch.ones(2, 2),) |
| 65 | + def get_inputs(self): |
| 66 | + return (torch.ones(2, 2),) |
79 | 67 |
|
80 |
| - def create_program( |
81 |
| - eager_module: torch.nn.Module, |
82 |
| - ) -> Tuple[ExecutorchProgramManager, Tuple[Any, ...]]: |
83 |
| - """Returns an executorch program based on ModuleAdd, along with inputs.""" |
84 | 68 |
|
85 |
| - # Trace the test module and create a serialized ExecuTorch program. |
86 |
| - inputs = eager_module.get_inputs() |
87 |
| - input_map = {} |
88 |
| - for method in eager_module.get_methods_to_export(): |
89 |
| - input_map[method] = inputs |
| 69 | +def create_program( |
| 70 | + eager_module: torch.nn.Module, |
| 71 | + et_config: Optional[ExecutorchBackendConfig] = None, |
| 72 | +) -> Tuple[ExecutorchProgramManager, Tuple[Any, ...]]: |
| 73 | + """Returns an executorch program based on ModuleAdd, along with inputs.""" |
90 | 74 |
|
91 |
| - class WrapperModule(torch.nn.Module): |
92 |
| - def __init__(self, fn): |
93 |
| - super().__init__() |
94 |
| - self.fn = fn |
| 75 | + # Trace the test module and create a serialized ExecuTorch program. |
| 76 | + inputs = eager_module.get_inputs() |
| 77 | + input_map = {} |
| 78 | + for method in eager_module.get_methods_to_export(): |
| 79 | + input_map[method] = inputs |
95 | 80 |
|
96 |
| - def forward(self, *args, **kwargs): |
97 |
| - return self.fn(*args, **kwargs) |
| 81 | + class WrapperModule(torch.nn.Module): |
| 82 | + def __init__(self, fn): |
| 83 | + super().__init__() |
| 84 | + self.fn = fn |
98 | 85 |
|
99 |
| - exported_methods = {} |
100 |
| - # These cleanup passes are required to convert the `add` op to its out |
101 |
| - # variant, along with some other transformations. |
102 |
| - for method_name, method_input in input_map.items(): |
103 |
| - wrapped_mod = WrapperModule( # pyre-ignore[16] |
104 |
| - getattr(eager_module, method_name) |
105 |
| - ) |
106 |
| - exported_methods[method_name] = export(wrapped_mod, method_input) |
| 86 | + def forward(self, *args, **kwargs): |
| 87 | + return self.fn(*args, **kwargs) |
| 88 | + |
| 89 | + exported_methods = {} |
| 90 | + # These cleanup passes are required to convert the `add` op to its out |
| 91 | + # variant, along with some other transformations. |
| 92 | + for method_name, method_input in input_map.items(): |
| 93 | + wrapped_mod = WrapperModule(getattr(eager_module, method_name)) |
| 94 | + exported_methods[method_name] = export(wrapped_mod, method_input) |
| 95 | + |
| 96 | + exec_prog = to_edge(exported_methods).to_executorch(config=et_config) |
107 | 97 |
|
108 |
| - exec_prog = to_edge(exported_methods).to_executorch() |
| 98 | + # Create the ExecuTorch program from the graph. |
| 99 | + exec_prog.dump_executorch_program(verbose=True) |
| 100 | + return (exec_prog, inputs) |
109 | 101 |
|
110 |
| - # Create the ExecuTorch program from the graph. |
111 |
| - exec_prog.dump_executorch_program(verbose=True) |
112 |
| - return (exec_prog, inputs) |
| 102 | + |
| 103 | +def make_test( # noqa: C901 |
| 104 | + tester: unittest.TestCase, |
| 105 | + runtime: ModuleType, |
| 106 | +) -> Callable[[unittest.TestCase], None]: |
| 107 | + """ |
| 108 | + Returns a function that operates as a test case within a unittest.TestCase class. |
| 109 | +
|
| 110 | + Used to allow the test code for pybindings to be shared across different pybinding libs |
| 111 | + which will all have different load functions. In this case each individual test case is a |
| 112 | + subfunction of wrapper. |
| 113 | + """ |
| 114 | + load_fn: Callable = runtime._load_for_executorch_from_buffer |
| 115 | + |
| 116 | + def wrapper(tester: unittest.TestCase) -> None: |
113 | 117 |
|
114 | 118 | ######### TEST CASES #########
|
115 | 119 |
|
@@ -255,7 +259,6 @@ def test_quantized_ops(tester):
|
255 | 259 |
|
256 | 260 | def test_verification_config(tester) -> None:
|
257 | 261 | # Create an ExecuTorch program from ModuleAdd.
|
258 |
| - # pyre-fixme[16]: Callable `make_test` has no attribute `wrapper`. |
259 | 262 | exported_program, inputs = create_program(ModuleAdd())
|
260 | 263 | Verification = runtime.Verification
|
261 | 264 |
|
|
0 commit comments