Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/NESoftmaxLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 32 | #include "arm_compute/core/NEON/NEMath.h" |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 303f0db | 2018-11-19 11:56:51 +0000 | [diff] [blame] | 38 | #include "arm_compute/core/utils/misc/SaturateCast.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <algorithm> |
| 41 | #include <arm_neon.h> |
| 42 | #include <cfloat> |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 43 | #include <functional> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 47 | template <typename float_vec_type, typename int_vec_type> |
| 48 | int_vec_type convert_float_to_int(const float_vec_type &in); |
| 49 | |
| 50 | template <typename float_vec_type, typename int_vec_type> |
| 51 | float_vec_type convert_int_to_float(const int_vec_type &in); |
| 52 | |
| 53 | template <> |
| 54 | uint8x16_t convert_float_to_int<float32x4x4_t, uint8x16_t>(const float32x4x4_t &in) |
| 55 | { |
| 56 | uint8x16_t out; |
| 57 | convert_float32x4x4_to_uint8x16(in, out); |
| 58 | return out; |
| 59 | } |
| 60 | |
| 61 | template <> |
| 62 | int8x16_t convert_float_to_int<float32x4x4_t, int8x16_t>(const float32x4x4_t &in) |
| 63 | { |
| 64 | int8x16_t out; |
| 65 | convert_float32x4x4_to_int8x16(in, out); |
| 66 | return out; |
| 67 | } |
| 68 | |
| 69 | template <> |
| 70 | float32x4x4_t convert_int_to_float<float32x4x4_t, uint8x16_t>(const uint8x16_t &in) |
| 71 | { |
| 72 | return convert_uint8x16_to_float32x4x4(in); |
| 73 | } |
| 74 | |
| 75 | template <> |
| 76 | float32x4x4_t convert_int_to_float<float32x4x4_t, int8x16_t>(const int8x16_t &in) |
| 77 | { |
| 78 | return convert_int8x16_to_float32x4x4(in); |
| 79 | } |
| 80 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | namespace |
| 82 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 83 | Status validate_arguments_logits_1d_max(const ITensorInfo &input, const ITensorInfo &output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 84 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 87 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 88 | // Validate in case of configured output |
| 89 | if(output.total_size() != 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&input, &output); |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output.tensor_shape(), TensorShape(input.tensor_shape()).set(0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 95 | |
| 96 | return Status{}; |
| 97 | } |
| 98 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 99 | template <typename T> |
| 100 | void logits_1d_max(const ITensor &in, ITensor &out, const Window &window) |
| 101 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 102 | /** NEON vector tag type. */ |
| 103 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 104 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 105 | constexpr int window_step_x = 16 / sizeof(T); |
| 106 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 107 | const auto window_end_x = static_cast<int>(window.x().end()); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 108 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 109 | Window win{ window }; |
| 110 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 111 | Iterator input(&in, win); |
| 112 | Iterator output(&out, win); |
| 113 | |
| 114 | const int sum_stages = log2(window_step_x / 2); |
| 115 | execute_window_loop(win, [&](const Coordinates &) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 116 | { |
| 117 | // Get pointers |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 118 | const auto in_ptr = reinterpret_cast<const T *>(input.ptr()); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 119 | const auto out_ptr = reinterpret_cast<T *>(output.ptr()); |
| 120 | |
| 121 | // Init max value |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 122 | auto vec_max = wrapper::vdup_n(support::cpp11::lowest<T>(), ExactTagType{}); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 123 | int x = window_start_x; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 124 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 125 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 126 | { |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 127 | const auto current_value = wrapper::vloadq(in_ptr + x); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 128 | vec_max = wrapper::vmax(vec_max, current_value); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 129 | } |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 130 | auto carry_max = wrapper::vpmax(wrapper::vgethigh(vec_max), wrapper::vgetlow(vec_max)); |
| 131 | |
| 132 | for(int i = 0; i < sum_stages; ++i) |
| 133 | { |
| 134 | carry_max = wrapper::vpmax(carry_max, carry_max); |
| 135 | } |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 136 | T max_val = wrapper::vgetlane(carry_max, 0); |
| 137 | |
| 138 | // Compute left-over elements |
| 139 | for(; x < window_end_x; ++x) |
| 140 | { |
| 141 | max_val = *(in_ptr + x) > max_val ? *(in_ptr + x) : max_val; |
| 142 | } |
| 143 | |
| 144 | *out_ptr = max_val; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 145 | }, |
| 146 | input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | } // namespace |
| 149 | |
| 150 | NELogits1DMaxKernel::NELogits1DMaxKernel() |
| 151 | : _func(nullptr), _border_size() |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | BorderSize NELogits1DMaxKernel::border_size() const |
| 156 | { |
| 157 | return _border_size; |
| 158 | } |
| 159 | |
| 160 | void NELogits1DMaxKernel::configure(const ITensor *input, ITensor *output) |
| 161 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 162 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 163 | ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), output->info()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 164 | // Perform validation step |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 165 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_1d_max(*input->info(), *output->info())); |
| 166 | // Configure kernel window |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 167 | |
| 168 | // Softmax across the x dimension |
| 169 | const TensorShape output_shape = TensorShape(input->info()->tensor_shape()).set(0, 1); |
| 170 | // Output auto initialization if not yet initialized |
| 171 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info()); |
| 172 | |
| 173 | Window win = calculate_max_window(*input->info(), Steps()); |
| 174 | Coordinates coord; |
| 175 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 176 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | |
| 178 | switch(input->info()->data_type()) |
| 179 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 180 | case DataType::QASYMM8: |
| 181 | _func = &logits_1d_max<qasymm8_t>; |
| 182 | break; |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 183 | case DataType::QASYMM8_SIGNED: |
| 184 | _func = &logits_1d_max<qasymm8_signed_t>; |
| 185 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 186 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 187 | case DataType::F16: |
| 188 | _func = &logits_1d_max<float16_t>; |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 189 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 190 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 191 | case DataType::F32: |
| 192 | _func = &logits_1d_max<float>; |
| 193 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | default: |
| 195 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 196 | } |
| 197 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 198 | _input = input; |
| 199 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 201 | const int input_width = input->info()->valid_region().shape.x(); |
| 202 | const int num_elems_processed_per_iteration = 16U / data_size_from_type(input->info()->data_type()); |
| 203 | const int num_elems_read_per_iteration = ceil_to_multiple(input_width, num_elems_processed_per_iteration); |
| 204 | |
| 205 | _border_size = BorderSize(0, num_elems_read_per_iteration - input_width, 0, 0); |
| 206 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 207 | INEKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 208 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 210 | Status NELogits1DMaxKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 211 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 212 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 213 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_1d_max(*input, *output)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 215 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 218 | void NELogits1DMaxKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 220 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 222 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 223 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 224 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 225 | (*_func)(*_input, *_output, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | namespace |
| 229 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 230 | Status validate_arguments_logits_softmax(const ITensorInfo &input, const ITensorInfo &max, |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 231 | const ITensorInfo &output, const float beta, const ITensorInfo &tmp, bool is_log) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 233 | ARM_COMPUTE_UNUSED(beta); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 234 | // Check input |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 235 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 236 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 237 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 238 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input.data_type()); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 239 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 240 | // Check max |
| 241 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &max); |
| 242 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(TensorShape(input.tensor_shape()).set(0, 1), max.tensor_shape()); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 243 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&input, &max); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 244 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 245 | // Check output if configured |
| 246 | if(output.total_size() != 0) |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 247 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 248 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(input.data_type(), is_log) : output.quantization_info(); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 249 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
| 250 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&input, &output); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 251 | ARM_COMPUTE_RETURN_ERROR_ON(output.quantization_info() != output_quantization); |
Georgios Pinitas | 9247c92 | 2017-06-28 18:29:47 +0100 | [diff] [blame] | 252 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 254 | // Check tmp if configured |
| 255 | if(tmp.total_size() != 0) |
| 256 | { |
| 257 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : input.data_type(); |
| 258 | ARM_COMPUTE_RETURN_ERROR_ON(tmp.data_type() != tmp_data_type); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 259 | // We could potentially reduce tmp memory if we could predict or make an assumption |
| 260 | // on the maximum number of threads that will run in parallel. |
| 261 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&input, &tmp); |
| 262 | } |
| 263 | |
| 264 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 265 | } |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 266 | template <typename T, bool is_log> |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 267 | void logits_1d_softmax_qasymm8(const ITensor &in, const ITensor &max, void *const tmp, ITensor &out, const float beta, const Window &window) |
| 268 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 269 | static_assert(std::is_same<T, qasymm8_t>::value |
| 270 | || std::is_same<T, qasymm8_signed_t>::value, |
| 271 | "quantized type should be either qasymm8_t or qasymm8_signed_t."); |
| 272 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 273 | const int start_x = in.info()->valid_region().anchor.x(); |
| 274 | const int input_width = in.info()->valid_region().shape.x(); |
| 275 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 276 | const float scale_beta = -beta * in.info()->quantization_info().uniform().scale; |
| 277 | const auto scale_beta_vec = vdupq_n_f32(scale_beta); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 278 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 279 | Iterator in_it(&in, window); |
| 280 | Iterator max_it(&max, window); |
| 281 | Iterator out_it(&out, window); |
| 282 | constexpr int vec_size = 16; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 283 | |
| 284 | execute_window_loop(window, [&](const Coordinates &) |
| 285 | { |
| 286 | /* Get pointers */ |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 287 | const auto in_ptr = reinterpret_cast<const T *>(in_it.ptr()) + start_x; |
| 288 | const auto out_ptr = reinterpret_cast<T *>(out_it.ptr()) + start_x; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 289 | const auto tmp_ptr = reinterpret_cast<float *>(tmp); |
| 290 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 291 | float sum{}; |
| 292 | float sum_inversed{}; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 293 | |
| 294 | /* Compute exponentials and sum */ |
| 295 | { |
| 296 | /* Get max value */ |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 297 | const auto max_val = *reinterpret_cast<const T *>(max_it.ptr()); |
| 298 | const auto vec_max = wrapper::vdup_n(max_val, wrapper::traits::vector_128_tag{}); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 299 | |
| 300 | /* Init sum to zero */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 301 | float32x4x4_t vec_sum = |
| 302 | { |
| 303 | vdupq_n_f32(0.f), |
| 304 | vdupq_n_f32(0.f), |
| 305 | vdupq_n_f32(0.f), |
| 306 | vdupq_n_f32(0.f), |
| 307 | }; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 308 | |
| 309 | /* Loop over row and compute exponentials and sum */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 310 | int x = 0; |
| 311 | for(; x <= (input_width - vec_size); x += vec_size) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 312 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 313 | auto vec_elements = wrapper::vloadq(in_ptr + x); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 314 | vec_elements = wrapper::vsub(vec_max, vec_elements); |
| 315 | auto vec_elements_flt = convert_int_to_float<float32x4x4_t>(vec_elements); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 316 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 317 | if(is_log) |
| 318 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 319 | vec_elements_flt.val[0] = vmulq_f32(vec_elements_flt.val[0], scale_beta_vec); |
| 320 | vec_elements_flt.val[1] = vmulq_f32(vec_elements_flt.val[1], scale_beta_vec); |
| 321 | vec_elements_flt.val[2] = vmulq_f32(vec_elements_flt.val[2], scale_beta_vec); |
| 322 | vec_elements_flt.val[3] = vmulq_f32(vec_elements_flt.val[3], scale_beta_vec); |
| 323 | vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vexpq_f32(vec_elements_flt.val[0])); |
| 324 | vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vexpq_f32(vec_elements_flt.val[1])); |
| 325 | vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vexpq_f32(vec_elements_flt.val[2])); |
| 326 | vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vexpq_f32(vec_elements_flt.val[3])); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 327 | } |
| 328 | else |
| 329 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 330 | vec_elements_flt.val[0] = vexpq_f32(vmulq_f32(vec_elements_flt.val[0], scale_beta_vec)); |
| 331 | vec_elements_flt.val[1] = vexpq_f32(vmulq_f32(vec_elements_flt.val[1], scale_beta_vec)); |
| 332 | vec_elements_flt.val[2] = vexpq_f32(vmulq_f32(vec_elements_flt.val[2], scale_beta_vec)); |
| 333 | vec_elements_flt.val[3] = vexpq_f32(vmulq_f32(vec_elements_flt.val[3], scale_beta_vec)); |
| 334 | vec_sum.val[0] = vaddq_f32(vec_sum.val[0], vec_elements_flt.val[0]); |
| 335 | vec_sum.val[1] = vaddq_f32(vec_sum.val[1], vec_elements_flt.val[1]); |
| 336 | vec_sum.val[2] = vaddq_f32(vec_sum.val[2], vec_elements_flt.val[2]); |
| 337 | vec_sum.val[3] = vaddq_f32(vec_sum.val[3], vec_elements_flt.val[3]); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 338 | } |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 339 | |
| 340 | vst4q_f32(tmp_ptr + x, vec_elements_flt); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 341 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 342 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 343 | /* Reduce sum */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 344 | const auto sum_16_byte = vaddq_f32(vaddq_f32(vec_sum.val[0], vec_sum.val[1]), vaddq_f32(vec_sum.val[2], vec_sum.val[3])); |
| 345 | auto sum_res = vpadd_f32(vget_high_f32(sum_16_byte), vget_low_f32(sum_16_byte)); |
| 346 | sum_res = vpadd_f32(sum_res, sum_res); |
| 347 | sum = wrapper::vgetlane(sum_res, 0); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 348 | |
| 349 | /* Run remaining elements */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 350 | for(; x < input_width; ++x) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 351 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 352 | float element{}; |
| 353 | if(is_log) |
| 354 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 355 | element = (max_val - in_ptr[x]) * scale_beta; |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 356 | sum += std::exp(element); |
| 357 | } |
| 358 | else |
| 359 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 360 | element = std::exp((max_val - in_ptr[x]) * scale_beta); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 361 | sum += element; |
| 362 | } |
| 363 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 364 | tmp_ptr[x] = element; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 367 | if(!is_log) |
| 368 | { |
| 369 | sum_inversed = 256.f / sum; |
| 370 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* Normalize exponentials */ |
| 374 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 375 | constexpr bool is_qasymm8_signed = std::is_same<T, qasymm8_signed_t>::value; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 376 | /* Loop over row and compute softmax */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 377 | int x = 0; |
| 378 | for(; x <= (input_width - vec_size); x += vec_size) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 379 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 380 | using int_vec_type = wrapper::traits::neon_vector_t<T, 16>; |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 381 | float32x4x4_t vec_in = vld4q_f32(tmp_ptr + x); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 382 | int_vec_type normalized_value{}; |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 383 | if(is_log) |
| 384 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 385 | const float32x4x4_t sub = |
| 386 | { |
| 387 | vsubq_f32(vec_in.val[0], vdupq_n_f32(sum)), |
| 388 | vsubq_f32(vec_in.val[1], vdupq_n_f32(sum)), |
| 389 | vsubq_f32(vec_in.val[2], vdupq_n_f32(sum)), |
| 390 | vsubq_f32(vec_in.val[3], vdupq_n_f32(sum)), |
| 391 | }; |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 392 | normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(sub); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 393 | } |
| 394 | else |
| 395 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 396 | float32x4x4_t mul = |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 397 | { |
| 398 | vmulq_f32(vec_in.val[0], vdupq_n_f32(sum_inversed)), |
| 399 | vmulq_f32(vec_in.val[1], vdupq_n_f32(sum_inversed)), |
| 400 | vmulq_f32(vec_in.val[2], vdupq_n_f32(sum_inversed)), |
| 401 | vmulq_f32(vec_in.val[3], vdupq_n_f32(sum_inversed)), |
| 402 | }; |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 403 | |
| 404 | if(is_qasymm8_signed) |
| 405 | { |
| 406 | const auto offset_vec = wrapper::vdup_n(128.f, wrapper::traits::vector_128_tag{}); |
| 407 | mul.val[0] = wrapper::vsub(mul.val[0], offset_vec); |
| 408 | mul.val[1] = wrapper::vsub(mul.val[1], offset_vec); |
| 409 | mul.val[2] = wrapper::vsub(mul.val[2], offset_vec); |
| 410 | mul.val[3] = wrapper::vsub(mul.val[3], offset_vec); |
| 411 | } |
| 412 | |
| 413 | normalized_value = convert_float_to_int<float32x4x4_t, int_vec_type>(mul); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 414 | } |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 415 | wrapper::vstore(out_ptr + x, normalized_value); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 416 | } |
| 417 | /* Run remaining elements */ |
| 418 | for(; x < input_width; ++x) |
| 419 | { |
| 420 | if(is_log) |
| 421 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 422 | out_ptr[x] = utils::cast::saturate_cast<T>(tmp_ptr[x] - sum); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 423 | } |
| 424 | else |
| 425 | { |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 426 | out_ptr[x] = utils::cast::saturate_cast<T>((tmp_ptr[x] * sum_inversed) - (is_qasymm8_signed ? 128.f : 0)); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 427 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | }, |
| 431 | in_it, max_it, out_it); |
| 432 | } |
| 433 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 434 | template <typename T, bool is_log = false> |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 435 | void logits_1d_softmax_float(const ITensor &in, const ITensor &max, void *const tmp, |
| 436 | ITensor &out, const float beta, const Window &window) |
| 437 | { |
| 438 | const int start_x = in.info()->valid_region().anchor.x(); |
| 439 | const int input_width = in.info()->valid_region().shape.x(); |
| 440 | |
| 441 | Iterator in_it(&in, window); |
| 442 | Iterator max_it(&max, window); |
| 443 | Iterator out_it(&out, window); |
| 444 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 445 | /** NEON vector tag type. */ |
| 446 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 447 | |
| 448 | constexpr int vec_size = 16 / sizeof(T); |
| 449 | const int sum_stages = log2(vec_size / 2); |
| 450 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 451 | execute_window_loop(window, [&](const Coordinates &) |
| 452 | { |
| 453 | /* Get pointers */ |
| 454 | const auto in_ptr = reinterpret_cast<const T *>(in_it.ptr()) + start_x; |
| 455 | const auto out_ptr = reinterpret_cast<T *>(out_it.ptr()) + start_x; |
| 456 | const auto tmp_ptr = reinterpret_cast<T *>(tmp); |
| 457 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 458 | T sum{}; |
| 459 | T sum_inversed{}; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 460 | |
| 461 | /* Compute exponentials and sum */ |
| 462 | { |
| 463 | /* Get max value */ |
| 464 | const auto max_val = *reinterpret_cast<const T *>(max_it.ptr()); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 465 | const auto vec_max = wrapper::vdup_n(max_val, ExactTagType{}); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 466 | |
| 467 | /* Init sum to zero */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 468 | auto vec_sum = wrapper::vdup_n(static_cast<T>(0), ExactTagType{}); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 469 | |
| 470 | /* Loop over row and compute exponentials and sum */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 471 | int x = 0; |
| 472 | for(; x <= (input_width - vec_size); x += vec_size) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 473 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 474 | auto vec_elements = wrapper::vloadq(in_ptr + x); |
| 475 | vec_elements = wrapper::vsub(vec_elements, vec_max); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 476 | if(is_log) |
| 477 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 478 | vec_elements = wrapper::vmul(vec_elements, wrapper::vdup_n(static_cast<T>(beta), ExactTagType{})); |
| 479 | vec_sum = wrapper::vadd(vec_sum, wrapper::vexpq(vec_elements)); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 480 | } |
| 481 | else |
| 482 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 483 | vec_elements = wrapper::vexpq(wrapper::vmul(vec_elements, wrapper::vdup_n(static_cast<T>(beta), ExactTagType{}))); |
| 484 | vec_sum = wrapper::vadd(vec_sum, vec_elements); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 485 | } |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 486 | wrapper::vstore(tmp_ptr + x, vec_elements); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 487 | } |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 488 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 489 | /* Reduce sum */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 490 | auto sum_res = wrapper::vpadd(wrapper::vgethigh(vec_sum), wrapper::vgetlow(vec_sum)); |
| 491 | for(int i = 0; i < sum_stages; ++i) |
| 492 | { |
| 493 | sum_res = wrapper::vpadd(sum_res, sum_res); |
| 494 | } |
| 495 | sum = wrapper::vgetlane(sum_res, 0); |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 496 | |
| 497 | /* Run remaining elements */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 498 | for(; x < input_width; ++x) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 499 | { |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 500 | T element{}; |
| 501 | |
| 502 | if(is_log) |
| 503 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 504 | element = (in_ptr[x] - max_val) * beta; |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 505 | sum += std::exp(element); |
| 506 | } |
| 507 | else |
| 508 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 509 | element = std::exp((in_ptr[x] - max_val) * beta); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 510 | sum += element; |
| 511 | } |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 512 | tmp_ptr[x] = element; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 515 | if(!is_log) |
| 516 | { |
| 517 | sum_inversed = T(1) / sum; |
| 518 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* Normalize exponentials */ |
| 522 | { |
| 523 | /* Loop over row and compute softmax */ |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 524 | int x = 0; |
| 525 | for(; x <= (input_width - vec_size); x += vec_size) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 526 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 527 | auto vec_in = wrapper::vloadq(tmp_ptr + x); |
| 528 | auto normalized_value = wrapper::vdup_n(static_cast<T>(0), ExactTagType{}); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 529 | if(is_log) |
| 530 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 531 | normalized_value = wrapper::vsub(vec_in, wrapper::vdup_n(static_cast<T>(sum), ExactTagType{})); |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 532 | } |
| 533 | else |
| 534 | { |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 535 | normalized_value = wrapper::vmul(vec_in, wrapper::vdup_n(static_cast<T>(sum_inversed), ExactTagType{})); |
| 536 | } |
| 537 | wrapper::vstore(out_ptr + x, normalized_value); |
| 538 | } |
| 539 | /* Run remaining elements */ |
| 540 | for(; x < input_width; ++x) |
| 541 | { |
| 542 | if(is_log) |
| 543 | { |
| 544 | out_ptr[x] = tmp_ptr[x] - sum; |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | out_ptr[x] = tmp_ptr[x] * sum_inversed; |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 549 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | }, |
| 553 | in_it, max_it, out_it); |
| 554 | } |
| 555 | } // namespace |
| 556 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 557 | template <bool IS_LOG> |
| 558 | NELogits1DSoftmaxKernel<IS_LOG>::NELogits1DSoftmaxKernel() |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 559 | : _func(nullptr), _input(nullptr), _max(nullptr), _output(nullptr), _beta(1.0f), _tmp(nullptr) |
| 560 | { |
| 561 | } |
| 562 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 563 | template <bool IS_LOG> |
| 564 | void NELogits1DSoftmaxKernel<IS_LOG>::configure(const ITensor *input, const ITensor *max, ITensor *output, const float beta, ITensor *tmp) |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 565 | { |
| 566 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, output, tmp); |
| 567 | ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), max->info(), output->info(), tmp->info()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 568 | // Perform validation step |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 569 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_logits_softmax(*input->info(), *max->info(), *output->info(), beta, *tmp->info(), IS_LOG)); |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 570 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 571 | // Configure kernel window |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 572 | const bool is_quantized_asymmetric = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 573 | |
| 574 | // Output auto initialization if not yet initialized |
| 575 | const QuantizationInfo output_quantization = is_quantized_asymmetric ? arm_compute::get_softmax_output_quantization_info(input->info()->data_type(), IS_LOG) : output->info()->quantization_info(); |
| 576 | auto_init_if_empty(*output->info(), TensorInfo(*input->info()).set_quantization_info(output_quantization).reset_padding()); |
| 577 | |
| 578 | // Tmp auto initialization if not yet initialized |
| 579 | const DataType tmp_data_type = is_quantized_asymmetric ? DataType::F32 : input->info()->data_type(); |
| 580 | auto_init_if_empty(*tmp->info(), TensorInfo(*input->info()).set_data_type(tmp_data_type).reset_padding()); |
| 581 | |
| 582 | // Configure kernel window |
| 583 | Window win = calculate_max_window(*max->info(), Steps()); |
| 584 | Coordinates coord; |
| 585 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 586 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 587 | |
| 588 | switch(input->info()->data_type()) |
| 589 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 590 | case DataType::QASYMM8: |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 591 | _func = &logits_1d_softmax_qasymm8<qasymm8_t, IS_LOG>; |
| 592 | break; |
| 593 | case DataType::QASYMM8_SIGNED: |
| 594 | _func = &logits_1d_softmax_qasymm8<qasymm8_signed_t, IS_LOG>; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 595 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 596 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 597 | case DataType::F16: |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 598 | _func = &logits_1d_softmax_float<float16_t, IS_LOG>; |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 599 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 600 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 601 | case DataType::F32: |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 602 | _func = &logits_1d_softmax_float<float, IS_LOG>; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 603 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 604 | default: |
| 605 | ARM_COMPUTE_ERROR("Unsupported data type."); |
Pablo Tello | b49a715 | 2017-07-11 16:31:35 +0100 | [diff] [blame] | 606 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | _input = input; |
| 610 | _max = max; |
| 611 | _output = output; |
Pablo Palmier | a2b89ca | 2017-10-05 15:01:34 +0100 | [diff] [blame] | 612 | _beta = beta; |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 613 | _tmp = tmp; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 614 | |
Michalis Spyrou | 2dc7e40 | 2020-02-28 14:41:35 +0000 | [diff] [blame] | 615 | INEKernel::configure(win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 616 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 617 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 618 | template <bool IS_LOG> |
| 619 | Status NELogits1DSoftmaxKernel<IS_LOG>::validate(const ITensorInfo *input, const ITensorInfo *max, |
| 620 | const ITensorInfo *output, const float beta, const ITensorInfo *tmp) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 621 | { |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 622 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, max, output, tmp); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 623 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_logits_softmax(*input, *max, *output, beta, *tmp, IS_LOG)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 624 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 625 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 628 | template <bool IS_LOG> |
| 629 | void NELogits1DSoftmaxKernel<IS_LOG>::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 630 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 631 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 632 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 633 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 634 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 635 | const unsigned int num_elems_processed_per_iteration = _input->info()->valid_region().shape.x(); |
| 636 | const unsigned int tmp_size_for_thread = _tmp->info()->element_size() * num_elems_processed_per_iteration; |
| 637 | |
| 638 | ARM_COMPUTE_ERROR_ON(_tmp->info()->total_size() < (info.num_threads * tmp_size_for_thread)); |
| 639 | |
| 640 | void *tmp_for_thread = _tmp->buffer() + (info.thread_id * tmp_size_for_thread); |
| 641 | |
| 642 | (*_func)(*_input, *_max, tmp_for_thread, *_output, _beta, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 643 | } |
| 644 | |
Sang-Hoon Park | d24affe | 2019-10-08 18:07:23 +0100 | [diff] [blame] | 645 | template class NELogits1DSoftmaxKernel<true>; |
| 646 | template class NELogits1DSoftmaxKernel<false>; |
| 647 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 648 | } // namespace arm_compute |