[enco] Use 'Appender' in Split pass (#1516)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 17 Sep 2018 06:36:18 +0000 (15:36 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 17 Sep 2018 06:36:18 +0000 (15:36 +0900)
* [enco] Use 'Appender' in Split pass

This commit introduces the concept of 'Appender' in Split pass.

Conceptually, each Appender encapsulates the update over ANN IR for
each coco IR instruction.

This change is a step to unify ANNGroupBuilder and ANNOpBuilder.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Remove unused variable avgpool

contrib/enco/core/src/Transforms/Split.cpp

index 706dbe3..31862e5 100644 (file)
@@ -166,12 +166,15 @@ void ANNGroupBuilder::build(void) const
 #include "Usage.h"
 
 #include <stdexcept>
+#include <functional>
 
 #include <nncc/core/ADT/kernel/NHWCLayout.h>
 
 namespace
 {
 
+using Appender = std::function<void(ANNBinder *binder)>;
+
 class ANNOpBuilder : public coco::Instr::Visitor<void>
 {
 public:
@@ -183,8 +186,48 @@ public:
 public:
   void visit(const coco::UnitF *unit)
   {
-    if (auto conv = unit->op()->asConv2D())
+    if (unit->op()->asConv2D())
+    {
+      auto f = conv2d(unit);
+      f(_binder);
+    }
+    else if (unit->op()->asReLU())
+    {
+      auto f = relu(unit);
+      f(_binder);
+    }
+    else if (unit->op()->asMaxPool2D())
+    {
+      auto f = maxpool2d(unit);
+      f(_binder);
+    }
+    else if (unit->op()->asAvgPool2D())
+    {
+      auto f = avgpool2d(unit);
+      f(_binder);
+    }
+    else if (unit->op()->asPadF())
     {
+      auto f = pad(unit);
+      f(_binder);
+    }
+    else
+    {
+      throw std::runtime_error{"Not supported, yet"};
+    }
+  }
+
+public:
+  void visit(const coco::Shuffle *) { throw std::runtime_error{"Not supported, yet"}; }
+
+private:
+  Appender conv2d(const coco::UnitF *unit) const
+  {
+    auto _data = this->_data;
+    assert(unit->op()->asConv2D());
+    // TODO Rename "_binder"
+    return [unit, _data](ANNBinder *_binder) {
+      auto conv = unit->op()->asConv2D();
       auto ifm = _binder->addOperand<float>(unit->ifm());
       auto ker = _binder->addOperand<float>(conv->ker());
 
@@ -255,16 +298,27 @@ public:
       _binder->addOperation(ann::Operation::Code::CONV_2D,
                             {ifm, ker, bias, left, right, top, bottom, hstride, vstride, fuse},
                             {ofm});
-    }
-    else if (unit->op()->asReLU())
-    {
+    };
+  }
+
+  Appender relu(const coco::UnitF *unit)
+  {
+    assert(unit->op()->asReLU());
+    // TODO Rename "_binder"
+    return [unit](ANNBinder *_binder) {
       auto ifm = _binder->addOperand<float>(unit->ifm());
       auto ofm = _binder->addOperand<float>(unit->ofm());
 
       _binder->addOperation(ann::Operation::Code::RELU, {ifm}, {ofm});
-    }
-    else if (auto maxpool = unit->op()->asMaxPool2D())
-    {
+    };
+  }
+
+  Appender maxpool2d(const coco::UnitF *unit)
+  {
+    assert(unit->op()->asMaxPool2D());
+    // TODO Rename "_binder"
+    return [unit](ANNBinder *_binder) {
+      auto maxpool = unit->op()->asMaxPool2D();
       auto ifm = _binder->addOperand<float>(unit->ifm());
 
       // Set padding
@@ -299,9 +353,15 @@ public:
       _binder->addOperation(ann::Operation::Code::MAX_POOL_2D,
                             {ifm, left, right, top, bottom, hstride, vstride, width, height, fuse},
                             {ofm});
-    }
-    else if (auto avgpool = unit->op()->asAvgPool2D())
-    {
+    };
+  }
+
+  Appender avgpool2d(const coco::UnitF *unit)
+  {
+    assert(unit->op()->asAvgPool2D());
+    // TODO Rename "_binder"
+    return [unit](ANNBinder *_binder) {
+      auto avgpool = unit->op()->asAvgPool2D();
       auto ifm = _binder->addOperand<float>(unit->ifm());
 
       auto left = _binder->addOperand<int32_t>();
@@ -334,9 +394,16 @@ public:
       _binder->addOperation(ann::Operation::Code::AVG_POOL_2D,
                             {ifm, left, right, top, bottom, hstride, vstride, width, height, fuse},
                             {ofm});
-    }
-    else if (auto op = unit->op()->asPadF())
-    {
+    };
+  }
+
+  Appender pad(const coco::UnitF *unit)
+  {
+    assert(unit->op()->asPadF());
+    // TODO Rename "_binder"
+    return [unit](ANNBinder *_binder) {
+      auto op = unit->op()->asPadF();
+
       using nncc::core::ADT::tensor::Shape;
 
       auto ifm = _binder->addOperand<float>(unit->ifm());
@@ -361,15 +428,9 @@ public:
       auto ofm = _binder->addOperand<float>(unit->ofm());
 
       _binder->addOperation(ann::Operation::Code::PAD, {ifm, pad}, {ofm});
-    }
-    else
-    {
-      throw std::runtime_error{"Not supported, yet"};
-    }
-  }
 
-public:
-  void visit(const coco::Shuffle *) { throw std::runtime_error{"Not supported, yet"}; }
+    };
+  }
 
 private:
   ANNBinder *const _binder;