Skip to content

Commit 0b1c8b8

Browse files
authored
Integrate LibTorch into ChAI (#65)
- Create CMake build system for ChAI. - Allow PyTorch and Chapel/ChAI compatability - Add Bridge library system to handle LibTorch memory allocation and communication - Give convolution and other operations a mega performance increase - Add lots of functionality to ndarray - Support loading .pt PyTorch tensor files - Support running PyTorch models via ChAI - Improve VGG example and simplicity (not usable right now) - Pave way for others to implement operations that use LibTorch
2 parents b4df21b + f77ef9f commit 0b1c8b8

File tree

101 files changed

+3397
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+3397
-89
lines changed

.gitignore

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,17 @@ examples/vgg/models/vgg16/*.npy
3131
new_sun.png
3232
new_sun*.png
3333
new_sun_hello.png
34-
test/correspondence/gh.out
34+
test/correspondence/gh.out
35+
!Makefile
36+
!modules/*
37+
modules/build
38+
!bridge/*
39+
bridge/build
40+
bridge/libtorch
41+
# pytorch/
42+
build/
43+
libtorch/
44+
.cache/
45+
build-release/
46+
libtorch_static/
47+
examples/vgg/**/*.pt

CMakeLists.txt

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
cmake_minimum_required(VERSION 3.31 FATAL_ERROR)
2+
# project(MyProject LANGUAGES CXX)
3+
4+
include(CMakePrintHelpers)
5+
6+
# set(CMAKE_VERBOSE_MAKEFILE ON)
7+
8+
if(UNIX AND NOT APPLE)
9+
set(LINUX TRUE)
10+
endif()
11+
12+
set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
13+
set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}")
14+
set(PROJECT_CACHE_DIR "${PROJECT_ROOT_DIR}/.cache")
15+
set(LIBTORCH_DIR "${PROJECT_ROOT_DIR}/libtorch")
16+
# set(LIBTORCH_STATIC_INSTALL_DIR "${PROJECT_ROOT_DIR}/libtorch_static")
17+
set(BRIDGE_DIR "${PROJECT_ROOT_DIR}/bridge")
18+
19+
20+
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
21+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
22+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")
23+
24+
project(MyProject LANGUAGES CXX C CHPL)
25+
message(STATUS "Using chpl: ${CMAKE_CHPL_COMPILER}")
26+
27+
28+
if(APPLE)
29+
set(CMAKE_C_COMPILER "/usr/bin/clang")
30+
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
31+
endif()
32+
set(CMAKE_CXX_STANDARD 23)
33+
34+
35+
include(LibTorchDL)
36+
download_libtorch(
37+
CACHE_DIR ${PROJECT_CACHE_DIR}
38+
DESTINATION ${LIBTORCH_DIR}
39+
)
40+
41+
42+
include(FetchContent)
43+
include(ExternalProject)
44+
45+
46+
# if(NOT EXISTS "${LIBTORCH_STATIC_INSTALL_DIR}/lib/libtorch.a")
47+
# ExternalProject_Add(
48+
# pytorch
49+
# GIT_REPOSITORY https://github.com/pytorch/pytorch.git
50+
# GIT_TAG v2.6.0 # Example: specify a particular release
51+
# UPDATE_COMMAND "" # Don’t auto-run 'git pull'
52+
# PATCH_COMMAND "" # No custom patch step
53+
54+
# # DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/pytorch-download" # Where to download the repo
55+
56+
# # We need all PyTorch submodules. By default, ExternalProject won't do submodule init.
57+
# # So we can do that in a separate step if we want a full build. For a minimal CPU build,
58+
# # you might not need them all, but let's be safe:
59+
# STEP_TARGETS clone
60+
# # After 'clone', run "git submodule update --init --recursive"
61+
# # to fetch all submodules.
62+
# # We can use a little trick with COMMAND.
63+
# # PATCH_COMMAND "git submodule update --init --recursive"
64+
65+
# # CMAKE_ARGS
66+
# # -DBUILD_SHARED_LIBS=OFF # Build static libraries
67+
# # -DBUILD_PYTHON=OFF # Don’t build Python bindings
68+
# # -DBUILD_TEST=OFF # Don’t build tests
69+
# # -DUSE_CUDA=OFF # Disable CUDA
70+
# # -DUSE_CUDNN=OFF # Disable cuDNN
71+
# # -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
72+
# # # -DBUILD_BINARY=ON
73+
# # # -DUSE_DISTRIBUTED=ON
74+
# # # -DBUILD_STATIC_RUNTIME_BENCHMARK=ON
75+
# # # -DBUILD_LITE_INTERPRETER=ON
76+
# # # -DUSE_STATIC_MKL=ON
77+
# # # -DSTATIC_DISPATCH_BACKEND=ON
78+
# # # -DCAFFE2_USE_MSVC_STATIC_RUNTIME=ON
79+
# # # -DUSE_DISTRIBUTED=ON
80+
# # # -DCMAKE_BUILD_TYPE=Release
81+
# # -DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
82+
# # # -DCMAKE_POLICY_VERSION_MINIMUM=3.5
83+
# CMAKE_ARGS
84+
# -DBUILD_SHARED_LIBS=OFF # Build static libraries
85+
# -DBUILD_PYTHON=OFF # Don’t build Python bindings
86+
# -DBUILD_TEST=OFF # Don’t build tests
87+
# -DUSE_CUDA=OFF # Disable CUDA
88+
# -DUSE_CUDNN=OFF # Disable cuDNN
89+
# -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
90+
# -DCMAKE_BUILD_TYPE=Release
91+
# # -DSTATIC_DISPATCH_BACKEND=ON
92+
# -DCMAKE_INSTALL_PREFIX=${LIBTORCH_STATIC_INSTALL_DIR}
93+
# -DCMAKE_POLICY_VERSION_MINIMUM=3.5
94+
95+
# INSTALL_DIR ${LIBTORCH_STATIC_INSTALL_DIR} # Where to install
96+
# )
97+
# endif()
98+
99+
100+
file(GLOB CHAI_LIB_FILES "${PROJECT_ROOT_DIR}/lib/*.chpl")
101+
102+
# file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c "int main(int argc, const char **argv){ return 1; }\n")
103+
# add_executable(ChAI ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c)
104+
# # file(GLOB RESOURCE_FILES *.bmp *.wav moose.dat utf8.txt)
105+
# foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
106+
# add_custom_command(
107+
# TARGET ChAI
108+
# POST_BUILD
109+
# COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:ChAI>
110+
# )
111+
# endforeach(RESOURCE_FILE)
112+
113+
# file(COPY ${CHAI_LIB_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
114+
115+
116+
117+
118+
add_custom_target(
119+
ChAI
120+
ALL
121+
DEPENDS ${PROJECT_SOURCE_DIR}/lib
122+
# SOURCES ${CHAI_LIB_FILES}
123+
# COMMAND ${CMAKE_COMMAND} -E echo "Building ChAI"
124+
)
125+
126+
foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
127+
add_custom_command(
128+
TARGET ChAI
129+
POST_BUILD
130+
# OUTPUT $<TARGET_FILE_DIR:ChAI>
131+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${RESOURCE_FILE} ${PROJECT_BINARY_DIR}/lib
132+
# DEPENDS ${CHAI_LIB_FILES}
133+
)
134+
endforeach(RESOURCE_FILE)
135+
136+
137+
138+
# foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
139+
# add_custom_command(
140+
# TARGET ChAI
141+
# POST_BUILD
142+
# COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:ChAI>
143+
# )
144+
# endforeach(RESOURCE_FILE)
145+
146+
147+
function(watch)
148+
set_property(
149+
DIRECTORY
150+
APPEND
151+
PROPERTY CMAKE_CONFIGURE_DEPENDS ${ARGV}
152+
)
153+
endfunction()
154+
155+
# cmake_print_variables(CHAI_LIB_FILES)
156+
157+
add_library(bridge STATIC ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)
158+
159+
target_include_directories(
160+
bridge
161+
PRIVATE
162+
${BRIDGE_DIR}/include
163+
${LIBTORCH_DIR}/include
164+
${LIBTORCH_DIR}/include/torch/csrc/api/include
165+
)
166+
167+
set(BRIDGE_OBJECT_FILES $<TARGET_OBJECTS:bridge>)
168+
169+
170+
171+
172+
file(GLOB LIBTORCH_ALL_LIB_FILES
173+
"${LIBTORCH_DIR}/lib/*.a"
174+
"${LIBTORCH_DIR}/lib/*.dylib"
175+
"${LIBTORCH_DIR}/lib/*.so")
176+
177+
set(LIBTORCH_ALL_LIBS "")
178+
foreach(lib_path IN LISTS LIBTORCH_ALL_LIB_FILES)
179+
get_filename_component(lib_name "${lib_path}" NAME_WE)
180+
list(APPEND LIBTORCH_ALL_LIBS "${lib_name}")
181+
endforeach()
182+
183+
184+
set(REQUIRED_LIBS
185+
"libtorch"
186+
"libtorch_cpu"
187+
"libc10"
188+
"libtorch_global_deps"
189+
)
190+
191+
set(DISALLOWED_LIBS
192+
"libtorch_python"
193+
)
194+
195+
set(LIBTORCH_LIBS_LINKER_ARGS "") # Will hold the list of "-l..." flags.
196+
foreach(lib_name IN LISTS LIBTORCH_ALL_LIBS)
197+
if(lib_name IN_LIST DISALLOWED_LIBS)
198+
if(lib_name IN_LIST REQUIRED_LIBS)
199+
message(FATAL_ERROR "Required lib ${lib_name} is disallowed.")
200+
else()
201+
message(STATUS "Skipping disallowed lib: ${lib_name}")
202+
continue()
203+
endif()
204+
endif()
205+
string(REGEX REPLACE "^lib" "" lib_name_short "${lib_name}")
206+
list(APPEND LIBTORCH_LIBS_LINKER_ARGS "-l${lib_name_short}")
207+
endforeach()
208+
209+
# cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
210+
# cmake_print_variables(${BRIDGE_OBJECT_FILES})
211+
# cmake_print_variables(BRIDGE_OBJECT_FILES)
212+
213+
214+
add_executable(TorchBridge ${BRIDGE_DIR}/lib/Bridge.chpl)
215+
add_dependencies(TorchBridge bridge)
216+
add_dependencies(TorchBridge ChAI)
217+
target_link_options(TorchBridge
218+
PRIVATE
219+
${BRIDGE_DIR}/include/bridge.h
220+
${BRIDGE_OBJECT_FILES}
221+
-L ${LIBTORCH_DIR}/lib
222+
# "-ltorch"
223+
# "-ltorch_cpu"
224+
# "-lc10"
225+
# "-ltorch_global_deps"
226+
${LIBTORCH_LIBS_LINKER_ARGS}
227+
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
228+
)
229+
230+
231+
232+
233+
add_executable(TinyLayerTest
234+
${PROJECT_ROOT_DIR}/test/tiny/layer_test.chpl
235+
${CHAI_LIB_FILES}
236+
)
237+
add_dependencies(TinyLayerTest bridge)
238+
add_dependencies(TinyLayerTest ChAI)
239+
target_link_options(TinyLayerTest
240+
PRIVATE
241+
--main-module layer_test.chpl
242+
-M ${PROJECT_ROOT_DIR}/lib
243+
${BRIDGE_DIR}/include/bridge.h
244+
${BRIDGE_OBJECT_FILES}
245+
-L ${LIBTORCH_DIR}/lib
246+
${LIBTORCH_LIBS_LINKER_ARGS}
247+
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
248+
)
249+
# chpl test/tiny/layer_test.chpl -M lib bridge/include/bridge.h build/CMakeFiles/bridge.dir/bridge/lib/bridge.cpp.o -L libtorch/lib -ltorch -ltorch_cpu -lc10 -ltorch_global_deps --ldflags "-Wl,-rpath,libtorch/lib"
250+
251+
# chpl --fast -o vgg test.chpl -M ../../lib /Users/iainmoncrief/Documents/Github/ChAI/bridge/include/bridge.h /Users/iainmoncrief/Documents/Github/ChAI/build/CMakeFiles/bridge.dir/bridge/lib/bridge.cpp.o -L /Users/iainmoncrief/Documents/Github/ChAI/libtorch/lib -ltorch -ltorch_cpu -lc10 -ltorch_global_deps --ldflags "-Wl,-rpath,/Users/iainmoncrief/Documents/Github/ChAI/libtorch/lib"
252+
253+
# chpl -o vgg test.chpl $(../../embed_libtorch.sh .)
254+
# chpl --fast -o vgg test.chpl $(../../embed_libtorch.sh .)
255+
256+
257+
258+
259+
set(CHAI_LINKER_ARGS
260+
-M ${PROJECT_ROOT_DIR}/lib
261+
${BRIDGE_DIR}/include/bridge.h
262+
${BRIDGE_OBJECT_FILES}
263+
-L ${LIBTORCH_DIR}/lib
264+
${LIBTORCH_LIBS_LINKER_ARGS}
265+
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
266+
)
267+
268+
269+
270+
add_executable(vgg
271+
"${PROJECT_ROOT_DIR}/examples/vgg/test.chpl"
272+
${PROJECT_ROOT_DIR}/examples/vgg/VGG.chpl
273+
${CHAI_LIB_FILES}
274+
)
275+
add_dependencies(vgg bridge)
276+
add_dependencies(vgg ChAI)
277+
target_link_options(vgg
278+
PRIVATE
279+
# -M ${PROJECT_ROOT_DIR}/examples/vgg
280+
-svggExampleDir="${PROJECT_ROOT_DIR}/examples/vgg"
281+
${CHAI_LINKER_ARGS}
282+
)
283+
284+
add_custom_command(
285+
TARGET vgg
286+
POST_BUILD
287+
COMMAND ${CMAKE_COMMAND} -E copy_directory
288+
"${PROJECT_ROOT_DIR}/examples/vgg/images"
289+
"$<TARGET_FILE_DIR:vgg>/images"
290+
COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/images to $<TARGET_FILE_DIR:vgg>/images"
291+
)
292+
293+
# ./vgg images/frog.jpg
294+
295+
add_subdirectory(examples)
296+
add_subdirectory("test")

bridge/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)