Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
154f7eb
Export to ExecuTorch: Initial Integration
Nov 6, 2024
b947760
Test I can push
michaelbenayoun Dec 12, 2024
301bbcf
Styling
michaelbenayoun Dec 12, 2024
dec0587
Use ungated models for the tests
michaelbenayoun Dec 12, 2024
32d7d79
Test if it OOMs
michaelbenayoun Dec 12, 2024
eaf92c6
Fix doc
michaelbenayoun Dec 12, 2024
7aa1562
Test with different instance
michaelbenayoun Dec 12, 2024
6d361ce
Update configuration.mdx
guangy10 Dec 12, 2024
0d9322a
Experiment to use 'require_read_token' for accessing gated models in …
Dec 13, 2024
3c5f757
Test with public models and disable linux CI
michaelbenayoun Dec 17, 2024
c36e2e2
Trying to fix the doc
michaelbenayoun Dec 17, 2024
f0c76d4
Split modeling tests to separate files and CI jobs
Dec 18, 2024
9063a90
Styling
michaelbenayoun Dec 18, 2024
09965ae
Fix doc
michaelbenayoun Dec 18, 2024
8f60d66
Disable LLama 3b since it ooms on GH instances
michaelbenayoun Dec 18, 2024
cbc7c43
Updating torch for executorch in the doc building PR
michaelbenayoun Dec 18, 2024
6242721
Test doc build
michaelbenayoun Dec 18, 2024
e4eccbe
Test doc build
michaelbenayoun Dec 18, 2024
e855005
Test doc build
michaelbenayoun Dec 18, 2024
5b32dea
Test doc build
michaelbenayoun Dec 18, 2024
093e95a
Test doc build
michaelbenayoun Dec 19, 2024
2c748df
Test doc build
michaelbenayoun Dec 19, 2024
9309d77
Build doc
michaelbenayoun Dec 19, 2024
b447d1a
Build doc
michaelbenayoun Dec 19, 2024
b8701ff
Build doc
michaelbenayoun Dec 19, 2024
3041f16
Build doc
michaelbenayoun Dec 19, 2024
045df37
Build doc
michaelbenayoun Dec 19, 2024
224101b
Try with the full doc
michaelbenayoun Dec 20, 2024
b9ce05d
Try with the full doc
michaelbenayoun Dec 20, 2024
e345f07
Restore setup.py
michaelbenayoun Dec 20, 2024
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
78 changes: 39 additions & 39 deletions .github/workflows/build_pr_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,45 @@ jobs:
pip install black
cd ..

- name: Make Habana documentation
run: |
sudo docker system prune -a -f
cd optimum-habana
make doc BUILD_DIR=habana-doc-build VERSION=pr_$PR_NUMBER
sudo mv habana-doc-build ../optimum
cd ..

- name: Make Intel documentation
run: |
sudo docker system prune -a -f
cd optimum-intel
make doc BUILD_DIR=intel-doc-build VERSION=pr_$PR_NUMBER
sudo mv intel-doc-build ../optimum
cd ..

# TODO: enable Furiosa doc build in PRs once archive.furiosa.ai is public
- name: Make Furiosa documentation
run: |
echo "For PRs we don't build Furiosa doc"

- name: Make AMD documentation
run: |
sudo docker system prune -a -f
cd optimum-amd
make doc BUILD_DIR=amd-doc-build VERSION=pr_$PR_NUMBER
sudo mv amd-doc-build ../optimum
cd ..

- name: Make TPU documentation
run: |
sudo docker system prune -a -f
source venv-doc/bin/activate
cd optimum-tpu
pip install -U pip
pip install . -f https://storage.googleapis.com/libtpu-releases/index.html
doc-builder build optimum.tpu docs/source/ --build_dir tpu-doc-build --version pr_$PR_NUMBER --version_tag_suffix "" --html --clean
mv tpu-doc-build ../optimum
cd ..
# - name: Make Habana documentation
# run: |
# sudo docker system prune -a -f
# cd optimum-habana
# make doc BUILD_DIR=habana-doc-build VERSION=pr_$PR_NUMBER
# sudo mv habana-doc-build ../optimum
# cd ..

