19
19
from executorch .exir .backend .test .backend_with_compiler_demo import (
20
20
BackendWithCompilerDemo ,
21
21
)
22
+ from executorch .exir .program import ExecutorchProgramManager
22
23
from torch import nn
23
24
from torch .export import export
24
25
@@ -111,10 +112,10 @@ def export_module_to_program(
111
112
* ,
112
113
backend_id : str ,
113
114
extract_delegate_segments : bool ,
114
- constant_tensor_alignemnt : Optional [int ] = None ,
115
+ constant_tensor_alignment : Optional [int ] = None ,
115
116
delegate_alignment : Optional [int ] = None ,
116
117
method : str = "forward" ,
117
- ) -> bytes :
118
+ ) -> ExecutorchProgramManager :
118
119
eager_module = module_class ().eval ()
119
120
inputs = ()
120
121
if hasattr (eager_module , "get_random_inputs" ):
@@ -135,7 +136,7 @@ def forward(self, *args, **kwargs):
135
136
edge_config = EdgeCompileConfig (_check_ir_validity = False )
136
137
et_config = exir .ExecutorchBackendConfig (
137
138
extract_delegate_segments = extract_delegate_segments ,
138
- constant_tensor_alignment = constant_tensor_alignemnt ,
139
+ constant_tensor_alignment = constant_tensor_alignment ,
139
140
delegate_alignment = delegate_alignment ,
140
141
)
141
142
@@ -170,7 +171,7 @@ def forward(self, *args, **kwargs):
170
171
export (composite_module , args = inputs , strict = True )
171
172
).to_executorch (config = et_config )
172
173
173
- return executorch_program . buffer
174
+ return executorch_program
174
175
175
176
176
177
def main () -> None :
@@ -199,6 +200,14 @@ def main() -> None:
199
200
help = "ID of the backend to use for delegation; "
200
201
+ f"one of { known_backend_ids } " ,
201
202
)
203
+ parser .add_argument (
204
+ "--inline_delegate_segments" ,
205
+ action = "store_true" ,
206
+ help = "Store delegate data inside the flatbuffer." ,
207
+ )
208
+ parser .add_argument (
209
+ "--delegate_alignment" , type = int , default = None , help = "Delegate alignment."
210
+ )
202
211
parser .add_argument (
203
212
"--outdir" ,
204
213
type = str ,
@@ -219,25 +228,22 @@ def main() -> None:
219
228
220
229
# Export and write to the output files.
221
230
os .makedirs (args .outdir , exist_ok = True )
231
+ suffix = ""
222
232
for module_name , module_class in module_names_to_classes .items ():
223
- for extract_delegate_segments in (True , False ):
224
- suffix = "" if extract_delegate_segments else "-nosegments"
225
- # Create files with the default alignment, and a large alignment.
226
- # This alignment should be so large that it's extremely unlikely for
227
- # the data to accidentally be aligned to it in the default case.
228
- for delegate_alignment in (None , 1024 ):
229
- suffix += f"-da{ delegate_alignment } " if delegate_alignment else ""
230
- outfile = os .path .join (args .outdir , f"{ module_name } { suffix } .pte" )
231
- with open (outfile , "wb" ) as fp :
232
- fp .write (
233
- export_module_to_program (
234
- module_class ,
235
- backend_id = args .backend_id ,
236
- extract_delegate_segments = extract_delegate_segments ,
237
- delegate_alignment = delegate_alignment ,
238
- )
239
- )
240
- print (f"Exported { module_name } and wrote program data to { outfile } " )
233
+ if args .inline_delegate_segments :
234
+ suffix += f"-nosegments"
235
+ if args .delegate_alignment is not None :
236
+ suffix += f"-da{ args .delegate_alignment } "
237
+ outfile = os .path .join (args .outdir , f"{ module_name } { suffix } .pte" )
238
+ executorch_program = export_module_to_program (
239
+ module_class ,
240
+ backend_id = args .backend_id ,
241
+ extract_delegate_segments = not args .inline_delegate_segments ,
242
+ delegate_alignment = args .delegate_alignment ,
243
+ )
244
+ with open (outfile , "wb" ) as fp :
245
+ fp .write (executorch_program .buffer )
246
+ print (f"Exported { module_name } and wrote program data to { outfile } " )
241
247
242
248
243
249
if __name__ == "__main__" :
0 commit comments