-
Notifications
You must be signed in to change notification settings - Fork 486
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
Export to ExecuTorch: Initial Integration #2090
Changes from 17 commits
154f7eb
b947760
301bbcf
dec0587
32d7d79
eaf92c6
7aa1562
6d361ce
0d9322a
3c5f757
c36e2e2
f0c76d4
9063a90
09965ae
8f60d66
cbc7c43
6242721
e4eccbe
e855005
5b32dea
093e95a
2c748df
9309d77
b447d1a
b8701ff
3041f16
045df37
224101b
b9ce05d
e345f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,exporters-executorch] | ||
pip list | ||
- name: Run tests | ||
working-directory: tests | ||
run: | | ||
RUN_SLOW=1 pytest executorch/export/test_*.py -s -vvvv --durations=0 |
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,exporters-executorch] | ||
pip list | ||
- name: Run tests | ||
working-directory: tests | ||
run: | | ||
RUN_SLOW=1 pytest executorch/runtime/${{ matrix.test-modeling }} -s -vvvv --durations=0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!--Copyright 2024 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. | ||
--> | ||
|
||
# Overview | ||
|
||
🤗 Optimum handles the export of PyTorch to ExecuTorch in the `exporters.executorch` module. It provides classes, functions, and a command line interface to perform the export easily. | ||
|
||
Supported architectures from [🤗 Transformers](https://huggingface.co/docs/transformers/index): | ||
|
||
- Gemma | ||
- Gemma2 | ||
- Llama2 | ||
- Llama3(Llama3.2) | ||
- OLMo | ||
- Qwen2(Qwen2.5) | ||
|
||
There are many more models are supported by ExecuTorch, we will add those models to Optimum over time. Read more at [pytorch/executorch/examples/](https://github.com/pytorch/executorch/tree/main/examples) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<!--Copyright 2024 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. | ||
--> | ||
|
||
# Configuration for ExecuTorch Export | ||
|
||
ExecuTorch export provides a flexible configuration mechanism through dynamic registration, enabling users to have | ||
complete control over the export process. The configuration system is divided into task configurations and recipe | ||
configurations, each addressing specific aspects of the export pipeline. | ||
|
||
|
||
## Task Configurations | ||
|
||
Task configurations determine how a Hugging Face model should be loaded and prepared for export, tailored to specific tasks. | ||
|
||
For instance, when exporting a model for a text generation task, the provided configuration utilizes **static caching** and | ||
**SDPA (Scaled Dot-Product Attention)** for inference optimization. | ||
|
||
By leveraging task configurations, users can ensure that their models are appropriately prepared for efficient execution on | ||
the ExecuTorch backend. | ||
|
||
[[autodoc]] exporters.executorch.task_registry.discover_tasks | ||
|
||
[[autodoc]] exporters.executorch.task_registry.register_task | ||
|
||
[[autodoc]] exporters.executorch.tasks.causal_lm.load_causal_lm_model | ||
|
||
|
||
## Recipe Configurations | ||
|
||
Recipe configurations control the specifics of lowering an eager PyTorch module to the ExecuTorch backend. These | ||
configurations allow users to: | ||
|
||
- Specify whether and how to **quantize** the model. | ||
- Delegate computation to various accelerators, such as **CPU**, **GPU**, **NPU**, **DSP**, and others. | ||
- Define **custom transformation passes**. | ||
- Implement advanced techniques like memory planning algorithms to optimize resource utilization. | ||
|
||
[[autodoc]] exporters.executorch.recipe_registry.discover_recipes | ||
|
||
[[autodoc]] exporters.executorch.recipe_registry.register_recipe | ||
|
||
[[autodoc]] exporters.executorch.recipes.xnnpack.export_to_executorch_with_xnnpack | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michaelbenayoun The doc build still complains about this line:
I verified that the package is valid. Any pointer what I missed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also observed the same thing, working on fixing this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still failing, but I guess we can ignore it and merge |
||
|
||
The combination of task and recipe configurations ensures that users can customize both the high-level task setup | ||
and the low-level export details to suit their deployment requirements. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!--Copyright 2024 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. | ||
--> | ||
|
||
# Export functions | ||
|
||
## Main functions | ||
|
||
[[autodoc]] exporters.executorch.convert.export_to_executorch | ||
|
||
The primary export function is designed to be **model- and task-independent** as well as **optimization-agnostic**, providing a | ||
highly flexible and modular interface for exporting Hugging Face models to the ExecuTorch backend. | ||
|
||
This approach highlights the **composability** of ExecuTorch export pipeline, where dynamically registered **task configurations** | ||
specify how a :hug model is prepared, and **recipe configurations** encapsulate device-specific optimizations during export. This | ||
separation allows users to customize the export process without altering the core function. | ||
|
||
For more details on task and recipe configurations, see the [Configuration for ExecuTorch Export](./configuration.mdx). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!--Copyright 2024 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. | ||
--> | ||
|
||
# Adding support for an unsupported architecture | ||
|
||
We welcome contributions to extend the functionality of ExecuTorch export. This guide provides high-level instructions for contributors who want to: | ||
|
||
1. Export a new model that is not currently supported. | ||
2. Add new recipes or support a new task for export. | ||
|
||
--- | ||
|
||
## Exporting a New Model | ||
|
||
If you want to export a model that is not already supported by the library, follow these steps: | ||
|
||
### Step 1: Export and Test the Model | ||
1. Attempt to export and lower the model using an existing task and recipe. On success, it will store the exported model in a `.pte` file. | ||
2. Add a test case for the model in the appropriate test suite. | ||
- For example, you can make sure tests pass for the new `my_new_model` by running: | ||
```bash | ||
pytest tests/executorch/export/test_*.py -k "test_my_new_model" # doctest: +SKIP | ||
pytest tests/executorch/runtime/test_*.py -k "test_my_new_model" # doctest: +SKIP | ||
``` | ||
|
||
### Step 2: Handle Export Failures | ||
1. If the export fails in Step 1, report the issue by opening a GitHub issue. | ||
2. If the issue requires changes to the model’s architecture or its Hugging Face implementation, these modifications may be made upstream in the Hugging Face Transformers library. | ||
|
||
--- | ||
|
||
## Adding New Recipes or Tasks | ||
|
||
To extend ExecuTorch with new recipes or tasks, follow these guidelines: | ||
|
||
### Registering a New Recipe | ||
You can add a custom recipe to define specific optimizations or configurations for exporting models. Below is an example: | ||
|
||
```python | ||
from exporters.executorch import register_recipe | ||
|
||
@register_recipe("my_custom_recipe") | ||
def export_with_custom_recipe(model, config, *args, **kwargs): | ||
# Example: Apply a custom quantization | ||
``` | ||
|
||
### Registering a Task | ||
The task registration process is same as adding a recipe. Besides that you may need to implement a new `ExecuTorchModelForXXX` class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @michaelbenayoun Compatible python versions and package installation.