Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
734782c
Merge branch 'main' into im-dev
Iainmon Mar 26, 2025
edd77d7
Merge branch 'main' into im-dev
Iainmon Mar 26, 2025
474d18d
Test
Iainmon Mar 26, 2025
ac5a2f3
Add good makefile example in clib folder.
Iainmon Apr 7, 2025
d3c07f8
Add initial code for TorchBridge.
Iainmon Apr 8, 2025
42fa424
Linking to pytorch works, but there are C++ runtime errors in certain…
Iainmon Apr 8, 2025
0afe591
Working on linking all of pytorch as a static lib.
Iainmon Apr 8, 2025
c8ca2b9
Add pytorch.
Iainmon Apr 8, 2025
f94d02a
Remove pytorch submodule.
Iainmon Apr 9, 2025
0f7a6a6
Add change to bridge cmake file. Need help from Chapel team.
Iainmon Apr 9, 2025
d65f528
Change CXX version in CMAKE.
Iainmon Apr 9, 2025
a585c5c
Add makefile to bridge folder. Working now.
Iainmon Apr 10, 2025
a7d5ad3
Bridge module is working.
Iainmon Apr 10, 2025
62f0a59
Communication with libtorch now only uses tensor_result_t.
Iainmon Apr 10, 2025
7d36321
Change tensor_result_t to bridge_tensor_t.
Iainmon Apr 10, 2025
5594567
Made example method for passing data to and from libtorch.
Iainmon Apr 10, 2025
f68e3c0
Improve chapel/pytorch interop with unsafe pointers.
Iainmon Apr 11, 2025
81fa1f2
New makefile working for bridge. Going to look into CMake for entire …
Iainmon Apr 12, 2025
b21b993
Make working CMake file for torchbridge
Iainmon Apr 12, 2025
817887f
Remove build folder.
Iainmon Apr 12, 2025
9d0de35
Add build dir to gitignore.
Iainmon Apr 12, 2025
df78e35
Remove error cmake file.
Iainmon Apr 12, 2025
3231eb3
Add dependency for Bridge.
Iainmon Apr 12, 2025
7f26b95
Add installation version for libtorch.
Iainmon Apr 12, 2025
418a2c7
Remove comments in CMakeLists.
Iainmon Apr 12, 2025
f00e90a
Compiling with all pytorch shared libs works for mac.
Iainmon Apr 12, 2025
4ff027d
Compiling with all pytorch shared libs DOES NOT work for mac.
Iainmon Apr 12, 2025
7b5b696
Add linker args for more torch libs
Iainmon Apr 12, 2025
085fd9b
Add linker args for more torch libs
Iainmon Apr 12, 2025
6a8fffa
Add tiny test to cmake file.
Iainmon Apr 12, 2025
889c69b
Working with tensor handles in test file. Going to try using owned.
Iainmon Apr 12, 2025
9cde086
Tensor Handle allocator working better.
Iainmon Apr 12, 2025
8bfcc77
Improved CMake config.
Iainmon Apr 14, 2025
a534ee8
VGG can compile with libtorch.
Iainmon Apr 14, 2025
43a96f8
VGG example working well. see bottom of cmake file.
Iainmon Apr 14, 2025
333e829
VGG example working well with new maxpool code.
Iainmon Apr 14, 2025
f0ab1c1
Rolled back static pytorch building.
Iainmon Apr 16, 2025
d5f44de
Improve cmake config and add vgg to it.
Iainmon Apr 16, 2025
9b3c674
Improve speed of vgg model example by adding parallel model chunk loa…
Iainmon Apr 16, 2025
20cc949
Remove debug prints in model loading code.
Iainmon Apr 16, 2025
ca79bbe
Improve VGG example with cmake.
Iainmon Apr 16, 2025
f5efcae
Improve vgg model build experience. See end of CMakeList.txt.
Iainmon Apr 16, 2025
34d4447
Improve vgg code. Going to add pytorch tensor file loading.
Iainmon Apr 16, 2025
5aba91a
Add pytorch file format loading functionality for ndarrays.
Iainmon Apr 16, 2025
0b180cd
Improve something.
Iainmon Apr 16, 2025
b5060e9
Add incredible improvements to vgg and vridge.
Iainmon Apr 16, 2025
471a060
Add example for CMake building.
Iainmon Apr 18, 2025
8384057
Change
Iainmon Apr 18, 2025
b0bac60
Undo change.
Iainmon Apr 18, 2025
955ff2e
Fix ndarray reading from chdata float16.
Iainmon Apr 18, 2025
adb8dc5
VGG reading jpg images still not working. see test case for frog.
Iainmon Apr 18, 2025
f77ef9f
Add files so that we can merge. VGG state is broken, since it needs n…
Iainmon Apr 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ examples/vgg/models/vgg16/*.npy
new_sun.png
new_sun*.png
new_sun_hello.png
test/correspondence/gh.out
test/correspondence/gh.out
!Makefile
!modules/*
modules/build
!bridge/*
bridge/build
bridge/libtorch
# pytorch/
build/
libtorch/
.cache/
build-release/
libtorch_static/
examples/vgg/**/*.pt
296 changes: 296 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
cmake_minimum_required(VERSION 3.31 FATAL_ERROR)
# project(MyProject LANGUAGES CXX)

