Skip to content

Commit 4e7f204

Browse files
authored
Update docs and .env file to reflect PYTHONPATH variability (#4332)
This updates the docs and `build_tools/write_env_file.sh` to correctly identify the `PYTHONPATH` based on in-tree vs. out-of-tree builds. Additionally, I've updated various build configure commands to include a directive to align `Python` with `Python3`, since having conflicting versions of python can cause issues when not explicitly setting both to the same python executable. Resolves #4331 --------- Signed-off-by: zjgarvey <[email protected]>
1 parent 7e1cd8b commit 4e7f204

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

build_tools/ci/build_posix.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ cmake -S "$repo_root/externals/llvm-project/llvm" -B "$build_dir" \
4141
-GNinja \
4242
-DCMAKE_BUILD_TYPE=Release \
4343
-DPython3_EXECUTABLE="$(which python3)" \
44+
-DPython_EXECUTABLE="$(which python3)" \
4445
-DLLVM_ENABLE_ASSERTIONS=ON \
4546
-DTORCH_MLIR_ENABLE_WERROR_FLAG=ON \
4647
-DCMAKE_INSTALL_PREFIX="$install_dir" \

build_tools/python_deploy/build_linux_packages.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ function build_in_tree() {
244244
-DTORCH_MLIR_SRC_PYTORCH_BRANCH=${TORCH_MLIR_SRC_PYTORCH_BRANCH} \
245245
-DTM_PYTORCH_INSTALL_WITHOUT_REBUILD=${TM_PYTORCH_INSTALL_WITHOUT_REBUILD} \
246246
-DPython3_EXECUTABLE="$(which python3)" \
247+
-DPython_EXECUTABLE="$(which python3)" \
247248
/main_checkout/torch-mlir/externals/llvm-project/llvm
248249
cmake --build /main_checkout/torch-mlir/build --target tools/torch-mlir/all
249250
ccache -s
@@ -387,6 +388,7 @@ function build_out_of_tree() {
387388
-DLLVM_TARGETS_TO_BUILD=host \
388389
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
389390
-DPython3_EXECUTABLE="$(which python3)" \
391+
-DPython_EXECUTABLE="$(which python3)" \
390392
/main_checkout/torch-mlir/externals/llvm-project/llvm
391393
cmake --build /main_checkout/torch-mlir/llvm-build
392394
fi
@@ -409,6 +411,7 @@ function build_out_of_tree() {
409411
-DTORCH_MLIR_SRC_PYTORCH_BRANCH=${TORCH_MLIR_SRC_PYTORCH_BRANCH} \
410412
-DTM_PYTORCH_INSTALL_WITHOUT_REBUILD=${TM_PYTORCH_INSTALL_WITHOUT_REBUILD} \
411413
-DPython3_EXECUTABLE="$(which python3)" \
414+
-DPython_EXECUTABLE="$(which python3)" \
412415
/main_checkout/torch-mlir
413416
cmake --build /main_checkout/torch-mlir/build_oot
414417
ccache -s

build_tools/write_env_file.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ portable_realpath() {
1313

1414
td="$(portable_realpath "$(dirname "$0")"/..)"
1515
build_dir="$(portable_realpath "${TORCH_MLIR_BUILD_DIR:-$td/build}")"
16-
python_packages_dir="$build_dir/python_packages"
16+
17+
in_tree_pkg_dir="${build_dir}/tools/torch-mlir/python_packages"
18+
out_of_tree_pkg_dir="${build_dir}/python_packages"
19+
20+
if [[ ! -d "${in_tree_pkg_dir}" && ! -d "${out_of_tree_pkg_dir}" ]]; then
21+
echo "Couldn't find in-tree or out-of-tree build, exiting."
22+
exit 1
23+
fi
24+
25+
# The `-nt` check works even if one of the two directories is missing.
26+
if [[ "${in_tree_pkg_dir}" -nt "${out_of_tree_pkg_dir}" ]]; then
27+
python_packages_dir="${in_tree_pkg_dir}"
28+
else
29+
python_packages_dir="${out_of_tree_pkg_dir}"
30+
fi
1731

1832
write_env_file() {
1933
echo "Updating $build_dir/.env file"

docs/development.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,30 +194,48 @@ TIP: add multiple target options to stack build phases
194194

195195
### Setup Python Environment to export the built Python packages
196196

197+
When CMake is configured with `-DMLIR_ENABLE_BINDINGS_PYTHON=ON`, the python packages will typically be located in either:
198+
199+
1. `./build/tools/torch-mlir/python_packages/` if doing an in-tree build.
200+
2. `./build/python_packages/` if doing an out-of-tree build.
201+
202+
For the following sections, let `python_pkg_dir` represent whichever of the above is relevant for your build setup. On Linux and macOS, you can run `./build_tools/write_env_file.sh` to generate a file `./.env` in your root source directory with the correct `PYTHONPATH`.
203+
197204
#### Linux and macOS
198205

206+
To get the base `PYTHONPATH`, run:
207+
199208
```shell
200-
export PYTHONPATH=`pwd`/build/python_packages/torch_mlir:`pwd`/test/python/fx_importer
209+
./build_tools/write_env_file.sh
210+
source ./.env && export PYTHONPATH
211+
```
212+
213+
To run fx_importer tests, you can append the following:
214+
215+
```
216+
export PYTHONPATH="${PYTHONPATH}":/test/python/fx_importer"
201217
```
202218
203219
#### Windows PowerShell
204220
221+
To get the base `PYTHONPATH`, identify your `python_pkg_dir` from above and set this variable in your environment:
222+
223+
```shell
224+
$env:PYTHONPATH = "<python_pkg_dir>/torch-mlir"
225+
```
226+
227+
To run fx_importer tests, you can append the following:
228+
205229
```shell
206-
$env:PYTHONPATH = "$PWD/build/tools/torch-mlir/python_packages/torch_mlir;$PWD/test/python/fx_importer"
230+
$env:PYTHONPATH += ";$PWD/test/python/fx_importer"
207231
```
208232
209233
### Testing MLIR output in various dialects
210234
211235
To test the MLIR output to torch dialect, you can use `test/python/fx_importer/basic_test.py`.
212236
213237
Make sure you have activated the virtualenv and set the `PYTHONPATH` above
214-
(if running on Windows, modify the environment variable as shown above):
215-
216-
```shell
217-
source mlir_venv/bin/activate
218-
export PYTHONPATH=`pwd`/build/tools/torch-mlir/python_packages/torch_mlir:`pwd`/test/python/fx_importer
219-
python test/python/fx_importer/basic_test.py
220-
```
238+
(if running on Windows, modify the environment variable as shown above).
221239
222240
This will display the basic example in TORCH dialect.
223241
@@ -226,10 +244,10 @@ using torchscript with the example `projects/pt1/examples/torchscript_resnet18_a
226244
This path doesn't give access to the current generation work that is being driven via the fx_importer
227245
and may lead to errors.
228246
229-
Same as above, but with different python path and example:
247+
The base `PYTHONPATH` should be set as above, then the example can be run with the following command (similar on Windows):
230248
231249
```shell
232-
export PYTHONPATH=`pwd`/build/tools/torch-mlir/python_packages/torch_mlir:`pwd`/projects/pt1/examples
250+
export PYTHONPATH="${PYTHONPATH}:$PWD/projects/pt1/examples"
233251
python projects/pt1/examples/torchscript_resnet18_all_output_types.py
234252
```
235253

0 commit comments

Comments
 (0)