Imported Upstream version 1.15.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / TransposeLayer.cc
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 "TransposeLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Transpose.h>
22 #include <numeric>
23
24 namespace onert
25 {
26 namespace backend
27 {
28 namespace cpu
29 {
30 namespace ops
31 {
32
33 TransposeLayer::TransposeLayer() : _input(nullptr), _perm(nullptr), _output(nullptr)
34 {
35   // DO NOTHING
36 }
37
38 template <typename T> void TransposeLayer::transpose()
39 {
40   nnfw::cker::TransposeParams param;
41   auto perm_shape = _perm->getShape();
42   assert(perm_shape.rank() == 1);
43
44   param.perm_count = _input->getShape().rank();
45   if (perm_shape.dim(0) == 0) // This means _perm is (n-1...0)
46   {
47     const auto begin = param.perm;
48     const auto end = param.perm + _input->getShape().rank();
49     std::iota(begin, end, 0);
50     std::reverse(begin, end);
51   }
52   else
53   {
54     assert(param.perm_count == static_cast<int>(perm_shape.dim(0)));
55     for (auto i = 0; i < param.perm_count; i++)
56     {
57       param.perm[i] = *(getBuffer<int32_t>(_perm) + i);
58     }
59   }
60
61   nnfw::cker::Transpose(param, getShape(_input), getBuffer<T>(_input), getShape(_output),
62                         getBuffer<T>(_output));
63 }
64
65 void TransposeLayer::transposeQuant8()
66 {
67   if (_input->data_zero_point() != _output->data_zero_point())
68   {
69     throw std::runtime_error("TransposeLayer : qassym8 input and output offsets unmatched");
70   }
71
72   if (_input->data_scale() != _output->data_scale())
73   {
74     throw std::runtime_error("TransposeLayer : qassym8 input and output scales unmatched");
75   }
76
77   transpose<uint8_t>();
78 }
79
80 void TransposeLayer::configure(const IPortableTensor *input, const IPortableTensor *perm,
81                                IPortableTensor *output)
82 {
83   _input = input;
84   _perm = perm;
85   _output = output;
86 }
87
88 void TransposeLayer::run()
89 {
90   if (_input->data_type() == OperandType::FLOAT32)
91   {
92     transpose<float>();
93   }
94   else if (_input->data_type() == OperandType::INT32)
95   {
96     transpose<int32_t>();
97   }
98   else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
99   {
100     transposeQuant8();
101   }
102   else
103   {
104     throw std::runtime_error{"Transpose: unsupported data type"};
105   }
106 }
107
108 } // namespace ops
109 } // namespace cpu
110 } // namespace backend
111 } // namespace onert