blob: cd752703927f05dbe9bde3b64a7e07954c00caca [file] [log] [blame]
Georgios Pinitasc9369172018-09-26 11:25:40 +01001/*
Teresa Charlin62687422021-04-28 10:58:49 +01002 * Copyright (c) 2018-2021 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_CLFUSEBATCHNORMALIZATION_H
25#define ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H
Georgios Pinitasc9369172018-09-26 11:25:40 +010026
Georgios Pinitasc9369172018-09-26 11:25:40 +010027#include "arm_compute/core/Types.h"
28#include "arm_compute/runtime/IFunction.h"
29
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include <memory>
31
Georgios Pinitasc9369172018-09-26 11:25:40 +010032namespace arm_compute
33{
34// Forward declarations
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035class CLCompileContext;
36class CLFuseBatchNormalizationKernel;
Georgios Pinitasc9369172018-09-26 11:25:40 +010037class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010038class ITensorInfo;
Georgios Pinitasc9369172018-09-26 11:25:40 +010039
40/** Basic function to fuse the batch normalization node to a preceding convolution node */
41class CLFuseBatchNormalization : public IFunction
42{
43public:
44 /** Default constructor */
45 CLFuseBatchNormalization();
46 /** Prevent instances of this class from being copied (As this class contains pointers) */
47 CLFuseBatchNormalization(const CLFuseBatchNormalization &) = delete;
48 /** Prevent instances of this class from being copied (As this class contains pointers) */
49 CLFuseBatchNormalization &operator=(const CLFuseBatchNormalization &) = delete;
50 /** Allow instances of this class to be moved */
51 CLFuseBatchNormalization(CLFuseBatchNormalization &&) = default;
52 /** Allow instances of this class to be moved */
53 CLFuseBatchNormalization &operator=(CLFuseBatchNormalization &&) = default;
54 /** Default destructor */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010055 ~CLFuseBatchNormalization();
Georgios Pinitasc9369172018-09-26 11:25:40 +010056 /** Set the input and output tensors.
57 *
Teresa Charlin62687422021-04-28 10:58:49 +010058 * Valid data layouts:
59 * - NHWC
60 * - NCHW
61 *
62 * Valid data type configurations:
63 * |src |dst |
64 * |:--------------|:--------------|
65 * |F32 |F32 |
66 * |F16 |F16 |
67 *
Manuel Bottini2732cca2019-05-28 11:44:41 +010068 * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
69 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights
70 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights
71 * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
72 * @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
73 * @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
74 * @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 +010075 * @note if nullptr, bn_beta is set to 0.0
Manuel Bottini2732cca2019-05-28 11:44:41 +010076 * @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 +010077 * @note if nullptr, bn_gamma is set to 1.0
Georgios Pinitasc9369172018-09-26 11:25:40 +010078 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
Manuel Bottini2732cca2019-05-28 11:44:41 +010079 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution.
Georgios Pinitasc9369172018-09-26 11:25:40 +010080 */
Manuel Bottini2732cca2019-05-28 11:44:41 +010081 void configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias,
82 const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
83 float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Manuel Bottini2b84be52020-04-08 10:15:51 +010084 /** Set the input and output tensors.
85 *
86 * @param[in] compile_context The compile context to be used.
87 * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
88 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights
89 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights
90 * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
91 * @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
92 * @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
93 * @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
94 * @note if nullptr, bn_beta is set to 0.0
95 * @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
96 * @note if nullptr, bn_gamma is set to 1.0
97 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
98 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution.
99 */
100 void configure(const CLCompileContext &compile_context, const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias,
101 const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
102 float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100103 /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalization
104 *
Manuel Bottini2732cca2019-05-28 11:44:41 +0100105 * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
106 * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights
107 * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights
108 * @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
109 * @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
110 * @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
111 * @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
112 * @note if nullptr, bn_beta is set to 0.0
113 * @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
114 * @note if nullptr, bn_gamma is set to 1.0
115 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
116 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution.
Georgios Pinitasc9369172018-09-26 11:25:40 +0100117 *
118 * @return a status
119 */
Manuel Bottini2732cca2019-05-28 11:44:41 +0100120 static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
Georgios Pinitasc9369172018-09-26 11:25:40 +0100121 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini2732cca2019-05-28 11:44:41 +0100122 const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr,
123 float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
Georgios Pinitasc9369172018-09-26 11:25:40 +0100124
125 // Inherited methods overridden:
126 void run() override;
127
128private:
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100129 std::unique_ptr<CLFuseBatchNormalizationKernel> _fuse_bn_kernel;
Georgios Pinitasc9369172018-09-26 11:25:40 +0100130};
131} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000132#endif /*ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H */