[neurun] Extract PlanBuilder into a separate file (#2276)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Mon, 13 Aug 2018 10:22:31 +0000 (19:22 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Mon, 13 Aug 2018 10:22:31 +0000 (19:22 +0900)
Extract PlanBuilder into a separate file and move it to codegen
directory.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/src/codegen/PlanBuilder.cc [new file with mode: 0644]
runtimes/neurun/src/codegen/PlanBuilder.h [new file with mode: 0644]
runtimes/neurun/src/compilation.cc

diff --git a/runtimes/neurun/src/codegen/PlanBuilder.cc b/runtimes/neurun/src/codegen/PlanBuilder.cc
new file mode 100644 (file)
index 0000000..6835949
--- /dev/null
@@ -0,0 +1,66 @@
+#include "PlanBuilder.h"
+
+namespace neurun
+{
+namespace codegen
+{
+
+void PlanBuilder::addShapeConstr(const ::internal::tflite::operand::Index &ind,
+                                 const ::arm_compute::TensorInfo &info)
+{
+  _tensor_info_ctx[ind.asInt()] = info;
+}
+
+void PlanBuilder::addInitializer(const ::internal::tflite::operand::Index &ind,
+                                 const Initializer &initializer)
+{
+  _initializer_ctx[ind.asInt()] = initializer;
+}
+
+void PlanBuilder::addStage(const Stage &stage) { _stages.emplace_back(stage); }
+
+void PlanBuilder::finalize(BackendResolver &backend_resolver)
+{
+  auto tensor_builders = backend_resolver.getAllTensorBuilders();
+  auto common_tensor_builder = backend_resolver.getCommonTensorBuilder();
+
+  // Prepare tensors
+  for (auto &tensor_builder : tensor_builders)
+  {
+    tensor_builder->prepare(_tensor_info_ctx);
+  }
+
+  common_tensor_builder->prepare(_tensor_info_ctx);
+
+  // Process Stage
+  ExecutionBuilder execution_builder{_plan};
+
+  for (const auto &stage : _stages)
+  {
+    stage(execution_builder);
+  }
+
+  // TODO Add code for CPU/ACL tensor allocation
+  // Allocate Tensor Memory for cl_tensors
+  for (auto &tensor_builder : tensor_builders)
+  {
+    tensor_builder->allocate();
+  }
+
+  common_tensor_builder->allocate();
+
+  // Fill weight/bias
+  for (auto it = _initializer_ctx.begin(); it != _initializer_ctx.end(); ++it)
+  {
+    const ::internal::tflite::operand::Index operand_index{it->first};
+    auto objects = _plan.operands().at(operand_index);
+
+    for (auto object : objects)
+    {
+      object->access(it->second);
+    }
+  }
+}
+
+} // namepsace codegen
+} // namespace neurun
diff --git a/runtimes/neurun/src/codegen/PlanBuilder.h b/runtimes/neurun/src/codegen/PlanBuilder.h
new file mode 100644 (file)
index 0000000..0a2cead
--- /dev/null
@@ -0,0 +1,69 @@
+#ifndef __NEURUN_CODEGEN_PLAN_BUILDER_H__
+#define __NEURUN_CODEGEN_PLAN_BUILDER_H__
+
+#include "IPlanBuilder.h"
+#include "internal/Plan.h"
+#include "codegen/BackendResolver.h"
+#include "backend/IStageGenerator.h"
+
+namespace neurun
+{
+namespace codegen
+{
+
+class ExecutionBuilder final : public IExecutionBuilder
+{
+public:
+  ExecutionBuilder(::internal::Plan &plan) : _plan{plan}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void append(std::unique_ptr<::arm_compute::IFunction> &&f) override
+  {
+    _plan.operations().append(std::move(f));
+  }
+
+private:
+  ::internal::Plan &_plan;
+};
+
+class PlanBuilder final : public IPlanBuilder
+{
+public:
+  PlanBuilder(::internal::Plan &plan) : _plan{plan}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void addShapeConstr(const ::internal::tflite::operand::Index &ind,
+                      const ::arm_compute::TensorInfo &info) override;
+
+public:
+  void addInitializer(const ::internal::tflite::operand::Index &ind,
+                      const Initializer &initializer) override;
+
+public:
+  void addStage(const Stage &stage) override;
+
+public:
+  void finalize(BackendResolver &backend_resolver);
+
+public:
+  const std::map<int, ::arm_compute::TensorInfo> &tensor_info_ctx() { return _tensor_info_ctx; }
+
+private:
+  ::internal::Plan &_plan;
+
+private:
+  std::map<int, ::arm_compute::TensorInfo> _tensor_info_ctx;
+  std::map<int, Initializer> _initializer_ctx;
+  std::vector<Stage> _stages;
+};
+
+} // namepsace codegen
+} // namespace neurun
+
+#endif // __NEURUN_CODEGEN_PLAN_BUILDER_H__
index 648d277..ace5778 100644 (file)
@@ -438,114 +438,7 @@ void TensorMarker::visit(const ::internal::tflite::op::TensorConvert::AclToCommo
   // DO NOTHING
 }
 
-class ExecutionBuilder final : public IExecutionBuilder
-{
-public:
-  ExecutionBuilder(::internal::Plan &plan) : _plan{plan}
-  {
-    // DO NOTHING
-  }
-
-public:
-  void append(std::unique_ptr<::arm_compute::IFunction> &&f) override
-  {
-    _plan.operations().append(std::move(f));
-  }
-
-private:
-  ::internal::Plan &_plan;
-};
-
-class PlanBuilder final : public neurun::codegen::IPlanBuilder
-{
-public:
-  PlanBuilder(::internal::Plan &plan) : _plan{plan}
-  {
-    // DO NOTHING
-  }
-
-public:
-  void addShapeConstr(const ::internal::tflite::operand::Index &ind,
-                      const ::arm_compute::TensorInfo &info) override;
-
-public:
-  void addInitializer(const ::internal::tflite::operand::Index &ind,
-                      const Initializer &initializer) override;
-
-public:
-  void addStage(const Stage &stage) override;
-
-public:
-  void finalize(neurun::codegen::BackendResolver &backend_resolver);
-
-public:
-  const std::map<int, ::arm_compute::TensorInfo> &tensor_info_ctx() { return _tensor_info_ctx; }
-
-private:
-  ::internal::Plan &_plan;
-
-private:
-  std::map<int, ::arm_compute::TensorInfo> _tensor_info_ctx;
-  std::map<int, Initializer> _initializer_ctx;
-  std::vector<Stage> _stages;
-};
-
-void PlanBuilder::addShapeConstr(const ::internal::tflite::operand::Index &ind,
-                                 const ::arm_compute::TensorInfo &info)
-{
-  _tensor_info_ctx[ind.asInt()] = info;
-}
-
-void PlanBuilder::addInitializer(const ::internal::tflite::operand::Index &ind,
-                                 const Initializer &initializer)
-{
-  _initializer_ctx[ind.asInt()] = initializer;
-}
-
-void PlanBuilder::addStage(const Stage &stage) { _stages.emplace_back(stage); }
-
-void PlanBuilder::finalize(neurun::codegen::BackendResolver &backend_resolver)
-{
-  auto tensor_builders = backend_resolver.getAllTensorBuilders();
-  auto common_tensor_builder = backend_resolver.getCommonTensorBuilder();
-
-  // Prepare tensors
-  for (auto &tensor_builder : tensor_builders)
-  {
-    tensor_builder->prepare(_tensor_info_ctx);
-  }
-
-  common_tensor_builder->prepare(_tensor_info_ctx);
-
-  // Process Stage
-  ExecutionBuilder execution_builder{_plan};
-
-  for (const auto &stage : _stages)
-  {
-    stage(execution_builder);
-  }
-
-  // TODO Add code for CPU/ACL tensor allocation
-  // Allocate Tensor Memory for cl_tensors
-  for (auto &tensor_builder : tensor_builders)
-  {
-    tensor_builder->allocate();
-  }
-
-  common_tensor_builder->allocate();
-
-  // Fill weight/bias
-  for (auto it = _initializer_ctx.begin(); it != _initializer_ctx.end(); ++it)
-  {
-    const ::internal::tflite::operand::Index operand_index{it->first};
-    auto objects = _plan.operands().at(operand_index);
-
-    for (auto object : objects)
-    {
-      object->access(it->second);
-    }
-  }
-}
+#include "codegen/PlanBuilder.h"
 
 int ANeuralNetworksCompilation::finish()
 {
@@ -568,7 +461,7 @@ int ANeuralNetworksCompilation::finish()
 
   ::internal::BackendManager backend_manager{plan};
   neurun::codegen::BackendResolver backend_resolver{backend_manager};
-  PlanBuilder plan_builder{plan};
+  neurun::codegen::PlanBuilder plan_builder{plan};
 
   for (uint32_t n = 0; n < operations.size(); ++n)
   {