Skip to content

Commit 4385d25

Browse files
author
Guang Yang
committed
Export to ExecuTorch: Initial Integration
1 parent 7e8d857 commit 4385d25

18 files changed

Lines changed: 1042 additions & 10 deletions

File tree

optimum/commands/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
from .base import BaseOptimumCLICommand, CommandInfo, RootOptimumCLICommand
1616
from .env import EnvironmentCommand
17-
from .export import ExportCommand, ONNXExportCommand, TFLiteExportCommand
17+
from .export import ExecuTorchExportCommand, ExportCommand, ONNXExportCommand, TFLiteExportCommand
1818
from .optimum_cli import optimum_cli_subcommand

optimum/commands/export/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414

1515

1616
from .base import ExportCommand
17+
from .executorch import ExecuTorchExportCommand
1718
from .onnx import ONNXExportCommand
1819
from .tflite import TFLiteExportCommand

optimum/commands/export/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""optimum.exporters command-line interface base classes."""
1616

1717
from .. import BaseOptimumCLICommand, CommandInfo
18+
from .executorch import ExecuTorchExportCommand
1819
from .onnx import ONNXExportCommand
1920
from .tflite import TFLiteExportCommand
2021

@@ -25,6 +26,11 @@ class ExportCommand(BaseOptimumCLICommand):
2526
help="Export PyTorch and TensorFlow models to several format.",
2627
)
2728
SUBCOMMANDS = (
29+
CommandInfo(
30+
name="executorch",
31+
help="Export PyTorch model to ExecuTorch.",
32+
subcommand_class=ExecuTorchExportCommand,
33+
),
2834
CommandInfo(
2935
name="onnx",
3036
help="Export PyTorch and TensorFlow to ONNX.",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Defines the command line for the export with ExecuTorch."""
2+
3+
from pathlib import Path
4+
from typing import TYPE_CHECKING
5+
6+
from ...exporters import TasksManager
7+
from ..base import BaseOptimumCLICommand
8+
9+
10+
if TYPE_CHECKING:
11+
from argparse import ArgumentParser
12+
13+
14+
def parse_args_executorch(parser):
15+
required_group = parser.add_argument_group("Required arguments")
16+
required_group.add_argument(
17+
"-m", "--model", type=str, required=True, help="Model ID on huggingface.co or path on disk to load model from."
18+
)
19+
required_group.add_argument(
20+
"-o",
21+
"--output_dir",
22+
type=Path,
23+
help="Path indicating the directory where to store the generated ExecuTorch model.",
24+
)
25+
required_group.add_argument(
26+
"--task",
27+
type=str,
28+
default="text-generation",
29+
help=(
30+
"The task to export the model for. Available tasks depend on the model, but are among:"
31+
f" {str(TasksManager.get_all_tasks())}."
32+
),
33+
)
34+
required_group.add_argument(
35+
"--recipe",
36+
type=str,
37+
default="xnnpack",
38+
help='Pre-defined recipes for export to ExecuTorch. Defaults to "xnnpack".',
39+
)
40+
41+
42+
class ExecuTorchExportCommand(BaseOptimumCLICommand):
43+
@staticmethod
44+
def parse_args(parser: "ArgumentParser"):
45+
return parse_args_executorch(parser)
46+
47+
def run(self):
48+
from ...exporters.executorch import main_export
49+
50+
main_export(
51+
model_name_or_path=self.args.model,
52+
task=self.args.task,
53+
recipe=self.args.recipe,
54+
output_dir=self.args.output_dir,
55+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import TYPE_CHECKING
2+
from transformers.utils import _LazyModule
3+
4+
5+
_import_structure = {
6+
"modeling_executorch": [
7+
"ExecuTorchModelForCausalLM",
8+
],
9+
}
10+
11+
if TYPE_CHECKING:
12+
from .modeling_executorch import ExecuTorchModelForCausalLM
13+
else:
14+
import sys
15+
16+
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

0 commit comments

Comments
 (0)