Skip to content
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

[executorch] Add program-data separation example to test files #7765

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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 @@ -69,6 +69,7 @@ def export(
capture_config=None,
skip_type_promotion: bool = False,
export_joint_graph: bool = False,
external_constants: bool = False,
) -> "ExportedModule":
"""
Creates a new ExportedModule for the specified module class.
Expand Down Expand Up @@ -206,6 +207,7 @@ def __init__(self, method):
dynamic_memory_planning_mode=dynamic_memory_planning_mode,
memory_planning_pass=memory_planning_pass,
to_out_var_pass=ToOutVarPass(ignore_to_out_var_failure),
external_constants=external_constants,
)
)

Expand Down
30 changes: 20 additions & 10 deletions test/models/export_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import torch
from executorch.exir import CaptureConfig
from executorch.exir.passes import MemoryPlanningPass
from executorch.exir.program._program import ExecutorchProgramManager
from torch import nn
from torch.export import Dim

Expand Down Expand Up @@ -190,7 +191,8 @@ def export_joint():
def export_module_to_program(
module_class: Type[nn.Module],
skip_type_promotion: bool,
):
external_constants: bool = False,
) -> ExecutorchProgramManager:
"""Exports the module and returns the serialized program data."""
torch.manual_seed(0)
# Look for an optional @staticmethod that defines custom trace params.
Expand All @@ -211,9 +213,10 @@ def export_module_to_program(
methods,
skip_type_promotion=skip_type_promotion,
export_joint_graph=export_joint,
external_constants=external_constants,
**export_kwargs,
)
return module.executorch_program.buffer
return module.executorch_program


def main() -> None:
Expand All @@ -235,7 +238,12 @@ def main() -> None:
"--outdir",
type=str,
required=True,
help="Path to the directory to write <classname>.pte files to",
help="Path to the directory to write <classname>.pte files and .ptd files to",
)
parser.add_argument(
"--external-constants",
action="store_true",
help="Export the model with external constants",
)
args = parser.parse_args()

Expand All @@ -257,14 +265,16 @@ def main() -> None:
# Type promotion will convert to fp32.
skip_type_promotion = True
outfile = os.path.join(args.outdir, f"{module_name}.pte")
prog = export_module_to_program(
module_class,
skip_type_promotion=skip_type_promotion,
external_constants=args.external_constants,
)
with open(outfile, "wb") as fp:
fp.write(
export_module_to_program(
module_class,
skip_type_promotion=skip_type_promotion,
)
)
print(f"Exported {module_name} and wrote program data to {outfile}")
prog.write_to_file(fp)
print(f"Exported {module_name} and wrote program data to {outfile}")

prog.write_tensor_data_to_file(args.outdir)


if __name__ == "__main__":
Expand Down
23 changes: 21 additions & 2 deletions test/models/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,28 @@ def define_common_targets():
name = "exported_programs",
cmd = "$(exe :export_program) --modules " + ",".join(MODULES_TO_EXPORT) + " --outdir $OUT",
outs = {
fname + seg_suffix + ".pte": [fname + seg_suffix + ".pte"]
fname + ".pte": [fname + ".pte"]
for fname in MODULES_TO_EXPORT
for seg_suffix in ["", "-no-constant-segment"]
},
default_outs = ["."],
visibility = [
"//executorch/...",
# This genrule can't run in xplat since it uses EXIR, so make its
# output visible to xplat tests. This is an exceptional case, and
# typically shouldn't be done.
"fbsource//xplat/executorch/...",
],
# Allow the xplat entry in the visibility list. This is an exceptional
# case, and typically shouldn't be done.
_is_external_target = True,
)

runtime.genrule(
name = "exported_programs_with_data_separated",
cmd = "$(exe :export_program) --modules ModuleLinear --external-constants --outdir $OUT",
outs = {
"ModuleLinear.pte": ["ModuleLinear.pte"],
"ModuleLinear.ptd": ["_default_external_constant.ptd"],
},
default_outs = ["."],
visibility = [
Expand Down
Loading