George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019 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 | #include "arm_compute/core/NEON/kernels/NEElementwiseUnaryKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/Validate.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/NEAsymm.h" |
| 32 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/NEMath.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 35 | #include "arm_compute/core/TensorInfo.h" |
| 36 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 37 | #include "support/ToolchainSupport.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 38 | |
| 39 | #include <algorithm> |
| 40 | #include <arm_neon.h> |
| 41 | #include <cstdint> |
| 42 | #include <map> |
| 43 | #include <string> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | class Coordinates; |
| 48 | |
| 49 | namespace |
| 50 | { |
| 51 | template <ElementWiseUnary op, typename ScalarType> |
| 52 | inline ScalarType elementwise_op_scalar(const ScalarType &a) |
| 53 | { |
| 54 | switch(op) |
| 55 | { |
| 56 | case ElementWiseUnary::RSQRT: |
| 57 | return 1 / sqrt(a); |
| 58 | case ElementWiseUnary::EXP: |
| 59 | return std::exp(a); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 60 | case ElementWiseUnary::NEG: |
| 61 | return -a; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 62 | case ElementWiseUnary::LOG: |
| 63 | return std::log(a); |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 64 | case ElementWiseUnary::ABS: |
| 65 | return std::abs(a); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 66 | case ElementWiseUnary::ROUND: |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 67 | return support::cpp11::nearbyint(a); |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 68 | case ElementWiseUnary::SIN: |
| 69 | return std::sin(a); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 70 | default: |
| 71 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 72 | } |
| 73 | } |
| 74 | |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 75 | /* Elementwise operations that are supported for float */ |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 76 | template <ElementWiseUnary op, typename ScalarType, bool is_float, typename VectorType, typename std::enable_if<is_float, int>::type = 0> |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 77 | inline VectorType elementwise_op(const VectorType &a) |
| 78 | { |
| 79 | switch(op) |
| 80 | { |
| 81 | case ElementWiseUnary::RSQRT: |
| 82 | return wrapper::vinvsqrt(a); |
| 83 | case ElementWiseUnary::EXP: |
| 84 | return wrapper::vexpq(a); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 85 | case ElementWiseUnary::NEG: |
| 86 | return wrapper::vneg(a); |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 87 | case ElementWiseUnary::LOG: |
| 88 | return wrapper::vlog(a); |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 89 | case ElementWiseUnary::ABS: |
| 90 | return wrapper::vabs(a); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 91 | case ElementWiseUnary::ROUND: |
| 92 | return wrapper::vround(a); |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 93 | case ElementWiseUnary::SIN: |
| 94 | return wrapper::vsin(a); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 95 | default: |
| 96 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 97 | } |
| 98 | } |
| 99 | |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 100 | /* Elementwise operations that are supported for non floats */ |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 101 | template < ElementWiseUnary op, typename ScalarType, bool is_float, typename VectorType, typename std::enable_if < !is_float, int >::type = 0 > |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 102 | inline VectorType elementwise_op(const VectorType &a) |
| 103 | { |
| 104 | switch(op) |
| 105 | { |
| 106 | case ElementWiseUnary::NEG: |
| 107 | return wrapper::vneg(a); |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 108 | case ElementWiseUnary::ABS: |
| 109 | return wrapper::vabs(a); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 110 | default: |
| 111 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | template <ElementWiseUnary op, typename ScalarType, bool is_float> |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 116 | void elementwise_op(const ITensor *in, ITensor *out, const Window &window) |
| 117 | { |
| 118 | const int window_step_x = 16 / sizeof(ScalarType); |
| 119 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 120 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 121 | |
| 122 | Window win = window; |
| 123 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 124 | |
| 125 | Iterator input(in, win); |
| 126 | Iterator output(out, win); |
| 127 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 128 | execute_window_loop(win, [&](const Coordinates &) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 129 | { |
| 130 | auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr()); |
| 131 | const auto input_ptr = reinterpret_cast<const ScalarType *>(input.ptr()); |
| 132 | |
| 133 | int x = window_start_x; |
| 134 | for(; x <= window_end_x - window_step_x; x += window_step_x) |
| 135 | { |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 136 | wrapper::vstore(output_ptr + x, elementwise_op<op, ScalarType, is_float>(wrapper::vloadq(input_ptr + x))); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 137 | } |
| 138 | for(; x < window_end_x; ++x) |
| 139 | { |
| 140 | *(output_ptr + x) = elementwise_op_scalar<op>(*(input_ptr + x)); |
| 141 | } |
| 142 | }, |
| 143 | input, output); |
| 144 | } |
| 145 | |
| 146 | template <ElementWiseUnary op> |
| 147 | std::function<void(const ITensor *input, ITensor *output, const Window &window)> |
| 148 | configure_func(const ITensor *input, ITensor *output) |
| 149 | { |
| 150 | std::string function_to_call("op_"); |
| 151 | function_to_call += string_from_data_type(input->info()->data_type()) + "_"; |
| 152 | function_to_call += string_from_data_type(output->info()->data_type()); |
| 153 | |
| 154 | static std::map<std::string, NEElementwiseUnaryKernel::ElementwiseUnaryFunction *> map_function = |
| 155 | { |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 156 | { "op_F32_F32", &elementwise_op<op, float, true> }, |
| 157 | { "op_S32_S32", &elementwise_op<op, int32_t, false> }, |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 158 | }; |
| 159 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 160 | map_function["op_F16_F16"] = &elementwise_op<op, float16_t, true>; |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 161 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 162 | |
| 163 | auto it = map_function.find(function_to_call); |
| 164 | |
| 165 | if(it != map_function.end()) |
| 166 | { |
| 167 | auto func = it->second; |
| 168 | return [func](const ITensor * input, ITensor * output, const Window & window) |
| 169 | { |
| 170 | func(input, output, window); |
| 171 | }; |
| 172 | } |
| 173 | return nullptr; |
| 174 | } |
| 175 | } // namespace |
| 176 | |
| 177 | NEElementwiseUnaryKernel::NEElementwiseUnaryKernel() |
| 178 | : _function(nullptr), _input(nullptr), _output(nullptr) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | void NEElementwiseUnaryKernel::configure(ElementWiseUnary op, const ITensor *input, ITensor *output) |
| 183 | { |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 184 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(op, *input->info(), *output->info())); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 185 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 186 | |
| 187 | // Configure kernel window |
| 188 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input->info()); |
| 189 | const TensorShape &out_shape = broadcast_pair.first; |
| 190 | const ValidRegion &valid_region = broadcast_pair.second; |
| 191 | |
| 192 | // Auto initialize output if not initialized |
| 193 | auto_init_if_empty(*output->info(), out_shape, 1, input->info()->data_type()); |
| 194 | |
| 195 | Window win = calculate_max_window(valid_region); |
| 196 | |
| 197 | _input = input; |
| 198 | _output = output; |
| 199 | |
| 200 | INEKernel::configure(win); |
| 201 | |
| 202 | switch(op) |
| 203 | { |
| 204 | case ElementWiseUnary::RSQRT: |
| 205 | _function = configure_func<ElementWiseUnary::RSQRT>(input, output); |
| 206 | break; |
| 207 | case ElementWiseUnary::EXP: |
| 208 | _function = configure_func<ElementWiseUnary::EXP>(input, output); |
| 209 | break; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 210 | case ElementWiseUnary::NEG: |
| 211 | _function = configure_func<ElementWiseUnary::NEG>(input, output); |
| 212 | break; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 213 | case ElementWiseUnary::LOG: |
| 214 | _function = configure_func<ElementWiseUnary::LOG>(input, output); |
| 215 | break; |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 216 | case ElementWiseUnary::ABS: |
| 217 | _function = configure_func<ElementWiseUnary::ABS>(input, output); |
| 218 | break; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 219 | case ElementWiseUnary::ROUND: |
| 220 | _function = configure_func<ElementWiseUnary::ROUND>(input, output); |
| 221 | break; |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 222 | case ElementWiseUnary::SIN: |
| 223 | _function = configure_func<ElementWiseUnary::SIN>(input, output); |
| 224 | break; |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 225 | default: |
| 226 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 227 | } |
| 228 | } |
| 229 | |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 230 | Status NEElementwiseUnaryKernel::validate_arguments(ElementWiseUnary op, const ITensorInfo &input, const ITensorInfo &output) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 231 | { |
| 232 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 233 | switch(op) |
| 234 | { |
| 235 | case ElementWiseUnary::EXP: |
| 236 | case ElementWiseUnary::RSQRT: |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 237 | case ElementWiseUnary::LOG: |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 238 | case ElementWiseUnary::ROUND: |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 239 | case ElementWiseUnary::SIN: |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 240 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32); |
| 241 | break; |
| 242 | case ElementWiseUnary::NEG: |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 243 | case ElementWiseUnary::ABS: |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 244 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32, DataType::S32); |
| 245 | break; |
| 246 | default: |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 247 | ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported"); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 248 | } |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 249 | // Validate in case of configured output |
| 250 | if(output.total_size() > 0) |
| 251 | { |
| 252 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
| 253 | } |
| 254 | |
| 255 | return Status{}; |
| 256 | } |
| 257 | |
| 258 | Status NEElementwiseUnaryKernel::validate(ElementWiseUnary op, const ITensorInfo *input, const ITensorInfo *output) |
| 259 | { |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 260 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 261 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(op, *input, *output)); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 262 | return Status{}; |
| 263 | } |
| 264 | |
| 265 | void NEElementwiseUnaryKernel::run(const Window &window, const ThreadInfo &info) |
| 266 | { |
| 267 | ARM_COMPUTE_UNUSED(info); |
| 268 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 269 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 270 | ARM_COMPUTE_ERROR_ON(_function == nullptr); |
| 271 | _function(_input, _output, window); |
| 272 | } |
| 273 | } // namespace arm_compute |