blob: bde3ac82e7b7bb835876ed97e081003448a42628 [file] [log] [blame]
Georgios Pinitas57c033b2018-02-15 12:29:44 +00001/*
Georgios Pinitas5a594532018-12-03 14:30:05 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitas57c033b2018-02-15 12:29:44 +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#ifndef __ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H__
25#define __ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H__
26
27#include "arm_compute/core/NEON/wrapper/wrapper.h"
28
29namespace arm_compute
30{
31namespace detail
32{
Georgios Pinitas57c033b2018-02-15 12:29:44 +000033/** Dummy activation object */
34template <typename T, int S>
35struct dummy
36{
Alex Gildayc357c472018-03-21 13:54:09 +000037 /** NEON vector type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000038 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
39
Alex Gildayc357c472018-03-21 13:54:09 +000040 /** Construct a dummy activation object.
41 *
42 * @param[in] act_info Activation layer information.
43 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000044 explicit dummy(ActivationLayerInfo act_info)
45 {
46 ARM_COMPUTE_UNUSED(act_info);
47 }
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Run activation function.
49 *
50 * @param[in] vval Vector of values.
51 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000052 void operator()(ExactType &vval)
53 {
54 ARM_COMPUTE_UNUSED(vval);
55 }
56};
Michalis Spyroua6825a42018-09-13 12:24:03 +010057/** Linear activation object */
58template <typename T, int S>
59struct linear
60{
61 /** NEON vector type. */
62 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
63 /** NEON vector tag type. */
64 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
65
66 /** Construct a Linear activation object.
67 *
68 * @param[in] act_info Activation layer information.
69 */
70 explicit linear(ActivationLayerInfo act_info)
71 : valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})),
72 vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{}))
73 {
74 }
75
76 /** Run activation function.
77 *
78 * @param[in] vval Vector of values.
79 */
80 void operator()(ExactType &vval)
81 {
82 vval = wrapper::vmla(vval, valpha, vbeta);
83 }
84
85 /** Vector of alphas. */
86 const ExactType valpha;
87 /** Vector of betas. */
88 const ExactType vbeta;
89};
90/** Square activation object */
91template <typename T, int S>
92struct square
93{
94 /** NEON vector type. */
95 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
96 /** NEON vector tag type. */
97 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
98
99 /** Construct a Square activation object.
100 *
101 * @param[in] act_info Activation layer information.
102 */
103 explicit square(ActivationLayerInfo act_info)
104 {
105 ARM_COMPUTE_UNUSED(act_info);
106 }
107
108 /** Run activation function.
109 *
110 * @param[in] vval Vector of values.
111 */
112 void operator()(ExactType &vval)
113 {
114 vval = wrapper::vmul(vval, vval);
115 }
116};
117/** Logistic activation object */
118template <typename T, int S>
119struct logistic
120{
121 /** NEON vector type. */
122 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
123 /** NEON vector tag type. */
124 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
125
126 /** Construct a Logistic activation object.
127 *
128 * @param[in] act_info Activation layer information.
129 */
130 explicit logistic(ActivationLayerInfo act_info)
131 : vone(wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{}))
132 {
133 ARM_COMPUTE_UNUSED(act_info);
134 }
135
136 /** Run activation function.
137 *
138 * @param[in] vval Vector of values.
139 */
140 void operator()(ExactType &vval)
141 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000142 vval = wrapper::vinv(wrapper::vadd(vone, wrapper::vexpq(wrapper::vneg(vval))));
Michalis Spyroua6825a42018-09-13 12:24:03 +0100143 }
144
145 /** Vector of ones. */
146 const ExactType vone;
147};
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000148/** RELU activation object */
149template <typename T, int S>
150struct relu
151{
Alex Gildayc357c472018-03-21 13:54:09 +0000152 /** NEON vector type. */
153 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
154 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000155 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
156
Alex Gildayc357c472018-03-21 13:54:09 +0000157 /** Construct a RELU activation object.
158 *
159 * @param[in] act_info Activation layer information.
160 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000161 explicit relu(ActivationLayerInfo act_info)
162 : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}))
163 {
164 ARM_COMPUTE_UNUSED(act_info);
165 }
166
Alex Gildayc357c472018-03-21 13:54:09 +0000167 /** Run activation function.
168 *
169 * @param[in] vval Vector of values.
170 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000171 void operator()(ExactType &vval)
172 {
173 vval = wrapper::vmax(vzero, vval);
174 }
175
Alex Gildayc357c472018-03-21 13:54:09 +0000176 /** Vector of zeroes. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000177 const ExactType vzero;
178};
179/** Bounded RELU activation object */
180template <typename T, int S>
181struct brelu
182{
Alex Gildayc357c472018-03-21 13:54:09 +0000183 /** NEON vector type. */
184 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
185 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000186 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
187
Alex Gildayc357c472018-03-21 13:54:09 +0000188 /** Construct a bounded RELU activation object.
189 *
190 * @param[in] act_info Activation layer information.
191 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000192 explicit brelu(ActivationLayerInfo act_info)
193 : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{})),
194 valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{}))
195 {
196 }
197
Alex Gildayc357c472018-03-21 13:54:09 +0000198 /** Run activation function.
199 *
200 * @param[in] vval Vector of values.
201 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000202 void operator()(ExactType &vval)
203 {
204 vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval));
205 }
206
Alex Gildayc357c472018-03-21 13:54:09 +0000207 /** Vector of zeroes. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000208 const ExactType vzero;
Alex Gildayc357c472018-03-21 13:54:09 +0000209 /** Vector of alphas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000210 const ExactType valpha;
211};
212/** Lower-Upper Bounded RELU activation object */
213template <typename T, int S>
214struct lubrelu
215{
Alex Gildayc357c472018-03-21 13:54:09 +0000216 /** NEON vector type. */
217 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
218 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000219 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
220
Alex Gildayc357c472018-03-21 13:54:09 +0000221 /** Construct a lower-upper bounded RELU activation object.
222 *
223 * @param[in] act_info Activation layer information.
224 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000225 explicit lubrelu(ActivationLayerInfo act_info)
226 : valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})),
227 vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{}))
228 {
229 }
230
Alex Gildayc357c472018-03-21 13:54:09 +0000231 /** Run activation function.
232 *
233 * @param[in] vval Vector of values.
234 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000235 void operator()(ExactType &vval)
236 {
237 vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval));
238 }
239
Alex Gildayc357c472018-03-21 13:54:09 +0000240 /** Vector of alphas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000241 const ExactType valpha;
Alex Gildayc357c472018-03-21 13:54:09 +0000242 /** Vector of betas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000243 const ExactType vbeta;
244};
245} // namespace detail
246} // namespace arm_compute
247#endif /* __ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H__ */