xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuCol2ImKernel.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/CpuCol2ImKernel.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/Size2D.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Types.h"
32 #include "arm_compute/core/Validate.h"
33 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
34 #include "src/core/helpers/AutoConfiguration.h"
35 #include "src/core/helpers/WindowHelpers.h"
36 
37 namespace arm_compute
38 {
39 using namespace misc::shape_calculator;
40 namespace cpu
41 {
42 namespace kernels
43 {
44 namespace
45 {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const Size2D & convolved_dims)46 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &convolved_dims)
47 {
48     //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions.
49     ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
50 
51     // Validate configured output
52     if(dst->total_size() != 0)
53     {
54         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), compute_col2im_shape(*src, convolved_dims, false));
55         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
56         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
57     }
58 
59     return Status{};
60 }
61 } // namespace
62 
configure(const ITensorInfo * src,ITensorInfo * dst,const Size2D & convolved_dims)63 void CpuCol2ImKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const Size2D &convolved_dims)
64 {
65     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
66     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, convolved_dims));
67 
68     _convolved_dims = convolved_dims;
69 
70     // Configure kernel window
71     // Output auto inizialitation if not yet initialized
72     auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_col2im_shape(*src, convolved_dims, false)));
73 
74     // Configure kernel window
75     Window win = calculate_max_window(*src, Steps());
76 
77     ICpuKernel::configure(win);
78 }
79 
validate(const ITensorInfo * src,const ITensorInfo * output,const Size2D & convolved_dims)80 Status CpuCol2ImKernel::validate(const ITensorInfo *src, const ITensorInfo *output, const Size2D &convolved_dims)
81 {
82     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, output, convolved_dims));
83     return Status{};
84 }
85 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)86 void CpuCol2ImKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
87 {
88     ARM_COMPUTE_UNUSED(info);
89     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
90     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
91 
92     auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
93     auto dst = tensors.get_tensor(TensorType::ACL_DST);
94 
95     const uint8_t el_size         = src->info()->element_size();
96     const int     output_stride_x = dst->info()->strides_in_bytes().x();
97     const int     output_stride_y = dst->info()->strides_in_bytes().y();
98     const int     output_stride_z = dst->info()->strides_in_bytes().z();
99 
100     Window window_out(window);
101     window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
102     window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
103     window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
104 
105     // Create iterators
106     Iterator in(src, window);
107     Iterator out(dst, window_out);
108 
109     execute_window_loop(window, [&](const Coordinates & id)
110     {
111         const int hidx = id.y();
112         const int idx  = id.x() * output_stride_z + (hidx / _convolved_dims.width) * output_stride_y + (hidx % _convolved_dims.width) * output_stride_x;
113         std::memcpy(out.ptr() + idx, in.ptr(), el_size);
114     },
115     in, out);
116 }
117 
name() const118 const char *CpuCol2ImKernel::name() const
119 {
120     return "CpuCol2ImKernel";
121 }
122 } // namespace kernels
123 } // namespace cpu
124 } // namespace arm_compute