Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +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/NEDequantizationLayerKernel.h" |
| 25 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/NEAsymm.h" |
| 31 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 43 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 46 | |
| 47 | if(output->tensor_shape().total_size() > 0) |
| 48 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(output); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 52 | } |
| 53 | |
| 54 | return Status{}; |
| 55 | } |
| 56 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 57 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 58 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 59 | // Configure kernel window |
| 60 | Window win = calculate_max_window(*input, Steps()); |
| 61 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 62 | // Output tensor auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 63 | auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 64 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 65 | // NEDequantizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
| 66 | Coordinates coord; |
| 67 | coord.set_num_dimensions(output->num_dimensions()); |
| 68 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 69 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 70 | return std::make_tuple(Status{}, win); |
| 71 | } |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 72 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 73 | template <typename T> |
| 74 | inline void store_result(T *ptr, const float32x4x4_t &v) |
| 75 | { |
| 76 | ARM_COMPUTE_UNUSED(ptr, v); |
| 77 | } |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 78 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 79 | template <> |
| 80 | inline void store_result<float>(float *ptr, const float32x4x4_t &v) |
| 81 | { |
| 82 | wrapper::vstore(ptr, v.val[0]); |
| 83 | wrapper::vstore(ptr + 4, v.val[1]); |
| 84 | wrapper::vstore(ptr + 8, v.val[2]); |
| 85 | wrapper::vstore(ptr + 12, v.val[3]); |
| 86 | } |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 87 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 88 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 89 | template <> |
| 90 | inline void store_result<float16_t>(float16_t *ptr, const float32x4x4_t &v) |
| 91 | { |
| 92 | wrapper::vstore(ptr, vcombine_f16(vcvt_f16_f32(v.val[0]), vcvt_f16_f32(v.val[1]))); |
| 93 | wrapper::vstore(ptr + 8, vcombine_f16(vcvt_f16_f32(v.val[2]), vcvt_f16_f32(v.val[3]))); |
| 94 | } |
| 95 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 96 | |
| 97 | template <typename T> |
| 98 | void run_dequantization(const ITensor *input, ITensor *output, const Window &window) |
| 99 | { |
| 100 | const QuantizationInfo &qinfo = input->info()->quantization_info(); |
| 101 | |
| 102 | const int window_step_x = 16; |
| 103 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 104 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 105 | |
| 106 | // Collapse window and reset first dimension to handle tail calculations manually |
| 107 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 108 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 109 | |
| 110 | // Create iterators |
| 111 | Iterator in(input, win_collapsed); |
| 112 | Iterator out(output, win_collapsed); |
| 113 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 114 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 115 | { |
| 116 | const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr()); |
| 117 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 118 | |
| 119 | int x = window_start_x; |
| 120 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 121 | { |
| 122 | const auto vin = wrapper::vloadq(in_ptr + x); |
| 123 | const auto vdeq = vdequantize(vin, qinfo); |
| 124 | |
| 125 | store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq); |
| 126 | } |
| 127 | |
| 128 | // Compute left-over elements |
| 129 | for(; x < window_end_x; ++x) |
| 130 | { |
| 131 | uint8_t val = *(in_ptr + x); |
| 132 | *(out_ptr + x) = static_cast<T>(qinfo.dequantize(val)); |
| 133 | } |
| 134 | }, |
| 135 | in, out); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 136 | } |
| 137 | } // namespace |
| 138 | |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 139 | NEDequantizationLayerKernel::NEDequantizationLayerKernel() |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 140 | : _input(nullptr), _output(nullptr) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 141 | { |
| 142 | } |
| 143 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 144 | void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 145 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 146 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 147 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 148 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 149 | _input = input; |
| 150 | _output = output; |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 151 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 152 | // Configure kernel window |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 153 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 154 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 156 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 157 | INEKernel::configure(std::get<1>(win_config)); |
| 158 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 159 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 160 | Status NEDequantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 161 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 162 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 163 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get()))); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 164 | return Status{}; |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 167 | void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 168 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 169 | ARM_COMPUTE_UNUSED(info); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 170 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 171 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 172 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 173 | switch(_output->info()->data_type()) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 174 | { |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 175 | case DataType::F32: |
| 176 | run_dequantization<float>(_input, _output, window); |
| 177 | break; |
| 178 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 179 | case DataType::F16: |
| 180 | run_dequantization<float16_t>(_input, _output, window); |
| 181 | break; |
| 182 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 183 | default: |
| 184 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 185 | } |
| 186 | } |
| 187 | } // namespace arm_compute |