ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Transpose.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Transpose.h"
18#include "ONNXHelpers.h"
19#include "AttributeHelpers.h"
20
21#include "mir/ops/TransposeOp.h"
22
23#include <numeric>
24
25namespace mir_onnx
26{
27
28void convertTransposeV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
29{
30 const auto inputs = context->getNodeInputs(onnx_node);
31 mir::Graph *graph = context->getGraph();
32
33 assert(inputs.size() == 1);
34 auto input = inputs[0];
35
36 const int num_axes = input->getShape().rank();
37 std::vector<std::size_t> axis_order(num_axes);
38 const auto *perm_attr = findAttribute(onnx_node, "perm");
39
40 if (perm_attr == nullptr)
41 {
42 // Reverse the dimensions.
43 std::iota(axis_order.rbegin(), axis_order.rend(), 0);
44 }
45 else
46 {
47 const auto perm = getAttributeValue<std::vector<std::int64_t>>(*perm_attr);
48 assert(static_cast<int>(perm.size()) == num_axes);
49 std::copy(perm.cbegin(), perm.cend(), axis_order.begin());
50 }
51
52 auto result = createOp<mir::ops::TransposeOp>(graph, input, axis_order)->getOutput(0);
53
54 context->setNodeOutputs(onnx_node, {result});
55}
56
57} // namespace mir_onnx
void setNodeOutputs(const onnx::NodeProto &onnx_node, const std::vector< mir::Operation::Output * > &outputs)
std::vector< mir::Operation::Output * > getNodeInputs(const onnx::NodeProto &onnx_node) const
void convertTransposeV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
Definition Transpose.cpp:28
const onnx::AttributeProto * findAttribute(const onnx::NodeProto &node, const std::string &name)