77# pyre-strict
88
99import logging
10+ from pathlib import Path
1011from typing import Optional
1112
1213import torch
2930 DecomposeScaledDotProductAttention ,
3031)
3132from executorch .backends .transforms .remove_clone_ops import RemoveCloneOpsTransform
32- from executorch .exir import EdgeCompileConfig , EdgeProgramManager , to_edge
33+ from executorch .devtools import generate_etrecord
34+ from executorch .exir import (
35+ EdgeCompileConfig ,
36+ EdgeProgramManager ,
37+ ExecutorchProgramManager ,
38+ to_edge ,
39+ )
3340from torch .ao .quantization .pt2e .export_utils import model_is_exported
3441from torch .ao .quantization .quantize_pt2e import convert_pt2e , prepare_pt2e
3542
@@ -197,11 +204,12 @@ def export_to_edge(
197204# Export the model and lower it to an EdgeProgramManager (in edge IR), and
198205# apply passes specific to Cadence DSP execution. Return both to print the
199206# differences.
200- def export_to_cadence (
207+ def export_to_cadence_edge_executorch (
201208 model : torch .nn .Module ,
202209 inputs : tuple [object , ...],
203210 dump_graphs : bool = False ,
204- ) -> EdgeProgramManager :
211+ output_dir : Optional [str ] = None ,
212+ ) -> ExecutorchProgramManager :
205213 edge_prog_manager = export_to_edge (model , inputs )
206214
207215 # Run a couple required passes for quant/dequant ops
@@ -225,4 +233,29 @@ def export_to_cadence(
225233 cadence_prog_manager .exported_program ().graph_module ,
226234 )
227235
228- return cadence_prog_manager
236+ # Get executorch program after Cadence specific passes
237+ exec_prog : ExecutorchProgramManager = cadence_prog_manager .to_executorch ()
238+ if output_dir :
239+ _gen_etrecord (edge_prog_manager , exec_prog , Path (output_dir ))
240+ else :
241+ logging .warning ("No output directory provided, skipping ETRecord generation" )
242+
243+ return exec_prog
244+
245+
246+ def _gen_etrecord (
247+ edge_program : EdgeProgramManager ,
248+ et_program : ExecutorchProgramManager ,
249+ output_dir : Path ,
250+ ) -> None :
251+ etrec_path = output_dir / "etrecord.bin"
252+ try :
253+ generate_etrecord (
254+ et_record = etrec_path ,
255+ edge_dialect_program = edge_program ,
256+ executorch_program = et_program ,
257+ )
258+ logging .info (f"Generated ETRecord at { etrec_path } " )
259+ except Exception :
260+ # Any errors here shouldn't block the rest of the flow
261+ logging .exception ("Encountered exception while generating ETRecord" )
0 commit comments