18
18
from executorch .backends .nxp .backend .ir .edge_passes .remove_io_quant_ops_pass import (
19
19
RemoveIOQuantOpsPass ,
20
20
)
21
+ from executorch .backends .nxp .edge_passes .neutron_edge_pass_manager import (
22
+ NeutronEdgePassManager ,
23
+ )
21
24
from executorch .backends .nxp .neutron_partitioner import NeutronPartitioner
22
25
from executorch .backends .nxp .nxp_backend import generate_neutron_compile_spec
23
26
from executorch .backends .nxp .quantizer .neutron_quantizer import NeutronQuantizer
24
27
from executorch .examples .models import MODEL_NAME_TO_MODEL
25
28
from executorch .examples .models .model_factory import EagerModelFactory
26
- from executorch .exir import (
27
- EdgeCompileConfig ,
28
- ExecutorchBackendConfig ,
29
- to_edge_transform_and_lower ,
30
- )
29
+ from executorch .exir import EdgeCompileConfig , ExecutorchBackendConfig
31
30
from executorch .extension .export_util import save_pte_program
32
- from torch . export import export
31
+ from executorch . extension . export_util . utils import export_to_edge
33
32
from torchao .quantization .pt2e .quantize_pt2e import convert_pt2e , prepare_pt2e
34
33
35
34
from .experimental .cifar_net .cifar_net import CifarNet , test_cifarnet_model
36
-
37
35
from .models .mobilenet_v2 import MobilenetV2
38
36
39
37
FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
@@ -228,7 +226,7 @@ def _get_batch_size(data):
228
226
229
227
module = exported_program .module ()
230
228
231
- # 4 . Quantize if required
229
+ # 3 . Quantize if required
232
230
if args .quantize :
233
231
if calibration_inputs is None :
234
232
logging .warning (
@@ -254,39 +252,42 @@ def _get_batch_size(data):
254
252
quantized_str = "quantized " if args .quantize else ""
255
253
print (f"\n Accuracy of the { quantized_str } `{ args .model_name } `: { accuracy } \n " )
256
254
257
- # 5. Export to edge program
258
- partitioner_list = []
259
- if args .delegate is True :
260
- partitioner_list = [
255
+ # 4. Export to edge program
256
+ edge_compile_config = EdgeCompileConfig ()
257
+ edge_program_manager = export_to_edge (
258
+ module ,
259
+ example_inputs ,
260
+ edge_compile_config = edge_compile_config ,
261
+ )
262
+
263
+ logging .debug (f"Exported graph:\n { edge_program_manager .exported_program ().graph } " )
264
+
265
+ edge_program_manager = NeutronEdgePassManager ()(edge_program_manager )
266
+
267
+ if args .remove_quant_io_ops :
268
+ edge_program_manager = edge_program_manager .transform (
269
+ [RemoveIOQuantOpsPass (edge_program_manager = edge_program_manager )]
270
+ )
271
+
272
+ # 5. Delegate to Neutron
273
+ if args .delegate :
274
+ logging .info ("Executing Neutron Partitioner and Delegate" )
275
+ edge_program_manager = edge_program_manager .to_backend (
261
276
NeutronPartitioner (
262
277
generate_neutron_compile_spec (
263
278
args .target ,
264
279
args .neutron_converter_flavor ,
265
280
operators_not_to_delegate = args .operators_not_to_delegate ,
266
281
)
267
282
)
268
- ]
269
-
270
- edge_program = to_edge_transform_and_lower (
271
- export (module , example_inputs , strict = True ),
272
- partitioner = partitioner_list ,
273
- compile_config = EdgeCompileConfig (
274
- _check_ir_validity = False ,
275
- ),
276
- )
277
- logging .debug (f"Exported graph:\n { edge_program .exported_program ().graph } " )
278
-
279
- if args .remove_quant_io_ops :
280
- edge_program = edge_program .transform (
281
- [RemoveIOQuantOpsPass (edge_program_manager = edge_program )]
282
283
)
283
284
logging .debug (
284
- f"Exported graph (RemoveIOQuantOpsPass) :\n { edge_program .exported_program ().graph } "
285
+ f"Lowered graph:\n { edge_program_manager .exported_program ().graph } "
285
286
)
286
287
287
288
# 6. Export to ExecuTorch program
288
289
try :
289
- exec_prog = edge_program .to_executorch (
290
+ exec_prog = edge_program_manager .to_executorch (
290
291
config = ExecutorchBackendConfig (extract_delegate_segments = False )
291
292
)
292
293
except RuntimeError as e :
0 commit comments