blob: a6da76bb065fa18e483ac8a3766af7fd82d24d32 [file] [log] [blame]
giuros01acce5042019-02-21 17:32:34 +00001/*
2 * Copyright (c) 2019 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
25#ifndef __ARM_COMPUTE_GRAPH_BACKENDS_FUSED_CONVOLUTION_BATCH_NORMAZLIZATION_FUNCTION_H__
26#define __ARM_COMPUTE_GRAPH_BACKENDS_FUSED_CONVOLUTION_BATCH_NORMAZLIZATION_FUNCTION_H__
27
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/IFunction.h"
30
31namespace arm_compute
32{
33namespace graph
34{
35namespace backends
36{
37/** Wrapper function to first apply {NE, CL}BatchNormalizationLayer on the weights and then run {NE, CL}ConvolutionLayer with the modified weights */
38template <typename TargetInfo, typename FusedLayerTypes>
39class FusedConvolutionBatchNormalizationFunction : public IFunction
40{
41public:
42 using TensorType = typename TargetInfo::TensorType;
43 using TensorConcreteType = typename TargetInfo::TensorConcreteType;
44
45 FusedConvolutionBatchNormalizationFunction()
46 : _conv_layer(), _fused_batch_norm_layer(), _fused_bias(), _is_prepared(false)
47 {
48 }
49
50 /** Set the input and output tensors.
51 *
52 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
53 * while every optional dimension from 4 and above represent a batch of inputs.
54 * Data types supported: QASYMM8/F16/F32.
55 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
56 * @param[in] bias Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Manuel Bottinibffb41e2019-06-20 16:00:27 +010057 * Data type supported: Should match @p input data type.
giuros01acce5042019-02-21 17:32:34 +000058 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
59 * Data types supported: Same as @p input.
60 * @param[in] mean Mean values tensor. 1 dimension with size equal to the feature maps [FM]. Data types supported: Same as @p input
61 * @param[in] var Variance values tensor. 1 dimension with size equal to the feature maps [FM]. Data types supported: Same as @p input
62 * @param[in] beta Beta values tensor info. 1 dimension with size equal to the feature maps [FM]. If not provided, default value for beta is 0. Data types supported: Same as @p input
63 * @param[in] gamma Gamma values tensor info. 1 dimension with size equal to the feature maps [FM]. If not provided, default value for gamma is 1. Data types supported: Same as @p input
64 * @param[in] epsilon Small value to avoid division with zero. Default value is 0.001f.
65 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
66 * @param[in] num_groups Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
67 * @param[in] fast_math Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
68 * available which may introduce a drop of accuracy as well. Default is false
69 * @param[in] fused_act Activation layer information in case of a fused activation.
70 *
71 */
72 void configure(TensorType *input,
73 TensorType *weights,
74 TensorType *bias,
75 TensorType *output,
76 const TensorType *mean,
77 const TensorType *var,
78 const TensorType *beta,
79 const TensorType *gamma,
80 float epsilon, const PadStrideInfo &conv_info, unsigned int num_groups, bool fast_math, ActivationLayerInfo const &fused_act)
81 {
82 // We don't run any validate, as we assume that the layers have been already validated
83 const bool has_bias = (bias != nullptr);
84 const TensorType *bias_to_use;
85
86 // We check if the layer has a bias. If yes, use it in-place. If not, we need to create one
87 // as batch normalization might end up with a bias != 0
88 if(has_bias)
89 {
90 _fused_batch_norm_layer.configure(weights, mean, var, nullptr, nullptr, bias, beta, gamma, epsilon);
91 bias_to_use = bias;
92 }
93 else
94 {
95 _fused_batch_norm_layer.configure(weights, mean, var, nullptr, &_fused_bias, nullptr, beta, gamma, epsilon);
96 bias_to_use = &_fused_bias;
97 }
98
99 _conv_layer.configure(input, weights, bias_to_use, output, conv_info, WeightsInfo(), Size2D(1U, 1U), fused_act, fast_math, num_groups);
100
101 if(!has_bias)
102 {
103 _fused_bias.allocator()->allocate();
104 }
105 }
106
107 // Inherited methods overridden:
108 void run()
109 {
110 prepare();
111 _conv_layer.run();
112 }
113
114 void prepare()
115 {
116 if(!_is_prepared)
117 {
118 _fused_batch_norm_layer.run();
119 _is_prepared = true;
120 }
121 }
122
123private:
124 typename FusedLayerTypes::ConvolutionLayer _conv_layer;
125 typename FusedLayerTypes::FuseBatchNormalization _fused_batch_norm_layer;
126 TensorConcreteType _fused_bias;
127 bool _is_prepared;
128};
129} // namespace backends
130} // namespace graph
131} // namespace arm_compute
132
133#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_FUSED_CONVOLUTION_BATCH_NORMAZLIZATION_FUNCTION_H__ */