From c4acddf7080b8f618d164ac605d69c904c3696c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Principal=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 6 Dec 2018 17:09:50 +0900 Subject: [PATCH] [enco] frontend/caffe: Separate OpBuilder and InstrBuilder (#2537) This will move OpBuilder and InstrBuilder class to separate file Signed-off-by: SaeHie Park --- contrib/enco/frontend/caffe/src/Frontend.cpp | 144 +---------------------- contrib/enco/frontend/caffe/src/IRBuilder.h | 164 +++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 143 deletions(-) create mode 100644 contrib/enco/frontend/caffe/src/IRBuilder.h diff --git a/contrib/enco/frontend/caffe/src/Frontend.cpp b/contrib/enco/frontend/caffe/src/Frontend.cpp index 01b9b18..b80b537 100644 --- a/contrib/enco/frontend/caffe/src/Frontend.cpp +++ b/contrib/enco/frontend/caffe/src/Frontend.cpp @@ -18,6 +18,7 @@ #include "ConvolutionSpec.h" #include "PoolingSpec.h" #include "ConcatSpec.h" +#include "IRBuilder.h" #include "Context.h" #include "Convert.h" @@ -55,149 +56,6 @@ using tensor::LexicalLayout; using nncc::foundation::make_unique; -/** - * coco IR builders - */ -namespace -{ - -class OpBuilder -{ -public: - OpBuilder(coco::Module *module) : _module{module} - { - // module SHOULD BE valid - assert(_module != nullptr); - } - -public: - /** - * @brief Return true if the internal stack is empty - */ - bool empty(void) const { return _stack.empty(); } - - /** - * @brief Return the operation at the top of the internal stack - */ - coco::Op *top(void) const - { - assert(_stack.size() > 0); - return _stack.front(); - } - - /** - * @brief Push op onto the internal stack - * - * BEFORE| Stack - * AFTER | Op; Stack - */ - OpBuilder &push(coco::Op *op) - { - _stack.push_front(op); - return (*this); - } - - /** - * @brief Create "Load" op and push it onto the internal stack - * - * BEFORE| Stack - * AFTER | Load(obj); Stack - */ - OpBuilder &load(coco::Object *obj) - { - auto op = _module->entity()->op()->create(); - op->object(obj); - push(op); - return (*this); - } - - /** - * @brief Create "Add" op and push it onto the internal stack - * - * BEFORE| Left; Right; Stack - * AFTER | Add(Left, Right); Stack - */ - OpBuilder &add(void) { return binary(); } - - /** - * @brief Create "Mul" op and push it onto the internal stack - * - * BEFORE| Left; Right; Stack - * AFTER | Mul(Left, Right); Stack - */ - OpBuilder &mul(void) { return binary(); } - - /** - * @brief Pop op from the internal stack - * - * BEFORE| Op; Stack - * AFTER | Stack - */ - coco::Op *pop(void) - { - assert(_stack.size() > 0); - auto op = _stack.front(); - _stack.pop_front(); - return op; - } - -private: - template OpBuilder &binary() - { - assert(_stack.size() >= 2); - auto left = pop(); - auto right = pop(); - - auto op = _module->entity()->op()->create(); - op->left(left); - op->right(right); - push(op); - - return (*this); - } - -private: - coco::Module *_module; - std::deque _stack; -}; - -OpBuilder op_builder(coco::Module *m) { return OpBuilder{m}; } -OpBuilder op_builder(const std::unique_ptr &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(); - ins->op(op); - ins->out(out); - return ins; - } - -private: - coco::Module *_module; -}; - -using ModuleHandle = std::unique_ptr; - -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} { // DO NOTHING diff --git a/contrib/enco/frontend/caffe/src/IRBuilder.h b/contrib/enco/frontend/caffe/src/IRBuilder.h new file mode 100644 index 0000000..d698947 --- /dev/null +++ b/contrib/enco/frontend/caffe/src/IRBuilder.h @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IR_BUILDER_H__ +#define __IR_BUILDER_H__ + +#include "coco/IR/Module.h" + +#include + +/** + * coco IR builders + */ + +class OpBuilder +{ +public: + OpBuilder(coco::Module *module) : _module{module} + { + // module SHOULD BE valid + assert(_module != nullptr); + } + +public: + /** + * @brief Return true if the internal stack is empty + */ + bool empty(void) const { return _stack.empty(); } + + /** + * @brief Return the operation at the top of the internal stack + */ + coco::Op *top(void) const + { + assert(_stack.size() > 0); + return _stack.front(); + } + + /** + * @brief Push op onto the internal stack + * + * BEFORE| Stack + * AFTER | Op; Stack + */ + OpBuilder &push(coco::Op *op) + { + _stack.push_front(op); + return (*this); + } + + /** + * @brief Create "Load" op and push it onto the internal stack + * + * BEFORE| Stack + * AFTER | Load(obj); Stack + */ + OpBuilder &load(coco::Object *obj) + { + auto op = _module->entity()->op()->create(); + op->object(obj); + push(op); + return (*this); + } + + /** + * @brief Create "Add" op and push it onto the internal stack + * + * BEFORE| Left; Right; Stack + * AFTER | Add(Left, Right); Stack + */ + OpBuilder &add(void) { return binary(); } + + /** + * @brief Create "Mul" op and push it onto the internal stack + * + * BEFORE| Left; Right; Stack + * AFTER | Mul(Left, Right); Stack + */ + OpBuilder &mul(void) { return binary(); } + + /** + * @brief Pop op from the internal stack + * + * BEFORE| Op; Stack + * AFTER | Stack + */ + coco::Op *pop(void) + { + assert(_stack.size() > 0); + auto op = _stack.front(); + _stack.pop_front(); + return op; + } + +private: + template OpBuilder &binary() + { + assert(_stack.size() >= 2); + auto left = pop(); + auto right = pop(); + + auto op = _module->entity()->op()->create(); + op->left(left); + op->right(right); + push(op); + + return (*this); + } + +private: + coco::Module *_module; + std::deque _stack; +}; + +inline OpBuilder op_builder(coco::Module *m) { return OpBuilder{m}; } +inline OpBuilder op_builder(const std::unique_ptr &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(); + ins->op(op); + ins->out(out); + return ins; + } + +private: + coco::Module *_module; +}; + +inline InstrBuilder instr_builder(coco::Module *m) { return InstrBuilder{m}; } +inline InstrBuilder instr_builder(const std::unique_ptr &m) +{ + return instr_builder(m.get()); +} + +#endif // __IR_BUILDER_H__ -- 2.7.4