blob: c597afd8049b3ac52c6579de42fe573bbf710d92 [file] [log] [blame]
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +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 */
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 Loganathan338595b2019-06-28 14:09:53 +010029#include "arm_compute/core/NEON/NEAsymm.h"
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010030#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 Spyrou3eda16a2020-03-04 17:22:55 +000036namespace arm_compute
37{
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010038namespace
39{
40template <typename T>
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000041void batch_concat(const ITensor *in, ITensor *out, unsigned int batch_offset, const Window &window)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010042{
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 Spyrou3eda16a2020-03-04 17:22:55 +000049 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 Loganathan338595b2019-06-28 14:09:53 +010059
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 Spyrou3eda16a2020-03-04 17:22:55 +000065 execute_window_loop(win, [&](const Coordinates &)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010066 {
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 Spyrou3eda16a2020-03-04 17:22:55 +000069
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 Loganathan338595b2019-06-28 14:09:53 +010081 },
82 input, output);
83 }
Georgios Pinitas33843562019-12-10 13:33:18 +000084 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
85 {
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000086 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +000087 {
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 Spyrou3eda16a2020-03-04 17:22:55 +000090 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 Pinitas33843562019-12-10 13:33:18 +0000100 },
101 input, output);
102 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100103 else
104 {
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000105 execute_window_loop(win, [&](const Coordinates &)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100106 {
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 Spyrou3eda16a2020-03-04 17:22:55 +0000110 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 Loganathan338595b2019-06-28 14:09:53 +0100121 },
122 input, output);
123 }
124}
125
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100126Status 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 Pinitas33843562019-12-10 13:33:18 +0000130 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100131 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
143NEBatchConcatenateLayerKernel::NEBatchConcatenateLayerKernel()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100144 : _func(nullptr), _batch_offset(0)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100145{
146}
147
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100148void NEBatchConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int batch_offset, ITensorInfo *output)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100149{
150 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100151 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, batch_offset, output));
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100152
153 _func = nullptr;
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100154 _batch_offset = batch_offset;
155
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100156 switch(input->data_type())
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100157 {
158 case DataType::S8:
159 case DataType::U8:
160 case DataType::QASYMM8:
Georgios Pinitas33843562019-12-10 13:33:18 +0000161 case DataType::QASYMM8_SIGNED:
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100162 _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 Pinitas4667ddd2020-07-13 21:21:33 +0100179 Window win = calculate_max_window(*output, Steps());
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000180 Coordinates coord;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100181 coord.set_num_dimensions(output->num_dimensions());
182 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000183 INEKernel::configure(win);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100184}
185
186Status 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 Loganathan338595b2019-06-28 14:09:53 +0100191 return Status{};
192}
193
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100194void NEBatchConcatenateLayerKernel::run_op(const InputTensorMap &inputs, const OutputTensorMap &outputs,
195 const Window &window, const ThreadInfo &info)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100196{
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 Pinitas4667ddd2020-07-13 21:21:33 +0100202 (*_func)(inputs.at(TensorType::ACL_SRC), outputs.at(TensorType::ACL_DST), _batch_offset, window);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100203}
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000204} // namespace arm_compute