telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
Matteo Martincigh | ada9cb2 | 2018-10-17 14:45:07 +0100 | [diff] [blame] | 6 | #pragma once |
| 7 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #include <armnn/Tensor.hpp> |
| 9 | |
| 10 | #include <boost/assert.hpp> |
| 11 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 12 | #include <DataLayoutIndexed.hpp> |
| 13 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | // Utility class providing access to raw tensor memory based on indices along each dimension. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | template <typename DataType> |
| 19 | class TensorBufferArrayView |
| 20 | { |
| 21 | public: |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 22 | TensorBufferArrayView(const TensorShape& shape, DataType* data, |
| 23 | armnnUtils::DataLayoutIndexed dataLayout = DataLayout::NCHW) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | : m_Shape(shape) |
| 25 | , m_Data(data) |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 26 | , m_DataLayout(dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | { |
Matteo Martincigh | 97a06fd | 2018-10-16 16:17:34 +0100 | [diff] [blame] | 28 | BOOST_ASSERT(m_Shape.GetNumDimensions() == 4); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const |
| 32 | { |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 33 | BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) ); |
| 34 | BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] || |
| 35 | ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) ); |
| 36 | BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] || |
| 37 | ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) ); |
| 38 | BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] || |
| 39 | ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) ); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | |
Matteo Martincigh | 97a06fd | 2018-10-16 16:17:34 +0100 | [diff] [blame] | 41 | // Offset the given indices appropriately depending on the data layout. |
| 42 | switch (m_DataLayout.GetDataLayout()) |
| 43 | { |
| 44 | case DataLayout::NHWC: |
| 45 | b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index; |
| 46 | h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()]; |
| 47 | w *= m_Shape[m_DataLayout.GetChannelsIndex()]; |
| 48 | // c stays unchanged |
| 49 | break; |
| 50 | case DataLayout::NCHW: |
| 51 | default: |
| 52 | b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index; |
| 53 | c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()]; |
| 54 | h *= m_Shape[m_DataLayout.GetWidthIndex()]; |
| 55 | // w stays unchanged |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | // Get the value using the correct offset. |
| 60 | return m_Data[b + c + h + w]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 64 | const TensorShape m_Shape; |
| 65 | DataType* m_Data; |
| 66 | armnnUtils::DataLayoutIndexed m_DataLayout; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } //namespace armnn |