include(CMakePrintHelpers)

# set(CMAKE_VERBOSE_MAKEFILE ON)

if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()

set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}")
set(PROJECT_CACHE_DIR "${PROJECT_ROOT_DIR}/.cache")
set(LIBTORCH_DIR "${PROJECT_ROOT_DIR}/libtorch")
# set(LIBTORCH_STATIC_INSTALL_DIR "${PROJECT_ROOT_DIR}/libtorch_static")
set(BRIDGE_DIR "${PROJECT_ROOT_DIR}/bridge")


find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")

project(MyProject LANGUAGES CXX C CHPL)
message(STATUS "Using chpl: ${CMAKE_CHPL_COMPILER}")


if(APPLE)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
endif()
set(CMAKE_CXX_STANDARD 23)


include(LibTorchDL)
download_libtorch(
CACHE_DIR ${PROJECT_CACHE_DIR}
DESTINATION ${LIBTORCH_DIR}
)


include(FetchContent)
include(ExternalProject)


# if(NOT EXISTS "${LIBTORCH_STATIC_INSTALL_DIR}/lib/libtorch.a")
# ExternalProject_Add(
# pytorch
# GIT_REPOSITORY https://github.com/pytorch/pytorch.git
# GIT_TAG v2.6.0 # Example: specify a particular release
# UPDATE_COMMAND "" # Don’t auto-run 'git pull'
# PATCH_COMMAND "" # No custom patch step

# # DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/pytorch-download" # Where to download the repo

# # We need all PyTorch submodules. By default, ExternalProject won't do submodule init.
# # So we can do that in a separate step if we want a full build. For a minimal CPU build,
# # you might not need them all, but let's be safe:
# STEP_TARGETS clone
# # After 'clone', run "git submodule update --init --recursive"
# # to fetch all submodules.
# # We can use a little trick with COMMAND.
# # PATCH_COMMAND "git submodule update --init --recursive"

# # CMAKE_ARGS
# # -DBUILD_SHARED_LIBS=OFF # Build static libraries
# # -DBUILD_PYTHON=OFF # Don’t build Python bindings
# # -DBUILD_TEST=OFF # Don’t build tests
# # -DUSE_CUDA=OFF # Disable CUDA
# # -DUSE_CUDNN=OFF # Disable cuDNN
# # -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
# # # -DBUILD_BINARY=ON
# # # -DUSE_DISTRIBUTED=ON
# # # -DBUILD_STATIC_RUNTIME_BENCHMARK=ON
# # # -DBUILD_LITE_INTERPRETER=ON
# # # -DUSE_STATIC_MKL=ON
# # # -DSTATIC_DISPATCH_BACKEND=ON
# # # -DCAFFE2_USE_MSVC_STATIC_RUNTIME=ON
# # # -DUSE_DISTRIBUTED=ON
# # # -DCMAKE_BUILD_TYPE=Release
# # -DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
# # # -DCMAKE_POLICY_VERSION_MINIMUM=3.5
# CMAKE_ARGS
# -DBUILD_SHARED_LIBS=OFF # Build static libraries
# -DBUILD_PYTHON=OFF # Don’t build Python bindings
# -DBUILD_TEST=OFF # Don’t build tests
# -DUSE_CUDA=OFF # Disable CUDA
# -DUSE_CUDNN=OFF # Disable cuDNN
# -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
# -DCMAKE_BUILD_TYPE=Release
# # -DSTATIC_DISPATCH_BACKEND=ON
# -DCMAKE_INSTALL_PREFIX=${LIBTORCH_STATIC_INSTALL_DIR}
# -DCMAKE_POLICY_VERSION_MINIMUM=3.5

# INSTALL_DIR ${LIBTORCH_STATIC_INSTALL_DIR} # Where to install
# )
# endif()


file(GLOB CHAI_LIB_FILES "${PROJECT_ROOT_DIR}/lib/*.chpl")

# file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c "int main(int argc, const char **argv){ return 1; }\n")
# add_executable(ChAI ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c)
# # file(GLOB RESOURCE_FILES *.bmp *.wav moose.dat utf8.txt)
# foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
# add_custom_command(
# TARGET ChAI
# POST_BUILD
# COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:ChAI>
# )
# endforeach(RESOURCE_FILE)

# file(COPY ${CHAI_LIB_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})




add_custom_target(
ChAI
ALL
DEPENDS ${PROJECT_SOURCE_DIR}/lib
# SOURCES ${CHAI_LIB_FILES}
# COMMAND ${CMAKE_COMMAND} -E echo "Building ChAI"
)

foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
add_custom_command(
TARGET ChAI
POST_BUILD
# OUTPUT $<TARGET_FILE_DIR:ChAI>
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${RESOURCE_FILE} ${PROJECT_BINARY_DIR}/lib
# DEPENDS ${CHAI_LIB_FILES}
)
endforeach(RESOURCE_FILE)



