From: Дилшоджон Умронхонович Пошшоев/AI Tools Lab /SRR/Engineer/삼성전자 Date: Thu, 6 Dec 2018 06:06:23 +0000 (+0300) Subject: [neurun] Do backends as shared and update BackendManager to support plugin (#3793) X-Git-Tag: 0.3~184 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3845b67822eb27f00f36d5baee1f2ac92064e905;p=platform%2Fcore%2Fml%2Fnnfw.git [neurun] Do backends as shared and update BackendManager to support plugin (#3793) Related issue: #3703 This is the initial step of converting backends to plugins Update BackendManager to creage a map using plugins Signed-off-by: Poshshoev Dilshodzhon --- diff --git a/runtimes/neurun/CMakeLists.txt b/runtimes/neurun/CMakeLists.txt index 4407f38..08237b6 100644 --- a/runtimes/neurun/CMakeLists.txt +++ b/runtimes/neurun/CMakeLists.txt @@ -38,7 +38,7 @@ target_link_libraries(${LIB_NEURUN} tensorflow-lite) target_link_libraries(${LIB_NEURUN} nnfw_util) target_link_libraries(${LIB_NEURUN} nnfw_support_nnapi) -# TODO This should be optional +# TODO This will be removed when backends are converted to plugins target_link_libraries(${LIB_NEURUN} ${LIB_NEURUN_BACKEND_CPU}) target_link_libraries(${LIB_NEURUN} ${LIB_NEURUN_BACKEND_ACL_CL}) diff --git a/runtimes/neurun/src/backend/BackendManager.cc b/runtimes/neurun/src/backend/BackendManager.cc index 0dbb14c..8b31af4 100644 --- a/runtimes/neurun/src/backend/BackendManager.cc +++ b/runtimes/neurun/src/backend/BackendManager.cc @@ -14,14 +14,12 @@ * limitations under the License. */ +#include #include "BackendManager.h" -#include "backend/acl_cl/Config.h" -#include "backend/acl_cl/TensorBuilder.h" -#include "backend/acl_cl/StageGenerator.h" -#include "backend/cpu/Config.h" -#include "backend/cpu/TensorBuilder.h" -#include "backend/cpu/StageGenerator.h" +#include "backend/interface/IConfig.h" +#include "backend/interface/ITensorBuilder.h" +#include "backend/interface/IStageGenerator.h" namespace neurun { @@ -47,25 +45,48 @@ const std::shared_ptr Backend::tensor_builder() return _stage_gen->tensor_builder(); } -BackendManager::BackendManager(const neurun::graph::operand::Set &operands) +template +void BackendManager::loadObjectFromPlugin(std::shared_ptr &object_of_plugin_class, + const std::string obj_creator_func_name, void *handle, + Types &&... args) { - // Add arm_compute backend + T *(*allocate_obj)(Types && ... Args); + // load object creator function + allocate_obj = (T * (*)(Types && ... Args))dlsym(handle, obj_creator_func_name.c_str()); + if (allocate_obj == nullptr) { - using namespace ::neurun::backend::acl_cl; - auto config = std::make_shared(); - auto tensor_builder = std::make_shared(); - auto stage_gen = std::make_shared(operands, tensor_builder); - - _gen_map[config->id()] = {config, stage_gen}; + fprintf(stderr, "BackendManager: unable to open function %s: %s", obj_creator_func_name.c_str(), + dlerror()); + abort(); } - // Add CPU backend + object_of_plugin_class.reset(allocate_obj(args...)); +} +BackendManager::BackendManager(const neurun::graph::operand::Set &operands) +{ + // TODO handle plugin loading: TBD how + std::string plugins[] = {"libbackend_acl_cl.so", "libbackend_cpu.so"}; + for (auto plugin : plugins) { - using namespace ::neurun::backend::cpu; - auto config = std::make_shared(); - auto tensor_builder = std::make_shared(); - auto stage_gen = std::make_shared(operands, tensor_builder); + void *handle = dlopen(plugin.c_str(), RTLD_LAZY | RTLD_LOCAL); + if (handle == nullptr) + { + fprintf(stderr, "BackendManager %s: %s\n", plugin.c_str(), dlerror()); + abort(); + } + + // load Config + std::shared_ptr config; + loadObjectFromPlugin(config, std::string("allocate_Config"), handle); + + // load TensorBuilder + std::shared_ptr tensor_builder; + loadObjectFromPlugin(tensor_builder, std::string("allocate_TensorBuilder"), handle); + // load StageGenerator + std::shared_ptr stage_gen; + loadObjectFromPlugin(stage_gen, std::string("allocate_StageGenerator"), handle, operands, + tensor_builder); _gen_map[config->id()] = {config, stage_gen}; } } diff --git a/runtimes/neurun/src/backend/BackendManager.h b/runtimes/neurun/src/backend/BackendManager.h index aeb4153..ed832dd 100644 --- a/runtimes/neurun/src/backend/BackendManager.h +++ b/runtimes/neurun/src/backend/BackendManager.h @@ -61,6 +61,21 @@ public: private: std::map _gen_map; + /** + * @brief Allocate an object of a class of a plugin by loading a plugin function, that does + * allocation, and calling it + * + * @param object_of_plugin_class target object + * @param obj_creator_func_name name of the plugin function, that allocates an object + * @param handle handle of the plugin + * @param args arguments to pass to constructor of the plugin class + * + * @return + */ + template + void loadObjectFromPlugin(std::shared_ptr &object_of_plugin_class, + const std::string obj_creator_func_name, void *handle, + Types &&... args); }; } // namespace backend diff --git a/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt b/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt index d64c23a..f1c17aa 100644 --- a/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt +++ b/runtimes/neurun/src/backend/acl_cl/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB_RECURSE SOURCES "*.cc") -add_library(${LIB_NEURUN_BACKEND_ACL_CL} STATIC ${SOURCES}) +add_library(${LIB_NEURUN_BACKEND_ACL_CL} SHARED ${SOURCES}) target_include_directories(${LIB_NEURUN_BACKEND_ACL_CL} PUBLIC ${NNFW_INCLUDE_DIR}) target_include_directories(${LIB_NEURUN_BACKEND_ACL_CL} PUBLIC ${NEURUN_INCLUDE_DIR}) @@ -12,6 +12,5 @@ target_link_libraries(${LIB_NEURUN_BACKEND_ACL_CL} ${LIB_NEURUN_KERNEL_ACL_CL}) target_compile_options(${LIB_NEURUN_BACKEND_ACL_CL} PRIVATE -Wall -Wextra -Werror) -set_target_properties(${LIB_NEURUN_BACKEND_ACL_CL} PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(${LIB_NEURUN_BACKEND_ACL_CL} PROPERTIES OUTPUT_NAME backend_acl_cl) install(TARGETS ${LIB_NEURUN_BACKEND_ACL_CL} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc b/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc new file mode 100644 index 0000000..fdb37c1 --- /dev/null +++ b/runtimes/neurun/src/backend/acl_cl/PluginClassesAllocator.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#include +#include "TensorBuilder.h" +#include "StageGenerator.h" +#include "Config.h" +#include "logging.h" + +extern "C" { +neurun::backend::acl_cl::TensorBuilder *allocate_TensorBuilder() +{ + VERBOSE(allocate_TensorBuilder) << "loaded from acl_cl\n"; + return new neurun::backend::acl_cl::TensorBuilder; +} + +neurun::backend::acl_cl::StageGenerator *allocate_StageGenerator( + const neurun::graph::operand::Set &operand_ctx, + const std::shared_ptr &tensor_builder) +{ + VERBOSE(allocate_StageGenerator) << "loaded from acl_cl\n"; + return new neurun::backend::acl_cl::StageGenerator(operand_ctx, tensor_builder); +} + +neurun::backend::acl_cl::Config *allocate_Config() +{ + VERBOSE(allocate_Config) << "loaded from acl_cl\n"; + return new neurun::backend::acl_cl::Config; +} +} diff --git a/runtimes/neurun/src/backend/cpu/CMakeLists.txt b/runtimes/neurun/src/backend/cpu/CMakeLists.txt index 95e9af6..731aa31 100644 --- a/runtimes/neurun/src/backend/cpu/CMakeLists.txt +++ b/runtimes/neurun/src/backend/cpu/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB_RECURSE SOURCES "*.cc") -add_library(${LIB_NEURUN_BACKEND_CPU} STATIC ${SOURCES}) +add_library(${LIB_NEURUN_BACKEND_CPU} SHARED ${SOURCES}) target_include_directories(${LIB_NEURUN_BACKEND_CPU} PUBLIC ${NNFW_INCLUDE_DIR}) target_include_directories(${LIB_NEURUN_BACKEND_CPU} PUBLIC ${NEURUN_INCLUDE_DIR}) @@ -11,9 +11,10 @@ target_link_libraries(${LIB_NEURUN_BACKEND_CPU} tensorflow-lite) target_link_libraries(${LIB_NEURUN_BACKEND_CPU} nnfw_util) target_link_libraries(${LIB_NEURUN_BACKEND_CPU} nnfw_support_nnapi) target_link_libraries(${LIB_NEURUN_BACKEND_CPU} ${LIB_NEURUN_KERNEL_CPU}) +# TODO remove this line once acl dependency is removed from PermuteLayer +target_link_libraries(${LIB_NEURUN_BACKEND_CPU} ${LIB_NEURUN_BACKEND_ACL_CL}) target_compile_options(${LIB_NEURUN_BACKEND_CPU} PRIVATE -Wall -Wextra -Werror) -set_target_properties(${LIB_NEURUN_BACKEND_CPU} PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(${LIB_NEURUN_BACKEND_CPU} PROPERTIES OUTPUT_NAME backend_cpu) install(TARGETS ${LIB_NEURUN_BACKEND_CPU} DESTINATION lib/neurun) diff --git a/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc b/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc new file mode 100644 index 0000000..5e1a366 --- /dev/null +++ b/runtimes/neurun/src/backend/cpu/PluginClassesAllocator.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#include +#include "TensorBuilder.h" +#include "StageGenerator.h" +#include "Config.h" +#include "logging.h" + +extern "C" { +neurun::backend::cpu::TensorBuilder *allocate_TensorBuilder() +{ + VERBOSE(allocate_TensorBuilder) << "loaded from CPU\n"; + return new neurun::backend::cpu::TensorBuilder; +} + +neurun::backend::cpu::StageGenerator * +allocate_StageGenerator(const neurun::graph::operand::Set &operand_ctx, + const std::shared_ptr &tensor_builder) +{ + VERBOSE(allocate_StageGenerator) << "loaded from CPU\n"; + return new neurun::backend::cpu::StageGenerator(operand_ctx, tensor_builder); +} + +neurun::backend::cpu::Config *allocate_Config() +{ + VERBOSE(allocate_Config) << "loaded from CPU\n"; + return new neurun::backend::cpu::Config; +} +}