[mir_onnx] ReduceMean operation support (#6798)
authorПавел Ильютченко/AI Tools Lab /SRR/Engineer/삼성전자 <p.iliutchenk@samsung.com>
Fri, 23 Aug 2019 11:48:27 +0000 (14:48 +0300)
committerAlexander Efimov/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Fri, 23 Aug 2019 11:48:27 +0000 (14:48 +0300)
* Added ReduceMeanNodeConverter
* Registration and cmake for converter

Signed-off-by: Pavel Iliutchenko <p.iliutchenk@samsung.com>
compiler/mir-onnx-importer/CMakeLists.txt
compiler/mir-onnx-importer/ONNXOpRegistration.h
compiler/mir-onnx-importer/Op/ReduceMean.cpp [new file with mode: 0644]
compiler/mir-onnx-importer/Op/ReduceMean.h [new file with mode: 0644]

index d7e3d05..cce8e00 100644 (file)
@@ -59,6 +59,8 @@ set(MIR_ONNX_IMPORTER_SOURCES
         Op/Mul.h
         Op/Pad.cpp
         Op/Pad.h
+        Op/ReduceMean.cpp
+        Op/ReduceMean.h
         Op/Relu.cpp
         Op/Relu.h
         Op/Reshape.cpp
index 6527b79..04da1b2 100644 (file)
@@ -35,6 +35,7 @@
 #include "Op/MaxPool.h"
 #include "Op/Mul.h"
 #include "Op/Pad.h"
+#include "Op/ReduceMean.h"
 #include "Op/Relu.h"
 #include "Op/Reshape.h"
 #include "Op/Shape.h"
@@ -69,6 +70,7 @@ inline void registerSupportedOps()
   registry.registerConverter("MaxPool", stdex::make_unique<MaxPoolNodeConverter>());
   registry.registerConverter("Mul", stdex::make_unique<MulNodeConverter>());
   registry.registerConverter("Pad", stdex::make_unique<PadNodeConverter>());
+  registry.registerConverter("ReduceMean", stdex::make_unique<ReduceMeanNodeConverter>());
   registry.registerConverter("Relu", stdex::make_unique<ReluNodeConverter>());
   registry.registerConverter("Reshape", stdex::make_unique<ReshapeNodeConverter>());
   registry.registerConverter("Shape", stdex::make_unique<ShapeNodeConverter>());
diff --git a/compiler/mir-onnx-importer/Op/ReduceMean.cpp b/compiler/mir-onnx-importer/Op/ReduceMean.cpp
new file mode 100644 (file)
index 0000000..28065ce
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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 "ReduceMean.h"
+
+#include "ONNXHelpers.h"
+#include "AttributeHelpers.h"
+
+#include "mir/ops/ReduceOp.h"
+
+#include <numeric>
+
+namespace mir_onnx
+{
+
+void ReduceMeanNodeConverter::convert(const onnx::NodeProto &onnx_node,
+                                      ConverterContext *context) const
+{
+  const auto opset_version = context->getOpsetVersion(onnx_node.domain());
+
+  if (opset_version >= 1)
+    convertV1(onnx_node, context);
+  else
+    throw std::runtime_error("Not supported opset version on ReduceMean operation!");
+}
+
+void ReduceMeanNodeConverter::convertV1(const onnx::NodeProto &onnx_node,
+                                        ConverterContext *context) const
+{
+  const auto inputs = context->getNodeInputs(onnx_node);
+  assert(inputs.size() == 1);
+
+  const auto axes = getAttributeValue<std::vector<std::int64_t>>(onnx_node, "axes");
+  const auto keepdims = getAttributeValue<int64_t>(onnx_node, "keepdims", 1);
+
+  std::vector<int32_t> reduce_dims;
+  if (axes.empty())
+  { // reduce over all dimensions
+    reduce_dims.resize(inputs[0]->getShape().rank());
+    std::iota(reduce_dims.begin(), reduce_dims.end(), 0);
+  }
+  else
+  {
+    reduce_dims.resize(axes.size());
+    std::copy(axes.begin(), axes.end(), reduce_dims.begin());
+  }
+  // Keep the reduced dimension or not, default 1 mean keep reduced dimension.
+  bool keep_dims = static_cast<bool>(keepdims);
+
+  mir::Graph *graph = context->getGraph();
+  auto result = createOp<mir::ops::ReduceOp>(graph, inputs[0], reduce_dims, keep_dims,
+                                             mir::ops::ReduceOp::FuncType::mean)
+                    ->getOutput(0);
+
+  context->setNodeOutputs(onnx_node, {result});
+}
+
+} // namespace mir_onnx
diff --git a/compiler/mir-onnx-importer/Op/ReduceMean.h b/compiler/mir-onnx-importer/Op/ReduceMean.h
new file mode 100644 (file)
index 0000000..f03903a
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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_REDUCEMEAN_H
+#define MIR_ONNX_OP_REDUCEMEAN_H
+
+#include "ONNXNodeConverterRegistry.h"
+
+namespace mir_onnx
+{
+
+class ReduceMeanNodeConverter : public NodeConverter
+{
+public:
+  void convert(const onnx::NodeProto &onnx_node, ConverterContext *context) const override;
+
+private:
+  void convertV1(const onnx::NodeProto &onnx_node, ConverterContext *context) const;
+};
+
+} // namespace mir_onnx
+
+#endif // MIR_ONNX_OP_REDUCEMEAN_H