From b177716d6b42cdd51f53ce95dec0269d9dbe35ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Mon, 17 Sep 2018 15:36:18 +0900 Subject: [PATCH] [enco] Use 'Appender' in Split pass (#1516) * [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 * Remove unused variable avgpool --- contrib/enco/core/src/Transforms/Split.cpp | 103 +++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/contrib/enco/core/src/Transforms/Split.cpp b/contrib/enco/core/src/Transforms/Split.cpp index 706dbe3..31862e5 100644 --- a/contrib/enco/core/src/Transforms/Split.cpp +++ b/contrib/enco/core/src/Transforms/Split.cpp @@ -166,12 +166,15 @@ void ANNGroupBuilder::build(void) const #include "Usage.h" #include +#include #include namespace { +using Appender = std::function; + class ANNOpBuilder : public coco::Instr::Visitor { 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(unit->ifm()); auto ker = _binder->addOperand(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(unit->ifm()); auto ofm = _binder->addOperand(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(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(unit->ifm()); auto left = _binder->addOperand(); @@ -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(unit->ifm()); @@ -361,15 +428,9 @@ public: auto ofm = _binder->addOperand(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; -- 2.7.4