1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/cpu/kernels/CpuConcatenateDepthKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Error.h"
27*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
28*c217d954SCole Faust #include "arm_compute/core/ITensor.h"
29*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
30*c217d954SCole Faust #include "arm_compute/core/Utils.h"
31*c217d954SCole Faust #include "arm_compute/core/Validate.h"
32*c217d954SCole Faust #include "arm_compute/core/Window.h"
33*c217d954SCole Faust #include "src/core/NEON/NEAsymm.h"
34*c217d954SCole Faust #include "src/core/NEON/NEFixedPoint.h"
35*c217d954SCole Faust #include "src/core/NEON/wrapper/wrapper.h"
36*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
37*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
38*c217d954SCole Faust
39*c217d954SCole Faust #include <cstdint>
40*c217d954SCole Faust
41*c217d954SCole Faust namespace arm_compute
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace cpu
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace kernels
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace
48*c217d954SCole Faust {
49*c217d954SCole Faust template <typename T>
depth_concat(const ITensor * src,ITensor * dst,unsigned int depth_offset,const Window & window)50*c217d954SCole Faust void depth_concat(const ITensor *src, ITensor *dst, unsigned int depth_offset, const Window &window)
51*c217d954SCole Faust {
52*c217d954SCole Faust // Offset source
53*c217d954SCole Faust uint8_t *src_ptr = src->buffer() + src->info()->offset_first_element_in_bytes();
54*c217d954SCole Faust
55*c217d954SCole Faust // Offset destination
56*c217d954SCole Faust uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + depth_offset * dst->info()->strides_in_bytes()[2];
57*c217d954SCole Faust
58*c217d954SCole Faust const auto window_start_x = static_cast<int>(window.x().start());
59*c217d954SCole Faust const auto window_end_x = static_cast<int>(window.x().end());
60*c217d954SCole Faust const int window_step_x = 16 / dst->info()->element_size();
61*c217d954SCole Faust
62*c217d954SCole Faust Window win{ window };
63*c217d954SCole Faust win.set(Window::DimX, Window::Dimension(0, 1, 1));
64*c217d954SCole Faust win.set(Window::DimZ, Window::Dimension(0, src->info()->tensor_shape().z(), 1));
65*c217d954SCole Faust
66*c217d954SCole Faust Iterator src_it(src, win);
67*c217d954SCole Faust Iterator dst_it(dst, win);
68*c217d954SCole Faust
69*c217d954SCole Faust const DataType dt = src->info()->data_type();
70*c217d954SCole Faust const UniformQuantizationInfo src_qinfo = src->info()->quantization_info().uniform();
71*c217d954SCole Faust const UniformQuantizationInfo dst_qinfo = dst->info()->quantization_info().uniform();
72*c217d954SCole Faust if(dt == DataType::QASYMM8 && src_qinfo != dst_qinfo)
73*c217d954SCole Faust {
74*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
75*c217d954SCole Faust {
76*c217d954SCole Faust const auto in_ptr = reinterpret_cast<const uint8_t *>(src_ptr + src_it.offset());
77*c217d954SCole Faust const auto out_ptr = reinterpret_cast<uint8_t *>(dst_ptr + dst_it.offset());
78*c217d954SCole Faust int x = window_start_x;
79*c217d954SCole Faust for(; x <= (window_end_x - window_step_x); x += window_step_x)
80*c217d954SCole Faust {
81*c217d954SCole Faust wrapper::vstore(out_ptr + x, vquantize(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
82*c217d954SCole Faust }
83*c217d954SCole Faust
84*c217d954SCole Faust // Compute left-over elements
85*c217d954SCole Faust for(; x < window_end_x; ++x)
86*c217d954SCole Faust {
87*c217d954SCole Faust *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), src_qinfo), dst_qinfo);
88*c217d954SCole Faust }
89*c217d954SCole Faust },
90*c217d954SCole Faust src_it, dst_it);
91*c217d954SCole Faust }
92*c217d954SCole Faust else if(dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo)
93*c217d954SCole Faust {
94*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
95*c217d954SCole Faust {
96*c217d954SCole Faust const auto in_ptr = reinterpret_cast<const int8_t *>(src_ptr + src_it.offset());
97*c217d954SCole Faust const auto out_ptr = reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset());
98*c217d954SCole Faust int x = window_start_x;
99*c217d954SCole Faust for(; x <= (window_end_x - window_step_x); x += window_step_x)
100*c217d954SCole Faust {
101*c217d954SCole Faust wrapper::vstore(out_ptr + x, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
102*c217d954SCole Faust }
103*c217d954SCole Faust
104*c217d954SCole Faust // Compute left-over elements
105*c217d954SCole Faust for(; x < window_end_x; ++x)
106*c217d954SCole Faust {
107*c217d954SCole Faust *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), src_qinfo), dst_qinfo);
108*c217d954SCole Faust }
109*c217d954SCole Faust },
110*c217d954SCole Faust src_it, dst_it);
111*c217d954SCole Faust }
112*c217d954SCole Faust else
113*c217d954SCole Faust {
114*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates &)
115*c217d954SCole Faust {
116*c217d954SCole Faust const auto in_ptr = reinterpret_cast<const T *>(src_ptr + src_it.offset());
117*c217d954SCole Faust const auto out_ptr = reinterpret_cast<T *>(dst_ptr + dst_it.offset());
118*c217d954SCole Faust int x = window_start_x;
119*c217d954SCole Faust for(; x <= (window_end_x - window_step_x); x += window_step_x)
120*c217d954SCole Faust {
121*c217d954SCole Faust wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
122*c217d954SCole Faust }
123*c217d954SCole Faust // Compute left-over elements
124*c217d954SCole Faust for(; x < window_end_x; ++x)
125*c217d954SCole Faust {
126*c217d954SCole Faust *(out_ptr + x) = *(in_ptr + x);
127*c217d954SCole Faust }
128*c217d954SCole Faust },
129*c217d954SCole Faust src_it, dst_it);
130*c217d954SCole Faust }
131*c217d954SCole Faust }
132*c217d954SCole Faust
validate_arguments(const ITensorInfo * input,unsigned int depth_offset,const ITensorInfo * output)133*c217d954SCole Faust Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
134*c217d954SCole Faust {
135*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
136*c217d954SCole Faust //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions.
137*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
138*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
139*c217d954SCole Faust
140*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
141*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
142*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
143*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
144*c217d954SCole Faust
145*c217d954SCole Faust return Status{};
146*c217d954SCole Faust }
147*c217d954SCole Faust } // namespace
148*c217d954SCole Faust
configure(const ITensorInfo * src,unsigned int depth_offset,ITensorInfo * dst)149*c217d954SCole Faust void CpuConcatenateDepthKernel::configure(const ITensorInfo *src, unsigned int depth_offset, ITensorInfo *dst)
150*c217d954SCole Faust {
151*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
152*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, depth_offset, dst));
153*c217d954SCole Faust
154*c217d954SCole Faust _func = nullptr;
155*c217d954SCole Faust _depth_offset = depth_offset;
156*c217d954SCole Faust
157*c217d954SCole Faust switch(src->data_type())
158*c217d954SCole Faust {
159*c217d954SCole Faust case DataType::QASYMM8:
160*c217d954SCole Faust _func = &depth_concat<uint8_t>;
161*c217d954SCole Faust break;
162*c217d954SCole Faust case DataType::QASYMM8_SIGNED:
163*c217d954SCole Faust _func = &depth_concat<int8_t>;
164*c217d954SCole Faust break;
165*c217d954SCole Faust case DataType::F16:
166*c217d954SCole Faust _func = &depth_concat<uint16_t>;
167*c217d954SCole Faust break;
168*c217d954SCole Faust case DataType::F32:
169*c217d954SCole Faust _func = &depth_concat<uint32_t>;
170*c217d954SCole Faust break;
171*c217d954SCole Faust default:
172*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported data type.");
173*c217d954SCole Faust }
174*c217d954SCole Faust
175*c217d954SCole Faust // Configure kernel window
176*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps());
177*c217d954SCole Faust ICpuKernel::configure(win);
178*c217d954SCole Faust }
179*c217d954SCole Faust
validate(const arm_compute::ITensorInfo * src,unsigned int depth_offset,const arm_compute::ITensorInfo * dst)180*c217d954SCole Faust Status CpuConcatenateDepthKernel::validate(const arm_compute::ITensorInfo *src,
181*c217d954SCole Faust unsigned int depth_offset,
182*c217d954SCole Faust const arm_compute::ITensorInfo *dst)
183*c217d954SCole Faust {
184*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, depth_offset, dst));
185*c217d954SCole Faust return Status{};
186*c217d954SCole Faust }
187*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)188*c217d954SCole Faust void CpuConcatenateDepthKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
189*c217d954SCole Faust {
190*c217d954SCole Faust ARM_COMPUTE_UNUSED(info);
191*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
192*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
193*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(_func == nullptr);
194*c217d954SCole Faust
195*c217d954SCole Faust (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC),
196*c217d954SCole Faust tensors.get_tensor(TensorType::ACL_DST),
197*c217d954SCole Faust _depth_offset,
198*c217d954SCole Faust window);
199*c217d954SCole Faust }
200*c217d954SCole Faust
name() const201*c217d954SCole Faust const char *CpuConcatenateDepthKernel::name() const
202*c217d954SCole Faust {
203*c217d954SCole Faust return "CpuConcatenateDepthKernel";
204*c217d954SCole Faust }
205*c217d954SCole Faust } // namespace kernels
206*c217d954SCole Faust } // namespace cpu
207*c217d954SCole Faust } // namespace arm_compute
208