blob: 76ec7a759ffee197cced6e941459329bd257f0a7 [file] [log] [blame]
Georgios Pinitasc9369172018-09-26 11:25:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasc9369172018-09-26 11:25:40 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H
25#define ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H
Georgios Pinitasc9369172018-09-26 11:25:40 +010026
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "src/core/CL/ICLKernel.h"
Georgios Pinitasc9369172018-09-26 11:25:40 +010028
29namespace arm_compute
30{
31// Forward declarations
32class ICLTensor;
33
34/** OpenCL kernel to fuse the batch normalization node to a preceding convolution node */
35class CLFuseBatchNormalizationKernel : public ICLKernel
36{
37public:
38 /** Default constructor */
39 CLFuseBatchNormalizationKernel();
40 /** Prevent instances of this class from being copied (As this class contains pointers) */
41 CLFuseBatchNormalizationKernel(const CLFuseBatchNormalizationKernel &) = delete;
42 /** Prevent instances of this class from being copied (As this class contains pointers) */
43 CLFuseBatchNormalizationKernel &operator=(const CLFuseBatchNormalizationKernel &) = delete;
44 /** Allow instances of this class to be moved */
45 CLFuseBatchNormalizationKernel(CLFuseBatchNormalizationKernel &&) = default;
46 /** Allow instances of this class to be moved */
47 CLFuseBatchNormalizationKernel &operator=(CLFuseBatchNormalizationKernel &&) = default;
48 /** Default destructor */
49 ~CLFuseBatchNormalizationKernel() = default;
50 /** Set the source, destination of the kernel
51 *
Manuel Bottini2732cca2019-05-28 11:44:41 +010052 * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
53 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights
54 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights
55 * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
56 * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights
57 * @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
58 * @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
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010059 * @note if nullptr, bn_beta is set to 0.0
Manuel Bottini2732cca2019-05-28 11:44:41 +010060 * @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
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010061 * @note if nullptr, bn_gamma is set to 1.0
Georgios Pinitasc9369172018-09-26 11:25:40 +010062 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
Manuel Bottini2732cca2019-05-28 11:44:41 +010063 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
Georgios Pinitasc9369172018-09-26 11:25:40 +010064 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 void configure(const ICLTensor *input_weights,
66 const ICLTensor *bn_mean,
67 const ICLTensor *bn_var,
68 ICLTensor *fused_weights,
69 ICLTensor *fused_bias,
70 const ICLTensor *input_bias = nullptr,
71 const ICLTensor *bn_beta = nullptr,
72 const ICLTensor *bn_gamma = nullptr,
73 float epsilon = 0.001f,
74 FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010075 /** Set the source, destination of the kernel
76 *
77 * @param[in] compile_context The compile context to be used.
78 * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
79 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights
80 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights
81 * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
82 * @param[out] fused_bias Output fused bias tensor. It can be a nullptr in case of in-place computation and input_bias != nullptr. Same as @p input_weights
83 * @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
84 * @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
85 * @note if nullptr, bn_beta is set to 0.0
86 * @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
87 * @note if nullptr, bn_gamma is set to 1.0
88 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
89 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
90 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 void configure(const CLCompileContext &compile_context,
92 const ICLTensor *input_weights,
93 const ICLTensor *bn_mean,
94 const ICLTensor *bn_var,
95 ICLTensor *fused_weights,
96 ICLTensor *fused_bias,
97 const ICLTensor *input_bias = nullptr,
98 const ICLTensor *bn_beta = nullptr,
99 const ICLTensor *bn_gamma = nullptr,
100 float epsilon = 0.001f,
101 FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100102 /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalizationKernel
103 *
Manuel Bottini2732cca2019-05-28 11:44:41 +0100104 * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
105 * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights
106 * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights
107 * @param[in] fused_weights Output fused weights tensor info. It can be a nullptr in case of in-place computation. Same as @p input_weights
108 * @param[in] fused_bias 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
109 * @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
110 * @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
111 * @note if nullptr, bn_beta is set to 0.0
112 * @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
113 * @note if nullptr, bn_gamma is set to 1.0
114 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
115 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
Georgios Pinitasc9369172018-09-26 11:25:40 +0100116 *
117 * @return a status
118 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 static Status validate(const ITensorInfo *input_weights,
120 const ITensorInfo *bn_mean,
121 const ITensorInfo *bn_var,
122 const ITensorInfo *fused_weights,
123 const ITensorInfo *fused_bias,
124 const ITensorInfo *input_bias = nullptr,
125 const ITensorInfo *bn_beta = nullptr,
126 const ITensorInfo *bn_gamma = nullptr,
127 float epsilon = 0.001f,
128 FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100129
130 // Inherited methods overridden:
131 void run(const Window &window, cl::CommandQueue &queue) override;
132
133private:
Michalis Spyrouf4643372019-11-29 16:17:13 +0000134 const ICLTensor *_input_weights;
135 const ICLTensor *_input_bias;
136 const ICLTensor *_bn_mean;
137 const ICLTensor *_bn_var;
138 const ICLTensor *_bn_gamma;
139 const ICLTensor *_bn_beta;
140 ICLTensor *_fused_weights;
141 ICLTensor *_fused_bias;
142 float _epsilon;
143 bool _run_in_place_weights;
144 bool _run_in_place_bias;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100145};
146} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000147#endif /*ARM_COMPUTE_CLFUSEBATCHNORMALIZATIONKERNEL_H */