10
10
import inspect
11
11
import os
12
12
import sys
13
+
14
+ from functools import partial
13
15
from typing import Dict , final , Optional , Sequence , Type
14
16
15
17
import executorch .exir as exir
21
23
from executorch .exir .backend .test .backend_with_compiler_demo import (
22
24
BackendWithCompilerDemo ,
23
25
)
26
+ from executorch .exir .passes .external_constants_pass import (
27
+ delegate_external_constants_pass ,
28
+ )
24
29
from executorch .exir .program import ExecutorchProgramManager
25
30
from torch import nn
26
31
from torch .export import export
@@ -129,6 +134,7 @@ def export_module_to_program(
129
134
constant_tensor_alignment : Optional [int ] = None ,
130
135
delegate_alignment : Optional [int ] = None ,
131
136
method_name : str = "forward" ,
137
+ external_constants : bool = False ,
132
138
) -> ExecutorchProgramManager :
133
139
eager_module = module_class ().eval ()
134
140
inputs = ()
@@ -158,8 +164,17 @@ def forward(self, *args, **kwargs):
158
164
XnnpackPartitioner ,
159
165
)
160
166
167
+ transform_passes = []
168
+ if external_constants :
169
+ partial_function = partial (
170
+ delegate_external_constants_pass ,
171
+ ep = exported_program ,
172
+ gen_tag_fn = lambda x : module_class .__name__ ,
173
+ )
174
+ transform_passes .append (partial_function )
161
175
executorch_program = to_edge_transform_and_lower (
162
176
exported_program ,
177
+ transform_passes = transform_passes ,
163
178
compile_config = edge_config ,
164
179
partitioner = [XnnpackPartitioner ()],
165
180
).to_executorch (config = et_config )
@@ -221,6 +236,11 @@ def main() -> None:
221
236
parser .add_argument (
222
237
"--delegate_alignment" , type = int , default = None , help = "Delegate alignment."
223
238
)
239
+ parser .add_argument (
240
+ "--external_constants" ,
241
+ action = "store_true" ,
242
+ help = "Export the model with all constants saved to an external file." ,
243
+ )
224
244
parser .add_argument (
225
245
"--outdir" ,
226
246
type = str ,
@@ -247,16 +267,22 @@ def main() -> None:
247
267
suffix += "-nosegments"
248
268
if args .delegate_alignment is not None :
249
269
suffix += f"-da{ args .delegate_alignment } "
270
+ if args .external_constants :
271
+ suffix += "-e"
250
272
outfile = os .path .join (args .outdir , f"{ module_name } { suffix } .pte" )
251
273
executorch_program = export_module_to_program (
252
274
module_class ,
253
275
backend_id = args .backend_id ,
254
276
extract_delegate_segments = not args .inline_delegate_segments ,
255
277
delegate_alignment = args .delegate_alignment ,
278
+ external_constants = args .external_constants ,
256
279
)
257
280
with open (outfile , "wb" ) as fp :
258
281
fp .write (executorch_program .buffer )
259
282
print (f"Exported { module_name } and wrote program data to { outfile } " )
283
+ if args .external_constants :
284
+ print (f"Saving external constants to { module_name } .ptd" )
285
+ executorch_program .write_tensor_data_to_file (args .outdir )
260
286
261
287
262
288
if __name__ == "__main__" :
0 commit comments