@@ -23,13 +23,120 @@ It should be provided to the `Inspector API <model-inspector.html>`__ to link ba
2323Generating an ``ETRecord ``
2424--------------------------
2525
26- The user should use the following API to generate an ``ETRecord `` file. They
27- will be expected to provide the Edge Dialect program (returned by the call to ``to_edge() ``),
28- the ExecuTorch program (returned by the call to ``to_executorch() ``), and optional models that
29- they are interested in working with via our tooling.
26+ There are multiple ways to generate an ``ETRecord `` for debugging purposes:
27+
28+ Method 1: Using the ``generate_etrecord `` Parameter (Recommended)
29+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+ The recommended approach is to enable ``ETRecord `` generation by passing ``generate_etrecord=True ``
32+ to your export API calls. This can be used with:
33+
34+ * ``executorch.export() `` - High-level export API
35+ * ``to_edge() `` - Edge dialect conversion
36+ * ``to_edge_transform_and_lower() `` - Edge conversion with transformations and lowering
37+
38+ After export completes, retrieve the ``ETRecord `` using the ``get_etrecord() `` method, and save it using the ``save() `` method:
39+
40+ **Example with ** ``executorch.export() ``:
41+
42+ .. code-block :: python
43+
44+ import executorch
45+ from executorch.export import ExportRecipe
46+
47+ # Export with ETRecord generation enabled
48+ session = executorch.export(
49+ model = model,
50+ example_inputs = [example_inputs],
51+ export_recipe = recipe,
52+ generate_etrecord = True # Enable ETRecord generation
53+ )
54+
55+ # Get and save the ETRecord
56+ etrecord = session.get_etrecord()
57+ etrecord.save(" model_debug.etrecord" )
58+
59+ **Example with ** ``to_edge() ``:
60+
61+ .. code-block :: python
62+
63+ from executorch.exir.program import to_edge
64+ from torch.export import export
65+
66+ # Export model first
67+ exported_program = export(model, example_inputs)
68+
69+ # Convert to edge with ETRecord generation
70+ edge_manager = to_edge(
71+ exported_program,
72+ generate_etrecord = True # Enable ETRecord generation
73+ )
74+
75+ # Apply transformations
76+ edge_manager = edge_manager.to_backend()
77+ et_manager = edge_manager.to_executorch()
78+
79+ # Get and save ETRecord
80+ etrecord = et_manager.get_etrecord()
81+ etrecord.save(" edge_debug.etrecord" )
82+
83+ **Example with ** ``to_edge_transform_and_lower() ``:
84+
85+ .. code-block :: python
86+
87+ from executorch.exir.program import to_edge_transform_and_lower
88+ from torch.export import export
89+
90+ # Export model first
91+ exported_program = export(model, example_inputs)
92+
93+ # Transform and lower with ETRecord generation
94+ edge_manager = to_edge_transform_and_lower(
95+ exported_program,
96+ partitioner = [MyPartitioner()],
97+ generate_etrecord = True # Enable ETRecord generation
98+ )
99+
100+ et_manager = edge_manager.to_executorch()
101+
102+ # Get and save ETRecord
103+ etrecord = et_manager.get_etrecord()
104+ etrecord.save(" debug.etrecord" )
105+
106+ Method 2: Using the ``generate_etrecord() `` Function
107+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
108+
109+ You can also use the standalone ``generate_etrecord() `` function to generate an ``ETRecord ``.
110+ This method requires you to provide the Edge Dialect program (returned by ``to_edge() ``),
111+ the ExecuTorch program (returned by ``to_executorch() ``), and optional models.
30112
31113.. warning ::
32- Users should do a deepcopy of the output of ``to_edge() `` and pass in the deepcopy to the ``generate_etrecord `` API. This is needed because the subsequent call, ``to_executorch() ``, does an in-place mutation and will lose debug data in the process.
114+ When using the standalone function, users should do a deepcopy of the output of ``to_edge() `` and pass in the deepcopy to the ``generate_etrecord `` API. This is needed because the subsequent call, ``to_executorch() ``, does an in-place mutation and will lose debug data in the process.
115+
116+ **Example: **
117+
118+ .. code-block :: python
119+
120+ import copy
121+ from executorch.devtools import generate_etrecord
122+ from torch.export import export
123+
124+ # Export and convert to edge
125+ aten_dialect = export(model, example_inputs, strict = True )
126+ edge_program = to_edge(aten_dialect)
127+
128+ # Create copy for ETRecord (needed because to_executorch modifies in-place)
129+ edge_program_copy = copy.deepcopy(edge_program)
130+
131+ # Convert to ExecutorchProgramManager
132+ executorch_program = edge_program_copy.to_executorch()
133+
134+ # Generate ETRecord separately
135+ generate_etrecord(
136+ " debug.etrecord" ,
137+ edge_program,
138+ executorch_program,
139+ )
33140
34141 .. currentmodule :: executorch.devtools.etrecord._etrecord
35142.. autofunction :: generate_etrecord
0 commit comments