Skip to content

[mlir] Inconsistency when searching for Python3 and Python #126162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dbabokin opened this issue Feb 7, 2025 · 3 comments · Fixed by #126329
Closed

[mlir] Inconsistency when searching for Python3 and Python #126162

dbabokin opened this issue Feb 7, 2025 · 3 comments · Fixed by #126329
Labels
cmake Build system in general and CMake in particular mlir

Comments

@dbabokin
Copy link
Contributor

dbabokin commented Feb 7, 2025

I've just stepped into a trap caused by these lines of CMake code:

# It's a little silly to detect Python a second time, but nanobind's cmake
# code looks for Python_ not Python3_.
find_package(Python ${LLVM_MINIMUM_PYTHON_VERSION}
COMPONENTS Interpreter ${_python_development_component} REQUIRED)

I was building torch-mlir on my Macbook and I was following the official build guidelines. On my system I have Python3.13 and Python3.11, but I was running in VirtualEnv with Python3.11 enabled. And CMake behaved really weirdly. It looked for Python three times, every time it was looking for Python3 twice and once for Python (that's expected). While it consistently found Python3 with version 3.11, the Python was found as 3.11 at first, but then as 3.13 (really unexpected).

Then if I was rerunning cmake with exactly the same command line (which succeeded on the first pass), it crashed with the following message:

CMake Error at /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
  Could NOT find Python (missing: Development.Module) (found suitable version
  "3.11.11", minimum required is "3.8")
Call Stack (most recent call first):
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython/Support.cmake:4002 (find_package_handle_standard_args)
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython.cmake:631 (include)
  /Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/cmake/modules/MLIRDetectPythonEnv.cmake:27 (find_package)
  /Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/CMakeLists.txt:196 (mlir_configure_python_dev_packages)

Running cmake again was successful, one more time - failing.

Debugging the problem I found out that the root cause was that I passed -DPython3_FIND_VIRTUALENV=ONLY to CMake, but not -DPython_FIND_VIRTUALENV=ONLY!!!

So that resolved my problem and in this sense it was a user error. But I think it really makes sense to check that Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV are defined (or not defined) in the same way and warn user otherwise.

Tagging @makslevental who is the last one who touched find_package(Python ...) code.

@llvmbot llvmbot added the mlir label Feb 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/issue-subscribers-mlir

Author: Dmitry Babokin (dbabokin)

I've just stepped into a trap caused by these lines of CMake code: https://github.com/llvm/llvm-project/blob/b7279ed5b3ae3e7b0fd61e0f08c86fb1958f0b6f/mlir/cmake/modules/MLIRDetectPythonEnv.cmake#L25-L28

I was building torch-mlir on my Macbook and I was following the official build guidelines. On my system I have Python3.13 and Python3.11, but I was running in VirtualEnv with Python3.11 enabled. And CMake behaved really weirdly. It looked for Python three times, every time it was looking for Python3 twice and once for Python (that's expected). While it consistently found Python3 with version 3.11, the Python was found as 3.11 at first, but then as 3.13 (really unexpected).

Then if I was rerunning cmake with exactly the same command line (which succeeded on the first pass), it crashed with the following message:

CMake Error at /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
  Could NOT find Python (missing: Development.Module) (found suitable version
  "3.11.11", minimum required is "3.8")
Call Stack (most recent call first):
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython/Support.cmake:4002 (find_package_handle_standard_args)
  /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython.cmake:631 (include)
  /Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/cmake/modules/MLIRDetectPythonEnv.cmake:27 (find_package)
  /Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/CMakeLists.txt:196 (mlir_configure_python_dev_packages)

Running cmake again was successful, one more time - failing.

Debugging the problem I found out that the root cause was that I passed -DPython3_FIND_VIRTUALENV=ONLY to CMake, but not -DPython_FIND_VIRTUALENV=ONLY!!!

So that resolved my problem and in this sense it was a user error. But I think it really makes sense to check that Python3_FIND_VIRTUALENV and Python_FIND_VIRTUALENV are defined (or not defined) in the same way and warn user otherwise.

Tagging @makslevental who is the last one who touched find_package(Python ...) code.

@makslevental
Copy link
Contributor

This thing is a constant source of frustration (which is why that CMake section is already hairy enough). I don't think anyone has any urge to try to root cause it so personally I think layering more bandaids on it is absolutely fine (i.e., I'll take that PR if you send it).

@dbabokin
Copy link
Contributor Author

dbabokin commented Feb 8, 2025

I'll take that PR if you send it

See #126329

makslevental pushed a commit that referenced this issue Feb 18, 2025
…126329)

Fixes #126162

I check locally that it works without warning for:
- neither options are defined
- both defined to the same value

And I checked that it warns if:
- only one is defined
- they defined to different values
wldfngrs pushed a commit to wldfngrs/llvm-project that referenced this issue Feb 19, 2025
llvm#126329)

Fixes llvm#126162

I check locally that it works without warning for:
- neither options are defined
- both defined to the same value

And I checked that it warns if:
- only one is defined
- they defined to different values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular mlir
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants