Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +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/NEBatchConcatenateLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/NEAsymm.h" |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | template <typename T> |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 41 | void batch_concat(const ITensor *in, ITensor *out, unsigned int batch_offset, const Window &window) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 42 | { |
| 43 | // Offset input |
| 44 | uint8_t *input_ptr = in->buffer() + in->info()->offset_first_element_in_bytes(); |
| 45 | |
| 46 | // Offset output |
| 47 | uint8_t *output_ptr = out->buffer() + out->info()->offset_first_element_in_bytes() + batch_offset * out->info()->strides_in_bytes()[3]; |
| 48 | |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 49 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 50 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 51 | const int window_step_x = 16 / out->info()->element_size(); |
| 52 | |
| 53 | Window win{ window }; |
| 54 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 55 | win.set(3, Window::Dimension(0, in->info()->tensor_shape()[3], 1)); |
| 56 | |
| 57 | Iterator input(in, win); |
| 58 | Iterator output(out, win); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 59 | |
| 60 | const DataType dt = in->info()->data_type(); |
| 61 | const UniformQuantizationInfo input_qinfo = in->info()->quantization_info().uniform(); |
| 62 | const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform(); |
| 63 | if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo) |
| 64 | { |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 65 | execute_window_loop(win, [&](const Coordinates &) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 66 | { |
| 67 | const auto in_ptr = reinterpret_cast<const uint8_t *>(input_ptr + input.offset()); |
| 68 | const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset()); |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 69 | |
| 70 | int x = window_start_x; |
| 71 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 72 | { |
| 73 | wrapper::vstore(out_ptr, vquantize(vdequantize(wrapper::vloadq(in_ptr), input_qinfo), output_qinfo)); |
| 74 | } |
| 75 | |
| 76 | // Compute left-over elements |
| 77 | for(; x < window_end_x; ++x) |
| 78 | { |
| 79 | *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), input_qinfo), output_qinfo); |
| 80 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 81 | }, |
| 82 | input, output); |
| 83 | } |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 84 | else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo) |
| 85 | { |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 86 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 87 | { |
| 88 | const auto in_ptr = reinterpret_cast<const int8_t *>(input_ptr + input.offset()); |
| 89 | const auto out_ptr = reinterpret_cast<int8_t *>(output_ptr + output.offset()); |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 90 | int x = window_start_x; |
| 91 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 92 | { |
| 93 | wrapper::vstore(out_ptr, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr), input_qinfo), output_qinfo)); |
| 94 | } |
| 95 | // Compute left-over elements |
| 96 | for(; x < window_end_x; ++x) |
| 97 | { |
| 98 | *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), input_qinfo), output_qinfo); |
| 99 | } |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 100 | }, |
| 101 | input, output); |
| 102 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 103 | else |
| 104 | { |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 105 | execute_window_loop(win, [&](const Coordinates &) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 106 | { |
| 107 | const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset()); |
| 108 | const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset()); |
| 109 | |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 110 | int x = window_start_x; |
| 111 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 112 | { |
| 113 | wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x)); |
| 114 | } |
| 115 | |
| 116 | // Compute left-over elements |
| 117 | for(; x < window_end_x; ++x) |
| 118 | { |
| 119 | *(out_ptr + x) = *(in_ptr + x); |
| 120 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 121 | }, |
| 122 | input, output); |
| 123 | } |
| 124 | } |
| 125 | |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 126 | Status validate_arguments(const ITensorInfo *input, unsigned int batch_offset, const ITensorInfo *output) |
| 127 | { |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 129 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions. |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 130 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 131 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 132 | |
| 133 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX)); |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY)); |
| 135 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimZ) != output->dimension(Window::DimZ)); |
| 136 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) + batch_offset > output->dimension(3)); |
| 137 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, input, output); |
| 138 | |
| 139 | return Status{}; |
| 140 | } |
| 141 | } // namespace |
| 142 | |
| 143 | NEBatchConcatenateLayerKernel::NEBatchConcatenateLayerKernel() |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 144 | : _func(nullptr), _batch_offset(0) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 145 | { |
| 146 | } |
| 147 | |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 148 | void NEBatchConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int batch_offset, ITensorInfo *output) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 149 | { |
| 150 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, batch_offset, output)); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 152 | |
| 153 | _func = nullptr; |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 154 | _batch_offset = batch_offset; |
| 155 | |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 156 | switch(input->data_type()) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 157 | { |
| 158 | case DataType::S8: |
| 159 | case DataType::U8: |
| 160 | case DataType::QASYMM8: |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 161 | case DataType::QASYMM8_SIGNED: |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 162 | _func = &batch_concat<uint8_t>; |
| 163 | break; |
| 164 | case DataType::S16: |
| 165 | case DataType::U16: |
| 166 | case DataType::F16: |
| 167 | _func = &batch_concat<uint16_t>; |
| 168 | break; |
| 169 | case DataType::S32: |
| 170 | case DataType::U32: |
| 171 | case DataType::F32: |
| 172 | _func = &batch_concat<uint32_t>; |
| 173 | break; |
| 174 | default: |
| 175 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 176 | } |
| 177 | |
| 178 | // Configure kernel window |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 179 | Window win = calculate_max_window(*output, Steps()); |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 180 | Coordinates coord; |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 181 | coord.set_num_dimensions(output->num_dimensions()); |
| 182 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 183 | INEKernel::configure(win); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | Status NEBatchConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input, |
| 187 | unsigned int batch_offset, |
| 188 | const arm_compute::ITensorInfo *output) |
| 189 | { |
| 190 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, batch_offset, output)); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 191 | return Status{}; |
| 192 | } |
| 193 | |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 194 | void NEBatchConcatenateLayerKernel::run_op(const InputTensorMap &inputs, const OutputTensorMap &outputs, |
| 195 | const Window &window, const ThreadInfo &info) |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 196 | { |
| 197 | ARM_COMPUTE_UNUSED(info); |
| 198 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 199 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 200 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 201 | |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 202 | (*_func)(inputs.at(TensorType::ACL_SRC), outputs.at(TensorType::ACL_DST), _batch_offset, window); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 203 | } |
Michalis Spyrou | 3eda16a | 2020-03-04 17:22:55 +0000 | [diff] [blame] | 204 | } // namespace arm_compute |