Skip to content

Commit c8f48db

Browse files
lucylqYIWENX14
authored andcommitted
[executorch] Add program-data separation example to test files (#7887)
Pull Request resolved: #7765 Add to test env to use for testing in next diff. ghstack-source-id: 262611426 @exported-using-ghexport Differential Revision: [D67376339](https://our.internmc.facebook.com/intern/diff/D67376339/)
1 parent 25c5a22 commit c8f48db

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

test/end2end/exported_module.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def export(
6969
capture_config=None,
7070
skip_type_promotion: bool = False,
7171
export_joint_graph: bool = False,
72+
external_constants: bool = False,
7273
) -> "ExportedModule":
7374
"""
7475
Creates a new ExportedModule for the specified module class.
@@ -206,6 +207,7 @@ def __init__(self, method):
206207
dynamic_memory_planning_mode=dynamic_memory_planning_mode,
207208
memory_planning_pass=memory_planning_pass,
208209
to_out_var_pass=ToOutVarPass(ignore_to_out_var_failure),
210+
external_constants=external_constants,
209211
)
210212
)
211213

test/models/export_program.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import torch
1616
from executorch.exir import CaptureConfig
1717
from executorch.exir.passes import MemoryPlanningPass
18+
from executorch.exir.program._program import ExecutorchProgramManager
1819
from torch import nn
1920
from torch.export import Dim
2021

@@ -190,7 +191,8 @@ def export_joint():
190191
def export_module_to_program(
191192
module_class: Type[nn.Module],
192193
skip_type_promotion: bool,
193-
):
194+
external_constants: bool = False,
195+
) -> ExecutorchProgramManager:
194196
"""Exports the module and returns the serialized program data."""
195197
torch.manual_seed(0)
196198
# Look for an optional @staticmethod that defines custom trace params.
@@ -211,9 +213,10 @@ def export_module_to_program(
211213
methods,
212214
skip_type_promotion=skip_type_promotion,
213215
export_joint_graph=export_joint,
216+
external_constants=external_constants,
214217
**export_kwargs,
215218
)
216-
return module.executorch_program.buffer
219+
return module.executorch_program
217220

218221

219222
def main() -> None:
@@ -235,7 +238,12 @@ def main() -> None:
235238
"--outdir",
236239
type=str,
237240
required=True,
238-
help="Path to the directory to write <classname>.pte files to",
241+
help="Path to the directory to write <classname>.pte files and .ptd files to",
242+
)
243+
parser.add_argument(
244+
"--external-constants",
245+
action="store_true",
246+
help="Export the model with external constants",
239247
)
240248
args = parser.parse_args()
241249

@@ -257,14 +265,16 @@ def main() -> None:
257265
# Type promotion will convert to fp32.
258266
skip_type_promotion = True
259267
outfile = os.path.join(args.outdir, f"{module_name}.pte")
268+
prog = export_module_to_program(
269+
module_class,
270+
skip_type_promotion=skip_type_promotion,
271+
external_constants=args.external_constants,
272+
)
260273
with open(outfile, "wb") as fp:
261-
fp.write(
262-
export_module_to_program(
263-
module_class,
264-
skip_type_promotion=skip_type_promotion,
265-
)
266-
)
267-
print(f"Exported {module_name} and wrote program data to {outfile}")
274+
prog.write_to_file(fp)
275+
print(f"Exported {module_name} and wrote program data to {outfile}")
276+
277+
prog.write_tensor_data_to_file(args.outdir)
268278

269279

270280
if __name__ == "__main__":

test/models/targets.bzl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,28 @@ def define_common_targets():
7575
name = "exported_programs",
7676
cmd = "$(exe :export_program) --modules " + ",".join(MODULES_TO_EXPORT) + " --outdir $OUT",
7777
outs = {
78-
fname + seg_suffix + ".pte": [fname + seg_suffix + ".pte"]
78+
fname + ".pte": [fname + ".pte"]
7979
for fname in MODULES_TO_EXPORT
80-
for seg_suffix in ["", "-no-constant-segment"]
80+
},
81+
default_outs = ["."],
82+
visibility = [
83+
"//executorch/...",
84+
# This genrule can't run in xplat since it uses EXIR, so make its
85+
# output visible to xplat tests. This is an exceptional case, and
86+
# typically shouldn't be done.
87+
"fbsource//xplat/executorch/...",
88+
],
89+
# Allow the xplat entry in the visibility list. This is an exceptional
90+
# case, and typically shouldn't be done.
91+
_is_external_target = True,
92+
)
93+
94+
runtime.genrule(
95+
name = "exported_programs_with_data_separated",
96+
cmd = "$(exe :export_program) --modules ModuleLinear --external-constants --outdir $OUT",
97+
outs = {
98+
"ModuleLinear.pte": ["ModuleLinear.pte"],
99+
"ModuleLinear.ptd": ["_default_external_constant.ptd"],
81100
},
82101
default_outs = ["."],
83102
visibility = [

0 commit comments

Comments
 (0)