blob: 9455effd7239c0ab5b5a6af7268c6bd949679d02 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins52ba29e2018-08-29 15:32:11 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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#include "ActivationLayer.h"
25
26#include "arm_compute/core/Types.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000027#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
38SimpleTensor<T> activation_layer(const SimpleTensor<T> &src, ActivationLayerInfo info)
39{
40 // Create reference
Jenkins52ba29e2018-08-29 15:32:11 +000041 SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 };
Anthony Barbier8140e1e2017-12-14 23:48:46 +000042
43 // Compute reference
44 const T a(info.a());
45 const T b(info.b());
46
47 for(int i = 0; i < src.num_elements(); ++i)
48 {
49 T x = src[i];
50
51 switch(info.activation())
52 {
53 case ActivationLayerInfo::ActivationFunction::ABS:
54 dst[i] = std::abs(x);
55 break;
56 case ActivationLayerInfo::ActivationFunction::LINEAR:
57 dst[i] = a * x + b;
58 break;
59 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
60 dst[i] = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-x));
61 break;
62 case ActivationLayerInfo::ActivationFunction::RELU:
63 dst[i] = std::max<T>(static_cast<T>(0), x);
64 break;
65 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
66 dst[i] = std::min<T>(a, std::max(static_cast<T>(0), x));
67 break;
68 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
69 dst[i] = std::min<T>(a, std::max<T>(b, x));
70 break;
71 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
72 dst[i] = (x > 0) ? x : a * x;
73 break;
74 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
75 dst[i] = std::log(static_cast<T>(1) + std::exp(x));
76 break;
77 case ActivationLayerInfo::ActivationFunction::SQRT:
78 dst[i] = std::sqrt(x);
79 break;
80 case ActivationLayerInfo::ActivationFunction::SQUARE:
81 dst[i] = x * x;
82 break;
83 case ActivationLayerInfo::ActivationFunction::TANH:
84 dst[i] = a * std::tanh(b * x);
85 break;
86 default:
87 ARM_COMPUTE_ERROR("Unsupported activation function");
88 }
89 }
90
91 return dst;
92}
93
Anthony Barbier8140e1e2017-12-14 23:48:46 +000094template <>
95SimpleTensor<uint8_t> activation_layer<uint8_t>(const SimpleTensor<uint8_t> &src, ActivationLayerInfo info)
96{
97 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
98 SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info);
99 SimpleTensor<uint8_t> dst = convert_to_asymmetric(dst_tmp, src.quantization_info());
100 return dst;
101}
102
103template SimpleTensor<float> activation_layer(const SimpleTensor<float> &src, ActivationLayerInfo info);
104template SimpleTensor<half> activation_layer(const SimpleTensor<half> &src, ActivationLayerInfo info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000105} // namespace reference
106} // namespace validation
107} // namespace test
108} // namespace arm_compute