Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion backends/openvino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
add_library(openvino_backend STATIC)

# Enable exceptions and RTTI for OpenVINO backend
target_compile_options(openvino_backend PRIVATE -frtti -fexceptions)
target_compile_options(
openvino_backend
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/GR /EHsc>
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

The MSVC generator expression contains a space ($<$<CXX_COMPILER_ID:MSVC>:/GR /EHsc>), which CMake will tokenize into separate arguments and can break the generator expression / pass an invalid flag. Split into two generator expressions (one per flag) or otherwise avoid spaces inside the unquoted expression so both /GR and /EHsc are applied correctly on MSVC.

Suggested change
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/GR /EHsc>
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/GR>
$<$<CXX_COMPILER_ID:MSVC>:/EHsc>

Copilot uses AI. Check for mistakes.
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-frtti -fexceptions>
)

# Add source files for OpenVINO backend
target_sources(
Expand Down
210 changes: 156 additions & 54 deletions backends/openvino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ For more information on the supported hardware, please refer to [OpenVINO System

## Quick Start (pip wheel)

On Linux, the OpenVINO backend is included in the ExecuTorch pip wheel. Install the OpenVINO runtime to activate it:
On Linux and Windows, the OpenVINO backend is included in the ExecuTorch pip wheel. Install the OpenVINO runtime to activate it:

```bash
pip install executorch[openvino]
```

The backend automatically discovers the OpenVINO C library from the pip-installed package — no `LD_LIBRARY_PATH` setup is needed.
The backend automatically discovers the OpenVINO C library from the pip-installed package — no `LD_LIBRARY_PATH` (Linux) or `PATH` (Windows) setup is needed.

If auto-discovery fails (e.g. non-standard install), you can point to the library explicitly:

**Linux:**
```bash
export OPENVINO_LIB_PATH=$(python3 -c "import openvino, os; print(os.path.join(os.path.dirname(openvino.__file__), 'libs', 'libopenvino_c.so'))")
```

**Windows (PowerShell):**
```powershell
$env:OPENVINO_LIB_PATH = python -c "import openvino, os; print(os.path.join(os.path.dirname(openvino.__file__), 'libs', 'openvino_c.dll'))"
```

Verify the backend is available:

```python
Expand All @@ -44,50 +50,169 @@ print(_get_registered_backend_names())
executorch
├── backends
│ └── openvino
│ ├── _passes
│ │ ├── __init__.py
│ │ └── decompose_floor_divide_pass.py
│ ├── quantizer
├── observers
── nncf_observers.py
├── __init__.py
└── quantizer.py
├── __init__.py
── llm_compression.py
├── observers.py
└── quantizer.py
│ ├── runtime
├── OpenvinoApi.h
├── OpenvinoBackend.cpp
└── OpenvinoBackend.h
├── OpenvinoApi.h
├── OpenvinoBackend.cpp
└── OpenvinoBackend.h
│ ├── scripts
│ └── openvino_build.sh
│ │ └── openvino_build.sh
│ ├── test
│ │ └── tester
│ │ ├── __init__.py
│ │ └── tester.py
│ ├── tests
│ │ ├── models
│ │ │ └── test_classification.py
│ │ ├── ops
│ │ │ ├── base_openvino_op_test.py
│ │ │ └── test_*.py
│ │ ├── quantizer
│ │ │ ├── synthetic_test_models.py
│ │ │ └── test_llm_compression.py
│ │ ├── README.md
│ │ └── test_runner.py
│ ├── CMakeLists.txt
│ ├── README.md
│ ├── __init__.py
│ ├── partitioner.py
│ ├── preprocess.py
│ └── requirements.txt
└── examples
└── openvino
├── aot_optimize_and_infer.py
└── README.md
└── openvino # See examples/openvino/README.md
```

## Build Instructions

### Prerequisites
### Setup

Follow the steps below to setup your build environment:


1. **Create a Virtual Environment**
- Create a virtual environment and activate it by executing the commands below.

**Linux:**
```bash
python -m venv env
source env/bin/activate
```

**Windows (PowerShell):**
```powershell
python -m venv env
env\Scripts\Activate.ps1
```
2. **Clone ExecuTorch Repository from GitHub**
- On Windows, enable symlinks before cloning. Refer to [Building from Source](https://docs.pytorch.org/executorch/main/using-executorch-building-from-source.html#environment-setup) for more details.
- Clone Executorch repository by executing the command below.
```bash
git clone --recurse-submodules https://github.com/pytorch/executorch.git
```
3. **Build ExecuTorch with OpenVINO Backend**
- The following commands build and install ExecuTorch with the OpenVINO backend into `cmake-out`.

**Linux:**
```bash
cmake -DCMAKE_INSTALL_PREFIX=cmake-out \
-DCMAKE_BUILD_TYPE=Release \
-DEXECUTORCH_BUILD_OPENVINO=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
-Bcmake-out
cmake --build cmake-out --target install --config Release -j $(nproc)
```

**Windows (PowerShell):**
```powershell
cmake -DCMAKE_INSTALL_PREFIX=cmake-out `
-DCMAKE_BUILD_TYPE=Release `
-DEXECUTORCH_BUILD_OPENVINO=ON `
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON `
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON `
-DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON `
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON `
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON `
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON `
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON `
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON `
-Bcmake-out
cmake --build cmake-out --target install --config Release -j $env:NUMBER_OF_PROCESSORS
```

To additionally build with LLM extension support, append `-DEXECUTORCH_BUILD_EXTENSION_LLM=ON -DEXECUTORCH_BUILD_EXTENSION_LLM_RUNNER=ON` to the configure step.

#### Build Python Package with Pybindings

Compiles and installs the ExecuTorch Python package with the OpenVINO backend into your Python environment, enabling python bindings required to execute OpenVINO backend tests and `aot_optimize_and_infer.py` script inside `executorch/examples/openvino` folder.

**Linux:**
```bash
pip install -r backends/openvino/requirements.txt
CMAKE_ARGS="-DEXECUTORCH_BUILD_OPENVINO=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON" \
CMAKE_BUILD_ARGS="--target openvino_backend" \
./install_executorch.sh --use-pt-pinned-commit
```
- On Linux, `backends/openvino/scripts/openvino_build.sh` can be used as a convenience wrapper with `--enable_python`, `--cpp_runtime`, and `--cpp_runtime_llm` options instead of running the above commands directly.

**Windows (PowerShell):**
```powershell
pip install -r backends/openvino/requirements.txt
$env:CMAKE_ARGS = "-DEXECUTORCH_BUILD_OPENVINO=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON"
$env:CMAKE_BUILD_ARGS = "--target openvino_backend"
.\install_executorch.bat --use-pt-pinned-commit
```



For more information about ExecuTorch environment setup, refer to the [Environment Setup](https://pytorch.org/executorch/main/getting-started-setup#environment-setup) guide.

## Runtime Setup

OpenVINO is a runtime-only dependency — it is not required at build time. The backend discovers and loads the OpenVINO C library dynamically when first used. You can provide the library via pip (recommended) or a manual install.

Before you begin, ensure you have openvino installed and configured on your system.
### Install via pip (Recommended)

```bash
pip install openvino
```

### Use OpenVINO from Release Packages

1. Download the OpenVINO release package from [here](https://docs.openvino.ai/2025/get-started/install-openvino.html). Make sure to select your configuration and click on **OpenVINO Archives** under the distribution section to download the appropriate archive for your platform.

2. Extract the release package from the archive and set the environment variables.

**Linux:**
```bash
tar -zxf openvino_toolkit_<your_release_configuration>.tgz
cd openvino_toolkit_<your_release_configuration>
source setupvars.sh
```

**Windows (PowerShell):**
```powershell
Expand-Archive openvino_toolkit_<your_release_configuration>.zip
cd openvino_toolkit_<your_release_configuration>
.\setupvars.ps1
```

### (Optional) Build OpenVINO from Source

**Linux:**
```bash
git clone https://github.com/openvinotoolkit/openvino.git
cd openvino
Expand All @@ -103,46 +228,23 @@ cd <your_preferred_install_location>
source setupvars.sh
```

For more information about OpenVINO build, refer to the [OpenVINO Build Instructions](https://github.com/openvinotoolkit/openvino/blob/master/docs/dev/build_linux.md).

### Setup

Follow the steps below to setup your build environment:


1. **Create a Virtual Environment**
- Create a virtual environment and activate it by executing the commands below.
```bash
python -m venv env
source env/bin/activate
```
2. **Clone ExecuTorch Repository from Github**
- Clone Executorch repository by executing the command below.
```bash
git clone --recurse-submodules https://github.com/pytorch/executorch.git
```
3. **Build ExecuTorch with OpenVINO Backend**
- Ensure that you are inside `executorch/backends/openvino/scripts` directory. The following command builds and installs ExecuTorch with the OpenVINO backend, also compiles the C++ runtime libraries and binaries into `<executorch_root>/cmake-out` for quick inference testing.
```bash
openvino_build.sh
```
- Optionally, `openvino_build.sh` script can be used to build python package or C++ libraries/binaries seperately.
**Windows (PowerShell):**
```powershell
git clone https://github.com/openvinotoolkit/openvino.git
cd openvino
git submodule update --init --recursive
mkdir build; cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_PYTHON=ON
cmake --build . --config Release -j $env:NUMBER_OF_PROCESSORS

**Build OpenVINO Backend Python Package with Pybindings**: To build and install the OpenVINO backend Python package with Python bindings, run the `openvino_build.sh` script with the `--enable_python` argument as shown in the below command. This will compile and install the ExecuTorch Python package with the OpenVINO backend into your Python environment. This option will also enable python bindings required to execute OpenVINO backend tests and `aot_optimize_and_infer.py` script inside `executorch/examples/openvino` folder.
```bash
./openvino_build.sh --enable_python
```
**Build C++ Runtime Libraries for OpenVINO Backend**: Run the `openvino_build.sh` script with the `--cpp_runtime` flag to build the C++ runtime libraries as shown in the below command. The compiled libraries files and binaries can be found in the `<executorch_root>/cmake-out` directory. The binary located at `<executorch_root>/cmake-out/executor_runner` can be used to run inference with vision models.
```bash
./openvino_build.sh --cpp_runtime
```
**Build C++ Runtime Libraries with LLM Extension**: Run the `openvino_build.sh` script with the `--cpp_runtime_llm` flag to build the C++ runtime libraries with LLM extension as shown in the below command. Use this option instead of `--cpp_runtime` for LLM extension support which is required by LLM examples.
```bash
./openvino_build.sh --cpp_runtime_llm
```
cd ..
cmake --install build --prefix <your_preferred_install_location>
cd <your_preferred_install_location>
.\setupvars.ps1
```

For more information about ExecuTorch environment setup, refer to the [Environment Setup](https://pytorch.org/executorch/main/getting-started-setup#environment-setup) guide.
For more information about OpenVINO build, refer to the [OpenVINO Build Instructions](https://github.com/openvinotoolkit/openvino/blob/master/docs/dev/build_linux.md).

### Run
### Examples

Please refer to [README.md](../../examples/openvino/README.md) for instructions on running examples of various of models with openvino backend.
Please refer to [README.md](../../examples/openvino/README.md) for instructions on running examples of various models with the OpenVINO backend.
8 changes: 8 additions & 0 deletions backends/openvino/runtime/OpenvinoApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

#pragma once

#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <cstddef>
#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -99,7 +103,11 @@ using ov_shape_free_fn = ov_status_e (*)(ov_shape_t*);
struct DlCloser {
void operator()(void* handle) {
if (handle) {
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle));
#else
dlclose(handle);
#endif
}
}
};
Expand Down
31 changes: 31 additions & 0 deletions backends/openvino/runtime/OpenvinoBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,34 @@ namespace openvino {

namespace {

#ifdef _WIN32
constexpr const char* kDefaultLibName = "openvino_c.dll";
#else
constexpr const char* kDefaultLibName = "libopenvino_c.so";
#endif

template <typename FuncPtr>
FuncPtr load_symbol(void* handle, const char* name) {
#ifdef _WIN32
void* sym = reinterpret_cast<void*>(
GetProcAddress(static_cast<HMODULE>(handle), name));
if (!sym) {
ET_LOG(
Error,
"OpenVINO: failed to resolve symbol '%s': error %lu",
name,
GetLastError());
return nullptr;
}
#else
dlerror(); // Clear any stale error state.
void* sym = dlsym(handle, name);
const char* err = dlerror();
if (err) {
ET_LOG(Error, "OpenVINO: failed to resolve symbol '%s': %s", name, err);
return nullptr;
}
#endif
return reinterpret_cast<FuncPtr>(sym);
}

Expand All @@ -47,6 +64,19 @@ bool OpenvinoBackend::ensure_loaded() const {
const char* lib_path = std::getenv("OPENVINO_LIB_PATH");
const char* effective_path = lib_path ? lib_path : kDefaultLibName;

#ifdef _WIN32
void* handle = static_cast<void*>(LoadLibrary(effective_path));
if (!handle) {
ET_LOG(
Error,
"OpenVINO runtime not found (LoadLibrary failed: error %lu). "
"Set OPENVINO_LIB_PATH to the full path of 'openvino_c.dll', "
"or add its containing directory to PATH, or install with: "
"pip install \"openvino>=2025.1.0,<2026.0.0\"",
GetLastError());
return;
}
#else
void* handle = dlopen(effective_path, RTLD_NOW | RTLD_LOCAL);
if (!handle) {
ET_LOG(
Expand All @@ -58,6 +88,7 @@ bool OpenvinoBackend::ensure_loaded() const {
dlerror());
return;
}
#endif
lib_handle_.reset(handle);

#define LOAD_SYM(field, symbol_name) \
Expand Down
Loading
Loading