Skip to content

Commit

Permalink
Merge branch 'GravityNNClean' of github.com:coin-or/Gravity into Grav…
Browse files Browse the repository at this point in the history
…ityNNClean
  • Loading branch information
hhijazi committed Oct 4, 2023
2 parents 8b323b6 + f03474e commit 2338f28
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 74 deletions.
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ option(OpenMPI "Link to OpenMPI libraries" OFF)
option(OPT_PARSER "Link to OPT_PARSER libraries" OFF)

if (OpenMPI)
SET(CMAKE_CXX_COMPILER mpicxx)
SET(CMAKE_C_COMPILER mpicc)
find_program(MPICXX NAMES mpicxx HINTS /usr/local/bin)
find_program(MPICC NAMES mpicc HINTS /usr/local/bin)
SET(CMAKE_CXX_COMPILER ${MPICXX})
SET(CMAKE_C_COMPILER ${MPICC})
message("Compilers used: ${CMAKE_C_COMPILER} and ${CMAKE_CXX_COMPILER}")
message(STATUS "Enable OpenMPI")
add_definitions(-DUSE_MPI)
# find_package(OpenMPI REQUIRED)
# include_directories(${OpenMPI_INCLUDE_DIRS})
# set(LIBS ${LIBS} ${OpenMPI_LIBRARIES})
find_package(OpenMPI REQUIRED)
include_directories(${OpenMPI_INCLUDE_DIRS})
set(LIBS ${LIBS} ${OpenMPI_LIBRARIES})
endif(OpenMPI)


Expand Down
45 changes: 17 additions & 28 deletions examples/MachineLearning/GravityNN/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
find_package(Protobuf REQUIRED)

