blob: ba90bfcd4fd6632f8dd145c98c6f7bd8e709cc63 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010034#include "src/core/NEON/NEAsymm.h"
35#include "src/core/NEON/NEFixedPoint.h"
36#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Georgios Pinitasac4e8732017-07-05 17:02:25 +010040#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
Michalis Spyrouc28d4282020-03-04 15:30:41 +000042namespace arm_compute
43{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010044namespace
45{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010046template <typename T>
Michalis Spyrouc28d4282020-03-04 15:30:41 +000047void depth_concat(const ITensor *in, ITensor *out, unsigned int depth_offset, const Window &window)
Georgios Pinitasac4e8732017-07-05 17:02:25 +010048{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010049 // Offset input
Michalis Spyroua9c44722019-04-05 17:18:36 +010050 uint8_t *input_ptr = in->buffer() + in->info()->offset_first_element_in_bytes();
Georgios Pinitasac4e8732017-07-05 17:02:25 +010051
52 // Offset output
Michalis Spyroua9c44722019-04-05 17:18:36 +010053 uint8_t *output_ptr = out->buffer() + out->info()->offset_first_element_in_bytes() + depth_offset * out->info()->strides_in_bytes()[2];
Georgios Pinitasac4e8732017-07-05 17:02:25 +010054
Michalis Spyrouc28d4282020-03-04 15:30:41 +000055 const auto window_start_x = static_cast<int>(window.x().start());
56 const auto window_end_x = static_cast<int>(window.x().end());
57 const int window_step_x = 16 / out->info()->element_size();
58
59 Window win{ window };
60 win.set(Window::DimX, Window::Dimension(0, 1, 1));
61 win.set(Window::DimZ, Window::Dimension(0, in->info()->tensor_shape().z(), 1));
62
63 Iterator input(in, win);
64 Iterator output(out, win);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010065
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010066 const DataType dt = in->info()->data_type();
67 const UniformQuantizationInfo input_qinfo = in->info()->quantization_info().uniform();
68 const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform();
Pablo Tello54e98d92019-02-05 16:16:19 +000069 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
Georgios Pinitasac4e8732017-07-05 17:02:25 +010070 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +000071 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +000072 {
73 const auto in_ptr = reinterpret_cast<const uint8_t *>(input_ptr + input.offset());
74 const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +000075 int x = window_start_x;
76 for(; x <= (window_end_x - window_step_x); x += window_step_x)
77 {
78 wrapper::vstore(out_ptr + x, vquantize(vdequantize(wrapper::vloadq(in_ptr + x), input_qinfo), output_qinfo));
79 }
80
81 // Compute left-over elements
82 for(; x < window_end_x; ++x)
83 {
84 *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), input_qinfo), output_qinfo);
85 }
Pablo Tello54e98d92019-02-05 16:16:19 +000086 },
87 input, output);
88 }
Georgios Pinitas33843562019-12-10 13:33:18 +000089 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
90 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +000091 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +000092 {
93 const auto in_ptr = reinterpret_cast<const int8_t *>(input_ptr + input.offset());
94 const auto out_ptr = reinterpret_cast<int8_t *>(output_ptr + output.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +000095 int x = window_start_x;
96 for(; x <= (window_end_x - window_step_x); x += window_step_x)
97 {
98 wrapper::vstore(out_ptr + x, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr + x), input_qinfo), output_qinfo));
99 }
100
101 // Compute left-over elements
102 for(; x < window_end_x; ++x)
103 {
104 *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), input_qinfo), output_qinfo);
105 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000106 },
107 input, output);
108 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000109 else
110 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000111 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000112 {
113 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
114 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000115 int x = window_start_x;
116 for(; x <= (window_end_x - window_step_x); x += window_step_x)
117 {
118 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
119 }
120 // Compute left-over elements
121 for(; x < window_end_x; ++x)
122 {
123 *(out_ptr + x) = *(in_ptr + x);
124 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000125 },
126 input, output);
127 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100128}
Georgios Pinitasae54e022018-07-16 15:41:27 +0100129
Georgios Pinitasae54e022018-07-16 15:41:27 +0100130Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
131{
132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100133 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitas33843562019-12-10 13:33:18 +0000134 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100135 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
136
Michalis Spyroua9c44722019-04-05 17:18:36 +0100137 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
138 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100139 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100140 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
141
Georgios Pinitasae54e022018-07-16 15:41:27 +0100142 return Status{};
143}
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100144} // namespace
145
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000146NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100147 : _func(nullptr), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148{
149}
150
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100151void NEDepthConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int depth_offset, ITensorInfo *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152{
Georgios Pinitasae54e022018-07-16 15:41:27 +0100153 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100154 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, depth_offset, output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100156 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 _depth_offset = depth_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100159 switch(input->data_type())
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100160 {
Georgios Pinitasae54e022018-07-16 15:41:27 +0100161 case DataType::QASYMM8:
162 _func = &depth_concat<uint8_t>;
163 break;
Georgios Pinitas33843562019-12-10 13:33:18 +0000164 case DataType::QASYMM8_SIGNED:
165 _func = &depth_concat<int8_t>;
166 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100167 case DataType::F16:
168 _func = &depth_concat<uint16_t>;
169 break;
170 case DataType::F32:
171 _func = &depth_concat<uint32_t>;
172 break;
173 default:
174 ARM_COMPUTE_ERROR("Unsupported data type.");
175 }
176
Georgios Pinitasae54e022018-07-16 15:41:27 +0100177 // Configure kernel window
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100178 Window win = calculate_max_window(*output, Steps());
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000179 Coordinates coord;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100180 coord.set_num_dimensions(output->num_dimensions());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100182 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000183 INEKernel::configure(win);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100184}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185
Georgios Pinitasae54e022018-07-16 15:41:27 +0100186Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
187 unsigned int depth_offset,
188 const arm_compute::ITensorInfo *output)
189{
190 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100191 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192}
193
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100194void NEDepthConcatenateLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100196 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
198 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100199 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100201 (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC),
202 tensors.get_tensor(TensorType::ACL_DST),
203 _depth_offset,
204 window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205}
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000206} // namespace arm_compute