blob: 5650b810d2db473ea1c760d1c782000a7cc4ed60 [file] [log] [blame]
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +01001/*
Michalis Spyrou3eda16a2020-03-04 17:22:55 +00002 * 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"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/NEAsymm.h"
31#include "arm_compute/core/NEON/NEFixedPoint.h"
32#include "arm_compute/core/NEON/wrapper/wrapper.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <cstdint>
39
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000040namespace arm_compute
41{
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010042namespace
43{
44template <typename T>
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000045void batch_concat(const ITensor *in, ITensor *out, unsigned int batch_offset, const Window &window)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010046{
47 // Offset input
48 uint8_t *input_ptr = in->buffer() + in->info()->offset_first_element_in_bytes();
49
50 // Offset output
51 uint8_t *output_ptr = out->buffer() + out->info()->offset_first_element_in_bytes() + batch_offset * out->info()->strides_in_bytes()[3];
52
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000053 const auto window_start_x = static_cast<int>(window.x().start());
54 const auto window_end_x = static_cast<int>(window.x().end());
55 const int window_step_x = 16 / out->info()->element_size();
56
57 Window win{ window };
58 win.set(Window::DimX, Window::Dimension(0, 1, 1));
59 win.set(3, Window::Dimension(0, in->info()->tensor_shape()[3], 1));
60
61 Iterator input(in, win);
62 Iterator output(out, win);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010063
64 const DataType dt = in->info()->data_type();
65 const UniformQuantizationInfo input_qinfo = in->info()->quantization_info().uniform();
66 const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform();
67 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
68 {
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000069 execute_window_loop(win, [&](const Coordinates &)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010070 {
71 const auto in_ptr = reinterpret_cast<const uint8_t *>(input_ptr + input.offset());
72 const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset());
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000073
74 int x = window_start_x;
75 for(; x <= (window_end_x - window_step_x); x += window_step_x)
76 {
77 wrapper::vstore(out_ptr, vquantize(vdequantize(wrapper::vloadq(in_ptr), input_qinfo), output_qinfo));
78 }
79
80 // Compute left-over elements
81 for(; x < window_end_x; ++x)
82 {
83 *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), input_qinfo), output_qinfo);
84 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010085 },
86 input, output);
87 }
Georgios Pinitas33843562019-12-10 13:33:18 +000088 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
89 {
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000090 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +000091 {
92 const auto in_ptr = reinterpret_cast<const int8_t *>(input_ptr + input.offset());
93 const auto out_ptr = reinterpret_cast<int8_t *>(output_ptr + output.offset());
Michalis Spyrou3eda16a2020-03-04 17:22:55 +000094 int x = window_start_x;
95 for(; x <= (window_end_x - window_step_x); x += window_step_x)
96 {
97 wrapper::vstore(out_ptr, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr), input_qinfo), output_qinfo));
98 }
99 // Compute left-over elements
100 for(; x < window_end_x; ++x)
101 {
102 *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), input_qinfo), output_qinfo);
103 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000104 },
105 input, output);
106 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100107 else
108 {
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000109 execute_window_loop(win, [&](const Coordinates &)
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100110 {
111 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
112 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
113
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000114 int x = window_start_x;
115 for(; x <= (window_end_x - window_step_x); x += window_step_x)
116 {
117 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
118 }
119
120 // Compute left-over elements
121 for(; x < window_end_x; ++x)
122 {
123 *(out_ptr + x) = *(in_ptr + x);
124 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100125 },
126 input, output);
127 }
128}
129
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100130Status validate_arguments(const ITensorInfo *input, unsigned int batch_offset, const ITensorInfo *output)
131{
132 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
133 //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(input->data_type() == DataType::UNKNOWN);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100135 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
136
137 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));
139 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimZ) != output->dimension(Window::DimZ));
140 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) + batch_offset > output->dimension(3));
141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, input, output);
142
143 return Status{};
144}
145} // namespace
146
147NEBatchConcatenateLayerKernel::NEBatchConcatenateLayerKernel()
148 : _func(nullptr), _input(nullptr), _output(nullptr), _batch_offset(0)
149{
150}
151
152void NEBatchConcatenateLayerKernel::configure(const ITensor *input, unsigned int batch_offset, ITensor *output)
153{
154 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
155 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), batch_offset, output->info()));
156
157 _func = nullptr;
158 _input = input;
159 _output = output;
160 _batch_offset = batch_offset;
161
162 switch(input->info()->data_type())
163 {
164 case DataType::S8:
165 case DataType::U8:
166 case DataType::QASYMM8:
Georgios Pinitas33843562019-12-10 13:33:18 +0000167 case DataType::QASYMM8_SIGNED:
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100168 _func = &batch_concat<uint8_t>;
169 break;
170 case DataType::S16:
171 case DataType::U16:
172 case DataType::F16:
173 _func = &batch_concat<uint16_t>;
174 break;
175 case DataType::S32:
176 case DataType::U32:
177 case DataType::F32:
178 _func = &batch_concat<uint32_t>;
179 break;
180 default:
181 ARM_COMPUTE_ERROR("Unsupported data type.");
182 }
183
184 // Configure kernel window
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000185 Window win = calculate_max_window(*output->info(), Steps());
186 Coordinates coord;
187 coord.set_num_dimensions(output->info()->num_dimensions());
188 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
189 INEKernel::configure(win);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100190}
191
192Status NEBatchConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
193 unsigned int batch_offset,
194 const arm_compute::ITensorInfo *output)
195{
196 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, batch_offset, output));
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100197 return Status{};
198}
199
200void NEBatchConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
201{
202 ARM_COMPUTE_UNUSED(info);
203 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
204 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
205 ARM_COMPUTE_ERROR_ON(_func == nullptr);
206
207 (*_func)(_input, _output, _batch_offset, window);
208}
Michalis Spyrou3eda16a2020-03-04 17:22:55 +0000209} // namespace arm_compute