From 09079eb5a22fd4bb7006268c97886283d504c485 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=91=D0=B0=D1=80?= =?utf8?q?=D0=B0=D0=BD=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2/AI=20Tools=20Lab=20/S?= =?utf8?q?RR/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 9 Aug 2019 14:48:52 +0300 Subject: [PATCH] [mir] Delete ElementwiseOp (#6418) It was replaced by several binary elementwise operations. Signed-off-by: Sergei Barannikov --- compiler/mir/CMakeLists.txt | 1 - compiler/mir/include/mir/IrDotDumper.h | 1 - compiler/mir/include/mir/OpDefs.h | 1 - compiler/mir/include/mir/Operations.inc | 1 - compiler/mir/include/mir/ops/ElementwiseOp.h | 70 ---------------------------- compiler/mir/src/IrDotDumper.cpp | 17 ------- compiler/mir/src/ops/ElementwiseOp.cpp | 61 ------------------------ compiler/mir/unittests/ShapeInference.cpp | 5 +- 8 files changed, 2 insertions(+), 155 deletions(-) delete mode 100644 compiler/mir/include/mir/ops/ElementwiseOp.h delete mode 100644 compiler/mir/src/ops/ElementwiseOp.cpp diff --git a/compiler/mir/CMakeLists.txt b/compiler/mir/CMakeLists.txt index 2e04cf3..fd1809f 100644 --- a/compiler/mir/CMakeLists.txt +++ b/compiler/mir/CMakeLists.txt @@ -4,7 +4,6 @@ set(MIR_SOURCES src/ops/Conv2DOp.cpp src/ops/DeConv2DOp.cpp src/ops/DepthwiseConv2DOp.cpp - src/ops/ElementwiseOp.cpp src/ops/FullyConnectedOp.cpp src/ops/GatherOp.cpp src/ops/GemmOp.cpp diff --git a/compiler/mir/include/mir/IrDotDumper.h b/compiler/mir/include/mir/IrDotDumper.h index 03158a6..3e3eca2 100644 --- a/compiler/mir/include/mir/IrDotDumper.h +++ b/compiler/mir/include/mir/IrDotDumper.h @@ -38,7 +38,6 @@ public: void visit(ops::DeConv2DOp &op) override; void visit(ops::DepthwiseConv2DOp &op) override; void visit(ops::DropoutOp &op) override; - void visit(ops::ElementwiseOp &op) override; void visit(ops::EluOp &op) override; void visit(ops::FullyConnectedOp &op) override; void visit(ops::GatherOp &op) override; diff --git a/compiler/mir/include/mir/OpDefs.h b/compiler/mir/include/mir/OpDefs.h index d685cd6..cdd3d71 100644 --- a/compiler/mir/include/mir/OpDefs.h +++ b/compiler/mir/include/mir/OpDefs.h @@ -28,7 +28,6 @@ #include "mir/ops/DepthwiseConv2DOp.h" #include "mir/ops/DivOp.h" #include "mir/ops/DropoutOp.h" -#include "mir/ops/ElementwiseOp.h" #include "mir/ops/EluOp.h" #include "mir/ops/FullyConnectedOp.h" #include "mir/ops/GatherOp.h" diff --git a/compiler/mir/include/mir/Operations.inc b/compiler/mir/include/mir/Operations.inc index 6c18200..9c15b30 100644 --- a/compiler/mir/include/mir/Operations.inc +++ b/compiler/mir/include/mir/Operations.inc @@ -28,7 +28,6 @@ HANDLE_OP(deConv2D, DeConv2DOp) HANDLE_OP(depthwiseConv, DepthwiseConv2DOp) HANDLE_OP(div, DivOp) HANDLE_OP(dropout, DropoutOp) -HANDLE_OP(elementwise, ElementwiseOp) HANDLE_OP(ELU, EluOp) HANDLE_OP(fullyConnected, FullyConnectedOp) HANDLE_OP(gather, GatherOp) diff --git a/compiler/mir/include/mir/ops/ElementwiseOp.h b/compiler/mir/include/mir/ops/ElementwiseOp.h deleted file mode 100644 index d3b57ec..0000000 --- a/compiler/mir/include/mir/ops/ElementwiseOp.h +++ /dev/null @@ -1,70 +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. - */ - -#ifndef _MIR_OPS_ELEMENTWISE_OP_H_ -#define _MIR_OPS_ELEMENTWISE_OP_H_ - -#include "mir/Operation.h" - -namespace mir -{ -namespace ops -{ - -class ElementwiseOp : public Operation -{ -public: - enum class OpType - { - mul, - add, - max, - div, - sub, - }; - - /** - * Apply 2-arg operation elementwise reducing along input tensors - * @param op_type Type of operation to perform - * @param num_inputs Number of inputs - */ - ElementwiseOp(const std::vector &args, OpType op_type) - : Operation(Type::elementwise, args), _op_type(op_type), _needs_broadcast(false) - { - inferOutputShapes(); - }; - - Operation *copyWithInputs(const std::vector &inputs) override - { - return new ElementwiseOp(inputs, _op_type); - } - - bool getBroadcast() const { return _needs_broadcast; } - -private: - void inferOutputShapes(); - - OpType _op_type; - bool _needs_broadcast; - -public: - OpType getOpType() const { return _op_type; } -}; - -} // namespace ops -} // namespace mir - -#endif //_MIR_OPS_ELEMENTWISE_OP_H_ diff --git a/compiler/mir/src/IrDotDumper.cpp b/compiler/mir/src/IrDotDumper.cpp index 1daa427..c4e3a33 100644 --- a/compiler/mir/src/IrDotDumper.cpp +++ b/compiler/mir/src/IrDotDumper.cpp @@ -241,23 +241,6 @@ void IrDotDumper::visit(ops::TanhOp &op) _dot_builder.updateWithOp(&op, nodeInfo); } -void mir::IrDotDumper::visit(ops::ElementwiseOp &op) -{ - static const std::map op_types{ - {ops::ElementwiseOp::OpType::mul, "mul"}, - {ops::ElementwiseOp::OpType::add, "add"}, - {ops::ElementwiseOp::OpType::max, "max"}, - {ops::ElementwiseOp::OpType::div, "div"}}; - - auto node_info = DotIrNodeInfo() - .withType("ElementwiseOp", op.getName()) - .withInShapes(getInputShapes(op)) - .withOutShapes(getOutputShapes(op)) - .withMisc("Operation", op_types.at(op.getOpType())); - - _dot_builder.updateWithOp(&op, node_info); -} - void IrDotDumper::visit(ops::SqueezeOp &op) { auto node_info = DotIrNodeInfo() diff --git a/compiler/mir/src/ops/ElementwiseOp.cpp b/compiler/mir/src/ops/ElementwiseOp.cpp deleted file mode 100644 index e1c5f85..0000000 --- a/compiler/mir/src/ops/ElementwiseOp.cpp +++ /dev/null @@ -1,61 +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 "mir/ops/ElementwiseOp.h" - -namespace mir -{ -namespace ops -{ - -void ElementwiseOp::inferOutputShapes() -{ - int max_rank = getInputShape(0).rank(); - size_t max_ind = 0; - for (size_t i = 0; i < getNumInputs(); i++) - { - if (max_rank < getInputShape(i).rank()) - { - max_rank = getInputShape(i).rank(); - max_ind = i; - } - } - Shape max_shape = getInputShape(max_ind); - for (size_t i = 0; i < getNumInputs(); i++) - { - const auto ¤t_shape = getInputShape(i); - _needs_broadcast = _needs_broadcast || max_shape != current_shape; // check not equal - const int rank = current_shape.rank(); - for (int axis = 0; axis < rank; axis++) - { - auto current_dim = current_shape.dim(rank - axis - 1); - // get max for all axes - if (max_shape.dim(max_rank - axis - 1) == 1 && current_dim != 1) - { - max_shape.dim(max_rank - axis - 1) = current_dim; - } - else - { - assert((current_dim == 1 || current_dim == max_shape.dim(max_rank - axis - 1)) && - "Incompatible shapes in broadcast!"); - } - } - } - setOutputShape(0, max_shape); -} - -} // namespace ops -} // namespace mir diff --git a/compiler/mir/unittests/ShapeInference.cpp b/compiler/mir/unittests/ShapeInference.cpp index b57aaa6..3d47dbd 100644 --- a/compiler/mir/unittests/ShapeInference.cpp +++ b/compiler/mir/unittests/ShapeInference.cpp @@ -15,8 +15,8 @@ */ #include "mir/Graph.h" +#include "mir/ops/AddOp.h" #include "mir/ops/ReshapeOp.h" -#include "mir/ops/ElementwiseOp.h" #include "mir/ops/ResizeOp.h" #include "mir/ops/SqueezeOp.h" #include "mir/ops/ReduceOp.h" @@ -147,8 +147,7 @@ TEST(ShapeInferenceTest, ElementwiseBC) auto input = g.create("input1", input_shape); auto input2 = g.create("input2", input2_shape); - std::vector add_inputs{input->getOutput(0), input2->getOutput(0)}; - auto add = g.create("add_1", add_inputs, ops::ElementwiseOp::OpType::add); + auto add = g.create("add_1", input->getOutput(0), input2->getOutput(0)); ASSERT_EQ(add->getOutputShape(0), Shape({1, 10, 10, 10})); } -- 2.7.4