Skip to content

Add constant segment to export_program for tests #1493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/end2end/exported_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def export(
ignore_to_out_var_failure: bool = False,
dynamic_memory_planning_mode: DynamicMemoryPlanningMode = DynamicMemoryPlanningMode.UPPER_BOUND,
capture_config=None,
extract_constant_segment: bool = True,
) -> "ExportedModule":
"""
Creates a new ExportedModule for the specified module class.
Expand Down Expand Up @@ -166,6 +167,7 @@ def return_wrapper():
dynamic_memory_planning_mode=dynamic_memory_planning_mode,
memory_planning_pass=memory_planning_pass,
to_out_var_pass=ToOutVarPass(ignore_to_out_var_failure),
extract_constant_segment=extract_constant_segment,
)
)
)
Expand Down
25 changes: 19 additions & 6 deletions test/models/export_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def get_method_names_to_export() -> List[str]:
#


def export_module_to_program(module_class: Type[nn.Module]):
def export_module_to_program(
module_class: Type[nn.Module], extract_constant_segment: bool
):
"""Exports the module and returns the serialized program data."""
# Look for an optional @staticmethod that defines custom trace params.
export_kwargs: Dict[str, Any] = {}
Expand All @@ -167,7 +169,12 @@ def export_module_to_program(module_class: Type[nn.Module]):
methods = module_class.get_method_names_to_export()
else:
methods = ["forward"]
module = ExportedModule.export(module_class, methods, **export_kwargs)
module = ExportedModule.export(
module_class,
methods,
extract_constant_segment=extract_constant_segment,
**export_kwargs,
)
return module.executorch_program.buffer


Expand Down Expand Up @@ -205,10 +212,16 @@ def main() -> None:
# Export and write to the output files.
os.makedirs(args.outdir, exist_ok=True)
for module_name, module_class in module_names_to_classes.items():
outfile = os.path.join(args.outdir, f"{module_name}.pte")
with open(outfile, "wb") as fp:
fp.write(export_module_to_program(module_class))
print(f"Exported {module_name} and wrote program data to {outfile}")
for extract_constant_segment in (True, False):
suffix = "" if extract_constant_segment else "-no-constant-segment"
outfile = os.path.join(args.outdir, f"{module_name}{suffix}.pte")
with open(outfile, "wb") as fp:
fp.write(
export_module_to_program(
module_class, extract_constant_segment=extract_constant_segment
)
)
print(f"Exported {module_name} and wrote program data to {outfile}")


if __name__ == "__main__":
Expand Down
6 changes: 5 additions & 1 deletion test/models/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def define_common_targets():
runtime.genrule(
name = "exported_programs",
cmd = "$(exe :export_program) --modules " + ",".join(MODULES_TO_EXPORT) + " --outdir $OUT",
outs = {fname + ".pte": [fname + ".pte"] for fname in MODULES_TO_EXPORT},
outs = {
fname + seg_suffix + ".pte": [fname + seg_suffix + ".pte"]
for fname in MODULES_TO_EXPORT
for seg_suffix in ["", "-no-constant-segment"]
},
default_outs = ["."],
visibility = [
"//executorch/...",
Expand Down