Skip to content

Commit

Permalink
[TORCH] Add librotch to cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
leondavi committed Jul 4, 2024
1 parent 62330e9 commit 57f5753
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O2")

# Options
option(NERLWOLF "Use Wolfram Engine workers extension" OFF)
option(NERLTORCH "use libtorch installed to /usr/local/lib/libtorch" OFF)
option(USE_OpenMP "Use-OpenMP" ON)

#add_compile_definitions(EIGEN_MAX_ALIGN_BYTES=8) #Open this line for RASPI
Expand All @@ -42,3 +43,8 @@ if (NERLWOLF)
add_library(nerlnet_wolf SHARED $<TARGET_OBJECTS:wolframBridge>)
target_link_libraries(nerlnet_wolf PUBLIC wolframBridge)
endif()

if (NERLTORCH)
add_library(nerlnet_torch SHARED $<TARGET_OBJECTS:torchBridge>)
target_link_libraries(nerlnet_torch PUBLIC torchBridge)
endif()
29 changes: 27 additions & 2 deletions NerlnetBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ INPUT_DATA_DIR="inputDataDir"
Branch="master"
JobsNum=4
NerlWolf=OFF
NerlTorch=OFF

help()
{
echo "-------------------------------------" && echo "Nerlnet Build" && echo "-------------------------------------"
echo "Usage:"
echo "--p or --pull Warning! this uses checkout -f! and branch name checkout to branch $Branch and pull the latest"
echo "--w or --wolf wolfram engine workers extension (nerlwolf)"
echo "--w or --wolf wolfram engine workers infra (nerlwolf)"
echo "--t or --torch torch workers infra (nerltorch)"
echo "--j or --jobs number of jobs to cmake build"
echo "--c or --clean remove build directory"
exit 2
}

print()
{
echo "$NERLNET_BUILD_PREFIX $1"
}

gitOperations()
{
echo "$NERLNET_PREFIX Warning! git checkout -f is about to be executed"
Expand Down Expand Up @@ -67,6 +74,8 @@ print_help()
printf '\t%s\n' "-j, --jobs: number of jobs (default: '4')"
printf '\t%s\n' "-p, --pull: pull from branch (default: '4')"
printf '\t%s\n' "-w, --wolf: wolfram engine extension build (default: 'off')"
printf '\t%s\n' "-t, --torch: torch engine extension build (default: 'off')"
printf '\t%s\n' "-c, --clean: clean build directory (default: 'off')"

}

Expand Down Expand Up @@ -110,6 +119,17 @@ parse_commandline()
-w*)
NerlWolf="${_key##-j}"
;;
-t|--torch)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
NerlTorch="$2"
shift
;;
--torch=*)
NerlTorch="${_key##--jobs=}"
;;
-t*)
NerlTorch="${_key##-j}"
;;
-j|--jobs)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
JobsNum="$2"
Expand Down Expand Up @@ -157,6 +177,11 @@ else
sed -i "s/^.*\(${OPTION}.*$\)/#\1/" CMakeLists.txt
fi

if [[ ! $NerlTorch =~ OFF ]]; then
print "NerlTorch is enabled"
print "Installation directory points to /usr/local/lib/libtorch"
fi

if command -v python3 >/dev/null 2>&1; then
echo "$NERLNET_BUILD_PREFIX Python 3 is installed"
# Generate auto-generated files
Expand Down Expand Up @@ -187,7 +212,7 @@ fi
echo "$NERLNET_BUILD_PREFIX Building Nerlnet Library"
echo "$NERLNET_BUILD_PREFIX Cmake command of Nerlnet NIFPP"
set -e
cmake -S . -B build/release -DNERLWOLF=$NerlWolf -DCMAKE_BUILD_TYPE=RELEASE
cmake -S . -B build/release -DNERLWOLF=$NerlWolf -DNERLTORCH=$NerlTorch -DCMAKE_BUILD_TYPE=RELEASE
cd build/release
echo "$NERLNET_BUILD_PREFIX Script CWD: $PWD"
echo "$NERLNET_BUILD_PREFIX Build Nerlnet"
Expand Down
5 changes: 5 additions & 0 deletions src_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ add_subdirectory(source)
if(NERLWOLF)
message("[NERLNET] Wolfram Engine nif extension is enabled")
add_subdirectory(wolframBridge)
endif()

if(NERLTORCH)
message("[NERLNET] Libtorch nif extension is enabled")
add_subdirectory(torchBridge)
endif()
41 changes: 41 additions & 0 deletions src_cpp/torchBridge/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#****************************************************
# Authors: David Leon
# 29/10/2021
#
# @copyright Copyright (c) 2021 Nerlnet
# *****************************************************/

project(torchBridge)

set(NIFPP_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../nifpp/")
set(SIMPLE_LOGGER_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../simple-cpp-logger/include")
set(COMMON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../common")
set(Torch_DIR "/usr/local/lib/libtorch/share/cmake/Torch")

find_package(Torch REQUIRED "TorchConfig.cmake" CONFIG )

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_FLAGS "-fpic")
set(ERL_NIF_DEFAULT_LOCATION "/usr/local/lib/erlang/usr/include")

# cpp Simple logger options
add_definitions( -D LOGGER_MAX_LOG_LEVEL_PRINTED=6 )
add_definitions( -D LOGGER_PREFIX_LEVEL=2 )
add_definitions( -D LOGGER_ENABLE_COLORS=1 )
add_definitions( -D LOGGER_ENABLE_COLORS_ON_USER_HEADER=0 )

set(SRC_CODE
"torchNIF.h"
"torchNIF.cpp"
)

add_library(${PROJECT_NAME} SHARED ${SRC_CODE})

target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}" common)

# Include NIF, OpenNN and Simple Cpp Logger
target_include_directories(${PROJECT_NAME} PUBLIC
${COMMON_PATH}
${NIFPP_PATH}
${SIMPLE_LOGGER_PATH}
${ERL_NIF_DEFAULT_LOCATION})
13 changes: 13 additions & 0 deletions src_cpp/torchBridge/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Torch Bridge

(Unsupported yet)
This is a bridge that extends Nerlnet to support libtorch as cpp neural network library.

### Installation

1. Go to [Pytorch site](https://pytorch.org/get-started/locally/) and download libtorch
2. Extract libotorch to ```/usr/local/lib/libtorch```
3. Execute Nerlnet build with --t=ON or --torch=ON
4. Select worker infrastructe in Nerlplanner as torch.

For apple silicon use [this repo](https://github.com/Nerlnet/libtorch_compiled) to download compiled libtorch.
Empty file.
1 change: 1 addition & 0 deletions src_cpp/torchBridge/torchNIF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <torch/torch.h>

0 comments on commit 57f5753

Please sign in to comment.