Unverified Commit 85c29ba7 authored by Yixing Lao's avatar Yixing Lao Committed by GitHub

static link argon-transformer (#430)

parent e81cf69a
# Copyright 2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# To build ngraph with Argon backend
# * Option 1: Build argon-api from scratch
# ```
# cmake -DNGRAPH_ARGON_ENABLE=True ..
# make -j
# ```
# * Option 2: Using pre-build argon-api
# Use PREBUILD_ARGON_PATH to specify the pre-build argon-api path
# ```
# cmake -DNGRAPH_ARGON_ENABLE=True -DPREBUILD_ARGON_PATH=$HOME/dev/system/_out/debug-x86_64-Linux ..
# make -j
# ```
# Enable ExternalProject CMake module
include(ExternalProject)
if (NGRAPH_ARGON_ENABLE)
# Repository
set(ARGON_CMAKE_GIT_REPOSITORY git@github.com:NervanaSystems/argon-transformer.git)
# Set argon_transformer tag
# Notes:
# - Before we have ngraph CI job for argon transformer, ngraph master might not be
# compatible with argon transformer. To ensure compatibility, checkout the ngraph commit point
# where the following `ARGON_CMAKE_GIT_TAG` is set and build ngraph with argon using this
# commit.
# - After we have ngraph CI job for argon transformer, ngraph master will be compatible with
# argon transformer guaranteed by CI.
set(ARGON_CMAKE_GIT_TAG 213f85d536d1525c8e83edb08164ae0aece749cf) # Thu Jan 11 2018
set(ARGON_CMAKE_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/argon)
if (NOT DEFINED PREBUILD_ARGON_PATH)
set(PREBUILD_ARGON_PATH "")
endif()
# Print
message(STATUS "NGRAPH_INCLUDE_PATH: ${NGRAPH_INCLUDE_PATH}")
message(STATUS "LLVM_INCLUDE_DIR: ${LLVM_INCLUDE_DIR}")
message(STATUS "PREBUILD_ARGON_PATH: ${PREBUILD_ARGON_PATH}")
# The 'BUILD_BYPRODUCTS' argument was introduced in CMake 3.2
if (${CMAKE_VERSION} VERSION_LESS 3.2)
ExternalProject_Add(
ext_argon
GIT_REPOSITORY ${ARGON_CMAKE_GIT_REPOSITORY}
GIT_TAG ${ARGON_CMAKE_GIT_TAG}
PREFIX ${ARGON_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_CMAKE_PREFIX}
-DPREBUILD_ARGON_PATH=${PREBUILD_ARGON_PATH}
-DARGON_AS_EXTERNAL=True
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
-DLLVM_INCLUDE_DIR=${LLVM_INCLUDE_DIR}
BUILD_ALWAYS 1
)
else()
ExternalProject_Add(
ext_argon
GIT_REPOSITORY ${ARGON_CMAKE_GIT_REPOSITORY}
GIT_TAG ${ARGON_CMAKE_GIT_TAG}
PREFIX ${ARGON_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_CMAKE_PREFIX}
-DPREBUILD_ARGON_PATH=${PREBUILD_ARGON_PATH}
-DARGON_AS_EXTERNAL=True
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
-DLLVM_INCLUDE_DIR=${LLVM_INCLUDE_DIR}
BUILD_BYPRODUCTS ${ARGON_CMAKE_PREFIX}
BUILD_ALWAYS 1
)
endif()
ExternalProject_Get_Property(ext_argon source_dir)
set(ARGON_INCLUDE_DIR ${source_dir}/argon/src PARENT_SCOPE)
set(ARGON_LIB_DIR ${ARGON_CMAKE_PREFIX}/lib PARENT_SCOPE)
endif()
# Copyright 2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# To build ngraph with Argon transformer using pre-build Argon API
# ```
# cmake -DNGRAPH_ARGON_ENABLE=True -DNGRAPH_PREBUILD_ARGON_API_PATH=$HOME/dev/system/_out/debug-x86_64-Linux ..
# make -j
# ```
# Enable ExternalProject CMake module
include(ExternalProject)
if (NGRAPH_ARGON_ENABLE)
# We require pre-build Argon API library
if (NOT DEFINED NGRAPH_PREBUILD_ARGON_API_PATH)
message(FATAL_ERROR "NGRAPH_PREBUILD_ARGON_API_PATH not defined, set it with -DNGRAPH_PREBUILD_ARGON_API_PATH=")
endif()
# Repository
set(ARGON_TRANSFORMER_CMAKE_GIT_REPOSITORY git@github.com:NervanaSystems/argon-transformer.git)
# Set argon_transformer tag
# Notes:
# - Before we have ngraph CI job for argon transformer, ngraph master might not be
# compatible with argon transformer. To ensure compatibility, checkout the ngraph commit point
# where the following `ARGON_TRANSFORMER_CMAKE_GIT_TAG` is set and build ngraph with argon using this
# commit.
# - After we have ngraph CI job for argon transformer, ngraph master will be compatible with
# argon transformer guaranteed by CI.
set(ARGON_TRANSFORMER_CMAKE_GIT_TAG cpp-master)
# Determines where argon-transformer will be located
set(ARGON_TRANSFORMER_CMAKE_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/argon_transformer)
# Print
message(STATUS "NGRAPH_INCLUDE_PATH: ${NGRAPH_INCLUDE_PATH}")
message(STATUS "LLVM_INCLUDE_DIR: ${LLVM_INCLUDE_DIR}")
message(STATUS "NGRAPH_PREBUILD_ARGON_API_PATH: ${NGRAPH_PREBUILD_ARGON_API_PATH}")
# The 'BUILD_BYPRODUCTS' argument was introduced in CMake 3.2
if (${CMAKE_VERSION} VERSION_LESS 3.2)
if (DEFINED CUSTOM_ARGON_TRANSFORMER_DIR)
ExternalProject_Add(
ext_argon_transformer
SOURCE_DIR ${CUSTOM_ARGON_TRANSFORMER_DIR}
PREFIX ${ARGON_TRANSFORMER_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_TRANSFORMER_CMAKE_PREFIX}
-DPREBUILD_ARGON_API_PATH=${NGRAPH_PREBUILD_ARGON_API_PATH}
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
BUILD_ALWAYS 1
)
else()
ExternalProject_Add(
ext_argon_transformer
GIT_REPOSITORY ${ARGON_TRANSFORMER_CMAKE_GIT_REPOSITORY}
GIT_TAG ${ARGON_TRANSFORMER_CMAKE_GIT_TAG}
PREFIX ${ARGON_TRANSFORMER_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_TRANSFORMER_CMAKE_PREFIX}
-DPREBUILD_ARGON_API_PATH=${NGRAPH_PREBUILD_ARGON_API_PATH}
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
BUILD_ALWAYS 1
)
endif()
else()
if (DEFINED CUSTOM_ARGON_TRANSFORMER_DIR)
ExternalProject_Add(
ext_argon_transformer
SOURCE_DIR ${CUSTOM_ARGON_TRANSFORMER_DIR}
PREFIX ${ARGON_TRANSFORMER_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_TRANSFORMER_CMAKE_PREFIX}
-DPREBUILD_ARGON_API_PATH=${NGRAPH_PREBUILD_ARGON_API_PATH}
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
BUILD_BYPRODUCTS ${ARGON_TRANSFORMER_CMAKE_PREFIX}
BUILD_ALWAYS 1
)
else()
ExternalProject_Add(
ext_argon_transformer
GIT_REPOSITORY ${ARGON_TRANSFORMER_CMAKE_GIT_REPOSITORY}
GIT_TAG ${ARGON_TRANSFORMER_CMAKE_GIT_TAG}
PREFIX ${ARGON_TRANSFORMER_CMAKE_PREFIX}
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DNGRAPH_INSTALL_PREFIX=${ARGON_TRANSFORMER_CMAKE_PREFIX}
-DPREBUILD_ARGON_API_PATH=${NGRAPH_PREBUILD_ARGON_API_PATH}
-DEXTERNAL_NGRAPH_INCLUDE_DIR=${NGRAPH_INCLUDE_PATH}
BUILD_BYPRODUCTS ${ARGON_TRANSFORMER_CMAKE_PREFIX}
BUILD_ALWAYS 1
)
endif()
endif()
ExternalProject_Get_Property(ext_argon_transformer source_dir)
set(ARGON_TRANSFORMER_INCLUDE_DIR ${source_dir}/src PARENT_SCOPE)
set(ARGON_TRANSFORMER_LIB_DIR ${ARGON_TRANSFORMER_CMAKE_PREFIX}/lib PARENT_SCOPE)
set(ARGON_API_INCLUDE_DIR ${NGRAPH_PREBUILD_ARGON_API_PATH}/include PARENT_SCOPE)
set(ARGON_API_LIB_DIR ${NGRAPH_PREBUILD_ARGON_API_PATH}/lib) # Used by find_library below
set(ARGON_API_LIB_DIR ${NGRAPH_PREBUILD_ARGON_API_PATH}/lib PARENT_SCOPE)
# Find prebuild argon library
find_library(ARGON_API_LIBS
NAMES
argon_api
optimizer
ir_builder
umd
flex
disasm
HINTS
${ARGON_API_LIB_DIR}
$ENV{LD_LIBRARY_PATH}
)
endif()
......@@ -177,10 +177,6 @@ if (NGRAPH_CPU_ENABLE AND LLVM_INCLUDE_DIR AND
set_property(SOURCE codegen/compiler.cpp APPEND_STRING PROPERTY COMPILE_DEFINITIONS
"NGCPU_DEBUGINFO=${NGRAPH_CPU_DEBUGINFO_ENABLE};")
if (NGRAPH_ARGON_ENABLE)
link_directories(${ARGON_LIB_DIR})
endif()
# GPU backend current requires CPU because they share compiler.cpp,
# and compiler.cpp requires MKLDNN
if(NGRAPH_GPU_ENABLE)
......@@ -202,6 +198,11 @@ endif()
set_property(SOURCE codegen/compiler.cpp APPEND_STRING PROPERTY COMPILE_DEFINITIONS
"CUDA_HEADER_PATHS=\"${CUDA_INCLUDE_DIRS}\";")
endif()
endif()
if (NGRAPH_ARGON_ENABLE)
link_directories(${ARGON_TRANSFORMER_LIB_DIR})
endif()
add_library(ngraph SHARED ${SRC})
......@@ -234,19 +235,9 @@ if (NOT APPLE)
endif()
endif()
# Colon separated string for specified runtime plugin loading, this is made explicit s.t. if a
# plugin is specified at compile time but the corresponding library could not be resolved at run-
# time, an error will be generated.
# E.g. assume compiling with Argon and Xpu, then -DRUNTIME_PLUGIN_LIBS="libargon.so:libxpu.so".
# Defines macro in C++ to load backend plugin
if (NGRAPH_ARGON_ENABLE)
set(RUNTIME_PLUGIN_LIBS "libargon.so") # TODO: currently hardcoded argon lib name if enabled
endif()
if (DEFINED RUNTIME_PLUGIN_LIBS)
target_compile_definitions(ngraph PRIVATE RUNTIME_PLUGIN_LIBS="${RUNTIME_PLUGIN_LIBS}")
else()
target_compile_definitions(ngraph PRIVATE RUNTIME_PLUGIN_LIBS="")
target_compile_definitions(ngraph PRIVATE NGRAPH_ARGON_ENABLE)
endif()
target_include_directories(ngraph PUBLIC "${NGRAPH_INCLUDE_PATH}")
......@@ -276,9 +267,10 @@ endif()
# Argon
if (NGRAPH_ARGON_ENABLE)
target_link_libraries(ngraph PRIVATE argon)
add_dependencies(ngraph ext_argon)
add_dependencies(ext_argon ext_llvm)
target_link_libraries(ngraph PRIVATE ${ARGON_TRANSFORMER_LIB_DIR}/libargon.a)
target_link_libraries(ngraph PRIVATE ${ARGON_API_LIBS})
add_dependencies(ngraph ext_argon_transformer)
add_dependencies(ext_argon_transformer ext_llvm)
endif()
#-----------------------------------------------------------------------------------------------
......@@ -311,10 +303,9 @@ if (NOT APPLE)
endif()
if (NGRAPH_ARGON_ENABLE)
install(DIRECTORY ${ARGON_LIB_DIR}/
install(DIRECTORY ${ARGON_TRANSFORMER_LIB_DIR}/
DESTINATION ${NGRAPH_INSTALL_LIB}
FILES_MATCHING PATTERN "*.so"
)
endif()
endif()
......@@ -14,73 +14,23 @@
#include <dlfcn.h>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include "ngraph/except.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/util.hpp"
using namespace ngraph;
using namespace std;
static mutex load_plugins_mutex;
static mutex close_plugins_mutex;
bool runtime::Manager::m_is_factory_map_initialized = false;
vector<void*> runtime::Manager::m_plugin_handles = {};
void runtime::Manager::load_plugins(const string& runtime_plugin_libs)
{
lock_guard<mutex> lock(load_plugins_mutex);
if (m_is_factory_map_initialized)
{
return;
}
vector<string> plugin_paths = ngraph::split(runtime_plugin_libs, ':', false);
for (auto plugin_path : plugin_paths)
{
if (plugin_path.size() > 0)
{
void* plugin_handle = dlopen(plugin_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (plugin_handle)
{
void (*register_plugin)() =
reinterpret_cast<void (*)()>(dlsym(plugin_handle, "register_plugin"));
if (register_plugin != NULL)
{
register_plugin();
m_plugin_handles.push_back(plugin_handle);
}
else
{
throw ngraph_error("register_plugin() not found in " + plugin_path);
}
}
else
{
throw ngraph_error("Cannot open library " + plugin_path);
}
}
}
m_is_factory_map_initialized = true;
}
// TODO: Should call this function after plugin is not needed anymore.
void runtime::Manager::close_plugins()
{
lock_guard<mutex> lock(close_plugins_mutex);
for (auto plugin_handle : m_plugin_handles)
{
dlclose(plugin_handle);
}
m_plugin_handles.clear();
}
// Put all runtime plugin here. Plugins are statically linked, and a known function need to be
// called to register the backend.
#ifdef NGRAPH_ARGON_ENABLE
bool REGISTER_ARGON_RUNTIME();
static bool argon_runtime_initialized = REGISTER_ARGON_RUNTIME();
#endif
runtime::Manager::FactoryMap& runtime::Manager::get_factory_map()
{
......@@ -91,8 +41,6 @@ runtime::Manager::FactoryMap& runtime::Manager::get_factory_map()
shared_ptr<runtime::Manager> runtime::Manager::get(const string& name)
{
load_plugins(RUNTIME_PLUGIN_LIBS);
auto iter = get_factory_map().find(name);
if (iter == get_factory_map().end())
......
......@@ -53,14 +53,6 @@ namespace ngraph
static Factory register_factory(const std::string& name, Factory factory);
private:
static void load_plugins(const std::string& runtime_plugin_libs);
static void close_plugins();
static std::vector<void*> m_plugin_handles;
static bool m_is_factory_map_initialized;
using FactoryMap = std::map<std::string, Factory>;
static FactoryMap& get_factory_map();
......
......@@ -16,4 +16,4 @@ include( ../cmake/external_eigen.cmake )
include( ../cmake/external_mkldnn.cmake )
include( ../cmake/external_llvm.cmake )
include( ../cmake/external_tbb.cmake )
include( ../cmake/external_argon.cmake )
include( ../cmake/external_argon_transformer.cmake )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment