aboutsummaryrefslogtreecommitdiff
path: root/src/backends/backendsCommon/test/ActivationEndToEndTestImpl.hpp
blob: 09d71923b625793db123319d3e09d286c709035c (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// Copyright © 2020-2021,2023-2024 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include "EndToEndTestImpl.hpp"

#include <armnn/INetwork.hpp>
#include <armnn/TypesUtils.hpp>

#include <CommonTestUtils.hpp>

#include <ResolveType.hpp>

namespace
{

/** Defines the acceptable tolerance of ActivationFunction-DataType combinations.
 *
 * @param activationFunction The activation function used
 * @param dataType  Data type used
 *
 * @return Tolerance depending on the activation function and data type
 */
float GetActivationTolerance(const armnn::ActivationFunction& activationFunction, DataType dataType)
{
    constexpr float defaultTolerance = 1e-6f;

    switch (activationFunction)
    {
        // The following values are taken from ArmComputeLibrary/tests/validation/CL/ActivationLayer.cpp
        case ActivationFunction::Elu:
            return (dataType == DataType::Float16 ? 0.01f : 0.00001f);
        case ActivationFunction::HardSwish:
            return (dataType == DataType::Float16 ? 0.01f : defaultTolerance);
        default:
            return defaultTolerance;
    }
}

/** Creates a network with one layer of the activation function specified in the activation descriptor.
 *
 * @param inputInfo  Tensor info of inputs
 * @param outputInfo  Tensor info of outputs
 * @param descriptor  Activation descriptor
 *
 * @return INetworkPtr  A pointer to the created network
 */
armnn::INetworkPtr CreateActivationNetwork(const armnn::TensorInfo& inputInfo,
                                           const armnn::TensorInfo& outputInfo,
                                           const armnn::ActivationDescriptor& descriptor)
{
    using namespace armnn;

    char const* ActivationName = GetActivationFunctionAsCString(descriptor.m_Function);

    INetworkPtr net(INetwork::Create());

    IConnectableLayer* inputLayer = net->AddInputLayer(0, "input");
    IConnectableLayer* activationLayer = net->AddActivationLayer(descriptor, ActivationName);
    IConnectableLayer* outputLayer = net->AddOutputLayer(0, "output");

    Connect(inputLayer, activationLayer, inputInfo, 0, 0);
    Connect(activationLayer, outputLayer, outputInfo, 0, 0);

    return net;
}

/** Specifies the implementation of end to end tests for activation functions.
 *
 *  - Converts input data and expected-output data to the data type that is desired for the test (ArmnnType)
 *  - Creates a network with one layer of the activation function specified in the activation descriptor.
 *  - Executes the network on specified backends and compares results to expected output values
 *
 * @tparam ArmnnType  The armnn data type for the input and expected-output data
 * @param backends  Backends to run test on
 * @param floatInputData  Input data given as vector of float
 * @param floatExpectedOutputData  Expected output data given as vector of float
 * @param inputInfo  Tensor info of inputs
 * @param outputInfo  Tensor info of outputs
 * @param descriptor  Activation descriptor
 */
template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
void ActivationEndToEndImpl(const std::vector<armnn::BackendId>& backends,
                     const std::vector<float>& floatInputData,
                     const std::vector<float>& floatExpectedOutputData,
                     const armnn::TensorInfo&  inputInfo,
                     const armnn::TensorInfo&  outputInfo,
                     const armnn::ActivationDescriptor& descriptor)
{
    using namespace armnn;

    // Selectively quantizes/transforms float values to the needed data type
    std::vector<T> inputData          = armnnUtils::QuantizedVector<T>( floatInputData,
                                                                        inputInfo.GetQuantizationScale(),
                                                                        inputInfo.GetQuantizationOffset());
    std::vector<T> expectedOutputData = armnnUtils::QuantizedVector<T>( floatExpectedOutputData,
                                                                        outputInfo.GetQuantizationScale(),
                                                                        outputInfo.GetQuantizationOffset());

    INetworkPtr net = CreateActivationNetwork(inputInfo, outputInfo, descriptor);

    std::map<int, std::vector<T>> inputTensorData          = { { 0, inputData } };
    std::map<int, std::vector<T>> expectedOutputTensorData = { { 0, expectedOutputData } };

    float tolerance = GetActivationTolerance(descriptor.m_Function, ArmnnType);

    EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(net),
                                                inputTensorData,
                                                expectedOutputTensorData,
                                                backends,
                                                tolerance);
}

std::vector<float> Activation(const std::vector<float>& input,
                              const ActivationDescriptor& descriptor)
{
    float a = descriptor.m_A;
    float b = descriptor.m_B;

    std::vector<float> output;
    output.reserve(input.size());

    // Compute the result of the activation function.
    switch (descriptor.m_Function)
    {
        case ActivationFunction::Linear:
        {
            for (auto in :input)
            {
                auto out = a * in + b;
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Sigmoid:
        {
            for (auto in :input)
            {
                auto out = 1.f / (1.f + expf(-in));
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::ReLu:
        {
            for (auto in :input)
            {
                auto out = std::max(0.f, in);
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::BoundedReLu:
        {
            for (auto in :input)
            {
                auto out = std::min(a, std::max(b, in));
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::SoftReLu:
        {
            for (auto in :input)
            {
                auto out = logf(1.0f + expf(in));
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::LeakyReLu:
        {
            for (auto in :input)
            {
                auto out = in > 0.0f ? in : (in * a);
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Abs:
        {
            for (auto in :input)
            {
                auto out = in < 0 ? -in : in;
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Sqrt:
        {
            for (auto in :input)
            {
                auto out = sqrtf(in);
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Square:
        {
            for (auto in :input)
            {
                auto out = in * in;
                output.push_back(out);
            }
            break;
       }
        case ActivationFunction::TanH:
        {
            for (auto in :input)
            {
                auto out = a * tanhf(b * in);
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Elu:
        {
            for (auto in: input) {
                auto out = (in >= 0) ? in : a * (expf(in) - 1);
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::HardSwish:
        {
            for (auto in :input)
            {
                // hard_swish(x) = x * relu6(x+3) / 6
                // relu6(x) = min(max(x,0),6)
                auto out = in * (std::min(std::max((in + 3), 0.0f), 6.0f)) / 6;
                output.push_back(out);
            }
            break;
        }
        case ActivationFunction::Gelu:
        {
            for (auto in :input)
            {
                // gelu(x) = x * 1/2 * (1 + erf(x / sqrt(2))),
                // where erf is Gaussian error function
                auto out = in * (0.5f * (1.0f + erff(static_cast<float>(in / std::sqrt(2)))));
                output.push_back(out);
            }
            break;
        }
        default:
        {
            throw InvalidArgumentException("Unsupported activation function");
        }
    }
    return output;
}

/** Executes an end to end test for activation layers with specific input and expected-output data
 *
 * @tparam ArmnnType  The armnn data type for the input and expected-output data
 * @param backends  The backends on which to run the test
 */
template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
void ActivationEndToEndTest(const std::vector<BackendId>& backends,
                            const ActivationFunction activationFunction,
                            const float qScale=1.0f,
                            const int32_t qOffset=0,
                            const float a = 1,
                            const float b = 0)
{
    std::vector<float> floatInputData{ -2.0f, -1.0f, -0.0f, 0.0f,
                                       1.0f,  2.0f,  3.0f, 4.0f };

    ActivationDescriptor descriptor(activationFunction, a, b);

    std::vector<float> floatExpectedOutputData = Activation(floatInputData, descriptor);

    armnn::TensorInfo inputInfo({ 2, 2, 2, 1 }, ArmnnType, qScale, qOffset, true);
    armnn::TensorInfo outputInfo({ 2, 2, 2, 1 }, ArmnnType, qScale, qOffset);

    ActivationEndToEndImpl<ArmnnType>(backends,
                                      floatInputData,
                                      floatExpectedOutputData,
                                      inputInfo,
                                      outputInfo,
                                      descriptor);
}

} // anonymous namespace