Differences from C++ API
The Python bindings attempt to mimic the Halide C++ API as closely as possible, with some differences where the C++ idiom is either inappropriate or impossible:
- Most APIs that take a variadic argumentlist of ints in C++ take an explicit list in Python. For instance, the usual version of the
Bufferctor in C++ offers variadic and list versions:
Buffer<>(Type t, int extent_dim_0, int extent_dim_1, ...., extent_dim_N, string name = "") Buffer<>(Type t, vector<int> extents, string name = "")
in Python, only the second variant is provided.
Bufferaccess is done using
- Some classes in the Halide API aren't provided because they are 'wrapped' with standard Python idioms:
Halide::Tupledoesn't exist in the Python bindings; an ordinary Python tuple of
Halide::Expris used instead.
Halide::Realizationdoesn't exist in the Python bindings; an ordinary Python tuple of
Halide::Bufferis used instead.
Halide::Errorand friends don't exist; standard Python error handling is used instead.
- static and instance method overloads with the same name in the same class aren't allowed, so some convenience methods are missing from
- Templated types (notably
Halide::Param<>) aren't provided, for obvious reasons; only the equivalents of
- Only things in the
Halidenamespace are supported; classes and methods that involve using the
Halide::Internalnamespace are not provided.
- The functions in
Halide::ConciseCastsare present in the toplevel Halide module in Python, rather than a submodule: e.g., use
- No mechanism is provided for overriding any runtime functions from Python.
- No mechanism is provided for supporting
Buffer::for_each_value()is hard to implement well in Python; it's omitted entirely for now.
Enhancements to the C++ API
Buffersupports the Python Buffer Protocol (https://www.python.org/dev/peps/pep-3118/) and thus is easily and cheaply converted to and from other compatible objects (e.g., NumPy's
ndarray), with storage being shared.
The bindings (and demonstration applications) should work well both for python2.7 and python3.4 (or higher), on Linux and OSX platforms. Windows is not yet supported, but could be with CMake work. (The Makefile defaults to using Python 3.x; to use Python 2, set
PYTHON = python before building.)
See requirements.txt (to be used with
pip install --user requirements.txt)
- Halide compiled to a distribution (e.g.
make distribor similar), with the
HALIDE_DISTRIB_PATHenv var pointing to it
- The PyBind11 package (https://github.com/pybind/pybind11), v2.2.1 or later, with the
PYBIND11_PATHenv var pointing to it
Documentation and Examples
The Python API reflects directly the C++ Halide API.
Check out the code for the example applications in the
You can run them as a batch via
make test_apps or
To run these examples, make sure the
PYTHONPATH environment variable points to your build directory (e.g.
The Python bindings use the same MIT license as Halide.
Python bindings provided by Connelly Barnes (2012-2013), Fred Rotbart (2014), Rodrigo Benenson (2015) and the Halide open-source community.