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 6, 2024
1 parent 7e8d857 commit e561925
Show file tree
Hide file tree
Showing 22 changed files with 1,170 additions and 4 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test_executorch_runtime.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: ExecuTorch Runtime / Python - Test

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
strategy:
fail-fast: false
matrix:
python-version: ['3.10', '3.11', '3.12']
os: [ubuntu-20.04, macos-15]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies for ExecuTorch
run: |
pip install .[tests,exporters-executorch]
python -c "import executorch; print(executorch.__version__)"
python -c "import torch; print(torch.__version__)"
python -c "import transformers; print(transformers.__version__)"
- name: Run tests
working-directory: tests
run: |
pytest tests/executorch/test_*.py -s -vvvv --durations=0
37 changes: 37 additions & 0 deletions .github/workflows/test_export_executorch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Exporters ExecuTorch / Python - Test

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
strategy:
fail-fast: false
matrix:
python-version: ['3.10', '3.11', '3.12']
os: [ubuntu-20.04, macos-15]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies for ExecuTorch
run: |
pip install .[tests,exporters-executorch]
python -c "import executorch; print(executorch.__version__)"
python -c "import torch; print(torch.__version__)"
python -c "import transformers; print(transformers.__version__)"
- name: Run tests
working-directory: tests
run: |
pytest tests/exporters/executorch/test_*.py -s -vvvv --durations=0
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 e561925

Please sign in to comment.