blob: a05e4169a7ae297187627dd5225199f9ad598035 [file] [log] [blame]
Gian Marco Iodice761c8d02019-06-10 14:46:49 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Gian Marco Iodice761c8d02019-06-10 14:46:49 +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#ifndef ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE
25#define ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/Helpers.h"
35#include "tests/validation/reference/FuseBatchNormalization.h"
36
37#include <tuple>
38#include <utility>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, int dims_weights, typename T>
47class FuseBatchNormalizationFixture : public framework::Fixture
48{
49public:
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010050 void setup(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta)
51 {
52 std::tie(_target_w, _target_b) = compute_target(shape_w, data_type, data_layout, in_place, with_bias, with_gamma, with_beta);
Michalis Spyrou6bff1952019-10-02 17:22:11 +010053 std::tie(_reference_w, _reference_b) = compute_reference(shape_w, data_type, with_bias, with_gamma, with_beta);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010054 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor, int i, float min, float max)
59 {
60 library->fill_tensor_uniform(tensor, i, min, max);
61 }
62
63 std::pair<TensorType, TensorType> compute_target(TensorShape shape_w, DataType data_type, DataLayout data_layout, bool in_place, bool with_bias, bool with_gamma, bool with_beta)
64 {
65 const TensorShape shape_v(shape_w[dims_weights - 1]);
66
67 if(data_layout == DataLayout::NHWC)
68 {
69 permute(shape_w, PermutationVector(2U, 0U, 1U));
70 }
71
72 const bool in_place_w = in_place;
73 const bool in_place_b = with_bias ? in_place : false;
74
75 // Create tensors
76 TensorType w = create_tensor<TensorType>(shape_w, data_type, 1, QuantizationInfo(), data_layout);
77 TensorType b = create_tensor<TensorType>(shape_v, data_type);
78 TensorType mean = create_tensor<TensorType>(shape_v, data_type);
79 TensorType var = create_tensor<TensorType>(shape_v, data_type);
80 TensorType w_fused = create_tensor<TensorType>(shape_w, data_type, 1, QuantizationInfo(), data_layout);
81 TensorType b_fused = create_tensor<TensorType>(shape_v, data_type);
82 TensorType beta = create_tensor<TensorType>(shape_v, data_type);
83 TensorType gamma = create_tensor<TensorType>(shape_v, data_type);
84
85 auto b_to_use = with_bias ? &b : nullptr;
86 auto gamma_to_use = with_gamma ? &gamma : nullptr;
87 auto beta_to_use = with_beta ? &beta : nullptr;
88 auto w_fused_to_use = in_place_w ? nullptr : &w_fused;
89 auto b_fused_to_use = in_place_b ? nullptr : &b_fused;
90
Manuel Bottini2732cca2019-05-28 11:44:41 +010091 const FuseBatchNormalizationType fuse_bn_type = dims_weights == 3 ?
92 FuseBatchNormalizationType::DEPTHWISECONVOLUTION :
93 FuseBatchNormalizationType::CONVOLUTION;
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010094 // Create and configure function
95 FunctionType fuse_batch_normalization;
Manuel Bottini2732cca2019-05-28 11:44:41 +010096 fuse_batch_normalization.configure(&w, &mean, &var, w_fused_to_use, b_fused_to_use, b_to_use, beta_to_use, gamma_to_use, _epsilon, fuse_bn_type);
Gian Marco Iodice761c8d02019-06-10 14:46:49 +010097
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010098 ARM_COMPUTE_ASSERT(w.info()->is_resizable());
99 ARM_COMPUTE_ASSERT(b.info()->is_resizable());
100 ARM_COMPUTE_ASSERT(mean.info()->is_resizable());
101 ARM_COMPUTE_ASSERT(var.info()->is_resizable());
102 ARM_COMPUTE_ASSERT(w_fused.info()->is_resizable());
103 ARM_COMPUTE_ASSERT(b_fused.info()->is_resizable());
104 ARM_COMPUTE_ASSERT(beta.info()->is_resizable());
105 ARM_COMPUTE_ASSERT(gamma.info()->is_resizable());
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100106
107 // Allocate tensors
108 w.allocator()->allocate();
109 b.allocator()->allocate();
110 mean.allocator()->allocate();
111 var.allocator()->allocate();
112 w_fused.allocator()->allocate();
113 b_fused.allocator()->allocate();
114 beta.allocator()->allocate();
115 gamma.allocator()->allocate();
116
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100117 ARM_COMPUTE_ASSERT(!w.info()->is_resizable());
118 ARM_COMPUTE_ASSERT(!b.info()->is_resizable());
119 ARM_COMPUTE_ASSERT(!mean.info()->is_resizable());
120 ARM_COMPUTE_ASSERT(!var.info()->is_resizable());
121 ARM_COMPUTE_ASSERT(!w_fused.info()->is_resizable());
122 ARM_COMPUTE_ASSERT(!b_fused.info()->is_resizable());
123 ARM_COMPUTE_ASSERT(!beta.info()->is_resizable());
124 ARM_COMPUTE_ASSERT(!gamma.info()->is_resizable());
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100125
126 // Fill tensors
127 fill(AccessorType(w), 0U, -1.0f, 1.0f);
128 fill(AccessorType(b), 1U, -1.0f, 1.0f);
129 fill(AccessorType(mean), 2U, -1.0f, 1.0f);
130 fill(AccessorType(var), 3U, 0.0f, 1.0f);
131 fill(AccessorType(beta), 4U, -1.0f, 1.0f);
132 fill(AccessorType(gamma), 5U, -1.0f, 1.0f);
133
134 // Compute function
135 fuse_batch_normalization.run();
136
137 return std::make_pair(std::move(in_place_w ? w : w_fused), std::move(in_place_b ? b : b_fused));
138 }
139
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100140 std::pair<SimpleTensor<T>, SimpleTensor<T>> compute_reference(TensorShape shape_w, DataType data_type, bool with_bias, bool with_gamma, bool with_beta)
Gian Marco Iodice761c8d02019-06-10 14:46:49 +0100141 {
142 const TensorShape shape_v(shape_w[dims_weights - 1]);
143
144 SimpleTensor<T> w{ shape_w, data_type };
145 SimpleTensor<T> b{ shape_v, data_type };
146 SimpleTensor<T> mean{ shape_v, data_type };
147 SimpleTensor<T> var{ shape_v, data_type };
148 SimpleTensor<T> w_fused{ shape_w, data_type };
149 SimpleTensor<T> b_fused{ shape_v, data_type };
150 SimpleTensor<T> beta{ shape_v, data_type };
151 SimpleTensor<T> gamma{ shape_v, data_type };
152
153 // Fill reference tensor
154 fill(w, 0U, -1.0f, 1.0f);
155 fill(b, 1U, -1.0f, 1.0f);
156 fill(mean, 2U, -1.0f, 1.0f);
157 fill(var, 3U, 0.0f, 1.0f);
158 fill(beta, 4U, -1.0f, 1.0f);
159 fill(gamma, 5U, -1.0f, 1.0f);
160
161 if(!with_bias)
162 {
163 // Fill with zeros
164 fill(b, 0U, 0.0f, 0.0f);
165 }
166
167 if(!with_gamma)
168 {
169 // Fill with ones
170 fill(gamma, 0U, 1.0f, 1.0f);
171 }
172
173 if(!with_beta)
174 {
175 // Fill with zeros
176 fill(beta, 0U, 0.0f, 0.0f);
177 }
178
179 switch(dims_weights)
180 {
181 case 3:
182 // Weights for depth wise convolution layer
183 reference::fuse_batch_normalization_dwc_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon);
184 break;
185 case 4:
186 // Weights for convolution layer
187 reference::fuse_batch_normalization_conv_layer(w, mean, var, w_fused, b_fused, b, beta, gamma, _epsilon);
188 break;
189 default:
190 ARM_COMPUTE_ERROR("Not supported number of dimensions for the input weights tensor");
191 }
192
193 return std::make_pair(std::move(w_fused), std::move(b_fused));
194 }
195
196 const float _epsilon{ 0.0001f };
197 TensorType _target_w{};
198 TensorType _target_b{};
199 SimpleTensor<T> _reference_w{};
200 SimpleTensor<T> _reference_b{};
201};
202} // namespace validation
203} // namespace test
204} // namespace arm_compute
205#endif /* ARM_COMPUTE_TEST_FUSEBATCHNORMALIZATION_FIXTURE */