From e3e000bc48a7195c3296adeb00f9cf6271c5fe88 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, 12 Oct 2018 16:56:19 +0900 Subject: [PATCH] [coco] Introduce UnaryOp trait (#1853) This commit introduces UnaryOp traits, and simplifies the implementation and declaration of several existing operation classes, such as ReLU and PadF. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Op.h | 29 +++++++++++++++- contrib/coco/core/include/coco/IR/Ops.h | 60 +++++---------------------------- contrib/coco/core/src/IR/AvgPool2D.cpp | 43 ----------------------- contrib/coco/core/src/IR/MaxPool2D.cpp | 43 ----------------------- contrib/coco/core/src/IR/Op.cpp | 24 +++++++++++++ contrib/coco/core/src/IR/PadF.cpp | 43 ----------------------- contrib/coco/core/src/IR/ReLU.cpp | 43 ----------------------- 7 files changed, 60 insertions(+), 225 deletions(-) delete mode 100644 contrib/coco/core/src/IR/AvgPool2D.cpp delete mode 100644 contrib/coco/core/src/IR/MaxPool2D.cpp delete mode 100644 contrib/coco/core/src/IR/PadF.cpp delete mode 100644 contrib/coco/core/src/IR/ReLU.cpp diff --git a/contrib/coco/core/include/coco/IR/Op.h b/contrib/coco/core/include/coco/IR/Op.h index 754a114..670973e 100644 --- a/contrib/coco/core/include/coco/IR/Op.h +++ b/contrib/coco/core/include/coco/IR/Op.h @@ -24,7 +24,7 @@ #include "coco/IR/Object.forward.h" #include "coco/IR/Instr.forward.h" #include "coco/IR/Step.forward.h" -#include "coco/IR/Part.forward.h" +#include "coco/IR/Part.h" #include "coco/IR/Entity.h" #include "coco/ADT/PtrLink.h" @@ -157,6 +157,33 @@ private: Part *_part = nullptr; }; +/** + * @brief Op with a single argument + */ +class UnaryOp : public Op +{ +public: + explicit UnaryOp(); + +public: + UnaryOp(const UnaryOp &) = delete; + UnaryOp(UnaryOp &&) = delete; + +public: + virtual ~UnaryOp() = default; + +public: + std::set uses(void) const final; + +public: + Op *arg(void) const { return _arg.child(); } + void arg(Op *arg) { _arg.child(arg); } + +private: + // @brief Link to Op's argument + Part _arg; +}; + } // namespace coco #endif // __COCO_IR_OP_H__ diff --git a/contrib/coco/core/include/coco/IR/Ops.h b/contrib/coco/core/include/coco/IR/Ops.h index 6f4df4e..12ab3a9 100644 --- a/contrib/coco/core/include/coco/IR/Ops.h +++ b/contrib/coco/core/include/coco/IR/Ops.h @@ -132,27 +132,20 @@ private: /** * @brief 2D Max Pooling */ -class MaxPool2D : public Op +class MaxPool2D final : public UnaryOp { public: - explicit MaxPool2D(); + explicit MaxPool2D() = default; public: MaxPool2D(const MaxPool2D &) = delete; MaxPool2D(MaxPool2D &&) = delete; public: - std::set uses(void) const override; - -public: MaxPool2D *asMaxPool2D(void) override { return this; } const MaxPool2D *asMaxPool2D(void) const override { return this; } public: - Op *arg(void) const { return _arg.child(); } - void arg(Op *arg) { _arg.child(arg); } - -public: Window2D *window(void) { return &_window; } const Window2D *window(void) const { return &_window; } @@ -165,10 +158,6 @@ public: const Padding2D *pad(void) const { return &_pad; } private: - // @brief An argument of MaxPool2D operation (= IFM) - Part _arg; - -private: Window2D _window; Stride2D _stride; Padding2D _pad; @@ -177,7 +166,7 @@ private: /** * @brief 2D Average Pooling */ -class AvgPool2D : public Op +class AvgPool2D final : public UnaryOp { public: enum class Divisor @@ -190,24 +179,17 @@ public: }; public: - explicit AvgPool2D(); + explicit AvgPool2D() = default; public: AvgPool2D(const AvgPool2D &) = delete; AvgPool2D(AvgPool2D &&) = delete; public: - std::set uses(void) const override; - -public: AvgPool2D *asAvgPool2D(void) override { return this; } const AvgPool2D *asAvgPool2D(void) const override { return this; } public: - Op *arg(void) const { return _arg.child(); } - void arg(Op *arg) { _arg.child(arg); } - -public: Divisor divisor(void) const { return _divisor; } void divisor(const Divisor &divisor) { _divisor = divisor; } @@ -224,10 +206,6 @@ public: const Stride2D *stride(void) const { return &_stride; } private: - // @brief An argument of AvgPool2D operation (= IFM) - Part _arg; - -private: Divisor _divisor = Divisor::Unknown; Window2D _window; @@ -238,64 +216,42 @@ private: /** * @brief Introduce padding area */ -class PadF : public Op +class PadF final : public UnaryOp { public: - explicit PadF(); + explicit PadF() = default; public: PadF(const PadF &) = delete; PadF(PadF &&) = delete; public: - std::set uses(void) const override; - -public: PadF *asPadF(void) override { return this; } const PadF *asPadF(void) const override { return this; } public: - Op *arg(void) const { return _arg.child(); } - void arg(Op *arg) { _arg.child(arg); } - -public: Padding2D *pad(void) { return &_pad; } const Padding2D *pad(void) const { return &_pad; } private: - // @brief An argument of PadF operation (= IFM) - Part _arg; - -private: Padding2D _pad; }; /** * @brief Apply ReLU over elements */ -class ReLU : public Op +class ReLU final : public UnaryOp { public: - explicit ReLU(); + explicit ReLU() = default; 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; } - -public: - Op *arg(void) const { return _arg.child(); } - void arg(Op *arg) { _arg.child(arg); } - -private: - // @brief An argument of ReLU operation (= IFM) - Part _arg; }; /** diff --git a/contrib/coco/core/src/IR/AvgPool2D.cpp b/contrib/coco/core/src/IR/AvgPool2D.cpp deleted file mode 100644 index a06cce7..0000000 --- a/contrib/coco/core/src/IR/AvgPool2D.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#include "coco/IR/Ops.h" - -namespace coco -{ - -AvgPool2D::AvgPool2D() : _arg{this} -{ - // DO NOTHING -} - -std::set AvgPool2D::uses(void) const -{ - // NOTE AvgPool2D accesses no object except IFM/OFM - std::set res; - - if (auto ifm = arg()) - { - for (auto obj : ifm->uses()) - { - res.insert(obj); - } - } - - return res; -} - -} // namespace coco diff --git a/contrib/coco/core/src/IR/MaxPool2D.cpp b/contrib/coco/core/src/IR/MaxPool2D.cpp deleted file mode 100644 index cf5ef2e..0000000 --- a/contrib/coco/core/src/IR/MaxPool2D.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#include "coco/IR/Ops.h" - -namespace coco -{ - -MaxPool2D::MaxPool2D() : _arg{this} -{ - // DO NOTHING -} - -std::set MaxPool2D::uses(void) const -{ - // NOTE MaxPool2D accesses no object except IFM/OFM - std::set res; - - if (auto ifm = arg()) - { - for (auto obj : ifm->uses()) - { - res.insert(obj); - } - } - - return res; -} - -} // namespace coco diff --git a/contrib/coco/core/src/IR/Op.cpp b/contrib/coco/core/src/IR/Op.cpp index 33a58c1..a2ab605 100644 --- a/contrib/coco/core/src/IR/Op.cpp +++ b/contrib/coco/core/src/IR/Op.cpp @@ -52,4 +52,28 @@ Op *Op::up(void) const } return nullptr; } + +// +// UnaryOP trait +// +UnaryOp::UnaryOp() : _arg{this} +{ + // DO NOTHING +} + +std::set UnaryOp::uses(void) const +{ + std::set res; + + if (auto ifm = arg()) + { + for (auto obj : ifm->uses()) + { + res.insert(obj); + } + } + + return res; +} + } // namespace coco diff --git a/contrib/coco/core/src/IR/PadF.cpp b/contrib/coco/core/src/IR/PadF.cpp deleted file mode 100644 index b3e7a6a..0000000 --- a/contrib/coco/core/src/IR/PadF.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#include "coco/IR/Ops.h" - -namespace coco -{ - -PadF::PadF() : _arg{this} -{ - // DO NOTHING -} - -std::set PadF::uses(void) const -{ - // NOTE PadF accesses no object - std::set res; - - if (auto ifm = arg()) - { - for (auto obj : ifm->uses()) - { - res.insert(obj); - } - } - - return res; -} - -} // namespace coco diff --git a/contrib/coco/core/src/IR/ReLU.cpp b/contrib/coco/core/src/IR/ReLU.cpp deleted file mode 100644 index d9a3b96..0000000 --- a/contrib/coco/core/src/IR/ReLU.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#include "coco/IR/Ops.h" - -namespace coco -{ - -ReLU::ReLU() : _arg{this} -{ - // DO NOTHING -} - -std::set ReLU::uses(void) const -{ - // NOTE ReLU itself accesses no object - std::set res; - - if (auto ifm = arg()) - { - for (auto obj : ifm->uses()) - { - res.insert(obj); - } - } - - return res; -} - -} // namespace coco -- 2.7.4