blob: f598530d1e04e013ad8a2478bda8f70df4364856 [file] [log] [blame]
giuros0115ecc9a2018-12-06 10:47:34 +00001/*
George Wort5a97b282018-12-21 16:21:04 +00002 * Copyright (c) 2018-2019 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
27#include "arm_compute/core/NEON/INEKernel.h"
28
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 */
Manuel Bottini11091762019-06-17 12:04:40 +010069 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);
giuros0115ecc9a2018-12-06 10:47:34 +000072 /** Static function to check if given info will lead to a valid configuration of @ref NEFuseBatchNormalizationKernel
73 *
Manuel Bottini11091762019-06-17 12:04:40 +010074 * @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
giuros0115ecc9a2018-12-06 10:47:34 +000084 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
Manuel Bottini11091762019-06-17 12:04:40 +010085 * @param[in] fbn_type (Optional) Fused batch normalization type. Defaults to CONVOLUTION.
giuros0115ecc9a2018-12-06 10:47:34 +000086 *
87 * @return a status
88 */
Manuel Bottini11091762019-06-17 12:04:40 +010089 static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
giuros0115ecc9a2018-12-06 10:47:34 +000090 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
Manuel Bottini11091762019-06-17 12:04:40 +010091 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);
giuros0115ecc9a2018-12-06 10:47:34 +000093
94 // Inherited methods overridden:
95 void run(const Window &window, const ThreadInfo &info) override;
96
97private:
Manuel Bottini11091762019-06-17 12:04:40 +010098 const ITensor *_input_weights;
99 const ITensor *_input_bias;
giuros0115ecc9a2018-12-06 10:47:34 +0000100 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
Manuel Bottini11091762019-06-17 12:04:40 +0100110 using FuseBatchNormFunction = void(const ITensor *input_weights, const ITensor *input_bias, ITensor *fused_weights, ITensor *fused_bias,
giuros0115ecc9a2018-12-06 10:47:34 +0000111 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
Michalis Spyrouf4643372019-11-29 16:17:13 +0000116#endif /*ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H */