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