• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
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
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  */
24 #ifndef ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H
25 #define ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H
26 
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/runtime/IFunction.h"
29 
30 #include <memory>
31 
32 namespace arm_compute
33 {
34 // Forward declarations
35 class CLCompileContext;
36 class CLFuseBatchNormalizationKernel;
37 class ICLTensor;
38 class ITensorInfo;
39 
40 /** Basic function to fuse the batch normalization node to a preceding convolution node */
41 class CLFuseBatchNormalization : public IFunction
42 {
43 public:
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 */
55     ~CLFuseBatchNormalization();
56     /** Set the input and output tensors.
57      *
58      * @param[in]  input_weights Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
59      * @param[in]  bn_mean       Batch normalization layer mean tensor. Same as @p input_weights
60      * @param[in]  bn_var        Batch normalization layer variance tensor. Same as @p input_weights
61      * @param[out] fused_weights Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
62      * @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
63      * @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
64      * @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
65      *                           @note if nullptr, bn_beta is set to 0.0
66      * @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
67      *                           @note if nullptr, bn_gamma is set to 1.0
68      * @param[in]  epsilon       (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
69      * @param[in]  fbn_type      (Optional) Fused batch normalization type. Defaults to Convolution.
70      */
71     void configure(const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias,
72                    const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
73                    float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
74     /** Set the input and output tensors.
75      *
76      * @param[in]  compile_context The compile context to be used.
77      * @param[in]  input_weights   Input weights tensor for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
78      * @param[in]  bn_mean         Batch normalization layer mean tensor. Same as @p input_weights
79      * @param[in]  bn_var          Batch normalization layer variance tensor. Same as @p input_weights
80      * @param[out] fused_weights   Output fused weights tensor. It can be a nullptr in case of in-place computation. Same as @p input_weights
81      * @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
82      * @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
83      * @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
84      *                             @note if nullptr, bn_beta is set to 0.0
85      * @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
86      *                             @note if nullptr, bn_gamma is set to 1.0
87      * @param[in]  epsilon         (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
88      * @param[in]  fbn_type        (Optional) Fused batch normalization type. Defaults to Convolution.
89      */
90     void configure(const CLCompileContext &compile_context, const ICLTensor *input_weights, const ICLTensor *bn_mean, const ICLTensor *bn_var, ICLTensor *fused_weights, ICLTensor *fused_bias,
91                    const ICLTensor *input_bias = nullptr, const ICLTensor *bn_beta = nullptr, const ICLTensor *bn_gamma = nullptr,
92                    float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
93     /** Static function to check if given info will lead to a valid configuration of @ref CLFuseBatchNormalization
94      *
95      * @param[in] input_weights Input weights tensor info for convolution or depthwise convolution layer. Data type supported: F16/F32. Data layout supported: NCHW, NHWC
96      * @param[in] bn_mean       Batch normalization layer mean tensor info. Same as @p input_weights
97      * @param[in] bn_var        Batch normalization layer variance tensor info. Same as @p input_weights
98      * @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
99      * @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
100      * @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
101      * @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
102      *                          @note if nullptr, bn_beta is set to 0.0
103      * @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
104      *                          @note if nullptr, bn_gamma is set to 1.0
105      * @param[in] epsilon       (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
106      * @param[in] fbn_type      (Optional) Fused batch normalization type. Defaults to Convolution.
107      *
108      * @return a status
109      */
110     static Status validate(const ITensorInfo *input_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
111                            const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
112                            const ITensorInfo *input_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr,
113                            float epsilon = 0.001f, FuseBatchNormalizationType fbn_type = FuseBatchNormalizationType::CONVOLUTION);
114 
115     // Inherited methods overridden:
116     void run() override;
117 
118 private:
119     std::unique_ptr<CLFuseBatchNormalizationKernel> _fuse_bn_kernel;
120 };
121 } // namespace arm_compute
122 #endif /*ARM_COMPUTE_CLFUSEBATCHNORMALIZATION_H */
123