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 auto perm_shape = _perm->getShape();
42 assert(perm_shape.rank() == 1);
44 param.perm_count = _input->getShape().rank();
45 if (perm_shape.dim(0) == 0) // This means _perm is (n-1...0)
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);
54 assert(param.perm_count == static_cast<int>(perm_shape.dim(0)));
55 for (auto i = 0; i < param.perm_count; i++)
57 param.perm[i] = *(getBuffer<int32_t>(_perm) + i);
61 nnfw::cker::Transpose(param, getShape(_input), getBuffer<T>(_input), getShape(_output),
62 getBuffer<T>(_output));
65 void TransposeLayer::transposeQuant8()
67 if (_input->data_zero_point() != _output->data_zero_point())
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