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
+ xnnpack_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,13 @@ def forward(self, *args, **kwargs):
158
164
XnnpackPartitioner ,
159
165
)
160
166
167
+ transform_passes = []
168
+ if external_constants :
169
+ partial_function = partial (xnnpack_external_constants_pass , names = None )
170
+ transform_passes .append (partial_function )
161
171
executorch_program = to_edge_transform_and_lower (
162
172
exported_program ,
173
+ transform_passes = transform_passes ,
163
174
compile_config = edge_config ,
164
175
partitioner = [XnnpackPartitioner ()],
165
176
).to_executorch (config = et_config )
@@ -221,6 +232,11 @@ def main() -> None:
221
232
parser .add_argument (
222
233
"--delegate_alignment" , type = int , default = None , help = "Delegate alignment."
223
234
)
235
+ parser .add_argument (
236
+ "--external_constants" ,
237
+ action = "store_true" ,
238
+ help = "Export the model with all constants saved to an external file." ,
239
+ )
224
240
parser .add_argument (
225
241
"--outdir" ,
226
242
type = str ,
@@ -247,16 +263,26 @@ def main() -> None:
247
263
suffix += "-nosegments"
248
264
if args .delegate_alignment is not None :
249
265
suffix += f"-da{ args .delegate_alignment } "
266
+ if args .external_constants :
267
+ suffix += f"-e"
250
268
outfile = os .path .join (args .outdir , f"{ module_name } { suffix } .pte" )
251
269
executorch_program = export_module_to_program (
252
270
module_class ,
253
271
backend_id = args .backend_id ,
254
272
extract_delegate_segments = not args .inline_delegate_segments ,
255
273
delegate_alignment = args .delegate_alignment ,
274
+ external_constants = args .external_constants ,
256
275
)
257
276
with open (outfile , "wb" ) as fp :
258
277
fp .write (executorch_program .buffer )
259
278
print (f"Exported { module_name } and wrote program data to { outfile } " )
279
+ if args .external_constants :
280
+ # current infra doesnt easily allow renaming this file, so just hackily do it here.
281
+ executorch_program ._tensor_data [f"{ module_name } { suffix } " ] = (
282
+ executorch_program ._tensor_data .pop ("_default_external_constant" )
283
+ )
284
+ print (f"Saving external constants to { module_name } { suffix } .ptd" )
285
+ executorch_program .write_tensor_data_to_file (args .outdir )
260
286
261
287
262
288
if __name__ == "__main__" :
0 commit comments