ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Transpose.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 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
19#include "mir/ShapeRange.h"
20#include "mir/Tensor.h"
21
22#include "Common.h"
23
24namespace mir_interpreter
25{
26
27template <typename T> struct TransposeImpl
28{
29 static void run(const mir::TensorVariant &input, const mir::ops::TransposeOp &op,
30 mir::TensorVariant &output);
31};
32
33template <typename T>
35 mir::TensorVariant &outputv)
36{
37 const auto &output_shape = op.getOutputShape(0);
38 const auto &axis_order = op.getAxisOrder();
39 const int32_t num_axis = static_cast<int32_t>(axis_order.size());
40 assert(num_axis == inputv.getShape().rank());
41 assert(num_axis == output_shape.rank());
42
43 mir::Index output_index;
44 output_index.resize(num_axis);
45
46 mir::Tensor<T> input(inputv);
47 mir::Tensor<T> output(outputv);
48
49 for (auto &input_index : mir::ShapeRange(input.getShape()))
50 {
51 for (int32_t i = 0; i < num_axis; i++)
52 output_index.at(i) = input_index.at(axis_order[i]);
53
54 output.at(output_index) = input.at(input_index);
55 }
56}
57
59 mir::TensorVariant &output)
60{
61 dispatch<TransposeImpl>(input.getElementType(), input, op, output);
62}
63
64} // namespace mir_interpreter
Index & resize(int32_t size)
resize index to given dimension number
Definition Index.cpp:24
int32_t & at(int32_t axis)
return position on given axis
Definition Index.h:64
const Shape & getOutputShape(std::size_t index) const
Definition Operation.h:163
int32_t rank() const
Definition Shape.h:43
const Shape & getShape() const
Tensor transpose operation.
Definition TransposeOp.h:34
const std::vector< std::size_t > & getAxisOrder() const
Definition TransposeOp.h:38
const luci_interpreter::RuntimeShape output_shape
void Transpose(const mir::TensorVariant &input, const mir::ops::TransposeOp &op, mir::TensorVariant &output)
Definition Transpose.cpp:58
static void run(const mir::TensorVariant &input, const mir::ops::TransposeOp &op, mir::TensorVariant &output)
Definition Transpose.cpp:34