aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/StaticRangeStrategy.cpp
blob: 84b8d24068d8c1160628505954fd56fbf673b2aa (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
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "StaticRangeStrategy.hpp"

#include <armnn/utility/IgnoreUnused.hpp>
#include <armnn/Descriptors.hpp>
#include <armnn/Types.hpp>

#include <limits>

namespace armnn
{

StaticRangeStrategy::StaticRangeStrategy(RangeTracker& rangeTracker)
    : m_RangeTracker(rangeTracker)
{}

void StaticRangeStrategy::SetRange(const IConnectableLayer* layer, unsigned int outputIdx, float min, float max)
{
    m_RangeTracker.SetRange(layer, outputIdx, min, max);
}

void StaticRangeStrategy::ForwardParentParameters(const IConnectableLayer* layer)
{
    const auto parentRange = m_RangeTracker.GetRange(layer->GetInputSlot(0).GetConnection()->GetOwningLayerGuid(), 0);
    SetRange(layer, 0, parentRange.first, parentRange.second);
}


void StaticRangeStrategy::ExecuteStrategy(const armnn::IConnectableLayer *layer,
                                          const BaseDescriptor &descriptor,
                                          const std::vector<armnn::ConstTensor> &constants,
                                          const char *name,
                                          const armnn::LayerBindingId id)
{
IgnoreUnused(id, name);

switch (layer->GetType())
{
    case armnn::LayerType::Activation :
    {
        const ActivationDescriptor& activationDescriptor = static_cast<const ActivationDescriptor&>(descriptor);

        switch (activationDescriptor.m_Function)
        {
            // Range is 0, 15 for Abs, Linear, ReLu and Soft ReLu
            case ActivationFunction::Abs:
            case ActivationFunction::Linear:
            case ActivationFunction::ReLu:
            case ActivationFunction::SoftReLu:
                SetRange(layer, 0, 0.f, 15.f);
                break;
            case ActivationFunction::BoundedReLu:
                SetRange(layer, 0, 0.f, activationDescriptor.m_A);
                break;
            case ActivationFunction::TanH:
                SetRange(layer, 0, -1.f, 1.f);
                break;
            case ActivationFunction::LeakyReLu:
                SetRange(layer, 0, -5.f, 15.f);
                break;
            default:
                SetRange(layer, 0, -15.f, 15.f);
                break;
        }
        break;
    }
    case armnn::LayerType::Addition :
    {
        SetRange(layer, 0, -20.f, 20.f);
        break;
    }
    case armnn::LayerType::ArgMinMax :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::BatchToSpaceNd :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::BatchNormalization :
    {
        SetRange(layer, 0, -15.0f, 15.0f);
        break;
    }
    case armnn::LayerType::Concat :
    {
        float min = std::numeric_limits<float>::max();
        float max = std::numeric_limits<float>::lowest();
        for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
        {
            const IOutputSlot* outputSlot = layer->GetInputSlot(i).GetConnection();
            LayerGuid layerId = outputSlot->GetOwningLayerGuid();
            unsigned int slotIndex = outputSlot->CalculateIndexOnOwner();
            RangeTracker::MinMaxRange range = m_RangeTracker.GetRange(layerId, slotIndex);
            min = std::min(min, range.first);
            max = std::max(max, range.second);
        }
        SetRange(layer, 0, min, max);
        break;
    }
    case armnn::LayerType::Constant :
    {

        if (constants[0].GetDataType() != DataType::Float32)
        {
            throw InvalidArgumentException("Quantization is supported only for FP32 tensors");
        }

        // Work out the range based on the input constants
        unsigned int inputNumElements = constants[0].GetNumElements();
        const float* inputData = reinterpret_cast<const float*>(constants[0].GetMemoryArea());

        float min = std::numeric_limits<float>::max();
        float max = std::numeric_limits<float>::lowest();

        for (unsigned int i = 0; i < inputNumElements; i++)
        {
            const float inputValue = inputData[i];

            min = std::min(min, inputValue);
            max = std::max(max, inputValue);
        }
        SetRange(layer, 0, min, max);
        break;
    }
    case armnn::LayerType::Convolution2d :
    {
        SetRange(layer, 0, -15.0f, 15.0f);
        break;
    }
    case armnn::LayerType::DepthwiseConvolution2d :
    {
        SetRange(layer, 0, -15.0f, 15.0f);
        break;
    }
    case armnn::LayerType::FullyConnected :
    {
        SetRange(layer, 0, -15.0f, 15.0f);
        break;
    }
    case armnn::LayerType::Permute :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::Pooling2d :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::Reshape :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::Resize :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::Splitter :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::SpaceToBatchNd :
    {
        ForwardParentParameters(layer);
        break;
    }
    case armnn::LayerType::Softmax :
    {
        SetRange(layer, 0, 0.f, 1.f);
        break;
    }
    case armnn::LayerType::StridedSlice :
    {
        ForwardParentParameters(layer);
        break;
    }
    default:
    {
    }
}
}

} //namespace armnn