55# This source code is licensed under the BSD-style license found in the
66# LICENSE file in the root directory of this source tree.
77
8- import copy
9-
108import pytest
119import torch
12- from executorch .backends .cortex_m .passes .cortex_m_pass_manager import (
13- CortexMPassManager ,
14- LiftConstantTensorsPass ,
15- )
1610from executorch .backends .cortex_m .quantizer .quantizer import CortexMQuantizer
1711from executorch .backends .cortex_m .target_config import CortexM , CortexMTargetConfig
1812from executorch .backends .cortex_m .test .tester import (
2115 CortexMTester ,
2216)
2317from executorch .backends .test .harness .stages import Quantize , StageType
24- from executorch .backends .transforms .remove_unused_constants_pass import (
25- RemoveUnusedConstantsPass ,
26- )
27- from executorch .exir import to_edge
2818from executorch .exir .dialects ._ops import ops as exir_ops
2919from torch .fx import Node
3020
@@ -166,11 +156,6 @@ def test_layout_pipelines_select_distinct_spatial_operators():
166156 )
167157 explicit_program = explicit .get_artifact (StageType .RUN_PASSES ).exported_program ()
168158
169- for program in (legacy_program , explicit_program ):
170- assert all (
171- node .users for node in program .graph .nodes if node .op == "placeholder"
172- )
173-
174159 assert _count (legacy_program , exir_ops .edge .cortex_m .quantized_conv2d .default ) == 1
175160 assert (
176161 _count (
@@ -192,104 +177,6 @@ def test_layout_pipelines_select_distinct_spatial_operators():
192177 assert _count (explicit_program , exir_ops .edge .cortex_m .transpose .default ) == 2
193178
194179
195- @pytest .mark .parametrize ("entry_point" , ["legacy" , "edge_passes" , "edge_manager" ])
196- @pytest .mark .parametrize (
197- "passes,lifted,pruned" ,
198- [
199- pytest .param ([], False , False , id = "empty" ),
200- pytest .param ([LiftConstantTensorsPass ], True , False , id = "lift" ),
201- pytest .param (
202- [LiftConstantTensorsPass , RemoveUnusedConstantsPass ],
203- True ,
204- True ,
205- id = "lift_and_prune" ,
206- ),
207- ],
208- )
209- def test_constant_cleanup_respects_pass_list_and_lift_order (
210- passes , lifted , pruned , entry_point
211- ):
212- class Model (torch .nn .Module ):
213- def __init__ (self ):
214- super ().__init__ ()
215- self .register_buffer ("_lifted_tensor_constant0" , torch .tensor ([1.0 ]))
216- self .register_buffer ("_lifted_tensor_constant1" , torch .tensor ([2.0 ]))
217-
218- def forward (self , input ):
219- return input + self ._lifted_tensor_constant1
220-
221- class OtherEntryPoint (torch .nn .Module ):
222- def __init__ (self ):
223- super ().__init__ ()
224- self .register_buffer ("_lifted_tensor_constant2" , torch .tensor ([7.0 ]))
225-
226- def forward (self , x ):
227- return x + self ._lifted_tensor_constant2
228-
229- inputs = (torch .zeros (1 ),)
230- edge = to_edge (torch .export .export (Model (), inputs ))
231- program = edge .exported_program ()
232- assert "_lifted_tensor_constant0" in program .graph_signature .buffers
233- program .graph_module .register_buffer ("new_tensor" , torch .tensor ([3.0 ]))
234- output = next (node for node in program .graph .nodes if node .op == "output" )
235- original = output .args [0 ][0 ]
236- with program .graph .inserting_before (original ):
237- constant = program .graph .get_attr ("new_tensor" )
238- constant .meta = original .meta .copy ()
239- result = program .graph .call_function (
240- exir_ops .edge .aten .add .Tensor , (original .args [1 ], constant )
241- )
242- result .meta = original .meta .copy ()
243- original .replace_input_with (original .args [1 ], result )
244- program .graph_module .recompile ()
245- program .validate ()
246-
247- other = to_edge (torch .export .export (OtherEntryPoint (), inputs )).exported_program ()
248- program .state_dict .update (other .state_dict )
249- other ._state_dict = program .state_dict
250- other .validate ()
251- original_code = program .graph_module .code
252- original_signature = copy .deepcopy (program .graph_signature )
253- original_state = program .state_dict .copy ()
254- program .graph_module .meta ["constant_lifting_test" ] = "preserved"
255-
256- if entry_point == "edge_passes" :
257- transformed = edge .transform (
258- [pass_cls () for pass_cls in passes ]
259- ).exported_program ()
260- elif entry_point == "edge_manager" :
261- transformed = edge .transform (
262- CortexMPassManager (passes = passes )
263- ).exported_program ()
264- else :
265- transformed = CortexMPassManager (program , passes = passes ).transform ()
266-
267- program .validate ()
268- other .validate ()
269- torch .testing .assert_close (program .module ()(* inputs ), torch .tensor ([5.0 ]))
270- torch .testing .assert_close (other .module ()(* inputs ), torch .tensor ([7.0 ]))
271- assert program .graph_module .code == original_code
272- assert program .graph_signature == original_signature
273- assert program .state_dict .keys () == original_state .keys ()
274- for name , tensor in original_state .items ():
275- assert program .state_dict [name ] is tensor
276- assert other .state_dict [name ] is tensor
277- transformed .validate ()
278- assert transformed .graph_signature .user_inputs == original_signature .user_inputs
279- assert transformed .graph_module .meta ["constant_lifting_test" ] == "preserved"
280- assert ("_lifted_tensor_constant0" in transformed .graph_signature .buffers ) == (
281- not pruned
282- )
283- assert sum (node .op == "get_attr" for node in transformed .graph .nodes ) == (
284- not lifted
285- )
286- assert (
287- transformed .state_dict ["_lifted_tensor_constant1" ]
288- is original_state ["_lifted_tensor_constant1" ]
289- )
290- torch .testing .assert_close (transformed .module ()(* inputs ), torch .tensor ([5.0 ]))
291-
292-
293180def test_conv1d_is_quantized_before_layout_conversion ():
294181 tester = CortexMTester (Conv1d ().eval (), (torch .randn (1 , 2 , 8 ),))
295182 tester .quantize (Quantize (CortexMQuantizer (use_explicit_layout = True )))
0 commit comments