| 1 | # reference to https://github.com/llvm/torch-mlir/blob/main/python/torch_mlir/dialects/torch/importer/jit_ir/cmake/modules/TorchMLIRPyTorch.cmake |
| 2 | |
| 3 | # PNNXProbeForPyTorchInstall |
| 4 | # Attempts to find a Torch installation and set the Torch_ROOT variable |
| 5 | # based on introspecting the python environment. This allows a subsequent |
| 6 | # call to find_package(Torch) to work. |
| 7 | function(PNNXProbeForPyTorchInstall) |
| 8 | if(Torch_ROOT) |
| 9 | message(STATUS "Using cached Torch root = ${Torch_ROOT}") |
| 10 | elseif(Torch_INSTALL_DIR) |
| 11 | message(STATUS "Using cached Torch install dir = ${Torch_INSTALL_DIR}") |
| 12 | set(Torch_DIR "${Torch_INSTALL_DIR}/share/cmake/Torch" CACHE STRING "Torch dir" FORCE) |
| 13 | else() |
| 14 | #find_package (Python3 COMPONENTS Interpreter Development) |
| 15 | find_package (Python3) |
| 16 | message(STATUS "Checking for PyTorch using ${Python3_EXECUTABLE} ...") |
| 17 | execute_process( |
| 18 | COMMAND "${Python3_EXECUTABLE}" |
| 19 | -c "import os;import torch;print(torch.utils.cmake_prefix_path, end='')" |
| 20 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 21 | RESULT_VARIABLE PYTORCH_STATUS |
| 22 | OUTPUT_VARIABLE PYTORCH_PACKAGE_DIR) |
| 23 | if(NOT PYTORCH_STATUS EQUAL "0") |
| 24 | message(STATUS "Unable to 'import torch' with ${Python3_EXECUTABLE} (fallback to explicit config)") |
| 25 | return() |
| 26 | endif() |
| 27 | message(STATUS "Found PyTorch installation at ${PYTORCH_PACKAGE_DIR}") |
| 28 | |
| 29 | set(Torch_ROOT "${PYTORCH_PACKAGE_DIR}" CACHE STRING |
| 30 | "Torch configure directory" FORCE) |
| 31 | endif() |
| 32 | endfunction() |