blob: 71d5a9eef732178c37e030f9b4d4f25c86c7681c [file] [log] [blame]
Georgios Pinitas57c033b2018-02-15 12:29:44 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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};
57/** RELU activation object */
58template <typename T, int S>
59struct relu
60{
Alex Gildayc357c472018-03-21 13:54:09 +000061 /** NEON vector type. */
62 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
63 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000064 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
65
Alex Gildayc357c472018-03-21 13:54:09 +000066 /** Construct a RELU activation object.
67 *
68 * @param[in] act_info Activation layer information.
69 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000070 explicit relu(ActivationLayerInfo act_info)
71 : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}))
72 {
73 ARM_COMPUTE_UNUSED(act_info);
74 }
75
Alex Gildayc357c472018-03-21 13:54:09 +000076 /** Run activation function.
77 *
78 * @param[in] vval Vector of values.
79 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000080 void operator()(ExactType &vval)
81 {
82 vval = wrapper::vmax(vzero, vval);
83 }
84
Alex Gildayc357c472018-03-21 13:54:09 +000085 /** Vector of zeroes. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000086 const ExactType vzero;
87};
88/** Bounded RELU activation object */
89template <typename T, int S>
90struct brelu
91{
Alex Gildayc357c472018-03-21 13:54:09 +000092 /** NEON vector type. */
93 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
94 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +000095 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
96
Alex Gildayc357c472018-03-21 13:54:09 +000097 /** Construct a bounded RELU activation object.
98 *
99 * @param[in] act_info Activation layer information.
100 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000101 explicit brelu(ActivationLayerInfo act_info)
102 : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{})),
103 valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{}))
104 {
105 }
106
Alex Gildayc357c472018-03-21 13:54:09 +0000107 /** Run activation function.
108 *
109 * @param[in] vval Vector of values.
110 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000111 void operator()(ExactType &vval)
112 {
113 vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval));
114 }
115
Alex Gildayc357c472018-03-21 13:54:09 +0000116 /** Vector of zeroes. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000117 const ExactType vzero;
Alex Gildayc357c472018-03-21 13:54:09 +0000118 /** Vector of alphas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000119 const ExactType valpha;
120};
121/** Lower-Upper Bounded RELU activation object */
122template <typename T, int S>
123struct lubrelu
124{
Alex Gildayc357c472018-03-21 13:54:09 +0000125 /** NEON vector type. */
126 using ExactType = typename wrapper::traits::neon_vector<T, S>::type;
127 /** NEON vector tag type. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000128 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
129
Alex Gildayc357c472018-03-21 13:54:09 +0000130 /** Construct a lower-upper bounded RELU activation object.
131 *
132 * @param[in] act_info Activation layer information.
133 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000134 explicit lubrelu(ActivationLayerInfo act_info)
135 : valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})),
136 vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{}))
137 {
138 }
139
Alex Gildayc357c472018-03-21 13:54:09 +0000140 /** Run activation function.
141 *
142 * @param[in] vval Vector of values.
143 */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000144 void operator()(ExactType &vval)
145 {
146 vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval));
147 }
148
Alex Gildayc357c472018-03-21 13:54:09 +0000149 /** Vector of alphas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000150 const ExactType valpha;
Alex Gildayc357c472018-03-21 13:54:09 +0000151 /** Vector of betas. */
Georgios Pinitas57c033b2018-02-15 12:29:44 +0000152 const ExactType vbeta;
153};
154} // namespace detail
155} // namespace arm_compute
156#endif /* __ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H__ */