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