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