aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/TensorBufferArrayView.hpp
blob: b149073ab76f02f5503ac27ebe812e6668a44c59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <armnn/Tensor.hpp>

#include <boost/assert.hpp>

namespace armnn
{

// Utility class providing access to raw tensor memory based on indices along each dimension.
template <typename DataType>
class TensorBufferArrayView
{
public:
    TensorBufferArrayView(const TensorShape& shape, DataType* data, DataLayoutIndexed dataLayout = DataLayout::NCHW)
        : m_Shape(shape)
        , m_Data(data)
        , m_DataLayout(dataLayout)
    {
        BOOST_ASSERT(m_Shape.GetNumDimensions() == 4);
    }

    DataType& Get(unsigned int b, unsigned int c, unsigned int h, unsigned int w) const
    {
        BOOST_ASSERT( b < m_Shape[0] || ( m_Shape[0]   == 0 && b == 0 ) );
        BOOST_ASSERT( c < m_Shape[m_DataLayout.GetChannelsIndex()] ||
            ( m_Shape[m_DataLayout.GetChannelsIndex()] == 0 && c == 0) );
        BOOST_ASSERT( h < m_Shape[m_DataLayout.GetHeightIndex()] ||
            ( m_Shape[m_DataLayout.GetHeightIndex()]   == 0 && h == 0) );
        BOOST_ASSERT( w < m_Shape[m_DataLayout.GetWidthIndex()] ||
            ( m_Shape[m_DataLayout.GetWidthIndex()]    == 0 && w == 0) );

        // Offset the given indices appropriately depending on the data layout.
        switch (m_DataLayout.GetDataLayout())
        {
        case DataLayout::NHWC:
            b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
            h *= m_Shape[m_DataLayout.GetWidthIndex()] * m_Shape[m_DataLayout.GetChannelsIndex()];
            w *= m_Shape[m_DataLayout.GetChannelsIndex()];
            // c stays unchanged
            break;
        case DataLayout::NCHW:
        default:
            b *= m_Shape[1] * m_Shape[2] * m_Shape[3]; // b *= height_index * width_index * channel_index;
            c *= m_Shape[m_DataLayout.GetHeightIndex()] * m_Shape[m_DataLayout.GetWidthIndex()];
            h *= m_Shape[m_DataLayout.GetWidthIndex()];
            // w stays unchanged
            break;
        }

        // Get the value using the correct offset.
        return m_Data[b + c + h + w];
    }

private:
    const TensorShape m_Shape;
    DataType*         m_Data;
    DataLayoutIndexed m_DataLayout;
};

} //namespace armnn