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 | |
| 6 | #include <armnn/Tensor.hpp> |
| 7 | |
| 8 | #include <boost/assert.hpp> |
| 9 | |
| 10 | namespace armnn |
| 11 | { |
| 12 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | // 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] | 14 | template <typename DataType> |
| 15 | class TensorBufferArrayView |
| 16 | { |
| 17 | public: |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 18 | TensorBufferArrayView(const TensorShape& shape, DataType* data, DataLayoutIndexed dataLayout = DataLayout::NCHW) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | : m_Shape(shape) |
| 20 | , m_Data(data) |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 21 | , m_DataLayout(dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | { |
Matteo Martincigh | 97a06fd | 2018-10-16 16:17:34 +0100 | [diff] [blame^] | 23 | BOOST_ASSERT(m_Shape.GetNumDimensions() == 4); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const |
| 27 | { |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 28 | BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0] == 0 && b == 0 ) ); |
| 29 | BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] || |
| 30 | ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) ); |
| 31 | BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] || |
| 32 | ( m_Shape[m_DataLayout.GetHeightIndex()] == 0 && h == 0) ); |
| 33 | BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] || |
| 34 | ( m_Shape[m_DataLayout.GetWidthIndex()] == 0 && w == 0) ); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | |
Matteo Martincigh | 97a06fd | 2018-10-16 16:17:34 +0100 | [diff] [blame^] | 36 | // Offset the given indices appropriately depending on the data layout. |
| 37 | switch (m_DataLayout.GetDataLayout()) |
| 38 | { |
| 39 | case DataLayout::NHWC: |
| 40 | b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index; |
| 41 | h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()]; |
| 42 | w *= m_Shape[m_DataLayout.GetChannelsIndex()]; |
| 43 | // c stays unchanged |
| 44 | break; |
| 45 | case DataLayout::NCHW: |
| 46 | default: |
| 47 | b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index; |
| 48 | c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()]; |
| 49 | h *= m_Shape[m_DataLayout.GetWidthIndex()]; |
| 50 | // w stays unchanged |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | // Get the value using the correct offset. |
| 55 | return m_Data[b + c + h + w]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private: |
| 59 | const TensorShape m_Shape; |
James Conroy | 5954082 | 2018-10-11 12:39:05 +0100 | [diff] [blame] | 60 | DataType* m_Data; |
| 61 | DataLayoutIndexed m_DataLayout; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } //namespace armnn |