From 02a1911a6a11836d354bc98cb968aa1aaeba0b83 Mon Sep 17 00:00:00 2001 From: "njzjz-bot (driven by OpenClaw (model: custom-chat-jinzhezeng-group/gpt-5.5))[bot]" <48687836+njzjz-bot@users.noreply.github.com> Date: Sun, 14 Jun 2026 18:28:19 +0000 Subject: [PATCH 1/2] fix(lmp): link torch for builtin shared builds Expose PyTorch backend metadata from the DeePMD CMake package and use it in the LAMMPS builtin integration to link Torch when DeePMD was built with the PyTorch backend. This resolves shared LAMMPS builds that fail to resolve torch/c10 symbols from libdeepmd_cc.so. Authored by OpenClaw (model: custom-chat-jinzhezeng-group/gpt-5.5) --- source/cmake/Config.cmake.in | 6 ++++++ source/lmp/builtin.cmake | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/source/cmake/Config.cmake.in b/source/cmake/Config.cmake.in index f457e43ce4..68eead5054 100644 --- a/source/cmake/Config.cmake.in +++ b/source/cmake/Config.cmake.in @@ -1,5 +1,11 @@ @PACKAGE_INIT@ include(CMakeFindDependencyMacro) +set(@CMAKE_PROJECT_NAME@_ENABLE_PYTORCH "@ENABLE_PYTORCH@") +set(@CMAKE_PROJECT_NAME@_USE_PT_PYTHON_LIBS "@USE_PT_PYTHON_LIBS@") +set(@CMAKE_PROJECT_NAME@_PYTORCH_CMAKE_PREFIX_PATH + "@PYTORCH_CMAKE_PREFIX_PATH@") +set(@CMAKE_PROJECT_NAME@_TORCH_LIBRARIES "@TORCH_LIBRARIES@") + 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..0b93a69474 100644 --- a/source/lmp/builtin.cmake +++ b/source/lmp/builtin.cmake @@ -58,6 +58,42 @@ 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() + + if(DeePMD_PYTORCH_CMAKE_PREFIX_PATH) + list(APPEND CMAKE_PREFIX_PATH ${DeePMD_PYTORCH_CMAKE_PREFIX_PATH}) + endif() + + find_package(Torch QUIET) + if(Torch_FOUND) + target_link_libraries(${target_name} PUBLIC ${TORCH_LIBRARIES}) + return() + endif() + + set(_deepmd_torch_libraries) + foreach(_deepmd_torch_library IN LISTS DeePMD_TORCH_LIBRARIES) + if(TARGET "${_deepmd_torch_library}" + OR EXISTS "${_deepmd_torch_library}" + OR "${_deepmd_torch_library}" MATCHES "^-") + list(APPEND _deepmd_torch_libraries "${_deepmd_torch_library}") + endif() + endforeach() + if(_deepmd_torch_libraries) + target_link_libraries(${target_name} PUBLIC ${_deepmd_torch_libraries}) + else() + message( + WARNING + "DeePMD-kit was built with the PyTorch backend, but Torch was not " + "found while configuring LAMMPS. Set CMAKE_PREFIX_PATH to PyTorch's " + "CMake prefix path if linking lmp reports unresolved torch/c10 " + "symbols.") + endif() +endfunction() + target_sources( lammps PRIVATE ${DEEPMD_LMP_SRC} @@ -69,6 +105,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) From 595c036475c6194dcafab8adbb7563e7d1cd909c Mon Sep 17 00:00:00 2001 From: njzjz-bot Date: Tue, 11 Aug 2026 03:02:42 +0800 Subject: [PATCH 2/2] fix(lmp): validate builtin Torch linkage Coding-Agent: Codex Codex-Version: codex-cli 0.144.6 Model: gpt-5.6-sol Reasoning-Effort: xhigh --- source/cmake/Config.cmake.in | 7 ++- source/lmp/builtin.cmake | 88 ++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/source/cmake/Config.cmake.in b/source/cmake/Config.cmake.in index 68eead5054..c8b3c754c1 100644 --- a/source/cmake/Config.cmake.in +++ b/source/cmake/Config.cmake.in @@ -2,10 +2,9 @@ include(CMakeFindDependencyMacro) set(@CMAKE_PROJECT_NAME@_ENABLE_PYTORCH "@ENABLE_PYTORCH@") -set(@CMAKE_PROJECT_NAME@_USE_PT_PYTHON_LIBS "@USE_PT_PYTHON_LIBS@") -set(@CMAKE_PROJECT_NAME@_PYTORCH_CMAKE_PREFIX_PATH - "@PYTORCH_CMAKE_PREFIX_PATH@") -set(@CMAKE_PROJECT_NAME@_TORCH_LIBRARIES "@TORCH_LIBRARIES@") +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 0b93a69474..5eecd32c11 100644 --- a/source/lmp/builtin.cmake +++ b/source/lmp/builtin.cmake @@ -64,34 +64,84 @@ function(_deepmd_lammps_link_torch target_name) return() endif() - if(DeePMD_PYTORCH_CMAKE_PREFIX_PATH) - list(APPEND CMAKE_PREFIX_PATH ${DeePMD_PYTORCH_CMAKE_PREFIX_PATH}) + 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() - find_package(Torch QUIET) - if(Torch_FOUND) - target_link_libraries(${target_name} PUBLIC ${TORCH_LIBRARIES}) + 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() - set(_deepmd_torch_libraries) - foreach(_deepmd_torch_library IN LISTS DeePMD_TORCH_LIBRARIES) - if(TARGET "${_deepmd_torch_library}" - OR EXISTS "${_deepmd_torch_library}" - OR "${_deepmd_torch_library}" MATCHES "^-") - list(APPEND _deepmd_torch_libraries "${_deepmd_torch_library}") - endif() - endforeach() - if(_deepmd_torch_libraries) - target_link_libraries(${target_name} PUBLIC ${_deepmd_torch_libraries}) + 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 was built with the PyTorch backend, but Torch was not " - "found while configuring LAMMPS. Set CMAKE_PREFIX_PATH to PyTorch's " - "CMake prefix path if linking lmp reports unresolved torch/c10 " - "symbols.") + "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(