blob: 4f8a8da1fbea3a985537f1fea1f5f21f4a1e1cef [file] [log] [blame]
Manuel Bottinibffb41e2019-06-20 16:00:27 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019 Arm Limited.
Manuel Bottinibffb41e2019-06-20 16:00:27 +01003 *
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
Michalis Spyrouf4643372019-11-29 16:17:13 +000025#ifndef ARM_COMPUTE_GRAPH_BACKENDS_FUSED_DEPTHWISE_CONVOLUTION_BATCH_NORMALIZATION_FUNCTION_H
26#define ARM_COMPUTE_GRAPH_BACKENDS_FUSED_DEPTHWISE_CONVOLUTION_BATCH_NORMALIZATION_FUNCTION_H
Manuel Bottinibffb41e2019-06-20 16:00:27 +010027
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}DepthwiseConvolutionLayer with the modified weights */
38template <typename TargetInfo, typename FusedLayerTypes>
39class FusedDepthwiseConvolutionBatchNormalizationFunction : public IFunction
40{
41public:
42 using TensorType = typename TargetInfo::TensorType;
43 using TensorConcreteType = typename TargetInfo::TensorConcreteType;
44
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +000045 FusedDepthwiseConvolutionBatchNormalizationFunction(std::shared_ptr<IMemoryManager> memory_manager = nullptr)
46 : _depth_conv_layer(memory_manager), _fused_batch_norm_layer(), _fused_bias(), _is_prepared(false)
Manuel Bottinibffb41e2019-06-20 16:00:27 +010047 {
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: F16/F32.
55 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
56 * @param[in] bias Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [IFM].
57 * Data type supported: Should match @p input data type.
58 * @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] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
67 * @param[in] fused_act Activation layer information in case of a fused activation.
68 *
69 */
70 void configure(TensorType *input,
71 TensorType *weights,
72 TensorType *bias,
73 TensorType *output,
74 const TensorType *mean,
75 const TensorType *var,
76 const TensorType *beta,
77 const TensorType *gamma,
78 float epsilon, const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo const &fused_act)
79 {
80 // We don't run any validate, as we assume that the layers have been already validated
81 const bool has_bias = (bias != nullptr);
82 const TensorType *bias_to_use;
83
84 // We check if the layer has a bias. If yes, use it in-place. If not, we need to create one
85 // as batch normalization might end up with a bias != 0
86 if(has_bias)
87 {
88 _fused_batch_norm_layer.configure(weights, mean, var, nullptr, nullptr, bias, beta, gamma, epsilon, FuseBatchNormalizationType::DEPTHWISECONVOLUTION);
89 bias_to_use = bias;
90 }
91 else
92 {
93 _fused_batch_norm_layer.configure(weights, mean, var, nullptr, &_fused_bias, nullptr, beta, gamma, epsilon, FuseBatchNormalizationType::DEPTHWISECONVOLUTION);
94 bias_to_use = &_fused_bias;
95 }
96
97 _depth_conv_layer.configure(input, weights, bias_to_use, output, conv_info, depth_multiplier, fused_act.enabled() ? fused_act : ActivationLayerInfo());
98
99 if(!has_bias)
100 {
101 _fused_bias.allocator()->allocate();
102 }
103 }
104
105 // Inherited methods overridden:
106 void run()
107 {
108 prepare();
109 _depth_conv_layer.run();
110 }
111
112 void prepare()
113 {
114 if(!_is_prepared)
115 {
116 _fused_batch_norm_layer.run();
117 _is_prepared = true;
118 }
119 }
120
121private:
122 typename FusedLayerTypes::DepthwiseConvolutionLayer _depth_conv_layer;
123 typename FusedLayerTypes::FuseBatchNormalization _fused_batch_norm_layer;
124 TensorConcreteType _fused_bias;
125 bool _is_prepared;
126};
127} // namespace backends
128} // namespace graph
129} // namespace arm_compute
130
Michalis Spyrouf4643372019-11-29 16:17:13 +0000131#endif /* ARM_COMPUTE_GRAPH_BACKENDS_FUSED_DEPTHWISE_CONVOLUTION_BATCH_NORMALIZATION_FUNCTION_H */