file(DOWNLOAD "https://raw.githubusercontent.com/onnx/onnx/master/onnx/onnx.proto3" "${CMAKE_CURRENT_BINARY_DIR}/onnx.proto")
PROTOBUF_GENERATE_CPP(ONNX_PROTO_SRCS ONNX_PROTO_HDRS ${CMAKE_CURRENT_BINARY_DIR}/onnx.proto)
add_custom_target(proto_dep DEPENDS ${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

find_package(PkgConfig QUIET REQUIRED)
pkg_check_modules(PC_Protobuf protobuf)
message(${PC_Protobuf_INCLUDEDIR})

include_directories(${PC_Protobuf_INCLUDEDIR} ${ONNX_PROTO_HDRS} ${ONNX_PROTO_SRCS})
include(${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/cmake/CPM.cmake)

include(FetchContent)
FetchContent_Declare(
cli11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG v2.3.2
CPMAddPackage("gh:CLIUtils/CLI11#v2.3.2")
CPMAddPackage("gh:onnx/onnx#v1.14.0")

file(GLOB NN_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/Layers/*.hpp
)

file(GLOB NN_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/Layers/*.hpp)
FetchContent_MakeAvailable(cli11)
message(${cli11_SOURCE_DIR})
add_executable(
GravityNN
MachineLearning/GravityNN/GravityNN.cpp
Expand All @@ -29,20 +17,21 @@ add_executable(
${NN_SOURCES}
)

add_dependencies(GravityNN proto_dep)


target_include_directories(GravityNN PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/Layers ${ONNX_PROTO_HDRS} ${cli11_SOURCE_DIR}/include)
target_link_libraries(GravityNN ${PROTOBUF_LIBRARIES} gravity CLI11)
target_link_libraries(GravityNN gravity CLI11 onnx)

find_package(OpenMP)
add_executable(
tighten
MachineLearning/GravityNN/tighten.cpp
${ONNX_PROTO_SRCS}
${ONNX_PROTO_HDRS}
${NN_SOURCES}
${ONNX_PROTO_SRCS}
${ONNX_PROTO_HDRS}
${NN_SOURCES}
)
add_dependencies(tighten proto_dep)

target_include_directories(tighten PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network ${CMAKE_CURRENT_SOURCE_DIR}/MachineLearning/GravityNN/network/Layers ${ONNX_PROTO_HDRS} ${cli11_SOURCE_DIR}/include)
target_link_libraries(tighten ${PROTOBUF_LIBRARIES} gravity OpenMP::OpenMP_CXX CLI11)
target_link_libraries(tighten gravity CLI11 onnx)
if (OpenMPI)
include_directories(${MPI_INCLUDE_PATH})
target_link_libraries(tighten ${MPI_LIBRARIES})
endif(OpenMPI)
2 changes: 1 addition & 1 deletion examples/MachineLearning/GravityNN/GravityNN.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include <fstream>
#include <onnx.pb.h>
#include <onnx/onnx_pb.h>
#include <vector>
#include <network/NeuralNet.hpp>
#include <gravity/solver.h>
Expand Down
33 changes: 33 additions & 0 deletions examples/MachineLearning/GravityNN/cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CPM_DOWNLOAD_VERSION 0.38.2)

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

function(download_cpm)
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endfunction()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
download_cpm()
else()
# resume download if it previously failed
file(READ ${CPM_DOWNLOAD_LOCATION} check)
if("${check}" STREQUAL "")
download_cpm()
endif()
unset(check)
endif()

include(${CPM_DOWNLOAD_LOCATION})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <fstream>
#include <onnx.pb.h>
#include <onnx/onnx_pb.h>
#include <vector>
#include <network/Tensor.hpp>
#include <network/IndexContainer.hpp>
Expand Down
3 changes: 2 additions & 1 deletion examples/MachineLearning/GravityNN/network/Layers/Linear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class GEMM : public Layer {
NN.add(Gemm.in(inds["Constr"]) == 0);
// add IBP bounds
auto f = x.in(inds["In"])*w.in(inds["B"]) + w.in(inds["C"]);
x.in(inds["Out"]).copy_bounds(*f._all_range);
// FIXME: add tighter bounds between existing one and the IBP one.
// x.in(inds["Out"]).copy_bounds(*f._all_range);
}

Tensor *A, *B, *C = nullptr; // Inputs
Expand Down
3 changes: 2 additions & 1 deletion examples/MachineLearning/GravityNN/network/Layers/Unary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ class Relu : public Layer {
// add IBP bounds
gravity::func<double> g = x.in(inds["In"]);
auto f = ReLU(g);
x.in(inds["Out"]).copy_bounds(*f._all_range);
// FIXME: add tighter bounds between existing one and the IBP one.
// x.in(inds["Out"]).copy_bounds(*f._all_range);

}

Expand Down
4 changes: 2 additions & 2 deletions examples/MachineLearning/GravityNN/network/NeuralNet.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <onnx.pb.h>
#include <onnx/onnx_pb.h>
#include <vector>
#include <network/Layers/LayerBase.hpp>
#include <network/Layers/Linear.hpp>
Expand Down Expand Up @@ -378,4 +378,4 @@ class NeuralNet {

std::map<std::string, Layer*> output_to_layer;
std::set<Layer*> folded_layers;
};
};
2 changes: 1 addition & 1 deletion examples/MachineLearning/GravityNN/network/Tensor.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <fstream>
#include <onnx.pb.h>
#include <onnx/onnx_pb.h>
#include <vector>
#include <utils/utils.hpp>
#include <gravity/param.h>
Expand Down
8 changes: 8 additions & 0 deletions examples/MachineLearning/GravityNN/network/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class Bound {
this->side = side;
this->old_value = value;
}

Bound(std::string layer_name, std::string neuron_name, double value, double old_value, Side side) {
this->layer_name = layer_name;
this->neuron_name = neuron_name;
this->value = value;
this->side = side;
this->old_value = old_value;
}

std::string layer_name;
std::string neuron_name;
Expand Down
Loading

0 comments on commit 2338f28

Please sign in to comment.