giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 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 |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 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 Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEFUSEBATCHNORMALIZATION_H |
| 25 | #define ARM_COMPUTE_NEFUSEBATCHNORMALIZATION_H |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/ITensor.h" |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/IFunction.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | // Forward declarations |
| 34 | class ITensor; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 35 | class NEFuseBatchNormalizationKernel; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 36 | |
| 37 | /** Basic function to fuse the batch normalization node to a preceding convolution node */ |
| 38 | class NEFuseBatchNormalization : public IFunction |
| 39 | { |
| 40 | public: |
| 41 | /** Default constructor */ |
| 42 | NEFuseBatchNormalization(); |
| 43 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 44 | NEFuseBatchNormalization(const NEFuseBatchNormalization &) = delete; |
| 45 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 46 | NEFuseBatchNormalization &operator=(const NEFuseBatchNormalization &) = delete; |
| 47 | /** Allow instances of this class to be moved */ |
| 48 | NEFuseBatchNormalization(NEFuseBatchNormalization &&) = default; |
| 49 | /** Allow instances of this class to be moved */ |
| 50 | NEFuseBatchNormalization &operator=(NEFuseBatchNormalization &&) = default; |
| 51 | /** Default destructor */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 52 | ~NEFuseBatchNormalization(); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 53 | /** Set the input and output tensors. |
| 54 | * |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 55 | * @param[in] input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC |
| 56 | * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p input_weights |
| 57 | * @param[in] bn_var Batch normalization layer variance tensor. Same as @p input_weights |
| 58 | * @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 |
| 59 | * @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 |
| 60 | * @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 |
| 61 | * @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 |
| 62 | * @note if nullptr, bn_beta is set to 0.0 |
| 63 | * @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 |
| 64 | * @note if nullptr, bn_gamma is set to 1.0 |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 65 | * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 66 | * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution. |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 67 | */ |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 68 | void configure(const ITensor *input_weights, const ITensor *bn_mean, const ITensor *bn_var, ITensor *fused_weights, ITensor *fused_bias, |
| 69 | const ITensor *input_bias = nullptr, const ITensor *bn_beta = nullptr, const ITensor *bn_gamma = nullptr, |
| 70 | float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 71 | /** Static function to check if given info will lead to a valid configuration of @ref NEFuseBatchNormalization |
| 72 | * |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 73 | * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC |
| 74 | * @param[in] bn_mean Batch normalization layer mean tensor info. Same as @p input_weights |
| 75 | * @param[in] bn_var Batch normalization layer variance tensor info. Same as @p input_weights |
| 76 | * @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 |
| 77 | * @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 |
| 78 | * @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 |
| 79 | * @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 |
| 80 | * @note if nullptr, bn_beta is set to 0.0 |
| 81 | * @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 |
| 82 | * @note if nullptr, bn_gamma is set to 1.0 |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 83 | * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f. |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 84 | * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to Convolution. |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 85 | * |
| 86 | * @return a status |
| 87 | */ |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 88 | static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var, |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 89 | const ITensorInfo *fused_weights, const ITensorInfo *fused_bias, |
Manuel Bottini | 1109176 | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 90 | const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr, |
| 91 | float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION); |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 92 | |
| 93 | // Inherited methods overridden: |
| 94 | void run() override; |
| 95 | |
| 96 | private: |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 97 | std::unique_ptr<NEFuseBatchNormalizationKernel> _fuse_bn_kernel; |
giuros01 | 15ecc9a | 2018-12-06 10:47:34 +0000 | [diff] [blame] | 98 | }; |
| 99 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 100 | #endif /*ARM_COMPUTE_NEFUSEBATCHNORMALIZATION_H */ |