Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace detail |
| 32 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 33 | /** Dummy activation object */ |
| 34 | template <typename T, int S> |
| 35 | struct dummy |
| 36 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 37 | /** NEON vector type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 38 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 39 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 40 | /** Construct a dummy activation object. |
| 41 | * |
| 42 | * @param[in] act_info Activation layer information. |
| 43 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 44 | explicit dummy(ActivationLayerInfo act_info) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(act_info); |
| 47 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 48 | /** Run activation function. |
| 49 | * |
| 50 | * @param[in] vval Vector of values. |
| 51 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 52 | void operator()(ExactType &vval) |
| 53 | { |
| 54 | ARM_COMPUTE_UNUSED(vval); |
| 55 | } |
| 56 | }; |
| 57 | /** RELU activation object */ |
| 58 | template <typename T, int S> |
| 59 | struct relu |
| 60 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 61 | /** NEON vector type. */ |
| 62 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 63 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 64 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 65 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 66 | /** Construct a RELU activation object. |
| 67 | * |
| 68 | * @param[in] act_info Activation layer information. |
| 69 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 70 | 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 76 | /** Run activation function. |
| 77 | * |
| 78 | * @param[in] vval Vector of values. |
| 79 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 80 | void operator()(ExactType &vval) |
| 81 | { |
| 82 | vval = wrapper::vmax(vzero, vval); |
| 83 | } |
| 84 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 85 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 86 | const ExactType vzero; |
| 87 | }; |
| 88 | /** Bounded RELU activation object */ |
| 89 | template <typename T, int S> |
| 90 | struct brelu |
| 91 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 92 | /** NEON vector type. */ |
| 93 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 94 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 95 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 96 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 97 | /** Construct a bounded RELU activation object. |
| 98 | * |
| 99 | * @param[in] act_info Activation layer information. |
| 100 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 101 | 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 107 | /** Run activation function. |
| 108 | * |
| 109 | * @param[in] vval Vector of values. |
| 110 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 111 | void operator()(ExactType &vval) |
| 112 | { |
| 113 | vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval)); |
| 114 | } |
| 115 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 116 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 117 | const ExactType vzero; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 118 | /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 119 | const ExactType valpha; |
| 120 | }; |
| 121 | /** Lower-Upper Bounded RELU activation object */ |
| 122 | template <typename T, int S> |
| 123 | struct lubrelu |
| 124 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 125 | /** NEON vector type. */ |
| 126 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 127 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 128 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 129 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 130 | /** Construct a lower-upper bounded RELU activation object. |
| 131 | * |
| 132 | * @param[in] act_info Activation layer information. |
| 133 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 134 | 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 140 | /** Run activation function. |
| 141 | * |
| 142 | * @param[in] vval Vector of values. |
| 143 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 144 | void operator()(ExactType &vval) |
| 145 | { |
| 146 | vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval)); |
| 147 | } |
| 148 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 149 | /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 150 | const ExactType valpha; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame^] | 151 | /** Vector of betas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 152 | const ExactType vbeta; |
| 153 | }; |
| 154 | } // namespace detail |
| 155 | } // namespace arm_compute |
| 156 | #endif /* __ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H__ */ |