[neurun] Simplify Backend C Interface (#4891)
author이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Mon, 1 Apr 2019 00:40:25 +0000 (09:40 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 1 Apr 2019 00:40:25 +0000 (09:40 +0900)
C API was 3 functions that creates each components of a Backend. This
commit revises it to have only one API function `neurun_backend_create`
which packs all the components.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/backend/acl_cl/Backend.h [new file with mode: 0644]
runtimes/neurun/backend/acl_cl/PluginClassesAllocator.cc
runtimes/neurun/backend/acl_neon/Backend.h [new file with mode: 0644]
runtimes/neurun/backend/acl_neon/PluginClassesAllocator.cc
runtimes/neurun/backend/cpu/Backend.h [new file with mode: 0644]
runtimes/neurun/backend/cpu/PluginClassesAllocator.cc
runtimes/neurun/core/src/backend/BackendManager.cc

diff --git a/runtimes/neurun/backend/acl_cl/Backend.h b/runtimes/neurun/backend/acl_cl/Backend.h
new file mode 100644 (file)
index 0000000..d94d1ee
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+
+#ifndef __NEURUN_BACKEND_ACL_CL_BACKEND_H__
+#define __NEURUN_BACKEND_ACL_CL_BACKEND_H__
+
+#include <memory>
+#include <backend/Backend.h>
+#include <model/operand/Set.h>
+
+#include "Config.h"
+#include "StageGenerator.h"
+
+namespace neurun
+{
+namespace backend
+{
+namespace acl_cl
+{
+
+class Backend : public ::neurun::backend::Backend
+{
+public:
+  Backend(const neurun::model::operand::Set &operand_ctx)
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<StageGenerator>(operand_ctx, std::make_shared<TensorBuilder>())}
+  {
+    // DO NOTHING
+  }
+};
+
+} // namespace acl_cl
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_ACL_CL_BACKEND_H__
index f33e71d..3a92bfa 100644 (file)
  * limitations under the License.
  */
 
-#include <memory>
-#include "TensorBuilder.h"
-#include "StageGenerator.h"
-#include "Config.h"
-#include "util/logging.h"
+#include <util/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;
-}
+#include "Backend.h"
 
-neurun::backend::acl_cl::StageGenerator *allocate_StageGenerator(
-    const neurun::model::operand::Set &operand_ctx,
-    const std::shared_ptr<neurun::backend::acl_cl::TensorBuilder> &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()
+extern "C" {
+neurun::backend::Backend neurun_backend_create(const neurun::model::operand::Set &operand_ctx)
 {
-  VERBOSE(allocate_Config) << "loaded from acl_cl\n";
-  return new neurun::backend::acl_cl::Config;
+  VERBOSE(neurun_backend_create) << "loaded from acl_cl\n";
+  return neurun::backend::acl_cl::Backend{operand_ctx};
 }
 }
diff --git a/runtimes/neurun/backend/acl_neon/Backend.h b/runtimes/neurun/backend/acl_neon/Backend.h
new file mode 100644 (file)
index 0000000..e958d95
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+
+#ifndef __NEURUN_BACKEND_ACL_NEON_BACKEND_H__
+#define __NEURUN_BACKEND_ACL_NEON_BACKEND_H__
+
+#include <memory>
+#include <backend/Backend.h>
+#include <model/operand/Set.h>
+
+#include "Config.h"
+#include "StageGenerator.h"
+
+namespace neurun
+{
+namespace backend
+{
+namespace acl_neon
+{
+
+class Backend : public ::neurun::backend::Backend
+{
+public:
+  Backend(const neurun::model::operand::Set &operand_ctx)
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<StageGenerator>(operand_ctx, std::make_shared<TensorBuilder>())}
+  {
+    // DO NOTHING
+  }
+};
+
+} // namespace acl_neon
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_ACL_NEON_BACKEND_H__
index 2206fb3..fdcdd94 100644 (file)
  * limitations under the License.
  */
 
-#include <memory>
-#include "TensorBuilder.h"
-#include "StageGenerator.h"
-#include "Config.h"
-#include "util/logging.h"
+#include <util/logging.h>
 
-extern "C" {
-neurun::backend::acl_neon::TensorBuilder *allocate_TensorBuilder()
-{
-  VERBOSE(allocate_TensorBuilder) << "loaded from acl_neon\n";
-  return new neurun::backend::acl_neon::TensorBuilder;
-}
+#include "Backend.h"
 
-neurun::backend::acl_neon::StageGenerator *allocate_StageGenerator(
-    const neurun::model::operand::Set &operand_ctx,
-    const std::shared_ptr<neurun::backend::acl_neon::TensorBuilder> &tensor_builder)
-{
-  VERBOSE(allocate_StageGenerator) << "loaded from acl_neon\n";
-  return new neurun::backend::acl_neon::StageGenerator(operand_ctx, tensor_builder);
-}
-
-neurun::backend::acl_neon::Config *allocate_Config()
+extern "C" {
+neurun::backend::Backend neurun_backend_create(const neurun::model::operand::Set &operand_ctx)
 {
-  VERBOSE(allocate_Config) << "loaded from acl_neon\n";
-  return new neurun::backend::acl_neon::Config;
+  VERBOSE(neurun_backend_create) << "loaded from acl_neon\n";
+  return neurun::backend::acl_neon::Backend{operand_ctx};
 }
 }
diff --git a/runtimes/neurun/backend/cpu/Backend.h b/runtimes/neurun/backend/cpu/Backend.h
new file mode 100644 (file)
index 0000000..e7878d2
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+
+#ifndef __NEURUN_BACKEND_CPU_BACKEND_H__
+#define __NEURUN_BACKEND_CPU_BACKEND_H__
+
+#include <memory>
+#include <backend/Backend.h>
+#include <model/operand/Set.h>
+
+#include "Config.h"
+#include "StageGenerator.h"
+
+namespace neurun
+{
+namespace backend
+{
+namespace cpu
+{
+
+class Backend : public ::neurun::backend::Backend
+{
+public:
+  Backend(const neurun::model::operand::Set &operand_ctx)
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<StageGenerator>(operand_ctx, std::make_shared<TensorBuilder>())}
+  {
+    // DO NOTHING
+  }
+};
+
+} // namespace cpu
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_CPU_BACKEND_H__
index 26d4d88..a29ad86 100644 (file)
  * limitations under the License.
  */
 
-#include <memory>
-#include "TensorBuilder.h"
-#include "StageGenerator.h"
-#include "Config.h"
-#include "util/logging.h"
+#include <util/logging.h>
 
-extern "C" {
-neurun::backend::cpu::TensorBuilder *allocate_TensorBuilder()
-{
-  VERBOSE(allocate_TensorBuilder) << "loaded from CPU\n";
-  return new neurun::backend::cpu::TensorBuilder;
-}
+#include "Backend.h"
 
-neurun::backend::cpu::StageGenerator *
-allocate_StageGenerator(const neurun::model::operand::Set &operand_ctx,
-                        const std::shared_ptr<neurun::backend::cpu::TensorBuilder> &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()
+extern "C" {
+neurun::backend::Backend neurun_backend_create(const neurun::model::operand::Set &operand_ctx)
 {
-  VERBOSE(allocate_Config) << "loaded from CPU\n";
-  return new neurun::backend::cpu::Config;
+  VERBOSE(neurun_backend_create) << "loaded from CPU\n";
+  return neurun::backend::cpu::Backend{operand_ctx};
 }
 }
index 4e0c90c..0e847d0 100644 (file)
@@ -17,9 +17,8 @@
 #include <dlfcn.h>
 #include "BackendManager.h"
 
+#include "backend/Backend.h"
 #include "backend/IConfig.h"
-#include "backend/ITensorBuilder.h"
-#include "backend/IStageGenerator.h"
 #include "util/logging.h"
 #include "util/config/ConfigManager.h"
 
@@ -60,19 +59,20 @@ void BackendManager::loadBackend(const std::string &backend,
   VERBOSE(BackendManager::loadBackend) << "loaded " << backend_plugin << " as a plugin of "
                                        << backend << " backend\n";
 
-  // load Config
-  std::shared_ptr<neurun::backend::IConfig> config;
-  loadObjectFromPlugin(config, std::string("allocate_Config"), handle);
-
-  // load TensorBuilder
-  std::shared_ptr<neurun::backend::ITensorBuilder> tensor_builder;
-  loadObjectFromPlugin(tensor_builder, std::string("allocate_TensorBuilder"), handle);
+  {
+    using backend_create_t = neurun::backend::Backend (*)(const neurun::model::operand::Set &);
 
-  // load StageGenerator
-  std::shared_ptr<neurun::backend::IStageGenerator> stage_gen;
-  loadObjectFromPlugin(stage_gen, std::string("allocate_StageGenerator"), handle, operands,
-                       tensor_builder);
-  _gen_map[config->id()] = {config, stage_gen};
+    // load object creator function
+    backend_create_t backend_create = (backend_create_t)dlsym(handle, "neurun_backend_create");
+    if (backend_create == nullptr)
+    {
+      fprintf(stderr, "BackendManager: unable to open function neurun_backend_create : %s\n",
+              dlerror());
+      abort();
+    }
+    auto backend_object = backend_create(operands);
+    _gen_map[backend_object.config()->id()] = backend_object;
+  }
 
   // Save backend handle (avoid warning by handle lost without dlclose())
   _handle_map.insert({backend, handle});