Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +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/NEWidthConcatenateLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/NEAsymm.h" |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <cstdint> |
| 38 | |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 46 | // 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] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0)); |
| 50 | |
| 51 | for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i) |
| 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i)); |
| 54 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 55 | |
| 56 | return Status{}; |
| 57 | } |
| 58 | } // namespace |
| 59 | |
| 60 | NEWidthConcatenateLayerKernel::NEWidthConcatenateLayerKernel() |
| 61 | : _input(nullptr), _output(nullptr), _width_offset(0) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void NEWidthConcatenateLayerKernel::configure(const ITensor *input, unsigned int width_offset, ITensor *output) |
| 66 | { |
| 67 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 68 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), width_offset, output->info())); |
| 69 | |
| 70 | _input = input; |
| 71 | _output = output; |
| 72 | _width_offset = width_offset; |
| 73 | |
| 74 | // Configure kernel window |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 75 | Window win = calculate_max_window(*input->info(), Steps()); |
| 76 | Coordinates coord; |
| 77 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 78 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 79 | |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 80 | INEKernel::configure(win); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Status NEWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output) |
| 84 | { |
| 85 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output)); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 86 | return Status{}; |
| 87 | } |
| 88 | |
| 89 | void NEWidthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 90 | { |
| 91 | ARM_COMPUTE_UNUSED(info); |
| 92 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 93 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 94 | |
| 95 | // Offset output pointer to the correct position |
| 96 | uint8_t *output_ptr = _output->buffer() + _output->info()->offset_first_element_in_bytes() + _width_offset * _output->info()->strides_in_bytes()[0]; |
| 97 | |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 98 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 99 | const auto window_end_x = static_cast<int>(window.x().end()) * static_cast<int>(_output->info()->element_size()); |
| 100 | constexpr int window_step_x = 16; |
| 101 | |
| 102 | Window win{ window }; |
| 103 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 104 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 105 | // Create iterators |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 106 | Iterator input(_input, win); |
| 107 | Iterator output(_output, win); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 108 | const DataType dt = _input->info()->data_type(); |
| 109 | const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform(); |
| 110 | const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform(); |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 111 | if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 112 | { |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 113 | execute_window_loop(win, [&](const Coordinates &) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 114 | { |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 115 | int x = window_start_x; |
| 116 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 117 | { |
| 118 | vst1q_u8(output_ptr + output.offset() + x, vquantize(vdequantize(vld1q_u8(input.ptr() + x), input_qinfo), output_qinfo)); |
| 119 | } |
| 120 | |
| 121 | // Compute left-over elements |
| 122 | for(; x < window_end_x; ++x) |
| 123 | { |
| 124 | *(output_ptr + output.offset() + x) = quantize_qasymm8(dequantize_qasymm8(*(input.ptr() + x), input_qinfo), output_qinfo); |
| 125 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 126 | }, |
| 127 | input, output); |
| 128 | } |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 129 | else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo) |
| 130 | { |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 131 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 132 | { |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 133 | int x = window_start_x; |
| 134 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 135 | { |
| 136 | vst1q_s8(reinterpret_cast<int8_t *>(output_ptr + output.offset() + x), |
| 137 | vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(input.ptr() + x)), input_qinfo), output_qinfo)); |
| 138 | } |
| 139 | |
| 140 | // Compute left-over elements |
| 141 | for(; x < window_end_x; ++x) |
| 142 | { |
| 143 | *(output_ptr + output.offset() + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(input.ptr() + x), input_qinfo), output_qinfo); |
| 144 | } |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 145 | }, |
| 146 | input, output); |
| 147 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 148 | else |
| 149 | { |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 150 | execute_window_loop(win, [&](const Coordinates &) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 151 | { |
| 152 | const auto in_ptr = input.ptr(); |
| 153 | const auto out_ptr = output_ptr + output.offset(); |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 154 | int x = window_start_x; |
| 155 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 156 | { |
| 157 | wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x)); |
| 158 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 159 | |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 160 | // Compute left-over elements |
| 161 | for(; x < window_end_x; ++x) |
| 162 | { |
| 163 | *(out_ptr + x) = *(in_ptr + x); |
| 164 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 165 | }, |
| 166 | input, output); |
| 167 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 168 | } |
Michalis Spyrou | 16a70e0 | 2020-03-04 15:57:04 +0000 | [diff] [blame^] | 169 | } // namespace arm_compute |