diff --git a/source/cmake/Config.cmake.in b/source/cmake/Config.cmake.in index f457e43ce4..c8b3c754c1 100644 --- a/source/cmake/Config.cmake.in +++ b/source/cmake/Config.cmake.in @@ -1,5 +1,10 @@ @PACKAGE_INIT@ include(CMakeFindDependencyMacro) +set(@CMAKE_PROJECT_NAME@_ENABLE_PYTORCH "@ENABLE_PYTORCH@") +set(@CMAKE_PROJECT_NAME@_TORCH_DIR "@Torch_DIR@") +set(@CMAKE_PROJECT_NAME@_TORCH_VERSION "@Torch_VERSION@") +set(@CMAKE_PROJECT_NAME@_TORCH_CXX11_ABI "@OP_CXX_ABI_PT@") + include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") check_required_components("@CMAKE_PROJECT_NAME@") diff --git a/source/lmp/builtin.cmake b/source/lmp/builtin.cmake index e051e5c24a..5eecd32c11 100644 --- a/source/lmp/builtin.cmake +++ b/source/lmp/builtin.cmake @@ -58,6 +58,92 @@ configure_file("${CMAKE_CURRENT_LIST_DIR}/deepmd_version.h.in" file(GLOB DEEPMD_LMP_SRC ${CMAKE_CURRENT_LIST_DIR}/*.cpp) find_package(DeePMD REQUIRED) + +function(_deepmd_lammps_link_torch target_name) + if(NOT DeePMD_ENABLE_PYTORCH) + return() + endif() + + set(_deepmd_torch_from_recorded_dir FALSE) + if(Torch_FOUND) + # A parent project may already have loaded Torch. Validate it below rather + # than attempting to redefine its imported targets from another package. + elseif(DeePMD_TORCH_DIR) + find_package( + Torch + CONFIG + QUIET + PATHS "${DeePMD_TORCH_DIR}" + NO_DEFAULT_PATH) + if(Torch_FOUND) + set(_deepmd_torch_from_recorded_dir TRUE) + endif() + endif() + + if(NOT Torch_FOUND) + # The recorded package may have moved or been removed. A caller-provided + # installation is acceptable only after the compatibility checks below. + unset(Torch_DIR CACHE) + unset(Torch_DIR) + find_package(Torch CONFIG QUIET) + endif() + + if(NOT Torch_FOUND) + message( + WARNING + "DeePMD-kit was built with the PyTorch backend, but Torch was not " + "found while configuring LAMMPS. Install the same Torch package or " + "set Torch_DIR to its CMake configuration directory.") + return() + endif() + + string(REGEX MATCH "_GLIBCXX_USE_CXX11_ABI=([0-9]+)" + _deepmd_torch_abi_match "${TORCH_CXX_FLAGS}") + set(_deepmd_torch_abi "") + if(_deepmd_torch_abi_match) + set(_deepmd_torch_abi "${CMAKE_MATCH_1}") + elseif(UNIX AND NOT APPLE AND Torch_VERSION VERSION_GREATER_EQUAL "2.8.0") + # Recent Linux Torch packages no longer publish the ABI in + # TORCH_CXX_FLAGS and use the C++11 ABI unconditionally. + set(_deepmd_torch_abi "1") + else() + # Match DeePMD's build-time fallback for platforms and older packages that + # do not expose a libstdc++ ABI flag. + set(_deepmd_torch_abi "0") + endif() + + if(NOT _deepmd_torch_from_recorded_dir + AND ("${DeePMD_TORCH_VERSION}" STREQUAL "" + OR "${DeePMD_TORCH_CXX11_ABI}" STREQUAL "")) + message( + WARNING + "DeePMD-kit did not record enough Torch compatibility metadata to " + "validate a different installation; refusing to link it into LAMMPS.") + return() + endif() + if(NOT "${DeePMD_TORCH_VERSION}" STREQUAL "" + AND NOT "${Torch_VERSION}" STREQUAL "${DeePMD_TORCH_VERSION}") + message( + FATAL_ERROR + "Torch version mismatch: DeePMD-kit was built with " + "${DeePMD_TORCH_VERSION}, but LAMMPS found ${Torch_VERSION}.") + endif() + if(NOT "${DeePMD_TORCH_CXX11_ABI}" STREQUAL "" + AND NOT "${_deepmd_torch_abi}" STREQUAL "${DeePMD_TORCH_CXX11_ABI}") + message( + FATAL_ERROR + "Torch C++ ABI mismatch: DeePMD-kit was built with " + "_GLIBCXX_USE_CXX11_ABI=${DeePMD_TORCH_CXX11_ABI}, but LAMMPS found " + "${_deepmd_torch_abi}.") + endif() + + # LAMMPS exports this target as LAMMPS::lammps. Torch is needed to resolve + # the in-tree executable's transitive DeePMD symbols, but must not leak + # imported targets or absolute paths into the installed LAMMPS interface. + target_link_libraries(${target_name} + PUBLIC "$") +endfunction() + target_sources( lammps PRIVATE ${DEEPMD_LMP_SRC} @@ -69,6 +155,7 @@ target_sources( ${LAMMPS_SOURCE_DIR}/EXTRA-FIX/fix_ttm.cpp # for ttm ) target_link_libraries(lammps PUBLIC DeePMD::deepmd_c) +_deepmd_lammps_link_torch(lammps) target_include_directories( lammps PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR} ${LAMMPS_SOURCE_DIR}/KSPACE ${LAMMPS_SOURCE_DIR}/EXTRA-FIX)