NOTE: Iree's Python API is currently being reworked. Some of these instructions may be in a state of flux as they document the end state.
IREE has two primary Python APIs:
pyiree.compiler2
, pyiree.compiler2.tf
pyiree.tf
There are additional ancillary modules that are not part of the public API.
You should already have IREE cloned and building on your machine. See the other getting started guides for instructions.
Note:
Support is only complete with CMake.
Minimally, the following CMake flags must be specified:
-DIREE_BUILD_PYTHON_BINDINGS=ON
-DIREE_BUILD_TENSORFLOW_COMPILER=ON
: Optional. Also builds the TensorFlow compiler integration.If building any parts of TensorFlow, you must have a working bazel
command on your path. See the relevant “OS with Bazel” getting started doc for more information.
Install Python 3 >= 3.6
and pip, if needed.
Note:
If usingpyenv
(or an interpreter manager that depends on it likeasdf
), you'll need to use--enable-shared
during interpreter installation.
(Recommended) Setup a virtual environment with venv
(or your preferred mechanism):
# Note that venv is only available in python3 and is therefore a good check # that you are in fact running a python3 binary. $ python -m venv .venv $ source .venv/bin/activate # When done: run 'deactivate'
Install packages:
$ python -m pip install --upgrade pip $ python -m pip install numpy absl-py # If using the TensorFlow integration $ python -m pip install tf-nightly
From the parent directory of the IREE git repository clone, create and enter a build directory, such as:
$ mkdir iree-build $ cd iree-build
Then build like this:
# Also include -DIREE_BUILD_TENSORFLOW_COMPILER=ON if you want the TF compiler. $ cmake ../iree -G Ninja \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ -DIREE_BUILD_PYTHON_BINDINGS=ON . $ cmake --build .
We continue to assume that we are in the build directory where we made the build in the previous section.
To run tests for core Python bindings built with CMake:
$ ctest -L bindings/python
To run tests for the TensorFlow integration, which include end-to-end backend comparison tests:
# TODO: Revisit once more patches land. $ ctest -L integrations/tensorflow/e2e # Or run individually as: $ export PYTHONPATH=$(pwd)/bindings/python # This is a Python 3 program. On some systems, such as Debian derivatives, # use 'python3' instead of 'python'. $ python ../iree/integrations/tensorflow/e2e/simple_arithmetic_test.py \ --target_backends=iree_vmla --artifacts_dir=/tmp/artifacts
There are some sample colabs in the colab
folder. If you have built the project with CMake/ninja and set your PYTHONPATH
to the bindings/python
directory in the build dir (or installed per below), you should be able to start a kernel by following the stock instructions at https://colab.research.google.com/ .
There is a setup.py
in the bindings/python
directory under the build dir. To install into your (hopefully isolated) virtual env:
# See the above note about python3, and the above step setting PYTHONPATH. python bindings/python/setup.py install
To create wheels (platform dependent and locked to your Python version without further config):
python bindings/python/setup.py bdist_wheel
Note that it is often helpful to differentiate between the environment used to build and the one used to install. While this is just “normal” python knowledge, here is an incantation to do so:
# From parent/build environment. python -m pip freeze > /tmp/requirements.txt deactivate # If already in an environment # Enter new scratch environment. python -m venv ./.venv-scratch source ./.venv-scratch/bin/activate python -m pip install -r /tmp/requirements.txt # Install IREE into the new environment. python bindings/python/setup.py install