blob: cfa98fb19a269cae70c56ac35e3d06f9088c4ff7 [file] [log] [blame]
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +01001/*
2 * Copyright (c) 2019 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 "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
40using namespace arm_compute;
41
42namespace
43{
44template <typename T>
45void batch_concat(const ITensor *in, ITensor *out, int batch_offset, const Window &window)
46{
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
53 Iterator input(in, window);
54 Iterator output(out, window);
55
56 const DataType dt = in->info()->data_type();
57 const UniformQuantizationInfo input_qinfo = in->info()->quantization_info().uniform();
58 const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform();
59 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
60 {
61 execute_window_loop(window, [&](const Coordinates &)
62 {
63 const auto in_ptr = reinterpret_cast<const uint8_t *>(input_ptr + input.offset());
64 const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset());
65 vst1q_u8(out_ptr, vquantize(vdequantize(vld1q_u8(in_ptr), input_qinfo), output_qinfo));
66 },
67 input, output);
68 }
69 else
70 {
71 execute_window_loop(window, [&](const Coordinates &)
72 {
73 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
74 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
75
76 wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr));
77 },
78 input, output);
79 }
80}
81
82std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int batch_offset, ITensorInfo *output)
83{
84 ARM_COMPUTE_UNUSED(batch_offset);
85
86 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
87
88 // The window needs to be based on input as we copy all the batchs of input
89 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
90 win.set(3, Window::Dimension(0, input->tensor_shape()[3], 1));
91
92 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
93 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
94 bool window_changed = update_window_and_padding(win, input_access, output_access);
95 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
96
97 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
98 return std::make_pair(err, win);
99}
100
101Status validate_arguments(const ITensorInfo *input, unsigned int batch_offset, const ITensorInfo *output)
102{
103 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
104 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
105 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
106 DataType::U16, DataType::S16,
107 DataType::U32, DataType::S32,
108 DataType::F16, DataType::F32);
109 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
110
111 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
112 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
113 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimZ) != output->dimension(Window::DimZ));
114 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) + batch_offset > output->dimension(3));
115 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, input, output);
116
117 return Status{};
118}
119} // namespace
120
121NEBatchConcatenateLayerKernel::NEBatchConcatenateLayerKernel()
122 : _func(nullptr), _input(nullptr), _output(nullptr), _batch_offset(0)
123{
124}
125
126void NEBatchConcatenateLayerKernel::configure(const ITensor *input, unsigned int batch_offset, ITensor *output)
127{
128 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
129 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), batch_offset, output->info()));
130
131 _func = nullptr;
132 _input = input;
133 _output = output;
134 _batch_offset = batch_offset;
135
136 switch(input->info()->data_type())
137 {
138 case DataType::S8:
139 case DataType::U8:
140 case DataType::QASYMM8:
141 _func = &batch_concat<uint8_t>;
142 break;
143 case DataType::S16:
144 case DataType::U16:
145 case DataType::F16:
146 _func = &batch_concat<uint16_t>;
147 break;
148 case DataType::S32:
149 case DataType::U32:
150 case DataType::F32:
151 _func = &batch_concat<uint32_t>;
152 break;
153 default:
154 ARM_COMPUTE_ERROR("Unsupported data type.");
155 }
156
157 // Configure kernel window
158 auto win_config = validate_and_configure_window(input->info(), batch_offset, output->info());
159 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
160
161 INEKernel::configure(std::get<1>(win_config));
Isabella Gottardif59b16f2019-07-25 12:03:39 +0100162
163 // Set output valid region
164 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100165}
166
167Status NEBatchConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
168 unsigned int batch_offset,
169 const arm_compute::ITensorInfo *output)
170{
171 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, batch_offset, output));
172 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), batch_offset, output->clone().get()).first);
173 return Status{};
174}
175
176void NEBatchConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
177{
178 ARM_COMPUTE_UNUSED(info);
179 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
180 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
181 ARM_COMPUTE_ERROR_ON(_func == nullptr);
182
183 (*_func)(_input, _output, _batch_offset, window);
184}