# - name: Make Intel documentation
# run: |
# sudo docker system prune -a -f
# cd optimum-intel
# make doc BUILD_DIR=intel-doc-build VERSION=pr_$PR_NUMBER
# sudo mv intel-doc-build ../optimum
# cd ..

# # TODO: enable Furiosa doc build in PRs once archive.furiosa.ai is public
# - name: Make Furiosa documentation
# run: |
# echo "For PRs we don't build Furiosa doc"

# - name: Make AMD documentation
# run: |
# sudo docker system prune -a -f
# cd optimum-amd
# make doc BUILD_DIR=amd-doc-build VERSION=pr_$PR_NUMBER
# sudo mv amd-doc-build ../optimum
# cd ..

# - name: Make TPU documentation
# run: |
# sudo docker system prune -a -f
# source venv-doc/bin/activate
# cd optimum-tpu
# pip install -U pip
# pip install . -f https://storage.googleapis.com/libtpu-releases/index.html
# doc-builder build optimum.tpu docs/source/ --build_dir tpu-doc-build --version pr_$PR_NUMBER --version_tag_suffix "" --html --clean
# mv tpu-doc-build ../optimum
# cd ..

- name: Make Optimum documentation
run: |
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/test_executorch_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,exporters-executorch]
Comment on lines +18 to +30
Copy link
Contributor Author

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.

pip list
- name: Run tests
working-directory: tests
run: |
RUN_SLOW=1 pytest executorch/export/test_*.py -s -vvvv --durations=0
42 changes: 42 additions & 0 deletions .github/workflows/test_executorch_runtime.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,exporters-executorch]
pip list
- name: Run tests
working-directory: tests
run: |
RUN_SLOW=1 pytest executorch/runtime/${{ matrix.test-modeling }} -s -vvvv --durations=0
2 changes: 1 addition & 1 deletion docs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip
RUN python3 -m pip install --no-cache-dir git+https://github.com/huggingface/doc-builder.git

RUN git clone $clone_url && cd optimum && git checkout $commit_sha
RUN python3 -m pip install --no-cache-dir ./optimum[onnxruntime,benchmark,quality,exporters-tf,doc-build,diffusers]
RUN python3 -m pip install --no-cache-dir ./optimum[onnxruntime,benchmark,quality,exporters-tf,exporters-executorch,doc-build,diffusers]
17 changes: 17 additions & 0 deletions docs/source/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@
title: Reference
isExpanded: false
title: "ONNX"
- sections:
- local: exporters/executorch/overview
title: Overview
- sections:
- local: exporters/executorch/usage_guides/export_a_model
title: Export a model to ExecuTorch
- local: exporters/executorch/usage_guides/contribute
title: Add support for exporting an architecture to ExecuTorch
title: How-to guides
- sections:
- local: exporters/executorch/package_reference/configuration
title: ExecuTorch configurations
- local: exporters/executorch/package_reference/export
title: Export functions
title: Reference
isExpanded: false
title: "ExecuTorch"
- sections:
- local: exporters/tflite/overview
title: Overview
Expand Down
26 changes: 26 additions & 0 deletions docs/source/exporters/executorch/overview.mdx
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelbenayoun The doc build still complains about this line:

ValueError: There was an error when converting /optimum/docs/source/exporters/executorch/package_reference/configuration.mdx to the MDX format.
Unable to find exporters.executorch.recipes.xnnpack.export_to_executorch_with_xnnpack in optimum. Make sure the path to that object is correct.

I verified that the package is valid. Any pointer what I missed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also observed the same thing, working on fixing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
26 changes: 26 additions & 0 deletions docs/source/exporters/executorch/package_reference/export.mdx
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).
57 changes: 57 additions & 0 deletions docs/source/exporters/executorch/usage_guides/contribute.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.
Loading
Loading