Skip to content

Commit

Permalink
Export to ExecuTorch: Initial Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Guang Yang committed Dec 5, 2024
1 parent 7e8d857 commit 4385d25
Show file tree
Hide file tree
Showing 18 changed files with 1,042 additions and 10 deletions.
2 changes: 1 addition & 1 deletion optimum/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

from .base import BaseOptimumCLICommand, CommandInfo, RootOptimumCLICommand
from .env import EnvironmentCommand
from .export import ExportCommand, ONNXExportCommand, TFLiteExportCommand
from .export import ExecuTorchExportCommand, ExportCommand, ONNXExportCommand, TFLiteExportCommand
from .optimum_cli import optimum_cli_subcommand
1 change: 1 addition & 0 deletions optimum/commands/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@


from .base import ExportCommand
from .executorch import ExecuTorchExportCommand
from .onnx import ONNXExportCommand
from .tflite import TFLiteExportCommand
6 changes: 6 additions & 0 deletions optimum/commands/export/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""optimum.exporters command-line interface base classes."""

from .. import BaseOptimumCLICommand, CommandInfo
from .executorch import ExecuTorchExportCommand
from .onnx import ONNXExportCommand
from .tflite import TFLiteExportCommand

Expand All @@ -25,6 +26,11 @@ class ExportCommand(BaseOptimumCLICommand):
help="Export PyTorch and TensorFlow models to several format.",
)
SUBCOMMANDS = (
CommandInfo(
name="executorch",
help="Export PyTorch model to ExecuTorch.",
subcommand_class=ExecuTorchExportCommand,
),
CommandInfo(
name="onnx",
help="Export PyTorch and TensorFlow to ONNX.",
Expand Down
55 changes: 55 additions & 0 deletions optimum/commands/export/executorch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Defines the command line for the export with ExecuTorch."""

from pathlib import Path
from typing import TYPE_CHECKING

from ...exporters import TasksManager
from ..base import BaseOptimumCLICommand


if TYPE_CHECKING:
from argparse import ArgumentParser


def parse_args_executorch(parser):
required_group = parser.add_argument_group("Required arguments")
required_group.add_argument(
"-m", "--model", type=str, required=True, help="Model ID on huggingface.co or path on disk to load model from."
)
required_group.add_argument(
"-o",
"--output_dir",
type=Path,
help="Path indicating the directory where to store the generated ExecuTorch model.",
)
required_group.add_argument(
"--task",
type=str,
default="text-generation",
help=(
"The task to export the model for. Available tasks depend on the model, but are among:"
f" {str(TasksManager.get_all_tasks())}."
),
)
required_group.add_argument(
"--recipe",
type=str,
default="xnnpack",
help='Pre-defined recipes for export to ExecuTorch. Defaults to "xnnpack".',
)


class ExecuTorchExportCommand(BaseOptimumCLICommand):
@staticmethod
def parse_args(parser: "ArgumentParser"):
return parse_args_executorch(parser)

def run(self):
from ...exporters.executorch import main_export

main_export(
model_name_or_path=self.args.model,
task=self.args.task,
recipe=self.args.recipe,
output_dir=self.args.output_dir,
)
16 changes: 16 additions & 0 deletions optimum/executorchruntime/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import TYPE_CHECKING
from transformers.utils import _LazyModule


_import_structure = {
"modeling_executorch": [
"ExecuTorchModelForCausalLM",
],
}

if TYPE_CHECKING:
from .modeling_executorch import ExecuTorchModelForCausalLM
else:
import sys

sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
Loading

0 comments on commit 4385d25

Please sign in to comment.