1- cmake_minimum_required (VERSION 3.31 FATAL_ERROR )
1+ cmake_minimum_required (VERSION 3.20 FATAL_ERROR )
22# project(MyProject LANGUAGES CXX)
33
44include (CMakePrintHelpers )
@@ -15,7 +15,7 @@ set(PROJECT_CACHE_DIR "${PROJECT_ROOT_DIR}/.cache")
1515set (LIBTORCH_DIR "${PROJECT_ROOT_DIR} /libtorch" )
1616# set(LIBTORCH_STATIC_INSTALL_DIR "${PROJECT_ROOT_DIR}/libtorch_static")
1717set (BRIDGE_DIR "${PROJECT_ROOT_DIR} /bridge" )
18-
18+ set (Torch_DIR " ${PROJECT_ROOT_DIR} /libtorch" )
1919
2020find_package (chpl REQUIRED HINTS ${PROJECT_ROOT_DIR} /cmake/chapel )
2121list (APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR} /cmake" )
@@ -29,7 +29,7 @@ if(APPLE)
2929 set (CMAKE_C_COMPILER "/usr/bin/clang" )
3030 set (CMAKE_CXX_COMPILER "/usr/bin/clang++" )
3131endif ()
32- set (CMAKE_CXX_STANDARD 23 )
32+ set (CMAKE_CXX_STANDARD 20 )
3333
3434
3535include (LibTorchDL )
@@ -38,63 +38,16 @@ download_libtorch(
3838 DESTINATION ${LIBTORCH_DIR}
3939)
4040
41+ list (APPEND CMAKE_PREFIX_PATH "${LIBTORCH_DIR} /share/cmake" )
42+ find_package (Torch REQUIRED )
43+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23 -lm -ldl ${TORCH_CXX_FLAGS} " )
4144
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()
45+
46+ cmake_print_variables (TORCH_LIBRARIES )
47+ cmake_print_variables (TORCH_INCLUDE_DIRS )
48+ cmake_print_variables (TORCH_INSTALL_PREFIX )
49+ cmake_print_variables (TORCH_CXX_FLAGS )
50+ cmake_print_variables (TORCH_LIBRARY )
9851
9952
10053file (GLOB CHAI_LIB_FILES "${PROJECT_ROOT_DIR} /lib/*.chpl" )
@@ -152,25 +105,39 @@ function(watch)
152105 )
153106endfunction ()
154107
108+
109+
155110# cmake_print_variables(CHAI_LIB_FILES)
156111
157- add_library (bridge STATIC ${BRIDGE_DIR} /include/bridge.h ${BRIDGE_DIR} /lib/bridge.cpp )
112+ add_library (bridge OBJECT ${BRIDGE_DIR} /include/bridge.h ${BRIDGE_DIR} /lib/bridge.cpp )
113+
114+ target_link_libraries (
115+ bridge
116+ PRIVATE
117+ ${TORCH_LIBRARIES}
118+ # ${LIBTORCH_DIR}/lib/libtorch.so
119+ # ${LIBTORCH_DIR}/lib/libtorch_cpu.so
120+ # ${LIBTORCH_DIR}/lib/libc10.so
121+ # ${LIBTORCH_DIR}/lib/libtorch_global_deps.so
122+ )
158123
159124target_include_directories (
160125 bridge
161126 PRIVATE
162127 ${BRIDGE_DIR} /include
163128 ${LIBTORCH_DIR} /include
164129 ${LIBTORCH_DIR} /include/torch/csrc/api/include
130+ # ${BRIDGE_DIR}/util
165131)
166132
133+
167134set (BRIDGE_OBJECT_FILES $<TARGET_OBJECTS :bridge >)
168135
169136
170137
171138
172139file (GLOB LIBTORCH_ALL_LIB_FILES
173- "${LIBTORCH_DIR} /lib/*.a"
140+ # "${LIBTORCH_DIR}/lib/*.a"
174141 "${LIBTORCH_DIR} /lib/*.dylib"
175142 "${LIBTORCH_DIR} /lib/*.so" )
176143
@@ -185,7 +152,6 @@ set(REQUIRED_LIBS
185152 "libtorch"
186153 "libtorch_cpu"
187154 "libc10"
188- "libtorch_global_deps"
189155)
190156
191157set (DISALLOWED_LIBS
@@ -206,11 +172,20 @@ foreach(lib_name IN LISTS LIBTORCH_ALL_LIBS)
206172 list (APPEND LIBTORCH_LIBS_LINKER_ARGS "-l${lib_name_short} " )
207173endforeach ()
208174
209- # cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
175+ cmake_print_variables (LIBTORCH_LIBS_LINKER_ARGS )
210176# cmake_print_variables(${BRIDGE_OBJECT_FILES})
211177# cmake_print_variables(BRIDGE_OBJECT_FILES)
212178
213179
180+ set (LIBTORCH_LIBS_LINKER_ARGS
181+ "-ltorch"
182+ # "-ltorch_cpu"
183+ # "-lc10"
184+ # "-ltorch_global_deps"
185+ )
186+ cmake_print_variables (LIBTORCH_LIBS_LINKER_ARGS )
187+
188+
214189add_executable (TorchBridge ${BRIDGE_DIR} /lib/Bridge.chpl )
215190add_dependencies (TorchBridge bridge )
216191add_dependencies (TorchBridge ChAI )
@@ -219,10 +194,10 @@ target_link_options(TorchBridge
219194 ${BRIDGE_DIR} /include/bridge.h
220195 ${BRIDGE_OBJECT_FILES}
221196 -L ${LIBTORCH_DIR} /lib
222- # "-ltorch"
223- # "-ltorch_cpu"
224- # "-lc10"
225- # "-ltorch_global_deps"
197+ "-ltorch"
198+ "-ltorch_cpu"
199+ "-lc10"
200+ "-ltorch_global_deps"
226201 ${LIBTORCH_LIBS_LINKER_ARGS}
227202 --ldflags "-Wl,-rpath,${LIBTORCH_DIR} /lib"
228203)
@@ -292,5 +267,33 @@ add_custom_command(
292267
293268# ./vgg images/frog.jpg
294269
270+ # add_subdirectory(bridge)
271+
295272add_subdirectory (examples )
296273add_subdirectory ("test" )
274+ add_subdirectory (demos )
275+
276+
277+
278+ add_executable (MPSTest
279+ "${PROJECT_ROOT_DIR} /test/tiny/mps.cpp"
280+ )
281+
282+ target_include_directories (MPSTest
283+ PRIVATE
284+ ${LIBTORCH_DIR} /include
285+ ${LIBTORCH_DIR} /include/torch/csrc/api/include
286+ )
287+
288+ target_link_directories (MPSTest
289+ PRIVATE
290+ ${LIBTORCH_DIR} /lib
291+ )
292+
293+ target_link_libraries (MPSTest
294+ PRIVATE
295+ -ltorch
296+ -ltorch_cpu
297+ -lc10
298+ -ltorch_global_deps
299+ )
0 commit comments