aboutsummaryrefslogtreecommitdiff
path: root/src/backends/neon/NeonBackendOptimizationUtils.hpp
blob: 3a8bf465994ada17f188a872409731ea1eb712e7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include <aclCommon/ArmComputeSubgraphUtils.hpp>

namespace armnn
{

// Changes shapes of the form [1, 1, ..., W] to [ W ]
inline bool CollapseLeadingUnitDimensions(const TensorInfo& in, TensorInfo& out)
{
    unsigned int numDimensions = in.GetNumDimensions();
    for (unsigned int i = 0; i < (numDimensions-1); ++i)
    {
        if (in.GetShape()[i] != 1)
        {
            return false;
        }
    }

    unsigned int w = in.GetShape()[numDimensions-1];
    out = in;
    out.SetShape({w});

    return true;
}

//
// Build slot and tensor info lists for Add/Mul/Add replacement
//
template<typename SlotListType>
void BuildAddMulAddSlotLists(bool handleReLu,
                             bool multipleOutputs,
                             std::vector<SlotListType>& inputLayersSlotLists,
                             std::vector<SlotListType>& outputLayersSlotLists)
{
    // Build input slot list
    inputLayersSlotLists.push_back({0, 1});     // Add
    inputLayersSlotLists.push_back({1});        // Mul
    inputLayersSlotLists.push_back({1});        // Add
    if (handleReLu)
    {
        inputLayersSlotLists.push_back({});     // Relu
    }

    // Build output slot list
    if (multipleOutputs)
    {
        outputLayersSlotLists.push_back({0});   // Add
    }
    else
    {
        outputLayersSlotLists.push_back({});    // Add
    }
    outputLayersSlotLists.push_back({});        // Mul
    if (handleReLu)
    {
        outputLayersSlotLists.push_back({});    // Add
        outputLayersSlotLists.push_back({0});   // Relu
    }
    else
    {
        outputLayersSlotLists.push_back({0});   // Add
    }
}

inline void GetFusedName(Layer *layerList[4], std::string& fusedName)
{
    // Build the fused name string
    fusedName = "fused";
    for (unsigned int layerIdx = 0; layerIdx< 4; ++layerIdx)
    {
        if (! layerList[layerIdx])
        {
            break;
        }
        fusedName += "-";
        fusedName += layerList[layerIdx]->GetNameStr();
    }
}

template<typename Type>
bool BuildAddMulAddTensorInfoLists(Type* layerList[4],
                                   unsigned int& numInputs,
                                   unsigned int& numOutputs,
                                   std::vector<TensorInfo>& inputInfos,
                                   std::vector<TensorInfo>& outputInfos,
                                   const ActivationDescriptor*& activationDescriptor,
                                   bool& fuseReLu)
{
    ARMNN_THROW_INVALIDARG_IF_FALSE(layerList[0]);
    ARMNN_THROW_INVALIDARG_IF_FALSE(layerList[1]);
    ARMNN_THROW_INVALIDARG_IF_FALSE(layerList[2]);

    ARMNN_THROW_INVALIDARG_IF_FALSE(IsSequenceLayerType(*layerList[0], BinaryOperation::Add));
    ARMNN_THROW_INVALIDARG_IF_FALSE(IsSequenceLayerType(*layerList[1], BinaryOperation::Mul));
    ARMNN_THROW_INVALIDARG_IF_FALSE(IsSequenceLayerType(*layerList[2], BinaryOperation::Add));

    fuseReLu = (layerList[3] != nullptr);
    if (fuseReLu)
    {
        activationDescriptor = &PolymorphicDowncast<ActivationLayer *>(layerList[3])->GetParameters();
        ARMNN_THROW_INVALIDARG_IF_FALSE((activationDescriptor->m_Function == ActivationFunction::ReLu) ||
                     (activationDescriptor->m_Function == ActivationFunction::BoundedReLu));
    }

    numInputs = 0;
    numOutputs = 0;

    // Ensure that there are 6 input slots in the add/mul/add layers
    // we are going to replace
    unsigned int layerIdx = 0;
    unsigned int inputSlotCount = 0;
    for (layerIdx = 0; layerIdx < 3; ++layerIdx)
    {
        for (unsigned int slotIdx = 0; slotIdx < layerList[layerIdx]->GetNumInputSlots(); ++slotIdx)
        {
            InputSlot* inputSlot = &layerList[layerIdx]->GetInputSlot(slotIdx);
            OutputSlot* outputSlot = inputSlot->GetConnectedOutputSlot();
            if (outputSlot)
            {
                if (layerIdx == 0)
                {
                    // Always count the input connections of the first add
                    inputInfos.push_back(inputSlot->GetTensorInfo());
                    numInputs++;
                }
                else
                {
                    // For subsequent layers, we skip connections to the previous layers in the counting
                    if (&outputSlot->GetOwningLayer() != layerList[layerIdx-1])
                    {
                        TensorInfo inputSlotInfo = inputSlot->GetTensorInfo();
                        if (numInputs == 2 || numInputs == 3)
                        {
                            // Workaround the broadcast optimization to collapse shapes such as
                            // [1, 1, 1, 2] to [2] as required by backend
                            if (CollapseLeadingUnitDimensions(inputSlot->GetTensorInfo(), inputSlotInfo))
                            {
                                OutputSlot* previousLayerSlot = inputSlot->GetConnectedOutputSlot();
                                if (previousLayerSlot)
                                {
                                    if (previousLayerSlot->GetOwningLayer().GetType() == LayerType::Constant)
                                    {
                                        // First update the TensorInfo in the constant owning layer
                                        previousLayerSlot->SetTensorInfo(inputSlotInfo);
                                        // Then update the TensorInfo in the workload for the owning layer
                                        ConstantLayer* layer = PolymorphicDowncast<ConstantLayer*>(
                                                &previousLayerSlot->GetOwningLayer());
                                        layer->m_LayerOutput
                                                = std::make_unique<ScopedTensorHandle>(
                                                ConstTensor(inputSlotInfo,
                                                            layer->m_LayerOutput.get()->GetConstTensor<void>()));
                                    }
                                }
                            }
                        }
                        inputInfos.push_back(inputSlotInfo);
                        numInputs++;
                    }
                }
                inputSlotCount++;
            }
        }
    }

    // Check the input counts
    bool validInputCount = (inputSlotCount == 6) && (inputInfos.size() == 4);
    if (! validInputCount)
    {
        return false;
    }

    const unsigned int maxIdx = (fuseReLu) ? 4 : 3;
    for (layerIdx = 0; layerIdx < maxIdx; ++layerIdx)
    {
        for (unsigned int slotIdx = 0; slotIdx < layerList[layerIdx]->GetNumOutputSlots(); ++slotIdx)
        {
            OutputSlot* outputSlot = &layerList[layerIdx]->GetOutputSlot(slotIdx);

            for (unsigned int connectionIdx = 0; connectionIdx < outputSlot->GetNumConnections(); ++connectionIdx)
            {
                InputSlot* inputSlot = outputSlot->GetConnection(connectionIdx);
                if (layerIdx < (maxIdx-1))
                {
                    if (&inputSlot->GetOwningLayer() != layerList[layerIdx+1])
                    {
                        outputInfos.push_back(outputSlot->GetTensorInfo());
                        numOutputs++;
                    }
                }
                else if (layerList[layerIdx] != nullptr)
                {
                    outputInfos.push_back(outputSlot->GetTensorInfo());
                    numOutputs++;
                }
            }
        }
    }

    // Check the output count
    bool validOutputCount = (outputInfos.size() > 0);
    if (! validOutputCount)
    {
        return false;
    }

    return true;
}

}