blob: 97d6a9f55b95078e40915ab880a82706afbc198f [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 */
24#ifndef __ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H__
25#define __ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H__
26
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 *
56 * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32
57 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights
58 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights
59 * @param[out] fused_weights Output fused weights tensor. Same as @p conv_weights
60 * @param[out] fused_bias Output fused bias tensor. Same as @p conv_weights
61 * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights
62 * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights
63 * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights
64 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
65 */
66 void configure(const ITensor *conv_weights, const ITensor *bn_mean, const ITensor *bn_var, ITensor *fused_weights, ITensor *fused_bias,
67 const ITensor *conv_bias = nullptr, const ITensor *bn_beta = nullptr, const ITensor *bn_gamma = nullptr,
68 float epsilon = 0.001f);
69 /** Static function to check if given info will lead to a valid configuration of @ref NEFuseBatchNormalizationKernel
70 *
71 * @param[in] conv_weights Convolution layer weights tensor. Data type supported: F16/F32
72 * @param[in] bn_mean Batch normalization layer mean tensor. Same as @p conv_weights
73 * @param[in] bn_var Batch normalization layer variance tensor. Same as @p conv_weights
74 * @param[in] fused_weights Output fused weights tensor. Same as @p conv_weights
75 * @param[in] fused_bias Output fused bias tensor. Same as @p conv_weights
76 * @param[in] conv_bias (Optional) Convolution layer bias tensor. Same as @p conv_weights
77 * @param[in] bn_beta (Optional) Batch normalization layer beta tensor. Same as @p conv_weights
78 * @param[in] bn_gamma (Optional) Batch normalization layer gamma tensor. Same as @p conv_weights
79 * @param[in] epsilon (Optional) Batch normalization layer epsilon parameter. Defaults to 0.001f.
80 *
81 * @return a status
82 */
83 static Status validate(const ITensorInfo *conv_weights, const ITensorInfo *bn_mean, const ITensorInfo *bn_var,
84 const ITensorInfo *fused_weights, const ITensorInfo *fused_bias,
85 const ITensorInfo *conv_bias = nullptr, const ITensorInfo *bn_beta = nullptr, const ITensorInfo *bn_gamma = nullptr,
86 float epsilon = 0.001f);
87
88 // Inherited methods overridden:
89 void run(const Window &window, const ThreadInfo &info) override;
90
91private:
92 const ITensor *_conv_weights;
93 const ITensor *_conv_bias;
94 const ITensor *_bn_mean;
95 const ITensor *_bn_var;
96 const ITensor *_bn_gamma;
97 const ITensor *_bn_beta;
98 ITensor *_fused_weights;
99 ITensor *_fused_bias;
100 float _epsilon;
101 bool _run_in_place_weights;
102 bool _run_in_place_bias;
103
104 using FuseBatchNormFunction = void(const ITensor *conv_weights, const ITensor *conv_bias, ITensor *fused_weights, ITensor *fused_bias,
105 const ITensor *bn_mean, const ITensor *bn_var, const ITensor *bn_beta, const ITensor *bn_gamma, float epsilon, const Window &window);
106
107 FuseBatchNormFunction *_func;
108};
109} // namespace arm_compute
110#endif /*__ARM_COMPUTE_NEFUSEBATCHNORMALIZATIONKERNEL_H__ */