xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClWeightsReshapeKernel.h (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 #ifndef ARM_COMPUTE_CL_WEIGHTSRESHAPE_KERNEL_H
25 #define ARM_COMPUTE_CL_WEIGHTSRESHAPE_KERNEL_H
26 
27 #include "src/core/common/Macros.h"
28 #include "src/gpu/cl/ClCompileContext.h"
29 #include "src/gpu/cl/IClKernel.h"
30 
31 namespace arm_compute
32 {
33 namespace opencl
34 {
35 namespace kernels
36 {
37 /** OpenCL kernel to perform reshaping on the weights used by convolution and locally connected layer
38  *
39  * Rearranges each 3-dimensional kernel to a single row leading to a matrix with linearized kernels.
40  * In combination with the @ref opencl::kernels::ClIm2ColKernel can transform a convolution to a matrix multiplication.
41  *
42  * For example assuming a 3D weight kernel of 3x3 dimensions and depth of 2 we have:
43  * @f[
44  * \left( \begin{array}{ccc}
45  * a000 & a001 & a002 \\
46  * a010 & a011 & a012 \\
47  * a020 & a021 & a022 \\
48  * \end{array} \right)
49  * \left( \begin{array}{ccc}
50  * a100 & a101 & a102 \\
51  * a110 & a111 & a112 \\
52  * a120 & a121 & a122 \\
53  * \end{array} \right)
54  * \rightarrow
55  * \left( \begin{array}{ccccccccc}
56  * a000 & a001 & a002 & a010 & a011 & a012 & a020 & a021 & a022 & a100 & a101 & a102 & a110 & a111 & a112 & a120 & a121 & a122 \\
57  * \end{array} \right)
58  * @f]
59  */
60 class ClWeightsReshapeKernel : public IClKernel
61 {
62 public:
63     ClWeightsReshapeKernel();
64     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClWeightsReshapeKernel);
65     /** Set the input and output of the kernel.
66      *
67      * @param[in]  compile_context The compile context to be used.
68      * @param[in]  src             The input tensor info to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared,
69      *                             and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM,  num_patches] if unshared. Data types supported: All
70      * @param[in]  biases          The shared biases tensor info to append.  Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with
71      *                             dimensions [OFM, num_patches] if unshared. Data types supported: F16/F32, for quantized types this must be nullptr.
72      *                             @warning Appending biases to weights reshaped matrix is not supported for quantized asymmetric types.
73      * @param[out] dst             The output tensor info. Should be a 2D Tensor if there are no groups and the weights are not shared; a 3D Tensor otherwise.
74      *                             Data types supported: Same as @p input
75      * @param[in]  num_groups      (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
76      *                             Number of groups greater than one are only supported for NCHW data layout, and the number of weights must be a multiple of it.
77      */
78     void configure(const ClCompileContext &compile_context, const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst, unsigned int num_groups = 1);
79     /** Static function to check if given info will lead to a valid configuration
80      *
81      * Similar to ClWeightsReshapeKernel::configure()
82      *
83      * @return a status
84      */
85     static Status validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst, unsigned int num_groups = 1);
86 
87     // Inherited methods overridden:
88     void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
89 };
90 } // namespace kernels
91 } // namespace opencl
92 } // namespace arm_compute
93 #endif /*ARM_COMPUTE_CL_WEIGHTSRESHAPE_KERNEL_H */