[enco] Introduce InstrBuilder (#2015)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 29 Oct 2018 01:08:22 +0000 (10:08 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 29 Oct 2018 01:08:22 +0000 (10:08 +0900)
This commit introduces InstrBuilder and simplifies the implementation of
caffe frontend.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/frontend/caffe/src/Frontend.cpp

index 3cf8024..e3664fa 100644 (file)
@@ -223,6 +223,38 @@ private:
 OpBuilder op_builder(coco::Module *m) { return OpBuilder{m}; }
 OpBuilder op_builder(const std::unique_ptr<coco::Module> &m) { return op_builder(m.get()); }
 
+class InstrBuilder
+{
+public:
+  InstrBuilder(coco::Module *module) : _module{module}
+  {
+    // NOTE _module SHOULD be valid
+    assert(_module != nullptr);
+  }
+
+public:
+  /**
+   * @brief Create "Eval" instruction with a given "Object" and "Op"
+   *
+   * @note "eval(out, op)" will create "%out <- Eval(op)" instruction
+   */
+  coco::Eval *eval(coco::Object *out, coco::Op *op) const
+  {
+    auto ins = _module->entity()->instr()->create<coco::Eval>();
+    ins->op(op);
+    ins->out(out);
+    return ins;
+  }
+
+private:
+  coco::Module *_module;
+};
+
+using ModuleHandle = std::unique_ptr<coco::Module>;
+
+InstrBuilder instr_builder(coco::Module *m) { return InstrBuilder{m}; }
+InstrBuilder instr_builder(const ModuleHandle &m) { return instr_builder(m.get()); }
+
 } // namespace
 
 Frontend::Frontend() : _prototxt{new ::caffe::NetParameter}, _caffemodel{new ::caffe::NetParameter}
@@ -427,10 +459,7 @@ enco::Bundle Frontend::load(void) const
       op->arg(load);
 
       // Create an Eval instruction
-      auto ins = m->entity()->instr()->create<coco::Eval>();
-
-      ins->out(ofm_obj);
-      ins->op(op);
+      auto ins = instr_builder(m).eval(ofm_obj, op);
 
       // Append the instruction to the block
       blk->instr()->append(ins);
@@ -464,10 +493,7 @@ enco::Bundle Frontend::load(void) const
         auto bias_add = op_builder(m).load(bias_obj).load(ofm_obj).add().pop();
 
         // Create Instr
-        auto bias_add_ins = m->entity()->instr()->create<coco::Eval>();
-
-        bias_add_ins->out(added_obj);
-        bias_add_ins->op(bias_add);
+        auto bias_add_ins = instr_builder(m).eval(added_obj, bias_add);
 
         // Append the instruction
         blk->instr()->append(bias_add_ins);
@@ -581,10 +607,7 @@ enco::Bundle Frontend::load(void) const
       auto op = builder(m.get(), spec);
 
       // Create a UnitF instruction
-      auto ins = m->entity()->instr()->create<coco::Eval>();
-
-      ins->out(ofm_obj);
-      ins->op(op);
+      auto ins = instr_builder(m).eval(ofm_obj, op);
 
       // Append the instruction to the block
       blk->instr()->append(ins);
@@ -629,10 +652,7 @@ enco::Bundle Frontend::load(void) const
       op->arg(load);
 
       // Create a Eval instruction
-      auto ins = m->entity()->instr()->create<coco::Eval>();
-
-      ins->out(ofm_obj);
-      ins->op(op);
+      auto ins = instr_builder(m).eval(ofm_obj, op);
 
       // Append the instruction to the block
       blk->instr()->append(ins);
@@ -797,10 +817,7 @@ enco::Bundle Frontend::load(void) const
       ofm_obj->layout(BCHW::create(morph::caffe::as_feature_shape(ofm_shape)));
 
       // Create "Eval" instruction
-      auto eval = m->entity()->instr()->create<coco::Eval>();
-
-      eval->op(op);
-      eval->out(ofm_obj);
+      auto eval = instr_builder(m).eval(ofm_obj, op);
 
       // Append the instruction to the block
       blk->instr()->append(eval);