aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/Merger.hpp
blob: 76d807cb2c2e09ed44193eec49e1553ddc5b359b (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
81
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include "RefWorkloadUtils.hpp"

#include <backendsCommon/WorkloadData.hpp>
#include <armnn/Tensor.hpp>

namespace armnn
{

template <typename DataType>
void Merger(const MergerQueueDescriptor& data)
{
    const TensorInfo& outputInfo0 = GetTensorInfo(data.m_Outputs[0]);

    for (unsigned int index = 0 ; index < outputInfo0.GetNumElements(); ++index)
    {
        unsigned int indices[MaxNumOfTensorDimensions] = { 0 };

        unsigned int indexRemainder = index;
        unsigned int dimensionStride = outputInfo0.GetNumElements();

        for (unsigned int i=0; i<outputInfo0.GetNumDimensions(); i++)
        {
            dimensionStride /= outputInfo0.GetShape()[i];
            indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
            indexRemainder -= indices[i] * dimensionStride;
        }

        for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
        {
            MergerQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];

            //Split view extents are defined by the size of (the corresponding) input tensor.
            const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[viewIdx]);
            BOOST_ASSERT(inputInfo.GetNumDimensions() == outputInfo0.GetNumDimensions());

            // Check all dimensions to see if this element is inside the given input view.
            bool insideView = true;
            for (unsigned int i=0; i<inputInfo.GetNumDimensions(); i++)
            {
                if (indices[i] < view.m_Origin[i])
                {
                    insideView = false;
                }
                if (indices[i] >= view.m_Origin[i] + inputInfo.GetShape()[i])
                {
                    insideView = false;
                }
            }

            if (insideView)
            {
                unsigned int inIndex = 0;
                unsigned int dimensionStride = 1;

                for (unsigned int i = inputInfo.GetNumDimensions(); i-- > 0;)
                {
                    inIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
                    dimensionStride *= inputInfo.GetShape()[i];
                }

                //We are within the view, copy input data to the output corresponding to this view.
                (GetOutputTensorData<DataType>(0, data))[index] =
                    (GetInputTensorData<DataType>(viewIdx, data))[inIndex];

                //What should we do if input views overlap on the output tensor?
                //We could error, take the average, or shm else...
                //For now just stop after finding first view (input) that matches.
                break;
            }
        }
    }
}

} //namespace armnn