Skip to content

[Code scan] Python builds should discover Torch CMake prefix when BUILD_PY_IF is true #142

Description

@njzjz

This issue was found by a Codex global scan of the repository at commit 19f9265.

pyproject.toml configures scikit-build to build the Python interface only:

[tool.scikit-build.cmake.define]
BUILD_PY_IF = true
BUILD_CPP_IF = false

However, the CMake logic that queries torch.utils.cmake_prefix_path only runs for selected C++ builds or CIBUILDWHEEL=1:

deepmd-gnn/CMakeLists.txt

Lines 85 to 109 in 19f9265

if(BUILD_CPP_IF
AND USE_PT_PYTHON_LIBS
AND NOT CMAKE_CROSSCOMPILING
AND NOT SKBUILD
OR "$ENV{CIBUILDWHEEL}" STREQUAL "1")
find_package(
Python
COMPONENTS Interpreter
REQUIRED)
execute_process(
COMMAND ${Python_EXECUTABLE} -c
"import torch;print(torch.utils.cmake_prefix_path)"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE PYTORCH_CMAKE_PREFIX_PATH
RESULT_VARIABLE PYTORCH_CMAKE_PREFIX_PATH_RESULT_VAR
ERROR_VARIABLE PYTORCH_CMAKE_PREFIX_PATH_ERROR_VAR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT ${PYTORCH_CMAKE_PREFIX_PATH_RESULT_VAR} EQUAL 0)
message(
FATAL_ERROR
"Cannot determine PyTorch CMake prefix path, error code: $PYTORCH_CMAKE_PREFIX_PATH_RESULT_VAR}, error message: ${PYTORCH_CMAKE_PREFIX_PATH_ERROR_VAR}"
)
endif()
list(APPEND CMAKE_PREFIX_PATH ${PYTORCH_CMAKE_PREFIX_PATH})
endif()

find_package(Torch REQUIRED) then runs unconditionally:

deepmd-gnn/CMakeLists.txt

Lines 120 to 124 in 19f9265

find_package(Torch REQUIRED)
if(Torch_VERSION VERSION_LESS "2.10.0")
message(FATAL_ERROR "deepmd-gnn OP requires PyTorch >= 2.10.0 for the "
"LibTorch Stable ABI.")
endif()

A normal local configure without user-supplied CMAKE_PREFIX_PATH fails with:

Could not find a package configuration file provided by "Torch"

Suggested fix: when BUILD_PY_IF is true and Torch_DIR/CMAKE_PREFIX_PATH is not already sufficient, query torch.utils.cmake_prefix_path using the build Python interpreter before find_package(Torch REQUIRED).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions