|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-unsafe |
| 8 | + |
| 9 | +import argparse |
| 10 | +import os |
| 11 | + |
| 12 | +from functools import partial |
| 13 | +from typing import Dict, final, Optional, Sequence, Type |
| 14 | + |
| 15 | +import executorch.exir as exir |
| 16 | +import torch |
| 17 | + |
| 18 | +from executorch.exir import ( |
| 19 | + EdgeCompileConfig, |
| 20 | + ExecutorchBackendConfig, |
| 21 | + to_edge, |
| 22 | + to_edge_transform_and_lower, |
| 23 | +) |
| 24 | +from executorch.exir.passes.external_constants_pass import ( |
| 25 | + delegate_external_constants_pass, |
| 26 | +) |
| 27 | +from executorch.exir.program import ExecutorchProgramManager |
| 28 | +from torch.export import export |
| 29 | + |
| 30 | + |
| 31 | +class ModuleLinear(torch.nn.Module): |
| 32 | + def __init__(self): |
| 33 | + super().__init__() |
| 34 | + self.linear = torch.nn.Linear(3, 3) |
| 35 | + |
| 36 | + def forward(self, x: torch.Tensor): |
| 37 | + return self.linear(x) |
| 38 | + |
| 39 | + def get_random_inputs(self): |
| 40 | + return (torch.randn(3),) |
| 41 | + |
| 42 | + |
| 43 | +def main() -> None: |
| 44 | + |
| 45 | + parser = argparse.ArgumentParser( |
| 46 | + prog="export_program", |
| 47 | + description="Exports nn.Module models to ExecuTorch .pte and .ptd files", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "--outdir", |
| 51 | + type=str, |
| 52 | + required=True, |
| 53 | + help="Path to the directory to write <classname>.pte files and .ptd files to", |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--xnnpack", |
| 57 | + action="store_true", |
| 58 | + help="Export the model lowered to XNNPACK", |
| 59 | + ) |
| 60 | + args = parser.parse_args() |
| 61 | + |
| 62 | + if args.xnnpack: |
| 63 | + print("Exporting to ExecuTorch with XNNPACK") |
| 64 | + else: |
| 65 | + print("Exporting to ExecuTorch") |
| 66 | + |
| 67 | + # Construct eager model. |
| 68 | + model = ModuleLinear() |
| 69 | + # Export model. |
| 70 | + exported_program = torch.export.export(model, model.get_random_inputs()) |
| 71 | + model_name = "linear_xnnpack" if args.xnnpack else "linear" |
| 72 | + |
| 73 | + # Lower to XNNPACK. |
| 74 | + if args.xnnpack: |
| 75 | + print("Lowering to XNNPACK...") |
| 76 | + from executorch.backends.xnnpack.partition.xnnpack_partitioner import ( |
| 77 | + XnnpackPartitioner, |
| 78 | + ) |
| 79 | + |
| 80 | + partial_function = partial( |
| 81 | + delegate_external_constants_pass, |
| 82 | + ep=exported_program, |
| 83 | + gen_tag_fn=lambda x: model_name, |
| 84 | + ) |
| 85 | + executorch_program = to_edge_transform_and_lower( |
| 86 | + exported_program, |
| 87 | + transform_passes=[partial_function], |
| 88 | + compile_config=EdgeCompileConfig(_check_ir_validity=False), |
| 89 | + partitioner=[XnnpackPartitioner()], |
| 90 | + ).to_executorch(config=ExecutorchBackendConfig()) |
| 91 | + |
| 92 | + # No backends. |
| 93 | + else: |
| 94 | + print("Lowering to ExecuTorch...") |
| 95 | + edge_program = to_edge(exported_program) |
| 96 | + executorch_program = edge_program.to_executorch( |
| 97 | + ExecutorchBackendConfig(external_constants=True) |
| 98 | + ) |
| 99 | + |
| 100 | + print("Saving PTE and PTD files.") |
| 101 | + os.makedirs(args.outdir, exist_ok=True) |
| 102 | + pte_file = os.path.join(args.outdir, f"{model_name}.pte") |
| 103 | + with open(pte_file, "wb") as fp: |
| 104 | + executorch_program.write_to_file(fp) |
| 105 | + if executorch_program._tensor_data.get("_default_external_constant"): |
| 106 | + executorch_program._tensor_data[model_name] = ( |
| 107 | + executorch_program._tensor_data.pop("_default_external_constant") |
| 108 | + ) |
| 109 | + executorch_program.write_tensor_data_to_file(args.outdir) |
| 110 | + |
| 111 | + print(f"Successfully exported {model_name}.pte and {model_name}.ptd") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments