Imported Upstream version 1.7.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
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 TransposeLayer::TransposeLayer() : _input(nullptr), _output(nullptr), _perm()
33 {
34   // DO NOTHING
35 }
36
37 template <typename T> void TransposeLayer::transpose()
38 {
39   nnfw::cker::TransposeParams param;
40   param.perm_count = _perm.size();
41   for (size_t i = 0; i < _perm.size(); i++)
42   {
43     param.perm[i] = _perm[i];
44   }
45
46   nnfw::cker::Transpose(param, getTensorShape(_input),
47                         reinterpret_cast<const T *>(_input->buffer()), getTensorShape(_output),
48                         reinterpret_cast<T *>(_output->buffer()));
49 }
50
51 void TransposeLayer::transposeQuant8()
52 {
53   if (_input->data_offset() != _output->data_offset())
54   {
55     throw std::runtime_error("TransposeLayer : qassym8 input and output offsets unmatched");
56   }
57
58   if (_input->data_scale() != _output->data_scale())
59   {
60     throw std::runtime_error("TransposeLayer : qassym8 input and output scales unmatched");
61   }
62
63   transpose<uint8_t>();
64 }
65
66 void TransposeLayer::configure(const IPortableTensor *input, IPortableTensor *output,
67                                const std::vector<int> &perm)
68 {
69   _input = input;
70   _perm = perm;
71   _output = output;
72 }
73
74 void TransposeLayer::run()
75 {
76   if (_input->data_type() == OperandType::FLOAT32)
77   {
78     transpose<float>();
79   }
80   else if (_input->data_type() == OperandType::INT32)
81   {
82     transpose<int32_t>();
83   }
84   else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
85   {
86     transposeQuant8();
87   }
88   else
89   {
90     throw std::runtime_error{"Transpose: unsupported data type"};
91   }
92 }
93
94 } // namespace ops
95 } // namespace cpu
96 } // namespace backend
97 } // namespace onert