xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/NEFuseBatchNormalizationKernel.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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_NEFUSEBATCHNORMALIZATIONKERNEL_H
25 #define ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H
26 
27 #include "src/core/NEON/INEKernel.h"
28 
29 namespace arm_compute
30 {
31 // Forward declarations
32 class ITensor;
33 
34 /** OpenNE kernel to fuse the batch normalization node to a preceding convolution node */
35 class NEFuseBatchNormalizationKernel : public INEKernel
36 {
37 public:
name()38     const char *name() const override
39     {
40         return "NEFuseBatchNormalizationKernel";
41     }
42     /** Default constructor */
43     NEFuseBatchNormalizationKernel();
44     /** Prevent instances of this class from being copied (As this class contains pointers) */
45     NEFuseBatchNormalizationKernel(const NEFuseBatchNormalizationKernel &) = delete;
46     /** Prevent instances of this class from being copied (As this class contains pointers) */
47     NEFuseBatchNormalizationKernel &operator=(const NEFuseBatchNormalizationKernel &) = delete;
48     /** Allow instances of this class to be moved */
49     NEFuseBatchNormalizationKernel(NEFuseBatchNormalizationKernel &&) = default;
50     /** Allow instances of this class to be moved */
51     NEFuseBatchNormalizationKernel &operator=(NEFuseBatchNormalizationKernel &&) = default;
52     /** Default destructor */
53     ~NEFuseBatchNormalizationKernel() = default;
54     /** Set the source, destination of the kernel
55      *
56      * @param[in]  input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
57      * @param[in]  bn_mean       Batch normalization layer mean tensor. Same as @p input_weights
58      * @param[in]  bn_var        Batch normalization layer variance tensor. Same as @p input_weights
59      * @param[out] fused_weights (Optional) Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
60      * @param[out] fused_bias    (Optional) Output fused bias tensor. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights
61      * @param[in]  input_bias    (Optional) Input bias tensor for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_weights
62      * @param[in]  bn_beta       (Optional) Batch normalization layer beta tensor. It can be a nullptr in case the beta tensor is not required. Same as @p input_weights
63      *                           @note if nullptr, bn_beta is set to 0.0
64      * @param[in]  bn_gamma      (Optional) Batch normalization layer gamma tensor. It can be a nullptr in case the gamma tensor is not required. Same as @p input_weights
65      *                           @note if nullptr, bn_gamma is set to 1.0
66      * @param[in]  epsilon       (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
67      * @param[in]  fbn_type      (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
68      */
69     void configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var, ITensor *fused_weights, ITensor *fused_bias,
70                    const ITensor *input_bias = nullptr, const ITensor *bn_beta = nullptr, const ITensor *bn_gamma = nullptr,
71                    float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
72     /** Static function to check if given info will lead to a valid configuration of @ref NEFuseBatchNormalizationKernel
73      *
74      * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
75      * @param[in] bn_mean       Batch normalization layer mean tensor info. Same as @p input_weights
76      * @param[in] bn_var        Batch normalization layer variance tensor info. Same as @p input_weights
77      * @param[in] fused_weights (Optional) Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p input_weights
78      * @param[in] fused_bias    (Optional) Output fused bias tensor info. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights
79      * @param[in] input_bias    (Optional) Input bias tensor info for convolution or depthwise convolution layer. It can be a nullptr in case the bias tensor is not required. Same as @p input_weights
80      * @param[in] bn_beta       (Optional) Batch normalization layer beta tensor info. It can be a nullptr in case the beta tensor is not required. Same as @p input_weights
81      *                          @note if nullptr, bn_beta is set to 0.0
82      * @param[in] bn_gamma      (Optional) Batch normalization layer gamma tensor info. It can be a nullptr in case the gamma tensor is not required. Same as @p input_weights
83      *                          @note if nullptr, bn_gamma is set to 1.0
84      * @param[in] epsilon       (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
85      * @param[in] fbn_type      (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
86      *
87      * @return a status
88      */
89     static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
90                            const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
91                            const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr,
92                            float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
93 
94     // Inherited methods overridden:
95     void run(const Window &window, const ThreadInfo &info) override;
96 
97 private:
98     const ITensor *_input_weights;
99     const ITensor *_input_bias;
100     const ITensor *_bn_mean;
101     const ITensor *_bn_var;
102     const ITensor *_bn_gamma;
103     const ITensor *_bn_beta;
104     ITensor       *_fused_weights;
105     ITensor       *_fused_bias;
106     float          _epsilon;
107     bool           _run_in_place_weights;
108     bool           _run_in_place_bias;
109 
110     using FuseBatchNormFunction = void(const ITensor *input_weights, const ITensor *input_bias, ITensor *fused_weights, ITensor *fused_bias,
111                                        const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window);
112 
113     FuseBatchNormFunction *_func;
114 };
115 } // namespace arm_compute
116 #endif /*ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H */
117