Skip to content

Move executorch export to optimum-executorch #1

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

Merged
merged 14 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
39 changes: 39 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Code Quality
on:
push:
branches:
- main
- v*-release
pull_request:
branches:
- main

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

jobs:
quality:
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.9

- name: Install dependencies
run: |
pip install --upgrade pip
pip install "black~=23.1" "ruff==0.4.4"

- name: Check style with black
run: |
black --check .

- name: Check style with ruff
run: |
ruff check .
35 changes: 35 additions & 0 deletions .github/workflows/test_export.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ExecuTorch Export / 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: [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]
pip list
- name: Run tests
working-directory: tests
run: |
RUN_SLOW=1 pytest export/test_*.py -s -vvvv --durations=0
42 changes: 42 additions & 0 deletions .github/workflows/test_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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: [macos-15]
test-modeling:
- test_modeling_gemma2.py
- test_modeling_gemma.py
- test_modeling_llama.py
- test_modeling_olmo.py
- test_modeling.py
- test_modeling_qwen2.py

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]
pip list
- name: Run tests
working-directory: tests
run: |
RUN_SLOW=1 pytest models/${{ matrix.test-modeling }} -s -vvvv --durations=0
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL := /bin/bash
CURRENT_DIR = $(shell pwd)
DEFAULT_CLONE_URL := https://github.com/huggingface/optimum-executorch.git
# If CLONE_URL is empty, revert to DEFAULT_CLONE_URL
REAL_CLONE_URL = $(if $(CLONE_URL),$(CLONE_URL),$(DEFAULT_CLONE_URL))

.PHONY: style test

# Run code quality checks
style_check:
black --check .
ruff check .

style:
black .
ruff check . --fix

# Run tests for the library
test:
python -m pytest tests

# Utilities to release to PyPi
build_dist_install_tools:
pip install build
pip install twine

build_dist:
rm -rf build
rm -rf dist
python -m build

pypi_upload: build_dist
python -m twine upload dist/*
71 changes: 71 additions & 0 deletions optimum/commands/export/executorch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""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, CommandInfo


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):
COMMAND = CommandInfo(name="executorch", help="Export models to ExecuTorch.")

@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,
)
19 changes: 19 additions & 0 deletions optimum/commands/register/register_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ..export import ExportCommand
from ..export.executorch import ExecuTorchExportCommand


REGISTER_COMMANDS = [(ExecuTorchExportCommand, ExportCommand)]
31 changes: 31 additions & 0 deletions optimum/executorch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from transformers.utils import _LazyModule


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

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

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