|
1 | 1 | # Halide Bindings for Python |
2 | 2 |
|
3 | 3 | <!-- TOC --> |
| 4 | + |
4 | 5 | * [Halide Bindings for Python](#halide-bindings-for-python) |
5 | 6 | * [Acquiring the Python bindings](#acquiring-the-python-bindings) |
6 | 7 | * [Building the Python bindings](#building-the-python-bindings) |
|
29 | 30 | * [Notable Differences Between C++ and Python Generators](#notable-differences-between-c-and-python-generators) |
30 | 31 | * [Keeping Up To Date](#keeping-up-to-date) |
31 | 32 | * [License](#license) |
| 33 | + |
32 | 34 | <!-- TOC --> |
33 | 35 |
|
34 | 36 | Halide provides Python bindings for most of its public API. Python 3.10 (or |
@@ -72,75 +74,49 @@ bindings require Halide to be built with RTTI and exceptions **enabled**, which |
72 | 74 | in turn requires LLVM to be built with RTTI, but this is not the default for |
73 | 75 | LLVM. |
74 | 76 |
|
75 | | -### Using CMake directly |
76 | | - |
77 | 77 | Before configuring with CMake, you should ensure you have prerequisite packages |
78 | 78 | installed in your local Python environment. The best way to get set up is to use |
79 | | -a virtual environment: |
| 79 | +a virtual environment with `uv`: |
80 | 80 |
|
81 | 81 | ```shell |
82 | | -$ python3 -m venv venv |
83 | | -$ . venv/bin/activate |
84 | | -$ python3 -m pip install -U pip "setuptools[core]" wheel |
85 | | -$ python3 -m pip install -r requirements.txt |
| 82 | +$ uv sync --no-install-project |
86 | 83 | ``` |
87 | 84 |
|
88 | | -Or, if using `uv` the following command is equivalent: |
89 | | - |
90 | | -```shell |
91 | | -uv venv --python 3.12 # for example |
92 | | -uv pip install -r requirements.txt |
93 | | -``` |
| 85 | +If you don't have LLVM installed already, you can try using the same ones the |
| 86 | +buildbots use by adding `--group ci-llvm-<VERSION>` to the `uv sync` command, |
| 87 | +where `<VERSION>` is the LLVM major version number (e.g. `23`) or `main`. |
94 | 88 |
|
95 | | -Then build and install Halide: |
| 89 | +If you install `ci-llvm-*`, you can set `Halide_LLVM_ROOT=$(halide-llvm |
| 90 | +--prefix)` in your environment. |
96 | 91 |
|
97 | | -```shell |
98 | | -$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release |
99 | | -$ cmake --build build |
100 | | -$ cmake --install build --prefix .local |
101 | | -``` |
102 | | - |
103 | | -Now you can set the `PYTHONPATH` variable to point to the freshly built Python |
104 | | -package: |
105 | | - |
106 | | -```shell |
107 | | -$ export PYTHONPATH="$PWD/.local/lib/python3/site-packages" |
108 | | -``` |
| 92 | +Ensure you have `flatbuffers` and `wabt` installed, too. |
109 | 93 |
|
110 | 94 | ### Using wheel infrastructure |
111 | 95 |
|
112 | | -You can also follow the same procedure that we use to build the published |
113 | | -wheels. First, create a virtual environment as before, but omit |
114 | | -`requirements.txt` |
| 96 | +When using `uv`, this entire workflow can be run via: |
115 | 97 |
|
116 | 98 | ```shell |
117 | | -$ python3 -m venv venv |
118 | | -$ . venv/bin/activate |
119 | | -$ python3 -m pip install -U pip "setuptools[core]" wheel |
| 99 | +$ uv pip install . --no-build-isolation |
120 | 100 | ``` |
121 | 101 |
|
122 | | -Next, ensure you have installed Halide's dependencies to locations where CMake |
123 | | -can find them, given your environment. The variables `Halide_LLVM_ROOT`, |
124 | | -`flatbuffers_ROOT`, and `wabt_ROOT` specify locations for the relevant packages |
125 | | -directly. If they are all installed to a common prefix, you can add it to the |
126 | | -environment variable `CMAKE_PREFIX_PATH`. |
| 102 | +### Using CMake directly |
127 | 103 |
|
128 | | -Then it should be as simple as: |
| 104 | +Assuming dependencies are available, you can build the Python bindings directly |
| 105 | +with CMake: |
129 | 106 |
|
130 | 107 | ```shell |
131 | | -$ pip install . |
| 108 | +$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release |
| 109 | +$ cmake --build build |
| 110 | +$ cmake --install build --prefix .local |
132 | 111 | ``` |
133 | 112 |
|
134 | | -When using `uv`, this entire workflow can be run via |
| 113 | +Now you can set the `PYTHONPATH` variable to point to the freshly built Python |
| 114 | +package: |
135 | 115 |
|
136 | 116 | ```shell |
137 | | -$ uv sync |
| 117 | +$ export PYTHONPATH="$PWD/.local/lib/python3/site-packages" |
138 | 118 | ``` |
139 | 119 |
|
140 | | -If you need a different Python version, you can add `--python 3.xy` to this |
141 | | -command. If you wish to install compatible build tools into the venv (e.g. |
142 | | -CMake, ninja), rather than use your system tools, pass `--group tools` here. |
143 | | - |
144 | 120 | ## Documentation and Examples |
145 | 121 |
|
146 | 122 | As mentioned elsewhere, the Python API attempts to mimic the |
@@ -538,9 +514,9 @@ add_halide_python_extension_library(my_extension |
538 | 514 | This compiles the Generator code in `logical_op_generator.py` with the |
539 | 515 | registered name `logical_op_generator` to produce the target `xor_filter`, and |
540 | 516 | then wraps the compiled output with a Python extension. The result will be a |
541 | | -shared library of the form `<target>.<soabi>.so`, where `<soabi>` describes |
542 | | -the specific Python version and platform (e.g., `cpython-310-darwin` for |
543 | | -Python 3.10 on OSX.) |
| 517 | +shared library of the form `<target>.<soabi>.so`, where `<soabi>` describes the |
| 518 | +specific Python version and platform (e.g., `cpython-310-darwin` for Python 3.10 |
| 519 | +on OSX.) |
544 | 520 |
|
545 | 521 | Note that you can combine multiple Halide libraries into a single Python module; |
546 | 522 | this is convenient for packaging, but also because all the libraries in a single |
|
0 commit comments