未验证 提交 91e9814a 编写于 作者: R rical730 提交者: GitHub

generate static library libEvoKit_torch.a (#249)

* generate static library libEvoKit_torch.a

* fix end of the file

* find EvoKit libraries by CMAKE_PREFIX_PATH

* set library path EVOKIT_LIBRARY
上级 43102ee4
cmake_minimum_required (VERSION 2.6)
project (EvoKit)
set(TARGET parallel_main)
########## options ##########
option(WITH_PADDLE "Compile EvoKit with PaddleLite framework." OFF)
......@@ -43,34 +42,48 @@ if (WITH_PADDLE)
"${PROJECT_SOURCE_DIR}/inference_lite_lib/third_party/mklml/include")
link_directories("${PROJECT_SOURCE_DIR}/inference_lite_lib/cxx/lib"
"${PROJECT_SOURCE_DIR}/inference_lite_lib/third_party/mklml/lib")
file(GLOB framework_src "paddle/src/*.cc")
set(demo "${PROJECT_SOURCE_DIR}/demo/paddle/cartpole_solver_parallel.cc")
#set(demo "${PROJECT_SOURCE_DIR}/demo/paddle/cartpole_async_solver.cc")
set(TARGET EvoKit_paddle)
########## Torch config ##########
elseif (WITH_TORCH)
list(APPEND CMAKE_PREFIX_PATH "./libtorch")
find_package(Torch REQUIRED ON)
# list(APPEND CMAKE_PREFIX_PATH "./libtorch")
# find_package(Torch REQUIRED ON) # TODO: not necessary for now
include_directories("torch/include")
include_directories("demo/torch")
file(GLOB framework_src "torch/src/*.cc")
set(demo "${PROJECT_SOURCE_DIR}/demo/torch/cartpole_solver_parallel.cc")
set(TARGET EvoKit_torch)
else ()
message("ERROR: You should choose at least one framework to compile EvoKit.")
endif()
add_executable(${TARGET} ${demo} ${src} ${framework_src})
add_library(${TARGET} STATIC ${src} ${framework_src})
target_link_libraries(${TARGET} gflags protobuf pthread glog)
########## PaddleLite libraries ##########
if (WITH_PADDLE)
target_link_libraries(${TARGET} -lpaddle_full_api_shared)
target_link_libraries(${TARGET} -lmklml_intel)
target_link_libraries(${TARGET} -ldl)
########## Torch libraries ##########
elseif (WITH_TORCH)
target_link_libraries(${TARGET} "${TORCH_LIBRARIES}")
endif()
# ########## PaddleLite libraries ##########
# if (WITH_PADDLE)
# target_link_libraries(${TARGET} -lpaddle_full_api_shared)
# target_link_libraries(${TARGET} -lmklml_intel)
# target_link_libraries(${TARGET} -ldl)
# ########## Torch libraries ##########
# elseif (WITH_TORCH)
# target_link_libraries(${TARGET} "${TORCH_LIBRARIES}")
# endif()
file(GLOB include "core/include/evo_kit/*.h")
file(GLOB proto_include "core/proto/evo_kit/*.h")
file(GLOB torch_include "torch/include/evo_kit/*.h")
file(GLOB paddle_include "paddle/include/evo_kit/*.h")
file(GLOB benchmark_include "benchmark/*.h")
file(GLOB findcmake "cmake/Torch/*.cmake")
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/libevokit")
install(TARGETS ${TARGET} ARCHIVE DESTINATION "lib")
install(FILES ${include} ${proto_include} DESTINATION "include/evo_kit")
install(FILES ${torch_include} DESTINATION "torch/evo_kit")
install(FILES ${paddle_include} DESTINATION "paddle/evo_kit")
install(FILES ${benchmark_include} DESTINATION "include")
install(FILES ${findcmake} DESTINATION "cmake/Torch")
# FindEvoKit
# -------
#
# Finds the EvoKit library
#
# This will define the following variables:
#
# EVOKIT_FOUND -- True if the system has the EvoKit library
# EVOKIT_INCLUDE_DIRS -- The include directories for EvoKit
# EVOKIT_LIBRARY -- Libraries to link against
#
# and the following imported targets:
#
# EvoKit
include(FindPackageHandleStandardArgs)
if (DEFINED ENV{EVOKIT_INSTALL_PREFIX})
set(EVOKIT_INSTALL_PREFIX $ENV{EVOKIT_INSTALL_PREFIX})
else()
# Assume we are in <install-prefix>/cmake/Torch/EvoKitConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(EVOKIT_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE)
endif()
# Include directories.
if (EXISTS "${EVOKIT_INSTALL_PREFIX}/include")
set(EVOKIT_INCLUDE_DIRS
${EVOKIT_INSTALL_PREFIX}/include
${EVOKIT_INSTALL_PREFIX}/torch)
else()
set(EVOKIT_INCLUDE_DIRS
${EVOKIT_INSTALL_PREFIX}/include
${EVOKIT_INSTALL_PREFIX}/torch)
endif()
find_library(EVOKIT_LIBRARY libEvoKit_torch.a PATHS "${EVOKIT_INSTALL_PREFIX}/lib")
include_directories("${EVOKIT_INSTALL_PREFIX}/torch")
include_directories("${EVOKIT_INSTALL_PREFIX}/include")
find_package_handle_standard_args(EvoKit DEFAULT_MSG EVOKIT_LIBRARY EVOKIT_INCLUDE_DIRS)
message(STATUS "EVOKIT_FOUND: ${EVOKIT_FOUND}")
message(STATUS "EVOKIT_INCLUDE_DIRS: ${EVOKIT_INCLUDE_DIRS}")
message(STATUS "EVOKIT_LIBRARY: ${EVOKIT_LIBRARY}")
cmake_minimum_required (VERSION 2.6)
project (EvoKit_demo)
set(TARGET parallel_main)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
list(APPEND CMAKE_PREFIX_PATH "./libtorch")
find_package(Torch REQUIRED ON)
set(demo "${PROJECT_SOURCE_DIR}/cartpole_solver_parallel.cc")
########## main ##########
add_executable(${TARGET} ${demo} ${framework_src})
target_link_libraries(${TARGET} gflags protobuf pthread glog)
########## Torch libraries ##########
target_link_libraries(${TARGET} "${TORCH_LIBRARIES}")
########## EvoKit libraries ##########
list(APPEND CMAKE_PREFIX_PATH "./libevokit/cmake/Torch")
find_package(EvoKit)
target_link_libraries(${TARGET} "${EVOKIT_LIBRARY}")
......@@ -52,7 +52,7 @@ int main(int argc, char* argv[]) {
auto model = std::make_shared<Model>(4, 2);
std::shared_ptr<ESAgent<Model>> agent = std::make_shared<ESAgent<Model>>(model,
"./demo/cartpole_config.prototxt");
"./cartpole_config.prototxt");
// Clone agents to sample (explore).
std::vector<std::shared_ptr<ESAgent<Model>>> sampling_agents;
......
#!/bin/bash
cd demo/torch
#---------------libtorch-------------#
if [ ! -d "./libtorch" ];then
echo "Cannot find the torch library: ./libtorch"
echo "Downloading Torch library"
wget -q https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.4.0%2Bcpu.zip
unzip -q libtorch-cxx11-abi-shared-with-deps-1.4.0+cpu.zip
rm -rf libtorch-cxx11-abi-shared-with-deps-1.4.0+cpu.zip
echo "Torch library Downloaded"
fi
#---------------libevokit-------------#
cp -r ../../libevokit ./
if [ ! -d "./libevokit" ];then
echo "Cannot find the EvoKit library: ./libevokit"
echo "Please put the EvoKit libraray to current folder according the instruction in README" # TODO: readme
exit 1
fi
# proto
cp ../cartpole_config.prototxt ./
#----------------build---------------#
rm -rf build
mkdir build
cd build
cmake ../
make -j10
cd -
#-----------------run----------------#
./build/parallel_main
cd ../..
......@@ -20,21 +20,13 @@ if [ $1 = "paddle" ]; then
FLAGS=" -DWITH_PADDLE=ON"
elif [ $1 = "torch" ]; then
#---------------libtorch-------------#
if [ ! -d "./libtorch" ];then
echo "Cannot find the torch library: ./libtorch"
echo "Downloading Torch library"
wget -q https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.4.0%2Bcpu.zip
unzip -q libtorch-cxx11-abi-shared-with-deps-1.4.0+cpu.zip
rm -rf libtorch-cxx11-abi-shared-with-deps-1.4.0+cpu.zip
echo "Torch library Downloaded"
fi
FLAGS=" -DWITH_TORCH=ON"
else
echo "Invalid arguments. [paddle/torch]"
exit 0
fi
#----------------protobuf-------------#
cd core/proto/
protoc evo_kit/evo_kit.proto --cpp_out .
......@@ -47,7 +39,5 @@ mkdir build
cd build
cmake ../ ${FLAGS}
make -j10
make install
cd -
#-----------------run----------------#
./build/parallel_main
cmake_minimum_required (VERSION 2.6)
project (DeepES)
project (EvoKit_demo)
set(TARGET unit_test_main)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
......@@ -20,16 +19,15 @@ list(APPEND CMAKE_PREFIX_PATH "../libtorch")
find_package(Torch REQUIRED ON)
# include and source
file(GLOB test_src "../test/src/*.cc")
file(GLOB core_src "../core/src/*.cc" "../core/proto/evo_kit/*.cc")
file(GLOB agent_src "../torch/src/*.cc")
include_directories("../torch/include")
include_directories("../core/include")
include_directories("../core/proto")
include_directories("../benchmark")
include_directories("../test/include")
include_directories("${PROJECT_SOURCE_DIR}/include")
file(GLOB test_src "${PROJECT_SOURCE_DIR}/src/*.cc")
# make
add_executable(${TARGET} "unit_test.cc" ${core_src} ${agent_src} ${test_src})
target_link_libraries(${TARGET} gflags protobuf pthread glog gtest "${TORCH_LIBRARIES}")
########## EvoKit libraries ##########
list(APPEND CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/libevokit/cmake/Torch")
find_package(EvoKit)
target_link_libraries(${TARGET} "${EVOKIT_LIBRARY}")
......@@ -16,13 +16,26 @@ cd core/proto/
protoc evo_kit/evo_kit.proto --cpp_out .
cd -
#----------------build---------------#
sh scripts/lib_install.sh torch
#----------------build test---------------#
cd test
cp -r ../libevokit ./
if [ ! -d "./libevokit" ];then
echo "Cannot find the EvoKit library: ./libevokit"
echo "Please put the EvoKit libraray to current folder according the instruction in README" # TODO: readme
exit 1
fi
rm -rf build
mkdir build
cd build
cmake ../test
cmake ../
make -j10
#-----------------run----------------#
./unit_test_main
cd ..
......@@ -64,7 +64,7 @@ protected:
void init_agent(const int in_dim, const int out_dim, const int h1_size, const int h2_size) {
std::shared_ptr<Model> model = std::make_shared<Model>(in_dim, out_dim, h1_size, h2_size);
agent = std::make_shared<ESAgent<Model>>(model, "../test/prototxt/torch_sin_config.prototxt");
agent = std::make_shared<ESAgent<Model>>(model, "../prototxt/torch_sin_config.prototxt");
}
void train_agent(std::string config_path) {
......@@ -130,14 +130,14 @@ protected:
};
TEST_F(TorchDemoTest, TrainingEffectUseNormalSampling) {
train_agent("../test/prototxt/torch_sin_config.prototxt");
train_agent("../prototxt/torch_sin_config.prototxt");
EXPECT_LT(train_loss(), 0.05);
EXPECT_LT(test_loss(), 0.05);
EXPECT_LT(train_test_gap(), 0.03);
}
TEST_F(TorchDemoTest, TrainingEffectTestUseTableSampling) {
train_agent("../test/prototxt/torch_sin_cached_config.prototxt");
train_agent("../prototxt/torch_sin_cached_config.prototxt");
EXPECT_LT(train_loss(), 0.05);
EXPECT_LT(test_loss(), 0.05);
EXPECT_LT(train_test_gap(), 0.03);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册