blob: f23280d55a78819611cf6d85933f46d650d2f709 [file] [log] [blame]
giuros0115ecc9a2018-12-06 10:47:34 +00001/*
Michalis Spyrouebcebf12020-10-21 00:04:14 +01002 * Copyright (c) 2018-2020 Arm Limited.
giuros0115ecc9a2018-12-06 10:47:34 +00003 *
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
George Wort5a97b282018-12-21 16:21:04 +000017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
giuros0115ecc9a2018-12-06 10:47:34 +000018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
George Wort5a97b282018-12-21 16:21:04 +000019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
giuros0115ecc9a2018-12-06 10:47:34 +000020 * 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H
25#define ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H
giuros0115ecc9a2018-12-06 10:47:34 +000026
Michalis Spyrouebcebf12020-10-21 00:04:14 +010027#include "src/core/NEON/INEKernel.h"
giuros0115ecc9a2018-12-06 10:47:34 +000028
29namespace arm_compute
30{
31// Forward declarations
32class ITensor;
33
34/** OpenNE kernel to fuse the batch normalization node to a preceding convolution node */
35class NEFuseBatchNormalizationKernel : public INEKernel
36{
37public:
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 *
Manuel Bottini11091762019-06-17 12:04:40 +010056 * @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
giuros0115ecc9a2018-12-06 10:47:34 +000066 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
Manuel Bottini11091762019-06-17 12:04:40 +010067 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
giuros0115ecc9a2018-12-06 10:47:34 +000068 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 void configure(const ITensor *input_weights,
70 const ITensor *bn_mean,
71 const ITensor *bn_var,
72 ITensor *fused_weights,
73 ITensor *fused_bias,
74 const ITensor *input_bias = nullptr,
75 const ITensor *bn_beta = nullptr,
76 const ITensor *bn_gamma = nullptr,
77 float epsilon = 0.001f,
78 FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
giuros0115ecc9a2018-12-06 10:47:34 +000079 /** Static function to check if given info will lead to a valid configuration of @ref NEFuseBatchNormalizationKernel
80 *
Manuel Bottini11091762019-06-17 12:04:40 +010081 * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
82 * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights
83 * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights
84 * @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
85 * @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
86 * @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
87 * @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
88 * @note if nullptr, bn_beta is set to 0.0
89 * @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
90 * @note if nullptr, bn_gamma is set to 1.0
giuros0115ecc9a2018-12-06 10:47:34 +000091 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
Manuel Bottini11091762019-06-17 12:04:40 +010092 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
giuros0115ecc9a2018-12-06 10:47:34 +000093 *
94 * @return a status
95 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 static Status validate(const ITensorInfo *input_weights,
97 const ITensorInfo *bn_mean,
98 const ITensorInfo *bn_var,
99 const ITensorInfo *fused_weights,
100 const ITensorInfo *fused_bias,
101 const ITensorInfo *input_bias = nullptr,
102 const ITensorInfo *bn_beta = nullptr,
103 const ITensorInfo *bn_gamma = nullptr,
104 float epsilon = 0.001f,
105 FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
giuros0115ecc9a2018-12-06 10:47:34 +0000106
107 // Inherited methods overridden:
108 void run(const Window &window, const ThreadInfo &info) override;
109
110private:
Manuel Bottini11091762019-06-17 12:04:40 +0100111 const ITensor *_input_weights;
112 const ITensor *_input_bias;
giuros0115ecc9a2018-12-06 10:47:34 +0000113 const ITensor *_bn_mean;
114 const ITensor *_bn_var;
115 const ITensor *_bn_gamma;
116 const ITensor *_bn_beta;
117 ITensor *_fused_weights;
118 ITensor *_fused_bias;
119 float _epsilon;
120 bool _run_in_place_weights;
121 bool _run_in_place_bias;
122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 using FuseBatchNormFunction = void(const ITensor *input_weights,
124 const ITensor *input_bias,
125 ITensor *fused_weights,
126 ITensor *fused_bias,
127 const ITensor *bn_mean,
128 const ITensor *bn_var,
129 const ITensor *bn_beta,
130 const ITensor *bn_gamma,
131 float epsilon,
132 const Window &window);
giuros0115ecc9a2018-12-06 10:47:34 +0000133
134 FuseBatchNormFunction *_func;
135};
136} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000137#endif /*ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H */