Python bindings and importers¶
These components are more complicated to build from source than the rest of the project. If your usage does not require making source changes, prefer to install from the official binary distributions instead.
This page covers how to build IREE's Python-based bindings and import tools from source. These components are built using CMake as well as other dependencies and each section extends the basic build steps in the getting started page.
Building Python bindings¶
This section describes how to build and interactively use built-from-source Python bindings for the following packages:
||IREE's generic compiler tools and helpers|
||IREE's runtime, including CPU and GPU backends|
- A relatively recent Python3 installation >=3.7 (we aim to support non-eol Python versions).
- Installation of python dependencies as specified in
Enables building of Python bindings under
bindings/pythonin the repository. Defaults to
Full path to the Python3 executable to build against. If not specified, CMake will auto-detect this, which often yields incorrect results on systems with multiple Python versions. Explicitly setting this is recommended. Note that mixed case of the option.
# Make sure your 'python' is what you expect. Note that on multi-python # systems, this may have a version suffix, and on many Linuxes and MacOS where # python2 and python3 co-exist, you may also want to use `python3`. which python python --version # Create a persistent virtual environment (first time only). python -m venv iree.venv # Activate the virtual environment (per shell). # Now the `python` command will resolve to your virtual environment # (even on systems where you typically use `python3`). source iree.venv/bin/activate # Upgrade PIP. On Linux, many packages cannot be installed for older # PIP versions. See: https://github.com/pypa/manylinux python -m pip install --upgrade pip # Install IREE build pre-requisites. python -m pip install -r ./bindings/python/build_requirements.txt
# Create a persistent virtual environment (first time only). python -m venv .venv # Activate the virtual environment (per shell). # Now the `python` command will resolve to your virtual environment # (even on systems where you typically use `python3`). .venv\Scripts\activate.bat # Upgrade PIP. python -m pip install --upgrade pip # Install IREE build pre-requisites. python -m pip install -r bindings\python\build_requirements.txt
When you are done with the venv, you can close it by closing your shell
Building with CMake¶
cmake \ -GNinja \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DIREE_BUILD_PYTHON_BINDINGS=ON \ -DPython3_EXECUTABLE="$(which python)" \ . cmake --build . # Add ./bindings/python and compiler-api/python_package to PYTHONPATH and # use the API. source .env && export PYTHONPATH export PYTHONPATH="$PWD/bindings/python" python -c "import iree.compiler" python -c "import iree.runtime"
cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DIREE_BUILD_PYTHON_BINDINGS=ON . cmake --build . # Add bindings\python and compiler-api\python_package to PYTHONPATH and use # the API. set PYTHONPATH="$pwd\compiler-api\python_package;$pwd\bindings\python;%PYTHONPATH%" python -c "import iree.compiler" python -c "import iree.runtime"
Tests can now be run individually via python or via ctest.
Building TensorFlow frontend bindings¶
This section describes how to build compiler tools and Python bindings for importing models from various TensorFlow-ecosystem frontends, including TensorFlow, XLA (used for JAX), and TFLite. It extends the instructions in Building Python Bindings above with additional steps that are TensorFlow specific. There are various ways to achieve these ends, but this section describes the canonical method that the core developers recommend. Upon completing these steps, you will have access to additional Python packages:
||Tools for importing from TensorFlow|
||Tools for importing from TensorFlow Lite|
||Tools for importing from XLA|
These tools packages are needed in order for the frontend specific, high-level
import iree.compiler.xla, and
import iree.jax to be fully functional.
This section is under construction. Refer to the source documentation for the latest building from source instructions.
Due to the difficulties using Bazel and compiling TensorFlow, only compilation on Linux with clang is supported. Other OS's and compilers are "best effort" with patches to improve support welcome.