-
Notifications
You must be signed in to change notification settings - Fork 886
Add openvino backend to wheel package #18362
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
Draft
suryasidd
wants to merge
4
commits into
pytorch:main
Choose a base branch
from
suryasidd:openvino_build_wheel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−10
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| #!/bin/bash | ||
| # === OpenVINO Wheel Build & Test Script === | ||
| # | ||
| # Builds the ExecuTorch wheel with OpenVINO support, installs it into both | ||
| # a conda env and a Python venv, and runs smoke tests verifying that the | ||
| # OpenVINO backend is registered and can export a simple model. | ||
|
|
||
|
|
||
| set -e | ||
| set -x | ||
| exec > >(tee -i openvino_wheel_build.log) 2>&1 | ||
|
|
||
| REPO_ROOT=$(pwd) | ||
| PYTHON_VERSION=${1:-3.11} | ||
|
|
||
| # ---------------------------- | ||
| # Dynamically create test script | ||
| # ---------------------------- | ||
| cat > "/tmp/script_openvino_wheel_test.py" << 'EOF' | ||
| import torch | ||
| from torch.export import export | ||
| from executorch.backends.openvino.partitioner import OpenvinoPartitioner | ||
| from executorch.exir import to_edge_transform_and_lower | ||
| from executorch.exir.backend.backend_details import CompileSpec | ||
| from executorch.exir.backend.utils import format_delegated_graph | ||
| from executorch.exir.capture._config import ExecutorchBackendConfig | ||
| from executorch.extension.export_util.utils import save_pte_program | ||
|
|
||
|
|
||
| class LinearModule(torch.nn.Module): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.linear = torch.nn.Linear(3, 3) | ||
|
|
||
| def forward(self, x): | ||
| return self.linear(x) | ||
|
|
||
| def get_example_inputs(self): | ||
| return (torch.randn(3, 3),) | ||
|
|
||
|
|
||
| model = LinearModule().eval() | ||
| example_inputs = model.get_example_inputs() | ||
|
|
||
| exported = export(model, example_inputs, strict=True) | ||
|
|
||
| compile_spec = [CompileSpec("device", b"CPU")] | ||
| edge = to_edge_transform_and_lower( | ||
| exported, | ||
| partitioner=[OpenvinoPartitioner(compile_spec)], | ||
| ) | ||
|
|
||
| # Verify OpenVINO delegation occurred | ||
| output_graph = format_delegated_graph(edge.exported_program().graph_module) | ||
| assert "OpenvinoBackend" in output_graph, \ | ||
| "Expected OpenVINO delegation but no delegate call found in graph" | ||
| print("OpenVINO delegation successful") | ||
|
|
||
| executorch_program = edge.to_executorch( | ||
| config=ExecutorchBackendConfig(extract_delegate_segments=False) | ||
| ) | ||
| save_pte_program(executorch_program, "linear_openvino", "") | ||
| print("linear_openvino.pte created successfully") | ||
| EOF | ||
|
|
||
| source .ci/scripts/utils.sh | ||
| # ---------------------------- | ||
| # Install OpenVINO and source its setupvars.sh | ||
| # ---------------------------- | ||
| echo "=== Installing OpenVINO ===" | ||
| source "${REPO_ROOT}/backends/openvino/scripts/install_openvino.sh" | ||
| install_openvino | ||
| echo "OpenVINO_DIR=${OpenVINO_DIR}" | ||
|
|
||
| # ---------------------------- | ||
| # Build the wheel | ||
| # ---------------------------- | ||
| echo "=== Building Wheel Package ===" | ||
| install_executorch | ||
|
|
||
| python setup.py bdist_wheel | ||
|
|
||
| WHEEL_FILE=$(ls dist/*.whl | head -n 1) | ||
| echo "Found wheel: ${WHEEL_FILE}" | ||
|
|
||
|
|
||
| # ---------------------------- | ||
| # Helpers | ||
| # ---------------------------- | ||
| run_core_tests() { | ||
| local PYBIN="$1" | ||
| local PIPBIN="$2" | ||
| local LABEL="$3" | ||
|
|
||
| echo "=== [${LABEL}] Installing wheel ===" | ||
| "${PIPBIN}" install --upgrade pip | ||
| "${PIPBIN}" install "${WHEEL_FILE}" | ||
|
|
||
| echo "=== [${LABEL}] Import smoke tests ===" | ||
| "${PYBIN}" -c "import executorch; print('executorch imported')" | ||
| "${PYBIN}" -c "import executorch.backends.openvino; print('executorch.backends.openvino imported')" | ||
|
|
||
| echo "=== [${LABEL}] Verify OpenvinoBackend is registered ===" | ||
| "${PYBIN}" - <<'PY' | ||
| from executorch.extension.pybindings.portable_lib import _get_registered_backend_names | ||
| backends = _get_registered_backend_names() | ||
| print(f"Registered backends: {backends}") | ||
| assert "OpenvinoBackend" in backends, \ | ||
| f"OpenvinoBackend not found in registered backends: {backends}" | ||
| print("OpenvinoBackend is registered") | ||
| PY | ||
|
|
||
| echo "=== [${LABEL}] Run export script to generate linear_openvino.pte ===" | ||
| (cd "${REPO_ROOT}" && "${PYBIN}" /tmp/script_openvino_wheel_test.py) | ||
|
|
||
| if [[ -f "${REPO_ROOT}/linear_openvino.pte" ]]; then | ||
| echo "[${LABEL}] linear_openvino.pte created successfully" | ||
| rm -f "${REPO_ROOT}/linear_openvino.pte" | ||
| else | ||
| echo "ERROR: [${LABEL}] linear_openvino.pte was not created" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # ---------------------------- | ||
| # Conda environment tests | ||
| # ---------------------------- | ||
| echo "=== Testing in Conda env ===" | ||
| TEMP_ENV_DIR=$(mktemp -d) | ||
| echo "Using temporary directory for conda: $TEMP_ENV_DIR" | ||
| conda create -y -p "${TEMP_ENV_DIR}/env" python="${PYTHON_VERSION}" | ||
| CONDA_PY="${TEMP_ENV_DIR}/env/bin/python" | ||
| CONDA_PIP="${TEMP_ENV_DIR}/env/bin/pip" | ||
| run_core_tests "${CONDA_PY}" "${CONDA_PIP}" "conda" | ||
| conda env remove -p "${TEMP_ENV_DIR}/env" -y || true | ||
| rm -rf "${TEMP_ENV_DIR}" | ||
|
|
||
| # ---------------------------- | ||
| # Python venv tests | ||
| # ---------------------------- | ||
| echo "=== Testing in Python venv ===" | ||
| TEMP_VENV_DIR=$(mktemp -d) | ||
| python3 -m venv "${TEMP_VENV_DIR}/venv" | ||
| VENV_PY="${TEMP_VENV_DIR}/venv/bin/python" | ||
| VENV_PIP="${TEMP_VENV_DIR}/venv/bin/pip" | ||
| run_core_tests "${VENV_PY}" "${VENV_PIP}" "venv" | ||
| rm -rf "${TEMP_VENV_DIR}" | ||
|
|
||
| echo "=== All OpenVINO wheel tests completed! ===" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| nncf==3.0.0 | ||
| git+https://github.com/openvinotoolkit/nncf@b101e7e#egg=nncf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/bin/bash | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
| # Downloads the OpenVINO toolkit archive and sources setupvars.sh to set | ||
| # OpenVINO_DIR and related environment variables. | ||
|
|
||
| set -ex | ||
|
|
||
| OPENVINO_VERSION="2026.0" | ||
| OPENVINO_BUILD="2026.0.0.20965.c6d6a13a886" | ||
| OPENVINO_ARCHIVE="openvino_toolkit_ubuntu22_${OPENVINO_BUILD}_x86_64" | ||
| OPENVINO_URL="https://storage.openvinotoolkit.org/repositories/openvino/packages/${OPENVINO_VERSION}/linux/${OPENVINO_ARCHIVE}.tgz" | ||
|
|
||
| install_openvino() { | ||
| # Skip if OpenVINO_DIR is already set and valid | ||
| if [[ -n "${OpenVINO_DIR:-}" && -d "${OpenVINO_DIR:-}" ]]; then | ||
|
Collaborator
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. Should we have any version checks here? Sometimes OpenVINO_DIR can be set for a previous unsupported version |
||
| echo "OpenVINO already set to ${OpenVINO_DIR} - skipping installation" | ||
| return | ||
| fi | ||
|
|
||
| # Skip if already extracted | ||
| if [[ -f "openvino/setupvars.sh" ]]; then | ||
| echo "OpenVINO already extracted at $(pwd)/openvino" | ||
| source openvino/setupvars.sh | ||
| return | ||
| fi | ||
|
|
||
| echo "Downloading OpenVINO ${OPENVINO_VERSION}..." | ||
| curl -Lo /tmp/openvino_toolkit.tgz --retry 3 --fail "${OPENVINO_URL}" | ||
|
|
||
| echo "Extracting OpenVINO archive..." | ||
| tar -xzf /tmp/openvino_toolkit.tgz | ||
| mv "${OPENVINO_ARCHIVE}" openvino | ||
| rm -f /tmp/openvino_toolkit.tgz | ||
|
|
||
| source openvino/setupvars.sh | ||
| echo "OpenVINO_DIR=${OpenVINO_DIR}" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What happens if this is executed on another Ubuntu version?