blob: b7e32a6f376925cae04462d74a737ac494ecb70d [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
Giorgio Arena11674872018-02-07 15:38:12 +00002 * Copyright (c) 2017-2018 ARM Limited.
Sanghoon Lee96883782017-09-15 14:10:48 +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_BATCH_NORMALIZATION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#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"
Sanghoon Lee96883782017-09-15 14:10:48 +010034#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/BatchNormalizationLayer.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class BatchNormalizationLayerValidationFixedPointFixture : public framework::Fixture
45{
46public:
47 template <typename...>
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000048 void setup(TensorShape shape0, TensorShape shape1, float epsilon, bool use_beta, bool use_gamma, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout, int fractional_bits)
Sanghoon Lee96883782017-09-15 14:10:48 +010049 {
50 _fractional_bits = fractional_bits;
51 _data_type = dt;
Michele Di Giorgio4d336302018-03-02 09:43:54 +000052 _use_beta = use_beta;
53 _use_gamma = use_gamma;
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000054
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000055 _target = compute_target(shape0, shape1, epsilon, act_info, dt, data_layout, fractional_bits);
Giorgio Arena563494c2018-04-30 17:29:41 +010056 _reference = compute_reference(shape0, shape1, epsilon, act_info, dt, fractional_bits);
Sanghoon Lee96883782017-09-15 14:10:48 +010057 }
58
59protected:
60 template <typename U>
61 void fill(U &&src_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor)
62 {
63 if(is_data_type_float(_data_type))
64 {
65 float min_bound = 0.f;
66 float max_bound = 0.f;
67 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>();
68 std::uniform_real_distribution<> distribution(min_bound, max_bound);
69 std::uniform_real_distribution<> distribution_var(0, max_bound);
70 library->fill(src_tensor, distribution, 0);
71 library->fill(mean_tensor, distribution, 1);
72 library->fill(var_tensor, distribution_var, 0);
Michele Di Giorgio4d336302018-03-02 09:43:54 +000073 if(_use_beta)
74 {
75 library->fill(beta_tensor, distribution, 3);
76 }
77 else
78 {
79 // Fill with default value 0.f
80 library->fill_tensor_value(beta_tensor, 0.f);
81 }
82 if(_use_gamma)
83 {
84 library->fill(gamma_tensor, distribution, 4);
85 }
86 else
87 {
88 // Fill with default value 1.f
89 library->fill_tensor_value(gamma_tensor, 1.f);
90 }
Sanghoon Lee96883782017-09-15 14:10:48 +010091 }
92 else
93 {
94 int min_bound = 0;
95 int max_bound = 0;
96 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>(_fractional_bits);
97 std::uniform_int_distribution<> distribution(min_bound, max_bound);
98 std::uniform_int_distribution<> distribution_var(0, max_bound);
99 library->fill(src_tensor, distribution, 0);
100 library->fill(mean_tensor, distribution, 1);
101 library->fill(var_tensor, distribution_var, 0);
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000102 if(_use_beta)
103 {
104 library->fill(beta_tensor, distribution, 3);
105 }
106 else
107 {
108 // Fill with default value 0
109 library->fill_tensor_value(beta_tensor, static_cast<T>(0));
110 }
111 if(_use_gamma)
112 {
113 library->fill(gamma_tensor, distribution, 4);
114 }
115 else
116 {
117 // Fill with default value 1
118 library->fill_tensor_value(gamma_tensor, static_cast<T>(1 << (_fractional_bits)));
119 }
Sanghoon Lee96883782017-09-15 14:10:48 +0100120 }
121 }
122
Giorgio Arena563494c2018-04-30 17:29:41 +0100123 TensorType compute_target(TensorShape shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout, int fixed_point_position)
Sanghoon Lee96883782017-09-15 14:10:48 +0100124 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100125 if(data_layout == DataLayout::NHWC)
126 {
127 permute(shape0, PermutationVector(2U, 0U, 1U));
128 }
129
Sanghoon Lee96883782017-09-15 14:10:48 +0100130 // Create tensors
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000131 TensorType src = create_tensor<TensorType>(shape0, dt, 1, fixed_point_position, QuantizationInfo(), data_layout);
132 TensorType dst = create_tensor<TensorType>(shape0, dt, 1, fixed_point_position, QuantizationInfo(), data_layout);
Sanghoon Lee96883782017-09-15 14:10:48 +0100133 TensorType mean = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
134 TensorType var = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
135 TensorType beta = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
136 TensorType gamma = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
137
138 // Create and configure function
139 FunctionType norm;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000140 TensorType *beta_ptr = _use_beta ? &beta : nullptr;
141 TensorType *gamma_ptr = _use_gamma ? &gamma : nullptr;
142 norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon, act_info);
Sanghoon Lee96883782017-09-15 14:10:48 +0100143
144 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
145 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
146 ARM_COMPUTE_EXPECT(mean.info()->is_resizable(), framework::LogLevel::ERRORS);
147 ARM_COMPUTE_EXPECT(var.info()->is_resizable(), framework::LogLevel::ERRORS);
148 ARM_COMPUTE_EXPECT(beta.info()->is_resizable(), framework::LogLevel::ERRORS);
149 ARM_COMPUTE_EXPECT(gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
150
151 // Allocate tensors
152 src.allocator()->allocate();
153 dst.allocator()->allocate();
154 mean.allocator()->allocate();
155 var.allocator()->allocate();
156 beta.allocator()->allocate();
157 gamma.allocator()->allocate();
158
159 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
160 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
161 ARM_COMPUTE_EXPECT(!mean.info()->is_resizable(), framework::LogLevel::ERRORS);
162 ARM_COMPUTE_EXPECT(!var.info()->is_resizable(), framework::LogLevel::ERRORS);
163 ARM_COMPUTE_EXPECT(!beta.info()->is_resizable(), framework::LogLevel::ERRORS);
164 ARM_COMPUTE_EXPECT(!gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
165
166 // Fill tensors
167 fill(AccessorType(src), AccessorType(mean), AccessorType(var), AccessorType(beta), AccessorType(gamma));
168
169 // Compute function
170 norm.run();
171
172 return dst;
173 }
174
Giorgio Arena563494c2018-04-30 17:29:41 +0100175 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, int fixed_point_position)
Sanghoon Lee96883782017-09-15 14:10:48 +0100176 {
177 // Create reference
Giorgio Arena563494c2018-04-30 17:29:41 +0100178 SimpleTensor<T> ref_src{ shape0, dt, 1, fixed_point_position };
Sanghoon Lee96883782017-09-15 14:10:48 +0100179 SimpleTensor<T> ref_mean{ shape1, dt, 1, fixed_point_position };
180 SimpleTensor<T> ref_var{ shape1, dt, 1, fixed_point_position };
181 SimpleTensor<T> ref_beta{ shape1, dt, 1, fixed_point_position };
182 SimpleTensor<T> ref_gamma{ shape1, dt, 1, fixed_point_position };
183
184 // Fill reference
185 fill(ref_src, ref_mean, ref_var, ref_beta, ref_gamma);
186
Giorgio Arena11674872018-02-07 15:38:12 +0000187 return reference::batch_normalization_layer(ref_src, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, act_info, fixed_point_position);
Sanghoon Lee96883782017-09-15 14:10:48 +0100188 }
189
190 TensorType _target{};
191 SimpleTensor<T> _reference{};
192 int _fractional_bits{};
193 DataType _data_type{};
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000194 bool _use_beta{};
195 bool _use_gamma{};
Sanghoon Lee96883782017-09-15 14:10:48 +0100196};
197
198template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
199class BatchNormalizationLayerValidationFixture : public BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
200{
201public:
202 template <typename...>
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000203 void setup(TensorShape shape0, TensorShape shape1, float epsilon, bool use_beta, bool use_gamma, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout)
Sanghoon Lee96883782017-09-15 14:10:48 +0100204 {
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000205 BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, epsilon, use_beta, use_gamma, act_info, dt, data_layout, 0);
Sanghoon Lee96883782017-09-15 14:10:48 +0100206 }
207};
208} // namespace validation
209} // namespace test
210} // namespace arm_compute
211#endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FIXTURE */