Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | c229b3f | 2023-06-27 22:34:54 +0100 | [diff] [blame] | 2 | // Copyright © 2018-2021,2023 Arm Ltd. All rights reserved. |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Matteo Martincigh | ee423ce | 2019-06-05 09:02:41 +0100 | [diff] [blame] | 5 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 6 | #pragma once |
Matteo Martincigh | ee423ce | 2019-06-05 09:02:41 +0100 | [diff] [blame] | 7 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 8 | #include <armnn/Types.hpp> |
Matteo Martincigh | ee423ce | 2019-06-05 09:02:41 +0100 | [diff] [blame] | 9 | #include <armnn/Tensor.hpp> |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 10 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 11 | #include <armnn/utility/Assert.hpp> |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 12 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 13 | namespace armnnUtils |
| 14 | { |
| 15 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 16 | /// Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 17 | class DataLayoutIndexed |
| 18 | { |
| 19 | public: |
| 20 | DataLayoutIndexed(armnn::DataLayout dataLayout); |
| 21 | |
| 22 | armnn::DataLayout GetDataLayout() const { return m_DataLayout; } |
| 23 | unsigned int GetChannelsIndex() const { return m_ChannelsIndex; } |
| 24 | unsigned int GetHeightIndex() const { return m_HeightIndex; } |
| 25 | unsigned int GetWidthIndex() const { return m_WidthIndex; } |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 26 | unsigned int GetDepthIndex() const { return m_DepthIndex; } |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 27 | |
| 28 | inline unsigned int GetIndex(const armnn::TensorShape& shape, |
| 29 | unsigned int batchIndex, unsigned int channelIndex, |
| 30 | unsigned int heightIndex, unsigned int widthIndex) const |
| 31 | { |
Ryan OShea | c229b3f | 2023-06-27 22:34:54 +0100 | [diff] [blame] | 32 | if (batchIndex >= shape[0] && !( shape[0] == 0 && batchIndex == 0)) |
| 33 | { |
| 34 | throw armnn::Exception("Unable to get batch index", CHECK_LOCATION()); |
| 35 | } |
| 36 | if (channelIndex >= shape[m_ChannelsIndex] && |
| 37 | !(shape[m_ChannelsIndex] == 0 && channelIndex == 0)) |
| 38 | { |
| 39 | throw armnn::Exception("Unable to get channel index", CHECK_LOCATION()); |
| 40 | |
| 41 | } |
| 42 | if (heightIndex >= shape[m_HeightIndex] && |
| 43 | !( shape[m_HeightIndex] == 0 && heightIndex == 0)) |
| 44 | { |
| 45 | throw armnn::Exception("Unable to get height index", CHECK_LOCATION()); |
| 46 | } |
| 47 | if (widthIndex >= shape[m_WidthIndex] && |
| 48 | ( shape[m_WidthIndex] == 0 && widthIndex == 0)) |
| 49 | { |
| 50 | throw armnn::Exception("Unable to get width index", CHECK_LOCATION()); |
| 51 | } |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 52 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 53 | /// Offset the given indices appropriately depending on the data layout |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 54 | switch (m_DataLayout) |
| 55 | { |
| 56 | case armnn::DataLayout::NHWC: |
| 57 | batchIndex *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex |
| 58 | heightIndex *= shape[m_WidthIndex] * shape[m_ChannelsIndex]; |
| 59 | widthIndex *= shape[m_ChannelsIndex]; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 60 | /// channelIndex stays unchanged |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 61 | break; |
| 62 | case armnn::DataLayout::NCHW: |
| 63 | default: |
| 64 | batchIndex *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex |
| 65 | channelIndex *= shape[m_HeightIndex] * shape[m_WidthIndex]; |
| 66 | heightIndex *= shape[m_WidthIndex]; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 67 | /// widthIndex stays unchanged |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 68 | break; |
| 69 | } |
| 70 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 71 | /// Get the value using the correct offset |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 72 | return batchIndex + channelIndex + heightIndex + widthIndex; |
| 73 | } |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | armnn::DataLayout m_DataLayout; |
| 77 | unsigned int m_ChannelsIndex; |
| 78 | unsigned int m_HeightIndex; |
| 79 | unsigned int m_WidthIndex; |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 80 | unsigned int m_DepthIndex; |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 83 | /// Equality methods |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 84 | bool operator==(const armnn::DataLayout& dataLayout, const DataLayoutIndexed& indexed); |
| 85 | bool operator==(const DataLayoutIndexed& indexed, const armnn::DataLayout& dataLayout); |
| 86 | |
| 87 | } // namespace armnnUtils |