From 414152bab5137b5b0e4eb0b82ba823692ac692de Mon Sep 17 00:00:00 2001 From: Gusev Dmitry/AI Tools Lab /SRR/Engineer/Samsung Electronics Date: Tue, 3 Dec 2019 19:21:48 +0300 Subject: [PATCH] [mir-onnx-importer] Import comparison operations from ONNX format (#9339) Operation Greater, Less, Equal were added. Signed-off-by: Dmitry Gusev --- compiler/mir-onnx-importer/CMakeLists.txt | 6 ++++ compiler/mir-onnx-importer/ONNXOpRegistration.h | 6 ++++ compiler/mir-onnx-importer/Op/Equal.cpp | 37 ++++++++++++++++++++++++ compiler/mir-onnx-importer/Op/Equal.h | 33 +++++++++++++++++++++ compiler/mir-onnx-importer/Op/Greater.cpp | 38 +++++++++++++++++++++++++ compiler/mir-onnx-importer/Op/Greater.h | 33 +++++++++++++++++++++ compiler/mir-onnx-importer/Op/Less.cpp | 37 ++++++++++++++++++++++++ compiler/mir-onnx-importer/Op/Less.h | 33 +++++++++++++++++++++ 8 files changed, 223 insertions(+) create mode 100644 compiler/mir-onnx-importer/Op/Equal.cpp create mode 100644 compiler/mir-onnx-importer/Op/Equal.h create mode 100644 compiler/mir-onnx-importer/Op/Greater.cpp create mode 100644 compiler/mir-onnx-importer/Op/Greater.h create mode 100644 compiler/mir-onnx-importer/Op/Less.cpp create mode 100644 compiler/mir-onnx-importer/Op/Less.h diff --git a/compiler/mir-onnx-importer/CMakeLists.txt b/compiler/mir-onnx-importer/CMakeLists.txt index a6f2dda..21b2592 100644 --- a/compiler/mir-onnx-importer/CMakeLists.txt +++ b/compiler/mir-onnx-importer/CMakeLists.txt @@ -45,14 +45,20 @@ set(MIR_ONNX_IMPORTER_SOURCES Op/ConvTranspose.h Op/Dropout.cpp Op/Dropout.h + Op/Equal.cpp + Op/Equal.h Op/Flatten.cpp Op/Flatten.h Op/Gather.cpp Op/Gather.h + Op/Greater.cpp + Op/Greater.h Op/Gemm.cpp Op/Gemm.h Op/Identity.cpp Op/Identity.h + Op/Less.cpp + Op/Less.h Op/MatMul.cpp Op/MatMul.h Op/GlobalAveragePool.cpp diff --git a/compiler/mir-onnx-importer/ONNXOpRegistration.h b/compiler/mir-onnx-importer/ONNXOpRegistration.h index 2014fbc..7868162 100644 --- a/compiler/mir-onnx-importer/ONNXOpRegistration.h +++ b/compiler/mir-onnx-importer/ONNXOpRegistration.h @@ -27,11 +27,14 @@ #include "Op/Conv.h" #include "Op/ConvTranspose.h" #include "Op/Dropout.h" +#include "Op/Equal.h" #include "Op/Flatten.h" #include "Op/Gather.h" +#include "Op/Greater.h" #include "Op/Gemm.h" #include "Op/GlobalAveragePool.h" #include "Op/Identity.h" +#include "Op/Less.h" #include "Op/MatMul.h" #include "Op/Max.h" #include "Op/MaxPool.h" @@ -67,12 +70,15 @@ inline void registerSupportedOps() registry.registerConverter("Conv", stdex::make_unique()); registry.registerConverter("ConvTranspose", stdex::make_unique()); registry.registerConverter("Dropout", stdex::make_unique()); + registry.registerConverter("Equal", stdex::make_unique()); registry.registerConverter("Flatten", stdex::make_unique()); registry.registerConverter("Gather", stdex::make_unique()); + registry.registerConverter("Greater", stdex::make_unique()); registry.registerConverter("Gemm", stdex::make_unique()); registry.registerConverter("GlobalAveragePool", stdex::make_unique()); registry.registerConverter("Identity", stdex::make_unique()); + registry.registerConverter("Less", stdex::make_unique()); registry.registerConverter("MatMul", stdex::make_unique()); registry.registerConverter("Max", stdex::make_unique()); registry.registerConverter("MaxPool", stdex::make_unique()); diff --git a/compiler/mir-onnx-importer/Op/Equal.cpp b/compiler/mir-onnx-importer/Op/Equal.cpp new file mode 100644 index 0000000..26f2b0e --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Equal.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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 "Equal.h" + +#include "ONNXHelpers.h" +#include "AttributeHelpers.h" + +#include "mir/ops/EqualOp.h" + +namespace mir_onnx +{ + +void EqualNodeConverter::convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const +{ + std::vector inputs = context->getNodeInputs(onnx_node); + mir::Graph *graph = context->getGraph(); + + auto result = createOp(graph, inputs[0], inputs[1])->getOutput(0); + + context->setNodeOutputs(onnx_node, {result}); +} + +} // namespace mir_onnx diff --git a/compiler/mir-onnx-importer/Op/Equal.h b/compiler/mir-onnx-importer/Op/Equal.h new file mode 100644 index 0000000..eacd706 --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Equal.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 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_ONNX_OP_EQUAL_H +#define MIR_ONNX_OP_EQUAL_H + +#include "ONNXNodeConverterRegistry.h" + +namespace mir_onnx +{ + +class EqualNodeConverter : public NodeConverter +{ +public: + void convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const override; +}; + +} // namespace mir_onnx + +#endif // MIR_ONNX_OP_EQUAL_H diff --git a/compiler/mir-onnx-importer/Op/Greater.cpp b/compiler/mir-onnx-importer/Op/Greater.cpp new file mode 100644 index 0000000..710fa24 --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Greater.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 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 "Greater.h" + +#include "ONNXHelpers.h" +#include "AttributeHelpers.h" + +#include "mir/ops/GreaterOp.h" + +namespace mir_onnx +{ + +void GreaterNodeConverter::convert(const onnx::NodeProto &onnx_node, + ConverterContext *context) const +{ + std::vector inputs = context->getNodeInputs(onnx_node); + mir::Graph *graph = context->getGraph(); + + auto result = createOp(graph, inputs[0], inputs[1])->getOutput(0); + + context->setNodeOutputs(onnx_node, {result}); +} + +} // namespace mir_onnx diff --git a/compiler/mir-onnx-importer/Op/Greater.h b/compiler/mir-onnx-importer/Op/Greater.h new file mode 100644 index 0000000..191d620 --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Greater.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 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_ONNX_OP_GREATER_H +#define MIR_ONNX_OP_GREATER_H + +#include "ONNXNodeConverterRegistry.h" + +namespace mir_onnx +{ + +class GreaterNodeConverter : public NodeConverter +{ +public: + void convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const override; +}; + +} // namespace mir_onnx + +#endif // MIR_ONNX_OP_GREATER_H diff --git a/compiler/mir-onnx-importer/Op/Less.cpp b/compiler/mir-onnx-importer/Op/Less.cpp new file mode 100644 index 0000000..2ca5aa4 --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Less.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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 "Less.h" + +#include "ONNXHelpers.h" +#include "AttributeHelpers.h" + +#include "mir/ops/LessOp.h" + +namespace mir_onnx +{ + +void LessNodeConverter::convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const +{ + std::vector inputs = context->getNodeInputs(onnx_node); + mir::Graph *graph = context->getGraph(); + + auto result = createOp(graph, inputs[0], inputs[1])->getOutput(0); + + context->setNodeOutputs(onnx_node, {result}); +} + +} // namespace mir_onnx diff --git a/compiler/mir-onnx-importer/Op/Less.h b/compiler/mir-onnx-importer/Op/Less.h new file mode 100644 index 0000000..17ec5d1 --- /dev/null +++ b/compiler/mir-onnx-importer/Op/Less.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 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_ONNX_OP_LESS_H +#define MIR_ONNX_OP_LESS_H + +#include "ONNXNodeConverterRegistry.h" + +namespace mir_onnx +{ + +class LessNodeConverter : public NodeConverter +{ +public: + void convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const override; +}; + +} // namespace mir_onnx + +#endif // MIR_ONNX_OP_LESS_H -- 2.7.4