Apply refactored ConstantInitializers (#5713)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Fri, 19 Jul 2019 06:40:42 +0000 (15:40 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 19 Jul 2019 06:40:42 +0000 (15:40 +0900)
This commit applies refactored ConstantInitialzers.
  - Introduce ConstantInitializer into each backend.
  - Replace legacy ConstantInitializer with refactored ConstantInitializer.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
12 files changed:
runtimes/neurun/backend/acl_cl/Backend.h
runtimes/neurun/backend/acl_cl/ConstantInitializer.cc
runtimes/neurun/backend/acl_neon/Backend.h
runtimes/neurun/backend/acl_neon/ConstantInitializer.cc
runtimes/neurun/backend/cpu/Backend.h
runtimes/neurun/backend/cpu/ConstantInitializer.cc
runtimes/neurun/core/include/backend/Backend.h
runtimes/neurun/core/include/backend/IConstantInitializer.h
runtimes/neurun/core/src/backend/Backend.cc
runtimes/neurun/core/src/compiler/ExecutorFactory.cc
runtimes/neurun/core/src/compiler/PlanBuilder.cc
runtimes/neurun/test/core/backend/ExecTime.test.cc

index 567388f..c82cf03 100644 (file)
@@ -22,6 +22,7 @@
 #include <model/Operands.h>
 
 #include "Config.h"
+#include "ConstantInitializer.h"
 #include "KernelGenerator.h"
 #include "ShapeFixer.h"
 #include "MemoryManager.h"
@@ -39,9 +40,11 @@ public:
   Backend(const neurun::model::Operands &operand_ctx,
           std::shared_ptr<TensorBuilder> tensor_builder =
               std::make_shared<TensorBuilder>(createMemoryManager()))
-      : ::neurun::backend::Backend{std::make_shared<Config>(),
-                                   std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
-                                   std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+            std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+            std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
   {
     // DO NOTHING
   }
index 255340a..ffaf51a 100644 (file)
@@ -41,6 +41,7 @@ void ConstantInitializer::run()
     auto tensor_obj = _tensor_builder->wrapTensor(ind);
     fn(model_obj, *tensor_obj);
   }
+  _init_map.clear();
 }
 
 void ConstantInitializer::visit(const model::operation::AbsNode &node)
index 434c801..d710e47 100644 (file)
@@ -22,6 +22,7 @@
 #include <model/Operands.h>
 
 #include "Config.h"
+#include "ConstantInitializer.h"
 #include "KernelGenerator.h"
 #include "ShapeFixer.h"
 #include "MemoryManager.h"
@@ -39,9 +40,11 @@ public:
   Backend(const neurun::model::Operands &operand_ctx,
           std::shared_ptr<TensorBuilder> tensor_builder =
               std::make_shared<TensorBuilder>(createMemoryManager()))
-      : ::neurun::backend::Backend{std::make_shared<Config>(),
-                                   std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
-                                   std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+            std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+            std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
   {
     // DO NOTHING
   }
index 4f9e224..7366582 100644 (file)
@@ -41,6 +41,7 @@ void ConstantInitializer::run()
     auto tensor_obj = _tensor_builder->wrapTensor(ind);
     fn(model_obj, *tensor_obj);
   }
+  _init_map.clear();
 }
 
 void ConstantInitializer::visit(const model::operation::AddNode &node)
index 9d47a92..47168e8 100644 (file)
@@ -22,6 +22,7 @@
 #include <model/Operands.h>
 
 #include "Config.h"
+#include "ConstantInitializer.h"
 #include "KernelGenerator.h"
 #include "ShapeFixer.h"
 
@@ -37,9 +38,11 @@ class Backend : public ::neurun::backend::Backend
 public:
   Backend(const neurun::model::Operands &operand_ctx,
           std::shared_ptr<TensorBuilder> tensor_builder = std::make_shared<TensorBuilder>())
