From cb0a615ad2d483a05235776028c97296a98a388d 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: Fri, 31 Aug 2018 17:35:24 +0900 Subject: [PATCH] [coco] Add ReLU operator (#1272) This commit introduces ReLU op class which denotes a ReLU op in coco IR. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Op.lst | 1 + contrib/coco/core/include/coco/IR/ReLU.h | 37 +++++++++++++++++ contrib/coco/core/src/IR/ReLU.cpp | 18 ++++++++ contrib/coco/core/src/IR/ReLU.test.cpp | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 contrib/coco/core/include/coco/IR/ReLU.h create mode 100644 contrib/coco/core/src/IR/ReLU.cpp create mode 100644 contrib/coco/core/src/IR/ReLU.test.cpp diff --git a/contrib/coco/core/include/coco/IR/Op.lst b/contrib/coco/core/include/coco/IR/Op.lst index 0262e78..5724964 100644 --- a/contrib/coco/core/include/coco/IR/Op.lst +++ b/contrib/coco/core/include/coco/IR/Op.lst @@ -5,3 +5,4 @@ // OP(Name) OP(Conv2D) +OP(ReLU) diff --git a/contrib/coco/core/include/coco/IR/ReLU.h b/contrib/coco/core/include/coco/IR/ReLU.h new file mode 100644 index 0000000..5dc3da7 --- /dev/null +++ b/contrib/coco/core/include/coco/IR/ReLU.h @@ -0,0 +1,37 @@ +#ifndef __COCO_IR_RELU_H__ +#define __COCO_IR_RELU_H__ + +#include "coco/IR/Op.h" + +namespace coco +{ + +/** + * @brief Apply ReLU over elements + */ +class ReLU : public Op +{ +public: + explicit ReLU(const PtrLink *); + +public: + ReLU(const ReLU &) = delete; + ReLU(ReLU &&) = delete; + +public: + std::set uses(void) const override; + +public: + ReLU *asReLU(void) override { return this; } + const ReLU *asReLU(void) const override { return this; } + +private: + void get(const PtrLink **out) const override { *out = _op_link; } + +private: + const PtrLink *const _op_link; +}; + +} // namespace coco + +#endif // __COCO_IR_RELU_H__ diff --git a/contrib/coco/core/src/IR/ReLU.cpp b/contrib/coco/core/src/IR/ReLU.cpp new file mode 100644 index 0000000..b280c14 --- /dev/null +++ b/contrib/coco/core/src/IR/ReLU.cpp @@ -0,0 +1,18 @@ +#include "coco/IR/ReLU.h" + +namespace coco +{ + +ReLU::ReLU(const PtrLink *op_link) : _op_link{op_link} +{ + // DO NOTHING +} + +std::set ReLU::uses(void) const +{ + // NOTE ReLU accesses no object + std::set res; + return res; +} + +} // namespace coco diff --git a/contrib/coco/core/src/IR/ReLU.test.cpp b/contrib/coco/core/src/IR/ReLU.test.cpp new file mode 100644 index 0000000..07a83ae --- /dev/null +++ b/contrib/coco/core/src/IR/ReLU.test.cpp @@ -0,0 +1,70 @@ +#include "coco/IR/ReLU.h" + +#include +#include + +#include + +namespace +{ +struct IsReLU : public coco::Op::DefaultVisitor +{ + bool visit(const coco::ReLU *) override { return true; } +}; + +class ReLUTest : public ::testing::Test +{ +public: + ReLUTest() + { + // DO NOTHING + } + +protected: + coco::ReLU *allocate(void) + { + auto op = new coco::ReLU{&op_link}; + _allocated.emplace_back(op); + return op; + } + +protected: + coco::PtrLink op_link; + +private: + std::vector> _allocated; +}; +} // namespace + +TEST_F(ReLUTest, initialization) +{ + auto op = allocate(); + + // uses() should be empty on construction + ASSERT_EQ(op->uses().size(), 0); + // parent() should be nullptr on construction + ASSERT_EQ(op->parent(), nullptr); +} + +TEST_F(ReLUTest, asReLU) +{ + auto op = allocate(); + + coco::Op *mutable_base = op; + const coco::Op *immutable_base = op; + + ASSERT_EQ(mutable_base->asReLU(), op); + ASSERT_EQ(mutable_base->asReLU(), immutable_base->asReLU()); +} + +TEST_F(ReLUTest, accept) +{ + // Test 'ReLU' class + auto op = allocate(); + + coco::ReLU *mutable_ptr = op; + const coco::ReLU *immutable_ptr = op; + + ASSERT_TRUE(mutable_ptr->accept(IsReLU{})); + ASSERT_TRUE(immutable_ptr->accept(IsReLU{})); +} -- 2.7.4