blob: 77b353056e05447bdab828b9d0d34204e03661cc [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Giorgio Arena73023022018-09-04 14:55:55 +01002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer572ade72017-07-21 17:36:33 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__
25#define __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__
26
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010027#include "tests/SimpleTensor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010028#include "tests/validation/Helpers.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
Giorgio Arena73023022018-09-04 14:55:55 +010038template <typename T>
39inline T activate_float(T x, T a, T b, ActivationLayerInfo::ActivationFunction activation)
40{
41 T ret;
42
43 switch(activation)
44 {
45 case ActivationLayerInfo::ActivationFunction::ABS:
46 ret = std::abs(x);
47 break;
48 case ActivationLayerInfo::ActivationFunction::LINEAR:
49 ret = a * x + b;
50 break;
51 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
52 ret = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-x));
53 break;
54 case ActivationLayerInfo::ActivationFunction::RELU:
55 ret = std::max<T>(static_cast<T>(0), x);
56 break;
57 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
58 ret = std::min<T>(a, std::max(static_cast<T>(0), x));
59 break;
60 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
61 ret = std::min<T>(a, std::max<T>(b, x));
62 break;
63 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
64 ret = (x > 0) ? x : a * x;
65 break;
66 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
67 ret = std::log(static_cast<T>(1) + std::exp(x));
68 break;
69 case ActivationLayerInfo::ActivationFunction::SQRT:
70 ret = std::sqrt(x);
71 break;
72 case ActivationLayerInfo::ActivationFunction::SQUARE:
73 ret = x * x;
74 break;
75 case ActivationLayerInfo::ActivationFunction::TANH:
76 ret = a * std::tanh(b * x);
77 break;
78 default:
79 ARM_COMPUTE_ERROR("Unsupported activation function");
80 break;
81 }
82
83 return ret;
84}
85
Isabella Gottardi651540f2018-09-13 15:33:35 +010086template <typename T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +010087SimpleTensor<T> activation_layer(const SimpleTensor<T> &src, ActivationLayerInfo info);
88} // namespace reference
89} // namespace validation
90} // namespace test
91} // namespace arm_compute
92#endif /* __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__ */