2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "TransposeLayer.h"
19 #include "OperationUtils.h"
21 #include <cker/operation/Transpose.h>
33 TransposeLayer::TransposeLayer() : _input(nullptr), _perm(nullptr), _output(nullptr)
38 template <typename T> void TransposeLayer::transpose()
40 nnfw::cker::TransposeParams param;
41 assert(_perm->num_dimensions() == 1);
43 param.perm_count = _input->num_dimensions();
44 if (_perm->dimension(0) == 0) // This means _perm is (n-1...0)
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);
53 assert(param.perm_count == static_cast<int>(_perm->dimension(0)));
54 for (auto i = 0; i < param.perm_count; i++)
56 param.perm[i] = *(reinterpret_cast<const int32_t *>(_perm->buffer()) + i);
60 nnfw::cker::Transpose(param, getTensorShape(_input),
61 reinterpret_cast<const T *>(_input->buffer()), getTensorShape(_output),
62 reinterpret_cast<T *>(_output->buffer()));
65 void TransposeLayer::transposeQuant8()
67 if (_input->data_offset() != _output->data_offset())
69 throw std::runtime_error("TransposeLayer : qassym8 input and output offsets unmatched");
72 if (_input->data_scale() != _output->data_scale())
74 throw std::runtime_error("TransposeLayer : qassym8 input and output scales unmatched");
80 void TransposeLayer::configure(const IPortableTensor *input, const IPortableTensor *perm,
81 IPortableTensor *output)
88 void TransposeLayer::run()
90 if (_input->data_type() == OperandType::FLOAT32)
94 else if (_input->data_type() == OperandType::INT32)
98 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
104 throw std::runtime_error{"Transpose: unsupported data type"};
110 } // namespace backend