aboutsummaryrefslogtreecommitdiff
path: root/src/armnnUtils/DataLayoutIndexed.cpp
blob: b02f07ec855189877b19ece23e5d4dba35023830 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "DataLayoutIndexed.hpp"

#include <boost/assert.hpp>

using namespace armnn;

namespace armnnUtils
{

DataLayoutIndexed::DataLayoutIndexed(armnn::DataLayout dataLayout)
    : m_DataLayout(dataLayout)
{
    switch (dataLayout)
    {
        case armnn::DataLayout::NHWC:
            m_ChannelsIndex = 3;
            m_HeightIndex   = 1;
            m_WidthIndex    = 2;
            break;
        case armnn::DataLayout::NCHW:
            m_ChannelsIndex = 1;
            m_HeightIndex   = 2;
            m_WidthIndex    = 3;
            break;
        default:
            throw armnn::InvalidArgumentException("Unknown DataLayout value: " +
                                                  std::to_string(static_cast<int>(dataLayout)));
    }
}

unsigned int DataLayoutIndexed::GetIndex(const TensorShape& shape,
                                         unsigned int batchIndex, unsigned int channelIndex,
                                         unsigned int heightIndex, unsigned int widthIndex) const
{
    BOOST_ASSERT( batchIndex < shape[0] || ( shape[0] == 0 && batchIndex == 0 ) );
    BOOST_ASSERT( channelIndex < shape[m_ChannelsIndex] ||
                ( shape[m_ChannelsIndex] == 0 && channelIndex == 0) );
    BOOST_ASSERT( heightIndex < shape[m_HeightIndex] ||
                ( shape[m_HeightIndex] == 0 && heightIndex == 0) );
    BOOST_ASSERT( widthIndex < shape[m_WidthIndex] ||
                ( shape[m_WidthIndex] == 0 && widthIndex == 0) );

    // Offset the given indices appropriately depending on the data layout
    switch (m_DataLayout)
    {
    case DataLayout::NHWC:
        batchIndex  *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex
        heightIndex *= shape[m_WidthIndex] * shape[m_ChannelsIndex];
        widthIndex  *= shape[m_ChannelsIndex];
        // channelIndex stays unchanged
        break;
    case DataLayout::NCHW:
    default:
        batchIndex   *= shape[1] * shape[2] * shape[3]; // batchIndex *= heightIndex * widthIndex * channelIndex
        channelIndex *= shape[m_HeightIndex] * shape[m_WidthIndex];
        heightIndex  *= shape[m_WidthIndex];
        // widthIndex stays unchanged
        break;
    }

    // Get the value using the correct offset
    return batchIndex + channelIndex + heightIndex + widthIndex;
}

bool operator==(const DataLayout& dataLayout, const DataLayoutIndexed& indexed)
{
    return dataLayout == indexed.GetDataLayout();
}

bool operator==(const DataLayoutIndexed& indexed, const DataLayout& dataLayout)
{
    return indexed.GetDataLayout() == dataLayout;
}

} // namespace armnnUtils