xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/operators/ClWinogradConv2d.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-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_WINOGRADCONV2D_H
25 #define ARM_COMPUTE_CL_WINOGRADCONV2D_H
26 
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 #include "src/core/CL/kernels/CLFillBorderKernel.h"
29 #include "src/gpu/cl/ClCompileContext.h"
30 #include "src/gpu/cl/IClOperator.h"
31 #include "src/gpu/cl/operators/ClGemm.h"
32 
33 namespace arm_compute
34 {
35 class CLCompileContext;
36 class ITensorInfo;
37 namespace opencl
38 {
39 namespace kernels
40 {
41 class ClWinogradInputTransformKernel;
42 class ClWinogradFilterTransformKernel;
43 class ClWinogradOutputTransformKernel;
44 } // kernels
45 /** Basic function to execute Winograd-based convolution on OpenCL. This function calls the following OpenCL functions/kernels:
46  *
47  *  -# @ref kernels::ClWinogradInputTransformKernel
48  *  -# @ref kernels::ClWinogradFilterTransformKernel (only once)
49  *  -# @ref ClGemm
50  *  -# @ref kernels::ClWinogradOutputTransformKernel
51  *
52  */
53 class ClWinogradConv2d : public IClOperator
54 {
55 public:
56     /** Default constructor */
57     ClWinogradConv2d();
58     /** Default destructor */
59     ~ClWinogradConv2d();
60     /** Prevent instances of this class from being copied (As this class contains pointers) */
61     ClWinogradConv2d(const ClWinogradConv2d &) = delete;
62     /** Default move constructor */
63     ClWinogradConv2d(ClWinogradConv2d &&) = default;
64     /** Prevent instances of this class from being copied (As this class contains pointers) */
65     ClWinogradConv2d &operator=(const ClWinogradConv2d &) = delete;
66     /** Default move assignment operator */
67     ClWinogradConv2d &operator=(ClWinogradConv2d &&) = default;
68     /** Set the input and output tensors.
69      *
70      * Valid data layouts:
71      * - NHWC
72      * - NCHW
73      *
74      * Valid data type configurations:
75      * |src0           |src1           |src2   |dst            |
76      * |:--------------|:--------------|:------|:--------------|
77      * |F16            |F16            |F16    |F16            |
78      * |F32            |F32            |F32    |F32            |
79      *
80      * @note: This function only works with 3x3,3x1,1x3,5x5,5x1,1x5,7x1 and 1x7 kernels along with unit strides for both NCHW and NHWC data layout
81      * @note  Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true
82      *
83      * @param[in]  compile_context  The compile context to be used.
84      * @param[in]  src              Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
85      *                              while every optional dimension from 4 and above represent a batch of inputs.
86      *                              Data types supported: F16/F32.
87      * @param[in]  weights          Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p src.
88      * @param[in]  biases           Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p src
89      * @param[out] dst              Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
90      *                              Data types supported: Same as @p src.
91      * @param[in]  conv_info        Contains padding and stride information described in @ref PadStrideInfo.
92      * @param[in]  act_info         (Optional) Activation layer information in case of a fused activation.
93      * @param[in]  enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
94      *                              available which may introduce a drop of accuracy as well. Default is false
95      */
96     void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst, const PadStrideInfo &conv_info,
97                    const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false);
98     /** Static function to check if given info will lead to a valid configuration
99      *
100      * Similar to ClWinogradConv2d::configure()
101      *
102      * @return a status
103      */
104     static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
105                            const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false);
106 
107     // Inherited method overridden
108     void run(ITensorPack &tensors) override;
109     void prepare(ITensorPack &tensors) override;
110     experimental::MemoryRequirements workspace() const override;
111 
112 private:
113     ClGemm                                                    _batched_mm;
114     std::unique_ptr<kernels::ClWinogradInputTransformKernel>  _input_transform;
115     std::unique_ptr<kernels::ClWinogradFilterTransformKernel> _filter_transform;
116     std::unique_ptr<kernels::ClWinogradOutputTransformKernel> _output_transform;
117     CLFillBorderKernel                                        _border_handler;
118     TensorInfo                                                _input0;
119     TensorInfo                                                _input1;
120     TensorInfo                                                _batched_mm_output;
121     bool                                                      _is_prepared;
122     experimental::MemoryRequirements                          _aux_mem{};
123 };
124 } // namespace opencl
125 } // namespace arm_compute
126 #endif /* ARM_COMPUTE_CL_WINOGRADCONV2D_H */
127