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" |
| 33 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | #include <arm_neon.h> |
| 39 | #include <cstdint> |
| 40 | #include <map> |
| 41 | #include <string> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | class Coordinates; |
| 46 | |
| 47 | namespace |
| 48 | { |
| 49 | template <ElementWiseUnary op, typename ScalarType> |
| 50 | inline ScalarType elementwise_op_scalar(const ScalarType &a) |
| 51 | { |
| 52 | switch(op) |
| 53 | { |
| 54 | case ElementWiseUnary::RSQRT: |
| 55 | return 1 / sqrt(a); |
| 56 | case ElementWiseUnary::EXP: |
| 57 | return std::exp(a); |
| 58 | default: |
| 59 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | template <ElementWiseUnary op, typename VectorType> |
| 64 | inline VectorType elementwise_op(const VectorType &a) |
| 65 | { |
| 66 | switch(op) |
| 67 | { |
| 68 | case ElementWiseUnary::RSQRT: |
| 69 | return wrapper::vinvsqrt(a); |
| 70 | case ElementWiseUnary::EXP: |
| 71 | return wrapper::vexpq(a); |
| 72 | default: |
| 73 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | template <ElementWiseUnary op, typename ScalarType> |
| 78 | void elementwise_op(const ITensor *in, ITensor *out, const Window &window) |
| 79 | { |
| 80 | const int window_step_x = 16 / sizeof(ScalarType); |
| 81 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 82 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 83 | |
| 84 | Window win = window; |
| 85 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 86 | |
| 87 | Iterator input(in, win); |
| 88 | Iterator output(out, win); |
| 89 | |
| 90 | execute_window_loop(win, [&](const Coordinates & id) |
| 91 | { |
| 92 | auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr()); |
| 93 | const auto input_ptr = reinterpret_cast<const ScalarType *>(input.ptr()); |
| 94 | |
| 95 | int x = window_start_x; |
| 96 | for(; x <= window_end_x - window_step_x; x += window_step_x) |
| 97 | { |
| 98 | wrapper::vstore(output_ptr + x, elementwise_op<op>(wrapper::vloadq(input_ptr + x))); |
| 99 | } |
| 100 | for(; x < window_end_x; ++x) |
| 101 | { |
| 102 | *(output_ptr + x) = elementwise_op_scalar<op>(*(input_ptr + x)); |
| 103 | } |
| 104 | }, |
| 105 | input, output); |
| 106 | } |
| 107 | |
| 108 | template <ElementWiseUnary op> |
| 109 | std::function<void(const ITensor *input, ITensor *output, const Window &window)> |
| 110 | configure_func(const ITensor *input, ITensor *output) |
| 111 | { |
| 112 | std::string function_to_call("op_"); |
| 113 | function_to_call += string_from_data_type(input->info()->data_type()) + "_"; |
| 114 | function_to_call += string_from_data_type(output->info()->data_type()); |
| 115 | |
| 116 | static std::map<std::string, NEElementwiseUnaryKernel::ElementwiseUnaryFunction *> map_function = |
| 117 | { |
| 118 | { "op_F32_F32", &elementwise_op<op, float> } |
| 119 | }; |
| 120 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 121 | map_function["op_F16_F16"] = &elementwise_op<op, float16_t>; |
| 122 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 123 | |
| 124 | auto it = map_function.find(function_to_call); |
| 125 | |
| 126 | if(it != map_function.end()) |
| 127 | { |
| 128 | auto func = it->second; |
| 129 | return [func](const ITensor * input, ITensor * output, const Window & window) |
| 130 | { |
| 131 | func(input, output, window); |
| 132 | }; |
| 133 | } |
| 134 | return nullptr; |
| 135 | } |
| 136 | } // namespace |
| 137 | |
| 138 | NEElementwiseUnaryKernel::NEElementwiseUnaryKernel() |
| 139 | : _function(nullptr), _input(nullptr), _output(nullptr) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | void NEElementwiseUnaryKernel::configure(ElementWiseUnary op, const ITensor *input, ITensor *output) |
| 144 | { |
| 145 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input->info(), *output->info())); |
| 146 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 147 | |
| 148 | // Configure kernel window |
| 149 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input->info()); |
| 150 | const TensorShape &out_shape = broadcast_pair.first; |
| 151 | const ValidRegion &valid_region = broadcast_pair.second; |
| 152 | |
| 153 | // Auto initialize output if not initialized |
| 154 | auto_init_if_empty(*output->info(), out_shape, 1, input->info()->data_type()); |
| 155 | |
| 156 | Window win = calculate_max_window(valid_region); |
| 157 | |
| 158 | _input = input; |
| 159 | _output = output; |
| 160 | |
| 161 | INEKernel::configure(win); |
| 162 | |
| 163 | switch(op) |
| 164 | { |
| 165 | case ElementWiseUnary::RSQRT: |
| 166 | _function = configure_func<ElementWiseUnary::RSQRT>(input, output); |
| 167 | break; |
| 168 | case ElementWiseUnary::EXP: |
| 169 | _function = configure_func<ElementWiseUnary::EXP>(input, output); |
| 170 | break; |
| 171 | default: |
| 172 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | Status NEElementwiseUnaryKernel::validate_arguments(const ITensorInfo &input, const ITensorInfo &output) |
| 177 | { |
| 178 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
| 179 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32); |
| 180 | |
| 181 | // Validate in case of configured output |
| 182 | if(output.total_size() > 0) |
| 183 | { |
| 184 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
| 185 | } |
| 186 | |
| 187 | return Status{}; |
| 188 | } |
| 189 | |
| 190 | Status NEElementwiseUnaryKernel::validate(ElementWiseUnary op, const ITensorInfo *input, const ITensorInfo *output) |
| 191 | { |
| 192 | ARM_COMPUTE_UNUSED(op); |
| 193 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 194 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output)); |
| 195 | return Status{}; |
| 196 | } |
| 197 | |
| 198 | void NEElementwiseUnaryKernel::run(const Window &window, const ThreadInfo &info) |
| 199 | { |
| 200 | ARM_COMPUTE_UNUSED(info); |
| 201 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 202 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 203 | ARM_COMPUTE_ERROR_ON(_function == nullptr); |
| 204 | _function(_input, _output, window); |
| 205 | } |
| 206 | } // namespace arm_compute |