From 78895e8f91d37dd929364980fceed4d3babbe335 Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Mon, 19 Jan 2026 13:31:57 +0100 Subject: [PATCH 1/6] Initial version for lettign cmake add libtorch automatically. cpu version is probably ok (at leat on my mac it works), but gpu still needs to filter versions correctly. --- cmake/gauxc-onedft.cmake | 4 ++- cmake/skala-torch.cmake | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 cmake/skala-torch.cmake diff --git a/cmake/gauxc-onedft.cmake b/cmake/gauxc-onedft.cmake index 6e799849..4fbcae8d 100644 --- a/cmake/gauxc-onedft.cmake +++ b/cmake/gauxc-onedft.cmake @@ -24,7 +24,9 @@ endif() # store and restore CMAKE_CUDA_ARCHITECTURES if Torch clobbers it set(_PREV_CUDA_ARCHS "${CMAKE_CUDA_ARCHITECTURES}") -find_package(Torch REQUIRED) + +include(skala-torch) + if(CMAKE_CUDA_ARCHITECTURES STREQUAL "OFF") set(CMAKE_CUDA_ARCHITECTURES "${_PREV_CUDA_ARCHS}" CACHE STRING "Restore CUDA archs after Torch override" FORCE) message(WARNING "Torch set CMAKE_CUDA_ARCHITECTURES to OFF. Restored previous value: ${CMAKE_CUDA_ARCHITECTURES}") diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake new file mode 100644 index 00000000..dd453900 --- /dev/null +++ b/cmake/skala-torch.cmake @@ -0,0 +1,60 @@ +# Find or download LibTorch +find_package(Torch QUIET) + +if(NOT Torch_FOUND) + message(STATUS "Torch not found. Downloading libtorch...") + + # Set libtorch version and download URL + set(LIBTORCH_VERSION "2.9.1") + + # Determine the appropriate libtorch variant based on platform and CUDA availability + if(UNIX AND NOT APPLE) + # Linux + if(GAUXC_HAS_CUDA) + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu121/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}%2Bcu121.zip") + else() + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip" + endif() + elseif(APPLE) + # macOS (CPU only) + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-${LIBTORCH_VERSION}.zip") + elseif(WIN32) + # Windows + if(GAUXC_HAS_CUDA) + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu121/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcu121.zip") + else() + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip") + endif() + endif() + + set(LIBTORCH_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/libtorch") + set(LIBTORCH_ZIP "${CMAKE_BINARY_DIR}/libtorch.zip") + + # Download libtorch + if(NOT EXISTS ${LIBTORCH_DOWNLOAD_DIR}) + message(STATUS "Downloading libtorch from ${LIBTORCH_URL}") + file(DOWNLOAD ${LIBTORCH_URL} ${LIBTORCH_ZIP} + SHOW_PROGRESS + STATUS DOWNLOAD_STATUS) + + list(GET DOWNLOAD_STATUS 0 STATUS_CODE) + if(NOT STATUS_CODE EQUAL 0) + message(FATAL_ERROR "Failed to download libtorch: ${DOWNLOAD_STATUS}") + endif() + + # Extract libtorch + message(STATUS "Extracting libtorch...") + file(ARCHIVE_EXTRACT INPUT ${LIBTORCH_ZIP} DESTINATION ${CMAKE_BINARY_DIR}) + + # Clean up zip file + file(REMOVE ${LIBTORCH_ZIP}) + endif() + + # Set CMAKE_PREFIX_PATH to find the downloaded libtorch + set(CMAKE_PREFIX_PATH "${LIBTORCH_DOWNLOAD_DIR};${CMAKE_PREFIX_PATH}") + + # Find Torch package again + find_package(Torch REQUIRED) +endif() + +message(STATUS "Using Torch version: ${Torch_VERSION}") From 3e2faef8f5e8926877c36e606ec0cbafea21c607 Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Thu, 22 Jan 2026 10:34:06 +0100 Subject: [PATCH 2/6] Include and verify the possible CUDA versions for which libtorch downloads are available. Still needs to be tested. --- cmake/skala-torch.cmake | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake index dd453900..06b9c4b2 100644 --- a/cmake/skala-torch.cmake +++ b/cmake/skala-torch.cmake @@ -6,22 +6,34 @@ if(NOT Torch_FOUND) # Set libtorch version and download URL set(LIBTORCH_VERSION "2.9.1") + set(USE_CUDA_LIBTORCH FALSE) #default is not to use the cuda version but cpu version + if(GAUXC_HAS_CUDA) + set(SUPPORTED_CUDA_VERSION_STRINGS "126" "128" "130") + set(CUDA_VERSION_STRING ${CUDAToolkit_VERSION_MAJOR}${CUDAToolkit_VERSION_MINOR}) + if(CUDA_VERSION_STRING IN_LIST SUPPORTED_CUDA_VERSION_STRINGS) + set(USE_CUDA_LIBTORCH TRUE) + else() + message(Found ${CUDAToolkit_VERSION}, for which there is no libtorch to download.) + message(Falling back to cpu version of libtorch.) + endif() + endif() + # Determine the appropriate libtorch variant based on platform and CUDA availability if(UNIX AND NOT APPLE) # Linux - if(GAUXC_HAS_CUDA) - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu121/libtorch-cxx11-abi-shared-with-deps-${LIBTORCH_VERSION}%2Bcu121.zip") + if(USE_CUDA_LIBTORCH) + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_STRING}/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_STRING}.zip") else() - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip" + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip") endif() elseif(APPLE) # macOS (CPU only) set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-${LIBTORCH_VERSION}.zip") elseif(WIN32) # Windows - if(GAUXC_HAS_CUDA) - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu121/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcu121.zip") + if(USE_CUDA_LIBTORCH) + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_STRING}/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_STRING}.zip") else() set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip") endif() From 442f29de5a1529794180812f9f5eeaa58606793e Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Thu, 22 Jan 2026 11:34:44 +0100 Subject: [PATCH 3/6] Added the message modewq --- cmake/skala-torch.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake index 06b9c4b2..faa81aff 100644 --- a/cmake/skala-torch.cmake +++ b/cmake/skala-torch.cmake @@ -13,8 +13,8 @@ if(NOT Torch_FOUND) if(CUDA_VERSION_STRING IN_LIST SUPPORTED_CUDA_VERSION_STRINGS) set(USE_CUDA_LIBTORCH TRUE) else() - message(Found ${CUDAToolkit_VERSION}, for which there is no libtorch to download.) - message(Falling back to cpu version of libtorch.) + message(WARNING CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.) + message(WARNING Falling back to cpu version of libtorch.) endif() endif() From 0c6d7ec04a67557a4bd54bf303d612d4db0e7c86 Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Thu, 22 Jan 2026 11:36:42 +0100 Subject: [PATCH 4/6] and quotation marks... --- cmake/skala-torch.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake index faa81aff..851b2475 100644 --- a/cmake/skala-torch.cmake +++ b/cmake/skala-torch.cmake @@ -13,8 +13,8 @@ if(NOT Torch_FOUND) if(CUDA_VERSION_STRING IN_LIST SUPPORTED_CUDA_VERSION_STRINGS) set(USE_CUDA_LIBTORCH TRUE) else() - message(WARNING CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.) - message(WARNING Falling back to cpu version of libtorch.) + message(WARNING "CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.") + message(WARNING "Falling back to cpu version of libtorch.") endif() endif() From 6cf0b673805db84655d970a90728a0009ea618ba Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Thu, 22 Jan 2026 14:27:18 +0000 Subject: [PATCH 5/6] Needed to find the cuda tollkit to get the proper environment variables set. --- cmake/skala-torch.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake index 851b2475..4c72f6bb 100644 --- a/cmake/skala-torch.cmake +++ b/cmake/skala-torch.cmake @@ -8,13 +8,14 @@ if(NOT Torch_FOUND) set(LIBTORCH_VERSION "2.9.1") set(USE_CUDA_LIBTORCH FALSE) #default is not to use the cuda version but cpu version if(GAUXC_HAS_CUDA) + find_package(CUDAToolkit) set(SUPPORTED_CUDA_VERSION_STRINGS "126" "128" "130") set(CUDA_VERSION_STRING ${CUDAToolkit_VERSION_MAJOR}${CUDAToolkit_VERSION_MINOR}) if(CUDA_VERSION_STRING IN_LIST SUPPORTED_CUDA_VERSION_STRINGS) set(USE_CUDA_LIBTORCH TRUE) else() - message(WARNING "CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.") - message(WARNING "Falling back to cpu version of libtorch.") + message(WARNING "CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.") + message(WARNING "Falling back to cpu version of libtorch.") endif() endif() From b0e70e4e15212c4261f773a9f580e31570890e4c Mon Sep 17 00:00:00 2001 From: Klaas Giesbertz Date: Fri, 23 Jan 2026 13:43:08 +0000 Subject: [PATCH 6/6] The Caffe cmake from libtorch uses the old way to include CUDA, so find_package(CUDA REQUIRED) needed to included. The CUDA_VERSION_STRING conflicts, so renamed that to CUDA_VERSION_NO_DOT. --- cmake/skala-torch.cmake | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmake/skala-torch.cmake b/cmake/skala-torch.cmake index 4c72f6bb..6e6214d0 100644 --- a/cmake/skala-torch.cmake +++ b/cmake/skala-torch.cmake @@ -9,10 +9,13 @@ if(NOT Torch_FOUND) set(USE_CUDA_LIBTORCH FALSE) #default is not to use the cuda version but cpu version if(GAUXC_HAS_CUDA) find_package(CUDAToolkit) - set(SUPPORTED_CUDA_VERSION_STRINGS "126" "128" "130") - set(CUDA_VERSION_STRING ${CUDAToolkit_VERSION_MAJOR}${CUDAToolkit_VERSION_MINOR}) - if(CUDA_VERSION_STRING IN_LIST SUPPORTED_CUDA_VERSION_STRINGS) + set(SUPPORTED_CUDA_VERSION_NO_DOTS "126" "128" "130") + set(CUDA_VERSION_NO_DOT ${CUDAToolkit_VERSION_MAJOR}${CUDAToolkit_VERSION_MINOR}) + if(CUDA_VERSION_NO_DOT IN_LIST SUPPORTED_CUDA_VERSION_NO_DOTS) + find_package(CUDA REQUIRED) # Needed for Caffe cmake, since it use the old CUDA inclusion set(USE_CUDA_LIBTORCH TRUE) + message(STATUS "CUDAToolkit_INCLUDE_DIRS: ${CUDAToolkit_INCLUDE_DIRS}") + set(CUDA_INCLUDE_DIRS "${CUDAToolkit_INCLUDE_DIRS}") # and pass over the correct headers to Caffe cmake else() message(WARNING "CUDA toolkit version is ${CUDAToolkit_VERSION}, for which there is no libtorch to download.") message(WARNING "Falling back to cpu version of libtorch.") @@ -24,7 +27,7 @@ if(NOT Torch_FOUND) if(UNIX AND NOT APPLE) # Linux if(USE_CUDA_LIBTORCH) - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_STRING}/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_STRING}.zip") + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_NO_DOT}/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_NO_DOT}.zip") else() set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip") endif() @@ -34,7 +37,7 @@ if(NOT Torch_FOUND) elseif(WIN32) # Windows if(USE_CUDA_LIBTORCH) - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_STRING}/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_STRING}.zip") + set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cu${CUDA_VERSION_NO_DOT}/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcu${CUDA_VERSION_NO_DOT}.zip") else() set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-${LIBTORCH_VERSION}%2Bcpu.zip") endif()