[mir_onnx] Add support for Transpose operator (#6764)
authorСергей Баранников/AI Tools Lab /SRR/Engineer/삼성전자 <s.barannikov@samsung.com>
Wed, 21 Aug 2019 13:51:22 +0000 (22:51 +0900)
committerAlexander Efimov/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Wed, 21 Aug 2019 13:51:22 +0000 (16:51 +0300)
Add support for `Transpose` operator.

Signed-off-by: Sergei Barannikov <s.barannikov@samsung.com>
compiler/mir-onnx-importer/CMakeLists.txt
compiler/mir-onnx-importer/ONNXOpRegistration.h
compiler/mir-onnx-importer/Op/Transpose.cpp [new file with mode: 0644]
compiler/mir-onnx-importer/Op/Transpose.h [new file with mode: 0644]

index 9747cce..ec7694c 100644 (file)
@@ -69,6 +69,8 @@ set(MIR_ONNX_IMPORTER_SOURCES
         Op/Softmax.h
         Op/Sum.cpp
         Op/Sum.h
+        Op/Transpose.cpp
+        Op/Transpose.h
         Op/Unsqueeze.cpp
         Op/Unsqueeze.h
         Op/Upsample.cpp
index eaa1d58..51e8635 100644 (file)
@@ -40,6 +40,7 @@
 #include "Op/Sigmoid.h"
 #include "Op/Softmax.h"
 #include "Op/Sum.h"
+#include "Op/Transpose.h"
 #include "Op/Unsqueeze.h"
 #include "Op/Upsample.h"
 
@@ -72,6 +73,7 @@ inline void registerSupportedOps()
   registry.registerConverter("Sigmoid", stdex::make_unique<SigmoidNodeConverter>());
   registry.registerConverter("Softmax", stdex::make_unique<SoftmaxNodeConverter>());
   registry.registerConverter("Sum", stdex::make_unique<SumNodeConverter>());
+  registry.registerConverter("Transpose", stdex::make_unique<TransposeNodeConverter>());
   registry.registerConverter("Unsqueeze", stdex::make_unique<UnsqueezeNodeConverter>());
   registry.registerConverter("Upsample", stdex::make_unique<UpsampleNodeConverter>());
 }
diff --git a/compiler/mir-onnx-importer/Op/Transpose.cpp b/compiler/mir-onnx-importer/Op/Transpose.cpp
new file mode 100644 (file)
index 0000000..74987da
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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 "Transpose.h"
+#include "ONNXHelpers.h"
+#include "AttributeHelpers.h"
+
+#include "mir/ops/TransposeOp.h"
+
+#include <numeric>
+
+namespace mir_onnx
+{
+
+void TransposeNodeConverter::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 Transpose operation!");
+}
+
+void TransposeNodeConverter::convertV1(const onnx::NodeProto &onnx_node,
+                                       ConverterContext *context) const
+{
+  const auto inputs = context->getNodeInputs(onnx_node);
+  mir::Graph *graph = context->getGraph();
+
+  assert(inputs.size() == 1);
+  auto input = inputs[0];
+
+  const auto num_axes = input->getShape().rank();
+  std::vector<std::size_t> axis_order(num_axes);
+  const auto *perm_attr = findAttribute(onnx_node, "perm");
+
+  if (perm_attr == nullptr)
+  {
+    // Reverse the dimensions.
+    std::iota(axis_order.rbegin(), axis_order.rend(), 0);
+  }
+  else
+  {
+    const auto perm = getAttributeValue<std::vector<std::int64_t>>(*perm_attr);
+    assert(perm.size() == num_axes);
+    std::copy(perm.cbegin(), perm.cend(), axis_order.begin());
+  }
+
+  auto result = createOp<mir::ops::TransposeOp>(graph, input, axis_order)->getOutput(0);
+
+  context->setNodeOutputs(onnx_node, {result});
+}
+
+} // namespace mir_onnx
diff --git a/compiler/mir-onnx-importer/Op/Transpose.h b/compiler/mir-onnx-importer/Op/Transpose.h
new file mode 100644 (file)
index 0000000..7c9eb52
--- /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_TRANSPOSE_H
+#define MIR_ONNX_OP_TRANSPOSE_H
+
+#include "ONNXNodeConverterRegistry.h"
+
+namespace mir_onnx
+{
+
+class TransposeNodeConverter : 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_TRANSPOSE_H