-      : ::neurun::backend::Backend{std::make_shared<Config>(),
-                                   std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
-                                   std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+      : ::neurun::backend::Backend{
+            std::make_shared<Config>(),
+            std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+            std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+            std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
   {
     // DO NOTHING
   }
index 8769cc8..8997933 100644 (file)
@@ -41,6 +41,7 @@ void ConstantInitializer::run()
     auto tensor_obj = _tensor_builder->wrapTensor(ind);
     fn(model_obj, *tensor_obj);
   }
+  _init_map.clear();
 }
 
 void ConstantInitializer::visit(const model::operation::AddNode &node)
index fdcd5a6..b9b33bd 100644 (file)
@@ -25,6 +25,7 @@ namespace backend
 {
 
 struct IConfig;
+class IConstantInitializer;
 class IKernelGenerator;
 class IShapeFixer;
 struct ITensorBuilder;
@@ -33,22 +34,27 @@ class Backend
 {
 public:
   Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config,
+          const std::shared_ptr<neurun::backend::IConstantInitializer> &constant_initializer,
           const std::shared_ptr<neurun::backend::IKernelGenerator> &kernel_gen,
           const std::shared_ptr<neurun::backend::IShapeFixer> &shape_fixer);
 
-  Backend(void) : _config(nullptr), _kernel_gen(nullptr), _shape_fixer(nullptr)
+  Backend(void)
+      : _config(nullptr), _constant_initializer{nullptr}, _kernel_gen(nullptr),
+        _shape_fixer(nullptr)
   {
     // DO NOTHING
   }
 
 public:
   const std::shared_ptr<neurun::backend::IConfig> config() const;
+  const std::shared_ptr<neurun::backend::IConstantInitializer> constant_initializer() const;
   const std::shared_ptr<neurun::backend::IKernelGenerator> kernel_gen() const;
   const std::shared_ptr<neurun::backend::IShapeFixer> shape_fixer() const;
   const std::shared_ptr<neurun::backend::ITensorBuilder> tensor_builder() const;
 
 private:
   std::shared_ptr<neurun::backend::IConfig> _config;
+  std::shared_ptr<neurun::backend::IConstantInitializer> _constant_initializer;
   std::shared_ptr<neurun::backend::IKernelGenerator> _kernel_gen;
   std::shared_ptr<neurun::backend::IShapeFixer> _shape_fixer;
 };
index 63fbb4b..0545391 100644 (file)
@@ -207,11 +207,7 @@ public:
 public:
   using Initializer = std::function<void(const model::Operand &, backend::operand::IObject &)>;
 
-  void generate(const model::Subgraph &subg)
-  {
-    _init_map.clear();
-    subg.accept(*this);
-  }
+  void generate(const model::Subgraph &subg) { subg.accept(*this); }
 
 protected:
 #define OP(InternalName, IsNnApi)                                     \
index 4ba86da..28e3f8e 100644 (file)
@@ -27,15 +27,22 @@ namespace backend
 {
 
 Backend::Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config,
+                 const std::shared_ptr<neurun::backend::IConstantInitializer> &constant_initializer,
                  const std::shared_ptr<neurun::backend::IKernelGenerator> &kernel_gen,
                  const std::shared_ptr<neurun::backend::IShapeFixer> &shape_fixer)
-    : _config(backend_config), _kernel_gen(kernel_gen), _shape_fixer(shape_fixer)
+    : _config(backend_config), _constant_initializer{constant_initializer}, _kernel_gen(kernel_gen),
+      _shape_fixer(shape_fixer)
 {
   backend_config->initialize();
 }
 
 const std::shared_ptr<neurun::backend::IConfig> Backend::config() const { return _config; }
 
