Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <armnn/Tensor.hpp> |
| 9 | #include <armnn/Types.hpp> |
| 10 | |
| 11 | #include <armnnUtils/Permute.hpp> |
| 12 | |
| 13 | template<typename T> |
| 14 | void PermuteTensorNchwToNhwc(armnn::TensorInfo& tensorInfo, std::vector<T>& tensorData) |
| 15 | { |
| 16 | const armnn::PermutationVector nchwToNhwc = { 0, 3, 1, 2 }; |
| 17 | |
| 18 | tensorInfo = armnnUtils::Permuted(tensorInfo, nchwToNhwc); |
| 19 | |
| 20 | std::vector<T> tmp(tensorData.size()); |
| 21 | armnnUtils::Permute(tensorInfo.GetShape(), nchwToNhwc, tensorData.data(), tmp.data(), sizeof(T)); |
| 22 | tensorData = tmp; |
| 23 | } |
| 24 | |
| 25 | template<typename T> |
| 26 | void PermuteTensorNhwcToNchw(armnn::TensorInfo& tensorInfo, std::vector<T>& tensorData) |
| 27 | { |
| 28 | const armnn::PermutationVector nhwcToNchw = { 0, 2, 3, 1 }; |
| 29 | |
| 30 | tensorInfo = armnnUtils::Permuted(tensorInfo, nhwcToNchw); |
| 31 | |
| 32 | std::vector<T> tmp(tensorData.size()); |
| 33 | armnnUtils::Permute(tensorInfo.GetShape(), nhwcToNchw, tensorData.data(), tmp.data(), sizeof(T)); |
| 34 | |
| 35 | tensorData = tmp; |
| 36 | } |
| 37 | |
| 38 | template<typename T> |
| 39 | void PermuteTensorNdhwcToNcdhw(armnn::TensorInfo& tensorInfo, std::vector<T>& tensorData) |
| 40 | { |
| 41 | const armnn::PermutationVector ndhwcToNcdhw = { 0, 2, 3, 4, 1 }; |
| 42 | |
| 43 | tensorInfo = armnnUtils::Permuted(tensorInfo, ndhwcToNcdhw); |
| 44 | |
| 45 | std::vector<T> tmp(tensorData.size()); |
| 46 | armnnUtils::Permute(tensorInfo.GetShape(), ndhwcToNcdhw, tensorData.data(), tmp.data(), sizeof(T)); |
| 47 | tensorData = tmp; |
| 48 | } |
| 49 | |
| 50 | template<typename T> |
| 51 | void PermuteTensorNcdhwToNdhwc(armnn::TensorInfo& tensorInfo, std::vector<T>& tensorData) |
| 52 | { |
| 53 | const armnn::PermutationVector ncdhwToNdhwc = { 0, 4, 1, 2, 3 }; |
| 54 | |
| 55 | tensorInfo = armnnUtils::Permuted(tensorInfo, ncdhwToNdhwc); |
| 56 | |
| 57 | std::vector<T> tmp(tensorData.size()); |
| 58 | armnnUtils::Permute(tensorInfo.GetShape(), ncdhwToNdhwc, tensorData.data(), tmp.data(), sizeof(T)); |
| 59 | tensorData = tmp; |
| 60 | } |