1515import executorch .kernels .quantized # noqa F401
1616
1717import torch
18- from executorch .backends .nxp .edge_passes .remove_io_quant_ops_pass import (
19- RemoveIOQuantOpsPass ,
18+ from executorch .backends .nxp .edge_passes .neutron_edge_pass_manager import (
19+ NeutronEdgePassManager ,
2020)
2121from executorch .backends .nxp .neutron_partitioner import NeutronPartitioner
2222from executorch .backends .nxp .nxp_backend import generate_neutron_compile_spec
3333from torchao .quantization .pt2e .quantize_pt2e import convert_pt2e , prepare_pt2e
3434
3535from .experimental .cifar_net .cifar_net import CifarNet , test_cifarnet_model
36-
3736from .models .mobilenet_v2 import MobilenetV2
3837
3938FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
@@ -228,7 +227,7 @@ def _get_batch_size(data):
228227
229228 module = exported_program .module ()
230229
231- # 4 . Quantize if required
230+ # 3 . Quantize if required
232231 if args .quantize :
233232 if calibration_inputs is None :
234233 logging .warning (
@@ -254,39 +253,30 @@ def _get_batch_size(data):
254253 quantized_str = "quantized " if args .quantize else ""
255254 print (f"\n Accuracy of the { quantized_str } `{ args .model_name } `: { accuracy } \n " )
256255
257- # 5. Export to edge program
258- partitioner_list = []
259- if args .delegate is True :
260- partitioner_list = [
261- NeutronPartitioner (
262- generate_neutron_compile_spec (
263- args .target ,
264- args .neutron_converter_flavor ,
265- operators_not_to_delegate = args .operators_not_to_delegate ,
266- )
267- )
268- ]
256+ # 4. Transform and lower
257+
258+ compile_spec = generate_neutron_compile_spec (
259+ args .target ,
260+ operators_not_to_delegate = args .operators_not_to_delegate ,
261+ neutron_converter_flavor = args .neutron_converter_flavor ,
262+ )
263+ partitioners = [NeutronPartitioner (compile_spec )] if args .delegate else []
269264
270- edge_program = to_edge_transform_and_lower (
265+ edge_program_manager = to_edge_transform_and_lower (
271266 export (module , example_inputs , strict = True ),
272- partitioner = partitioner_list ,
273- compile_config = EdgeCompileConfig (
274- _check_ir_validity = False ,
275- ),
267+ partitioner = partitioners ,
268+ compile_config = EdgeCompileConfig (),
276269 )
277- logging .debug (f"Exported graph:\n { edge_program .exported_program ().graph } " )
278270
279- if args .remove_quant_io_ops :
280- edge_program = edge_program .transform (
281- [RemoveIOQuantOpsPass (edge_program_manager = edge_program )]
282- )
283- logging .debug (
284- f"Exported graph (RemoveIOQuantOpsPass):\n { edge_program .exported_program ().graph } "
285- )
271+ edge_program_manager = NeutronEdgePassManager (
272+ remove_io_quant_ops = args .remove_quant_io_ops
273+ )(edge_program_manager )
274+
275+ logging .debug (f"Lowered graph:\n { edge_program_manager .exported_program ().graph } " )
286276
287- # 6 . Export to ExecuTorch program
277+ # 5 . Export to ExecuTorch program
288278 try :
289- exec_prog = edge_program .to_executorch (
279+ exec_prog = edge_program_manager .to_executorch (
290280 config = ExecutorchBackendConfig (extract_delegate_segments = False )
291281 )
292282 except RuntimeError as e :
@@ -306,7 +296,7 @@ def executorch_program_to_str(ep, verbose=False):
306296
307297 logging .debug (f"Executorch program:\n { executorch_program_to_str (exec_prog )} " )
308298
309- # 7 . Serialize to *.pte
299+ # 6 . Serialize to *.pte
310300 model_name = f"{ args .model_name } " + (
311301 "_nxp_delegate" if args .delegate is True else ""
312302 )
0 commit comments