Skip to content

Commit 7f26b95

Browse files
committed
Add installation version for libtorch.
1 parent 3231eb3 commit 7f26b95

4 files changed

Lines changed: 325 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ bridge/build
4040
bridge/libtorch
4141
# pytorch/
4242
build/
43+
libtorch/
44+
.cache/

CMakeLists.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ endif()
1111

1212
set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
1313
set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}")
14-
15-
14+
set(PROJECT_CACHE_DIR "${PROJECT_ROOT_DIR}/.cache")
15+
set(LIBTORCH_DIR "${PROJECT_ROOT_DIR}/libtorch")
1616
set(BRIDGE_DIR "${PROJECT_ROOT_DIR}/bridge")
1717

18-
set(LIBTORCH_DIR "${BRIDGE_DIR}/libtorch")
1918

2019
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
2120
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
@@ -29,6 +28,17 @@ set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
2928
set(CMAKE_CXX_STANDARD 17)
3029

3130

31+
include(LibTorchDL)
32+
33+
34+
download_libtorch(
35+
CACHE_DIR ${PROJECT_CACHE_DIR}
36+
DESTINATION ${LIBTORCH_DIR}
37+
)
38+
39+
40+
41+
3242
add_library(bridge OBJECT ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)
3343

3444
target_include_directories(
@@ -62,9 +72,9 @@ set(BRIDGE_OBJECT_FILES $<TARGET_OBJECTS:bridge>)
6272
# message(STATUS "Library path: ${bridge_o_path}")
6373

6474

65-
add_executable(BridgeTest ${BRIDGE_DIR}/lib/Bridge.chpl)
66-
add_dependencies(BridgeTest bridge)
67-
target_link_options(BridgeTest
75+
add_executable(TorchBridge ${BRIDGE_DIR}/lib/Bridge.chpl)
76+
add_dependencies(TorchBridge bridge)
77+
target_link_options(TorchBridge
6878
PRIVATE
6979
${BRIDGE_DIR}/include/bridge.h
7080
${BRIDGE_OBJECT_FILES}
@@ -74,4 +84,4 @@ target_link_options(BridgeTest
7484
"-lc10"
7585
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
7686
)
77-
# install(TARGETS BridgeTest DESTINATION ".")
87+
install(TARGETS TorchBridge DESTINATION ".")

cmake/LibTorchDL.cmake

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
cmake_minimum_required(VERSION 3.31 FATAL_ERROR)
2+
3+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
4+
5+
include(dlcache)
6+
7+
include(CMakePrintHelpers)
8+
9+
set(TORCH_URL_PREFIX "https://download.pytorch.org/libtorch/nightly/cpu")
10+
11+
12+
13+
14+
if(UNIX AND NOT APPLE)
15+
set(LINUX TRUE)
16+
elseif(WIN32)
17+
message(FATAL_ERROR "Windows is not supported yet.")
18+
endif()
19+
20+
if(APPLE)
21+
set(TORCH_DISTRIBUTION "${TORCH_URL_PREFIX}/libtorch-macos-arm64-latest.zip")
22+
elseif(LINUX)
23+
set(TORCH_DISTRIBUTION "$${TORCH_URL_PREFIX}/libtorch-cxx11-abi-static-with-deps-latest.zip")
24+
endif()
25+
26+
function(download_libtorch)
27+
cmake_parse_arguments(
28+
DL_TORCH
29+
"CLEAR_CACHE"
30+
"CACHE_DIR"
31+
"DESTINATION"
32+
${ARGV}
33+
)
34+
35+
cmake_print_variables(DL_TORCH_CLEAR_CACHE)
36+
cmake_print_variables(DL_TORCH_CACHE_DIR)
37+
cmake_print_variables(DL_TORCH_DESTINATION)
38+
39+
40+
41+
if(DL_TORCH_CACHE_DIR)
42+
set(TORCH_CACHE_DIR ${DL_TORCH_CACHE_DIR})
43+
else()
44+
set(TORCH_CACHE_DIR "${CMAKE_SOURCE_DIR}/.cache")
45+
endif()
46+
47+
set(TORCH_CACHE_NAME "libtorch_cache.zip")
48+
set(TORCH_CACHE "${TORCH_CACHE_DIR}/${TORCH_CACHE_NAME}")
49+
# set(TORCH_DIR "${TORCH_CACHE_DIR}/libtorch")
50+
51+
52+
if(DL_TORCH_DESTINATION)
53+
set(TORCH_DIR ${DL_TORCH_DESTINATION})
54+
else()
55+
set(TORCH_DIR "${TORCH_CACHE_DIR}/torchlib")
56+
endif()
57+
58+
if(EXISTS ${TORCH_CACHE})
59+
if(DL_TORCH_CLEAR_CACHE)
60+
message(STATUS "Clearing cache: ${TORCH_CACHE}")
61+
file(REMOVE "${TORCH_CACHE}")
62+
set(SHOULD_DOWNLOAD TRUE)
63+
else()
64+
message(STATUS "Cache exists, skipping download.")
65+
set(SHOULD_DOWNLOAD FALSE)
66+
endif()
67+
else()
68+
message(STATUS "Cache does not exist, downloading.")
69+
set(SHOULD_DOWNLOAD TRUE)
70+
message(STATUS "Creating cache directory: ${TORCH_CACHE_DIR}")
71+
file(MAKE_DIRECTORY "${TORCH_CACHE_DIR}")
72+
endif()
73+
74+
if(SHOULD_DOWNLOAD)
75+
message(STATUS "Downloading from ${TORCH_DISTRIBUTION}")
76+
file(DOWNLOAD "${TORCH_DISTRIBUTION}" "${TORCH_CACHE}" STATUS dlstatus)
77+
list(GET dlstatus 0 status_code)
78+
if(status_code)
79+
message(FATAL_ERROR "${dlstatus} (${url})")
80+
endif()
81+
message(STATUS "Downloaded ${TORCH_DISTRIBUTION} to ${TORCH_CACHE}")
82+
83+
if(EXISTS ${TORCH_DIR})
84+
message(STATUS "Removing existing directory: ${TORCH_DIR}")
85+
file(REMOVE_RECURSE "${TORCH_DIR}")
86+
file(MAKE_DIRECTORY "${TORCH_DIR}")
87+
endif()
88+
message(STATUS "Unpacking ${TORCH_CACHE} to ${TORCH_DIR}")
89+
90+
cmake_print_variables(TORCH_DIR)
91+
92+
file(MAKE_DIRECTORY "${TORCH_CACHE_DIR}/tmp")
93+
file(ARCHIVE_EXTRACT
94+
INPUT ${TORCH_CACHE}
95+
DESTINATION "${TORCH_CACHE_DIR}/tmp"
96+
)
97+
file(RENAME "${TORCH_CACHE_DIR}/tmp/libtorch" "${TORCH_DIR}")
98+
99+
if(NOT EXISTS "${TORCH_CACHE_DIR}/tmp")
100+
message(FATAL_ERROR "Failed to extract ${TORCH_CACHE} to ${TORCH_DIR}")
101+
else()
102+
message(STATUS "Extracted ${TORCH_CACHE} to ${TORCH_DIR}")
103+
file(REMOVE_RECURSE "${TORCH_CACHE_DIR}/tmp")
104+
endif()
105+
106+
endif()
107+
108+
endfunction()
109+
110+
# dlcache("${TORCH_DISTRIBUTION}" OUT url)
111+
112+
# message(STATUS "file loc >>>>> ${url}")

cmake/dlcache.cmake

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
2+
cmake_minimum_required(VERSION 3.19)
3+
4+
5+
6+
# dlcache("https://github.com/g-truc/glm/archive/refs/heads/master.zip"
7+
# SHA256 97198B71B24AD5087114C1FB64DC3111AEE1C7976CB5AE8A7C4476F3EEAB8D69
8+
# OUT url
9+
# )
10+
# message(STATUS "file loc >>>>> ${url}")
11+
12+
function(gitcache url githash)
13+
14+
if(WIN32)
15+
file(TO_CMAKE_PATH $ENV{LocalAppData} appdatadir)
16+
set(DLCACHEDIR "${appdatadir}/dlcache")
17+
else()
18+
set(DLCACHEDIR "$ENV{HOME}/.cache/dlcache")
19+
endif()
20+
string(REGEX REPLACE [^A-Za-z0-9/.-] _ urlstripped "${url}")
21+
set(dlpath "${DLCACHEDIR}/${urlstripped}")
22+
23+
set(TMPDEST "${dlpath}.tmp")
24+
if(EXISTS "${TMPDEST}")
25+
file(REMOVE_RECURSE "${TMPDEST}")
26+
endif()
27+
28+
if(NOT EXISTS "${dlpath}")
29+
execute_process(
30+
COMMAND git clone --recursive "${url}" "${TMPDEST}"
31+
COMMAND_ERROR_IS_FATAL ANY
32+
)
33+
execute_process(
34+
COMMAND ${CMAKE_COMMAND} -E rename "${TMPDEST}" "${dlpath}"
35+
COMMAND_ERROR_IS_FATAL ANY
36+
)
37+
endif()
38+
39+
set(FOUND 0)
40+
if(EXISTS "${dlpath}")
41+
execute_process(
42+
COMMAND git show --format="%H" --no-patch
43+
WORKING_DIRECTORY "${dlpath}"
44+
OUTPUT_VARIABLE lout
45+
)
46+
if(lout MATCHES "${githash}")
47+
set(FOUND 1)
48+
endif()
49+
endif()
50+
51+
if(NOT FOUND)
52+
execute_process(
53+
COMMAND ${CMAKE_COMMAND} -E rename "${dlpath}" "${TMPDEST}"
54+
COMMAND_ERROR_IS_FATAL ANY
55+
)
56+
execute_process(
57+
COMMAND git clean -fdx
58+
WORKING_DIRECTORY "${TMPDEST}"
59+
COMMAND_ERROR_IS_FATAL ANY
60+
)
61+
execute_process(
62+
COMMAND git checkout --recurse-submodules -f "${githash}"
63+
WORKING_DIRECTORY "${TMPDEST}"
64+
COMMAND_ERROR_IS_FATAL ANY
65+
)
66+
execute_process(
67+
COMMAND ${CMAKE_COMMAND} -E rename "${TMPDEST}" "${dlpath}"
68+
COMMAND_ERROR_IS_FATAL ANY
69+
)
70+
endif()
71+
72+
set(dl_local_path "${dlpath}" PARENT_SCOPE)
73+
endfunction()
74+
75+
macro(ghgitcache urlpart githash)
76+
gitcache("https://github.com/${urlpart}.git" ${githash})
77+
endmacro()
78+
79+
# dlcache("https://..." [SHA256 0123...] [OUT localFileLocVar] [UNPACK <subdir rel to current bin dir>] [UNPACK_OUT unpackDirLocVar])
80+
function(dlcache url)
81+
# https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html
82+
cmake_parse_arguments(DL "" "SHA256;OUT;UNPACK;UNPACK_OUT" "" ${ARGN} )
83+
84+
if(WIN32)
85+
file(TO_CMAKE_PATH $ENV{LocalAppData} appdatadir)
86+
set(DLCACHEDIR "${appdatadir}/dlcache")
87+
else()
88+
set(DLCACHEDIR "$ENV{HOME}/.cache/dlcache")
89+
endif()
90+
string(REGEX REPLACE [^A-Za-z0-9/.-] _ urlstripped "${url}")
91+
set(dlpath "${DLCACHEDIR}/${urlstripped}")
92+
93+
IF(EXISTS "${dlpath}")
94+
ELSE()
95+
IF(DL_SHA256)
96+
file(DOWNLOAD "${url}" "${dlpath}" STATUS dlstatus)
97+
ELSE()
98+
file(DOWNLOAD "${url}" "${dlpath}" STATUS dlstatus)
99+
ENDIF()
100+
LIST(GET dlstatus 0 status_code)
101+
IF(status_code)
102+
MESSAGE(FATAL_ERROR "${dlstatus} (${url})")
103+
ENDIF()
104+
ENDIF()
105+
106+
IF(DL_SHA256)
107+
STRING(TOLOWER ${DL_SHA256} DL_SHA256)
108+
FILE(SHA256 "${dlpath}" cksum)
109+
STRING(TOLOWER ${cksum} cksum)
110+
IF(cksum STREQUAL "${DL_SHA256}")
111+
ELSE()
112+
FILE(REMOVE "${dlpath}")
113+
MESSAGE(FATAL_ERROR "CHECKSUM MISMATCH for: ${url}\nEXPECTED: ${DL_SHA256}\nGOT: ${cksum}")
114+
ENDIF()
115+
ENDIF()
116+
117+
IF(DL_OUT)
118+
set(${DL_OUT} "${dlpath}" PARENT_SCOPE)
119+
ENDIF()
120+
121+
IF(DL_UNPACK)
122+
cmake_path(SET unpack_path NORMALIZE "${CMAKE_CURRENT_BINARY_DIR}/${DL_UNPACK}")
123+
message("unpack path = ${unpack_path}")
124+
IF(NOT EXISTS ${unpack_path})
125+
execute_process(
126+
COMMAND ${CMAKE_COMMAND} -E make_directory "${unpack_path}"
127+
)
128+
execute_process(
129+
COMMAND ${CMAKE_COMMAND} -E tar x "${dlpath}"
130+
WORKING_DIRECTORY ${unpack_path}
131+
)
132+
ENDIF()
133+
IF(DL_UNPACK_OUT)
134+
set(${DL_UNPACK_OUT} "${unpack_path}" PARENT_SCOPE)
135+
ENDIF()
136+
ENDIF()
137+
endfunction()
138+
139+
140+
macro(dlcache2 url sha256)
141+
dlcache("${url}" SHA256 "${sha256}" OUT "dl_local_path")
142+
endmacro()
143+
144+
# fetch source code zip from github
145+
#
146+
# ${dl_local_path} will contain the file path
147+
#
148+
# project: github-user/project-name
149+
# tag: either 40 chars (commit id) or a ref tag
150+
macro(dlghzip project tag sha256)
151+
string(LENGTH "${tag}" taglen)
152+
if(taglen EQUAL 40)
153+
dlcache("https://github.com/${project}/archive/${tag}.zip" SHA256 "${sha256}" OUT "dl_local_path")
154+
else()
155+
dlcache("https://github.com/${project}/archive/refs/tags/${tag}.zip" SHA256 "${sha256}" OUT "dl_local_path")
156+
endif()
157+
endmacro()
158+
159+
160+
# Examples:
161+
#
162+
# dlcache_assets(
163+
# LIST
164+
# https://raw.githubusercontent.com/Overv/VulkanTutorial/master/images/texture.jpg
165+
# 663a43377a9d3b42a1925a17313b12e339b146d219a62c4c07a56c89032858bb
166+
# https://raw.githubusercontent.com/Overv/VulkanTutorial/master/resources/viking_room.png
167+
# facb693858cafcb70b7eed264e34107f06bbf3f41805e7b8084e5b42bd914a66
168+
# OUTDIR textures
169+
# )
170+
# dlcache_assets(
171+
# LIST
172+
# https://raw.githubusercontent.com/Overv/VulkanTutorial/master/resources/viking_room.obj
173+
# 0af27cd99ce43f48c89d9c73cff47cbdfc3d29c3754b29bd0ccfe7e3fe7de869
174+
# OUTDIR models
175+
# )
176+
#
177+
# Writes the given files into textures/modles directories, using the last part of each url as filename.
178+
#
179+
function(dlcache_assets)
180+
cmake_parse_arguments(DL "EXTRACT_FILENAMES" "OUTDIR" "LIST" ${ARGN})
181+
if(NOT DL_OUTDIR)
182+
set(DL_OUTDIR "assets")
183+
endif()
184+
list(LENGTH DL_LIST len1)
185+
math(EXPR len2 "${len1} / 2 - 1")
186+
foreach(val RANGE ${len2})
187+
math(EXPR vala "2 * ${val}")
188+
math(EXPR valb "2 * ${val} + 1")
189+
list(GET DL_LIST ${vala} val1)
190+
list(GET DL_LIST ${valb} val2)
191+
dlcache2("${val1}" "${val2}")
192+
file(COPY "${dl_local_path}" DESTINATION "${DL_OUTDIR}")
193+
endforeach()
194+
endfunction()

0 commit comments

Comments
 (0)