# foreach(RESOURCE_FILE ${CHAI_LIB_FILES})
# add_custom_command(
# TARGET ChAI
# POST_BUILD
# COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:ChAI>
# )
# endforeach(RESOURCE_FILE)


function(watch)
set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS ${ARGV}
)
endfunction()

# cmake_print_variables(CHAI_LIB_FILES)

add_library(bridge STATIC ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)

target_include_directories(
bridge
PRIVATE
${BRIDGE_DIR}/include
${LIBTORCH_DIR}/include
${LIBTORCH_DIR}/include/torch/csrc/api/include
)

set(BRIDGE_OBJECT_FILES $<TARGET_OBJECTS:bridge>)




file(GLOB LIBTORCH_ALL_LIB_FILES
"${LIBTORCH_DIR}/lib/*.a"
"${LIBTORCH_DIR}/lib/*.dylib"
"${LIBTORCH_DIR}/lib/*.so")

set(LIBTORCH_ALL_LIBS "")
foreach(lib_path IN LISTS LIBTORCH_ALL_LIB_FILES)
get_filename_component(lib_name "${lib_path}" NAME_WE)
list(APPEND LIBTORCH_ALL_LIBS "${lib_name}")
endforeach()


set(REQUIRED_LIBS
"libtorch"
"libtorch_cpu"
"libc10"
"libtorch_global_deps"
)

set(DISALLOWED_LIBS
"libtorch_python"
)

set(LIBTORCH_LIBS_LINKER_ARGS "") # Will hold the list of "-l..." flags.
foreach(lib_name IN LISTS LIBTORCH_ALL_LIBS)
if(lib_name IN_LIST DISALLOWED_LIBS)
if(lib_name IN_LIST REQUIRED_LIBS)
message(FATAL_ERROR "Required lib ${lib_name} is disallowed.")
else()
message(STATUS "Skipping disallowed lib: ${lib_name}")
continue()
endif()
endif()
string(REGEX REPLACE "^lib" "" lib_name_short "${lib_name}")
list(APPEND LIBTORCH_LIBS_LINKER_ARGS "-l${lib_name_short}")
endforeach()

# cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
# cmake_print_variables(${BRIDGE_OBJECT_FILES})
# cmake_print_variables(BRIDGE_OBJECT_FILES)


add_executable(TorchBridge ${BRIDGE_DIR}/lib/Bridge.chpl)
add_dependencies(TorchBridge bridge)
add_dependencies(TorchBridge ChAI)
target_link_options(TorchBridge
PRIVATE
${BRIDGE_DIR}/include/bridge.h
${BRIDGE_OBJECT_FILES}
-L ${LIBTORCH_DIR}/lib
# "-ltorch"
# "-ltorch_cpu"
# "-lc10"
# "-ltorch_global_deps"
${LIBTORCH_LIBS_LINKER_ARGS}
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
)




add_executable(TinyLayerTest
${PROJECT_ROOT_DIR}/test/tiny/layer_test.chpl
${CHAI_LIB_FILES}
)
add_dependencies(TinyLayerTest bridge)
add_dependencies(TinyLayerTest ChAI)
target_link_options(TinyLayerTest
PRIVATE
--main-module layer_test.chpl
-M ${PROJECT_ROOT_DIR}/lib
${BRIDGE_DIR}/include/bridge.h
${BRIDGE_OBJECT_FILES}
-L ${LIBTORCH_DIR}/lib
${LIBTORCH_LIBS_LINKER_ARGS}
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
)
# 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"

# 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"

# chpl -o vgg test.chpl $(../../embed_libtorch.sh .)
# chpl --fast -o vgg test.chpl $(../../embed_libtorch.sh .)




set(CHAI_LINKER_ARGS
-M ${PROJECT_ROOT_DIR}/lib
${BRIDGE_DIR}/include/bridge.h
${BRIDGE_OBJECT_FILES}
-L ${LIBTORCH_DIR}/lib
${LIBTORCH_LIBS_LINKER_ARGS}
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
)



add_executable(vgg
"${PROJECT_ROOT_DIR}/examples/vgg/test.chpl"
${PROJECT_ROOT_DIR}/examples/vgg/VGG.chpl
${CHAI_LIB_FILES}
)
add_dependencies(vgg bridge)
add_dependencies(vgg ChAI)
target_link_options(vgg
PRIVATE
# -M ${PROJECT_ROOT_DIR}/examples/vgg
-svggExampleDir="${PROJECT_ROOT_DIR}/examples/vgg"
${CHAI_LINKER_ARGS}
)

add_custom_command(
TARGET vgg
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_ROOT_DIR}/examples/vgg/images"
"$<TARGET_FILE_DIR:vgg>/images"
COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/images to $<TARGET_FILE_DIR:vgg>/images"
)

# ./vgg images/frog.jpg

add_subdirectory(examples)
add_subdirectory("test")
Binary file added bridge/.DS_Store
Binary file not shown.
Loading
Loading