Imported Upstream version 1.11.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   assert(_perm->num_dimensions() == 1);
42
43   param.perm_count = _input->num_dimensions();
44   if (_perm->dimension(0) == 0) // This means _perm is (n-1...0)
45   {
46     const auto begin = param.perm;
47     const auto end = param.perm + _input->num_dimensions();
48     std::iota(begin, end, 0);
49     std::reverse(begin, end);
50   }
51   else
52   {
53     assert(param.perm_count == static_cast<int>(_perm->dimension(0)));
54     for (auto i = 0; i < param.perm_count; i++)
55     {
56       param.perm[i] = *(reinterpret_cast<const int32_t *>(_perm->buffer()) + i);
57     }
58   }
59
60   nnfw::cker::Transpose(param, getTensorShape(_input),
61                         reinterpret_cast<const T *>(_input->buffer()), getTensorShape(_output),
62                         reinterpret_cast<T *>(_output->buffer()));
63 }
64
65 void TransposeLayer::transposeQuant8()
66 {
67   if (_input->data_offset() != _output->data_offset())
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