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