Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 2 | // Copyright © 2019 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; } |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 26 | |
| 27 | inline unsigned int GetIndex(const armnn::TensorShape& shape, |
| 28 | unsigned int batchIndex, unsigned int channelIndex, |
| 29 | unsigned int heightIndex, unsigned int widthIndex) const |
| 30 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 31 | ARMNN_ASSERT( batchIndex < shape[0] || ( shape[0] == 0 && batchIndex == 0 ) ); |
| 32 | ARMNN_ASSERT( channelIndex < shape[m_ChannelsIndex] || |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 33 | ( shape[m_ChannelsIndex] == 0 && channelIndex == 0) ); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 34 | ARMNN_ASSERT( heightIndex < shape[m_HeightIndex] || |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 35 | ( shape[m_HeightIndex] == 0 && heightIndex == 0) ); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 36 | ARMNN_ASSERT( widthIndex < shape[m_WidthIndex] || |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 37 | ( shape[m_WidthIndex] == 0 && widthIndex == 0) ); |
| 38 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 39 | /// Offset the given indices appropriately depending on the data layout |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 40 | switch (m_DataLayout) |
| 41 | { |
| 42 | case armnn::DataLayout::NHWC: |
| 43 | batchIndex *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex |
| 44 | heightIndex *= shape[m_WidthIndex] * shape[m_ChannelsIndex]; |
| 45 | widthIndex *= shape[m_ChannelsIndex]; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 46 | /// channelIndex stays unchanged |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 47 | break; |
| 48 | case armnn::DataLayout::NCHW: |
| 49 | default: |
| 50 | batchIndex *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex |
| 51 | channelIndex *= shape[m_HeightIndex] * shape[m_WidthIndex]; |
| 52 | heightIndex *= shape[m_WidthIndex]; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 53 | /// widthIndex stays unchanged |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 54 | break; |
| 55 | } |
| 56 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 57 | /// Get the value using the correct offset |
Matteo Martincigh | f2aaab3 | 2019-06-06 15:46:22 +0100 | [diff] [blame] | 58 | return batchIndex + channelIndex + heightIndex + widthIndex; |
| 59 | } |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | armnn::DataLayout m_DataLayout; |
| 63 | unsigned int m_ChannelsIndex; |
| 64 | unsigned int m_HeightIndex; |
| 65 | unsigned int m_WidthIndex; |
| 66 | }; |
| 67 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 68 | /// Equality methods |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 69 | bool operator==(const armnn::DataLayout& dataLayout, const DataLayoutIndexed& indexed); |
| 70 | bool operator==(const DataLayoutIndexed& indexed, const armnn::DataLayout& dataLayout); |
| 71 | |
| 72 | } // namespace armnnUtils |