Skip to content

Commit a81c2d4

Browse files
lucylqfacebook-github-bot
authored andcommitted
Add constant segment to export_program for tests (#1493)
Summary: Pull Request resolved: #1493 - add `extract_constant_segment` to export_program - for each program, generate pte file with constants in segment, and constants in flatbuffer. Reviewed By: dbort Differential Revision: D52434374 fbshipit-source-id: 4a387c6ad46b88723f1865b16558f20e226aff0f
1 parent 38b6b97 commit a81c2d4

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

test/end2end/exported_module.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def export(
6363
ignore_to_out_var_failure: bool = False,
6464
dynamic_memory_planning_mode: DynamicMemoryPlanningMode = DynamicMemoryPlanningMode.UPPER_BOUND,
6565
capture_config=None,
66+
extract_constant_segment: bool = True,
6667
) -> "ExportedModule":
6768
"""
6869
Creates a new ExportedModule for the specified module class.
@@ -166,6 +167,7 @@ def return_wrapper():
166167
dynamic_memory_planning_mode=dynamic_memory_planning_mode,
167168
memory_planning_pass=memory_planning_pass,
168169
to_out_var_pass=ToOutVarPass(ignore_to_out_var_failure),
170+
extract_constant_segment=extract_constant_segment,
169171
)
170172
)
171173
)

test/models/export_program.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def get_method_names_to_export() -> List[str]:
155155
#
156156

157157

158-
def export_module_to_program(module_class: Type[nn.Module]):
158+
def export_module_to_program(
159+
module_class: Type[nn.Module], extract_constant_segment: bool
160+
):
159161
"""Exports the module and returns the serialized program data."""
160162
# Look for an optional @staticmethod that defines custom trace params.
161163
export_kwargs: Dict[str, Any] = {}
@@ -167,7 +169,12 @@ def export_module_to_program(module_class: Type[nn.Module]):
167169
methods = module_class.get_method_names_to_export()
168170
else:
169171
methods = ["forward"]
170-
module = ExportedModule.export(module_class, methods, **export_kwargs)
172+
module = ExportedModule.export(
173+
module_class,
174+
methods,
175+
extract_constant_segment=extract_constant_segment,
176+
**export_kwargs,
177+
)
171178
return module.executorch_program.buffer
172179

173180

@@ -205,10 +212,16 @@ def main() -> None:
205212
# Export and write to the output files.
206213
os.makedirs(args.outdir, exist_ok=True)
207214
for module_name, module_class in module_names_to_classes.items():
208-
outfile = os.path.join(args.outdir, f"{module_name}.pte")
209-
with open(outfile, "wb") as fp:
210-
fp.write(export_module_to_program(module_class))
211-
print(f"Exported {module_name} and wrote program data to {outfile}")
215+
for extract_constant_segment in (True, False):
216+
suffix = "" if extract_constant_segment else "-no-constant-segment"
217+
outfile = os.path.join(args.outdir, f"{module_name}{suffix}.pte")
218+
with open(outfile, "wb") as fp:
219+
fp.write(
220+
export_module_to_program(
221+
module_class, extract_constant_segment=extract_constant_segment
222+
)
223+
)
224+
print(f"Exported {module_name} and wrote program data to {outfile}")
212225

213226

214227
if __name__ == "__main__":

test/models/targets.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def define_common_targets():
7272
runtime.genrule(
7373
name = "exported_programs",
7474
cmd = "$(exe :export_program) --modules " + ",".join(MODULES_TO_EXPORT) + " --outdir $OUT",
75-
outs = {fname + ".pte": [fname + ".pte"] for fname in MODULES_TO_EXPORT},
75+
outs = {
76+
fname + seg_suffix + ".pte": [fname + seg_suffix + ".pte"]
77+
for fname in MODULES_TO_EXPORT
78+
for seg_suffix in ["", "-no-constant-segment"]
79+
},
7680
default_outs = ["."],
7781
visibility = [
7882
"//executorch/...",

0 commit comments

Comments
 (0)