+const std::shared_ptr<neurun::backend::IConstantInitializer> Backend::constant_initializer() const
+{
+  return _constant_initializer;
+}
+
 const std::shared_ptr<neurun::backend::IKernelGenerator> Backend::kernel_gen() const
 {
   return _kernel_gen;
index e76088e..4328c00 100644 (file)
@@ -28,9 +28,9 @@
 #include "OperationValidator.h"
 #include "SubTensorAnalyzer.h"
 #include "PlanBuilder.h"
+#include "backend/IConstantInitializer.h"
 #include "backend/IKernelGenerator.h"
 #include "backend/IShapeFixer.h"
-#include "ConstantInitializer.h"
 #include "cpp14/memory.h"
 
 namespace neurun
@@ -107,7 +107,11 @@ exec::IExecutor *ExecutorFactory::createLinearExecutor(graph::Graph &graph)
   // TODO Add optimization passes
   plan_builder.finalize(linear.get(), tensor_builders);
 
-  ConstantInitializer{graph, *operand_context, *linear->getLowerInfo()}();
+  // Initialize constant tensors
+  for (const auto backend : graph.backend_resolver()->getAllBackends())
+  {
+    backend->constant_initializer()->run();
+  }
 
   // Prepare each MemoryManager on each backend
   auto mem_mgrs = nnfw::cpp14::make_unique<backend::MemoryManagerSet>();
@@ -221,6 +225,8 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo
   graph.subgraphs().iterate(
       [&](const model::SubgraphIndex &subg_index, const model::Subgraph &subg) {
         auto backend = graph.getLowerInfo(subg_index)->backend();
+        auto constant_initializer = backend->constant_initializer();
+        constant_initializer->generate(subg);
         // TODO This approach is temporal. See declaration of `setNextIndex`.
         execution_builder->setNextIndex(subg_index);
         auto kernel_gen = backend->kernel_gen();
@@ -232,9 +238,13 @@ exec::IExecutor *ExecutorFactory::createDataflowExecutor(graph::Graph &graph, bo
     tensor_builder->allocate();
   }
 
-  auto lower_info = graph.releaseLowerInfo();
+  // Initialize constant tensors
+  for (const auto backend : graph.backend_resolver()->getAllBackends())
+  {
+    backend->constant_initializer()->run();
+  }
 
-  ConstantInitializer{graph, *operand_context, *lower_info}();
+  auto lower_info = graph.releaseLowerInfo();
 
   // Prepare each MemoryManager on each backend
   auto mem_mgrs = nnfw::cpp14::make_unique<backend::MemoryManagerSet>();
index e3958c7..fbef2cc 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "backend/operand/IObject.h"
 #include "backend/Backend.h"
+#include "backend/IConstantInitializer.h"
 #include "backend/IKernelGenerator.h"
 #include "linear/Linear.h"
 
@@ -45,6 +46,8 @@ void PlanBuilder::finalize(const linear::Linear *linear,
   auto execution_builder = nnfw::cpp14::make_unique<ExecutionBuilder>(_functions);
   linear->iterate([&](const linear::Element &element) {
     auto backend = element.lower_info->backend();
+    auto constant_initializer = backend->constant_initializer();
+    constant_initializer->generate(*element.subgraph);
     auto kernel_gen = backend->kernel_gen();
     kernel_gen->generate(*element.subgraph, execution_builder.get());
   });
index 334b45f..b9d511c 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "backend/ExecTime.h"
 #include "backend/IConfig.h"
+#include "backend/IConstantInitializer.h"
 #include "backend/IKernelGenerator.h"
 #include "backend/IShapeFixer.h"
 #include "backend/Backend.h"
@@ -35,6 +36,11 @@ struct MockConfig : public IConfig
   bool SupportSubTensorAlloc() override { return false; }
 };
 
+struct MockConstantInitializer : IConstantInitializer
+{
+  void run() {}
+};
+
 struct MockKernelGenerator : IKernelGenerator
 {
   std::shared_ptr<ITensorBuilder> tensor_builder() final { return nullptr; }
@@ -51,8 +57,8 @@ struct MockBackend : public ::neurun::backend::Backend
    *    Required because we use pointers to backends instead of string identifiers.
    */
   MockBackend()
-      : Backend{std::make_shared<MockConfig>(), std::make_shared<MockKernelGenerator>(),
-                std::make_shared<MockShapeFixer>()} {};
+      : Backend{std::make_shared<MockConfig>(), std::make_shared<MockConstantInitializer>(),
+                std::make_shared<MockKernelGenerator>(), std::make_shared<MockShapeFixer>()} {};
 };
 
 TEST(ExecTime, roundtrip_ok)