Skip to content

Commit

Permalink
Merge pull request #358 from leondavi/torch
Browse files Browse the repository at this point in the history
Torch WIP
  • Loading branch information
leondavi authored Aug 3, 2024
2 parents 1b5505f + ca4a653 commit 66c8f8c
Show file tree
Hide file tree
Showing 34 changed files with 887 additions and 80 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
179 changes: 179 additions & 0 deletions inputJsonsFiles/DistributedConfig/dc_FedTorchTest_5d_2s_2r_4c_4w.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"nerlnetSettings": {
"frequency": "300",
"batchSize": "100"
},
"mainServer": {
"port": "8900",
"args": ""
},
"apiServer": {
"port": "8901",
"args": ""
},
"devices": [
{
"name": "C0VM0",
"ipv4": "10.0.0.5",
"entities": "apiServer,mainServer"
},
{
"name": "Minion2",
"ipv4": "10.0.0.19",
"entities": "c1,r1,s1"
},
{
"name": "Minion3",
"ipv4": "10.0.0.20",
"entities": "c2,r2,s2"
},
{
"name": "Minion7",
"ipv4": "10.0.0.24",
"entities": "c3"
},
{
"name": "Minion8",
"ipv4": "10.0.0.25",
"entities": "c4"
}
],
"routers": [
{
"name": "r1",
"port": "8086",
"policy": "0"
},
{
"name": "r2",
"port": "8087",
"policy": "0"
}
],
"sources": [
{
"name": "s1",
"port": "8085",
"frequency": "200",
"policy": "0",
"epochs": "1",
"type": "0"
},
{
"name": "s2",
"port": "8085",
"frequency": "200",
"policy": "0",
"epochs": "1",
"type": "0"
}
],
"clients": [
{
"name": "c1",
"port": "8083",
"workers": "w1"
},
{
"name": "c2",
"port": "8083",
"workers": "w2"
},
{
"name": "c3",
"port": "8083",
"workers": "w3"
},
{
"name": "c4",
"port": "8083",
"workers": "w4"
}
],
"workers": [
{
"name": "w1",
"model_sha": "0771693392e898393c9b2b8235497537b5fbed1fd0c9a5a7ec6aab665d2c1896"
},
{
"name": "w2",
"model_sha": "0771693392e898393c9b2b8235497537b5fbed1fd0c9a5a7ec6aab665d2c1896"
},
{
"name": "w3",
"model_sha": "0771693392e898393c9b2b8235497537b5fbed1fd0c9a5a7ec6aab665d2c1896"
},
{
"name": "w4",
"model_sha": "c081daf49b8332585243b68cb828ebc9b947528601a6852688cea0312b3e3914"
}
],
"model_sha": {
"0771693392e898393c9b2b8235497537b5fbed1fd0c9a5a7ec6aab665d2c1896": {
"modelType": "0",
"_doc_modelType": " nn:0 | approximation:1 | classification:2 | forecasting:3 | image_classification:4 | text_classification:5 | text_generation:6 | auto_association:7 | autoencoder:8 | ae_classifier:9 |",
"modelArgs": "",
"_doc_modelArgs": "Extra arguments to model",
"layersSizes": "5,6,6,4,3",
"_doc_layersSizes": "List of postive integers [L0, L1, ..., LN]",
"layerTypesList": "1,3,3,3,3",
"_doc_LayerTypes": " Default:0 | Scaling:1 | Conv:2 | Perceptron:3 | Pooling:4 | Probabilistic:5 | LSTM:6 | Reccurrent:7 | Unscaling:8 | Flatten:9 | Bounding:10 |",
"layers_functions": "1,8,8,8,11",
"_doc_layers_functions_activation": " Threshold:1 | Sign:2 | Logistic:3 | Tanh:4 | Linear:5 | ReLU:6 | eLU:7 | SeLU:8 | Soft-plus:9 | Soft-sign:10 | Hard-sigmoid:11 |",
"_doc_layer_functions_pooling": " none:1 | Max:2 | Avg:3 |",
"_doc_layer_functions_probabilistic": " Binary:1 | Logistic:2 | Competitive:3 | Softmax:4 |",
"_doc_layer_functions_scaler": " none:1 | MinMax:2 | MeanStd:3 | STD:4 | Log:5 |",
"lossMethod": "2",
"_doc_lossMethod": " SSE:1 | MSE:2 | NSE:3 | MinkowskiE:4 | WSE:5 | CEE:6 |",
"lr": "0.001",
"_doc_lr": "Positve float",
"epochs": "1",
"_doc_epochs": "Positve Integer",
"optimizer": "5",
"_doc_optimizer": " GD:0 | CGD:1 | SGD:2 | QuasiNeuton:3 | LVM:4 | ADAM:5 |",
"optimizerArgs": "none",
"_doc_optimizerArgs": "String",
"infraType": "0",
"_doc_infraType": " opennn:0 | wolfengine:1 |",
"distributedSystemType": "1",
"_doc_distributedSystemType": " none:0 | fedClientAvg:1 | fedServerAvg:2 |",
"distributedSystemArgs": "SyncMaxCount=50",
"_doc_distributedSystemArgs": "String",
"distributedSystemToken": "9922u",
"_doc_distributedSystemToken": "Token that associates distributed group of workers and parameter-server"
},
"c081daf49b8332585243b68cb828ebc9b947528601a6852688cea0312b3e3914": {
"modelType": "0",
"_doc_modelType": " nn:0 | approximation:1 | classification:2 | forecasting:3 | image_classification:4 | text_classification:5 | text_generation:6 | auto_association:7 | autoencoder:8 | ae_classifier:9 |",
"modelArgs": "",
"_doc_modelArgs": "Extra arguments to model",
"layersSizes": "5,6,6,4,3",
"_doc_layersSizes": "List of postive integers [L0, L1, ..., LN]",
"layerTypesList": "1,3,3,3,3",
"_doc_LayerTypes": " Default:0 | Scaling:1 | Conv:2 | Perceptron:3 | Pooling:4 | Probabilistic:5 | LSTM:6 | Reccurrent:7 | Unscaling:8 | Flatten:9 | Bounding:10 |",
"layers_functions": "1,8,8,8,11",
"_doc_layers_functions_activation": " Threshold:1 | Sign:2 | Logistic:3 | Tanh:4 | Linear:5 | ReLU:6 | eLU:7 | SeLU:8 | Soft-plus:9 | Soft-sign:10 | Hard-sigmoid:11 |",
"_doc_layer_functions_pooling": " none:1 | Max:2 | Avg:3 |",
"_doc_layer_functions_probabilistic": " Binary:1 | Logistic:2 | Competitive:3 | Softmax:4 |",
"_doc_layer_functions_scaler": " none:1 | MinMax:2 | MeanStd:3 | STD:4 | Log:5 |",
"lossMethod": "2",
"_doc_lossMethod": " SSE:1 | MSE:2 | NSE:3 | MinkowskiE:4 | WSE:5 | CEE:6 |",
"lr": "0.001",
"_doc_lr": "Positve float",
"epochs": "1",
"_doc_epochs": "Positve Integer",
"optimizer": "5",
"_doc_optimizer": " GD:0 | CGD:1 | SGD:2 | QuasiNeuton:3 | LVM:4 | ADAM:5 |",
"optimizerArgs": "none",
"_doc_optimizerArgs": "String",
"infraType": "0",
"_doc_infraType": " opennn:0 | wolfengine:1 |",
"distributedSystemType": "2",
"_doc_distributedSystemType": " none:0 | fedClientAvg:1 | fedServerAvg:2 |",
"distributedSystemArgs": "SyncMaxCount=50",
"_doc_distributedSystemArgs": "String",
"distributedSystemToken": "9922u",
"_doc_distributedSystemToken": "Token that associates distributed group of workers and parameter-server"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"experimentName": "synthetic_3_gausians",
"experimentType": "classification",
"batchSize": 100,
"csvFilePath": "/tmp/nerlnet/data/NerlnetData-master/nerlnet/synthetic_norm/synthetic_full.csv",
"numOfFeatures": "5",
"numOfLabels": "3",
"headersNames": "Norm(0:1),Norm(4:1),Norm(10:3)",
"Phases":
[
{
"phaseName": "training_phase",
"phaseType": "training",
"sourcePieces":
[
{
"sourceName": "s1",
"startingSample": "0",
"numOfBatches": "500",
"workers": "w1,w2",
"nerltensorType": "float"
},
{
"sourceName": "s2",
"startingSample": "0",
"numOfBatches": "500",
"workers": "w3",
"nerltensorType": "float"
}
]
},
{
"phaseName": "prediction_phase",
"phaseType": "prediction",
"sourcePieces":
[
{
"sourceName": "s1",
"startingSample": "50000",
"numOfBatches": "500",
"workers": "w1,w2",
"nerltensorType": "float"
},
{
"sourceName": "s2",
"startingSample": "50000",
"numOfBatches": "500",
"workers": "w3",
"nerltensorType": "float"
}
]
}
]
}
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()
46 changes: 46 additions & 0 deletions src_cpp/torchBridge/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#****************************************************
# 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
"nerltensorTorchDefs.h"
"nifppNerlTensorTorch.h"
"torchNIF.h"
"torchNIF.cpp"
"NerlWorkerTorch.h"
"NerlWorkerTorch.cpp"
"NerlWorkerTorchNIF.h"
)

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})
1 change: 1 addition & 0 deletions src_cpp/torchBridge/NerlWorkerTorch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "NerlWorkerTorch.h"
Loading

0 comments on commit 66c8f8c

Please sign in to comment.