blob: 298c9ca4112d2f366b8726cf0ef860df23b38ca0 [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
2 * Copyright (c) 2017 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_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...>
48 void setup(TensorShape shape0, TensorShape shape1, float epsilon, DataType dt, int fractional_bits)
49 {
50 _fractional_bits = fractional_bits;
51 _data_type = dt;
52 _target = compute_target(shape0, shape1, epsilon, dt, fractional_bits);
53 _reference = compute_reference(shape0, shape1, epsilon, dt, fractional_bits);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&src_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor)
59 {
60 if(is_data_type_float(_data_type))
61 {
62 float min_bound = 0.f;
63 float max_bound = 0.f;
64 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>();
65 std::uniform_real_distribution<> distribution(min_bound, max_bound);
66 std::uniform_real_distribution<> distribution_var(0, max_bound);
67 library->fill(src_tensor, distribution, 0);
68 library->fill(mean_tensor, distribution, 1);
69 library->fill(var_tensor, distribution_var, 0);
70 library->fill(beta_tensor, distribution, 3);
71 library->fill(gamma_tensor, distribution, 4);
72 }
73 else
74 {
75 int min_bound = 0;
76 int max_bound = 0;
77 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>(_fractional_bits);
78 std::uniform_int_distribution<> distribution(min_bound, max_bound);
79 std::uniform_int_distribution<> distribution_var(0, max_bound);
80 library->fill(src_tensor, distribution, 0);
81 library->fill(mean_tensor, distribution, 1);
82 library->fill(var_tensor, distribution_var, 0);
83 library->fill(beta_tensor, distribution, 3);
84 library->fill(gamma_tensor, distribution, 4);
85 }
86 }
87
88 TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, float epsilon, DataType dt, int fixed_point_position)
89 {
90 // Create tensors
91 TensorType src = create_tensor<TensorType>(shape0, dt, 1, fixed_point_position);
92 TensorType dst = create_tensor<TensorType>(shape0, dt, 1, fixed_point_position);
93 TensorType mean = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
94 TensorType var = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
95 TensorType beta = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
96 TensorType gamma = create_tensor<TensorType>(shape1, dt, 1, fixed_point_position);
97
98 // Create and configure function
99 FunctionType norm;
100 norm.configure(&src, &dst, &mean, &var, &beta, &gamma, epsilon);
101
102 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(mean.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(var.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 src.allocator()->allocate();
111 dst.allocator()->allocate();
112 mean.allocator()->allocate();
113 var.allocator()->allocate();
114 beta.allocator()->allocate();
115 gamma.allocator()->allocate();
116
117 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
118 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
119 ARM_COMPUTE_EXPECT(!mean.info()->is_resizable(), framework::LogLevel::ERRORS);
120 ARM_COMPUTE_EXPECT(!var.info()->is_resizable(), framework::LogLevel::ERRORS);
121 ARM_COMPUTE_EXPECT(!beta.info()->is_resizable(), framework::LogLevel::ERRORS);
122 ARM_COMPUTE_EXPECT(!gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
123
124 // Fill tensors
125 fill(AccessorType(src), AccessorType(mean), AccessorType(var), AccessorType(beta), AccessorType(gamma));
126
127 // Compute function
128 norm.run();
129
130 return dst;
131 }
132
133 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, float epsilon, DataType dt, int fixed_point_position)
134 {
135 // Create reference
136 SimpleTensor<T> ref_src{ shape0, dt, 1, fixed_point_position };
137 SimpleTensor<T> ref_mean{ shape1, dt, 1, fixed_point_position };
138 SimpleTensor<T> ref_var{ shape1, dt, 1, fixed_point_position };
139 SimpleTensor<T> ref_beta{ shape1, dt, 1, fixed_point_position };
140 SimpleTensor<T> ref_gamma{ shape1, dt, 1, fixed_point_position };
141
142 // Fill reference
143 fill(ref_src, ref_mean, ref_var, ref_beta, ref_gamma);
144
145 return reference::batch_normalization_layer(ref_src, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, fixed_point_position);
146 }
147
148 TensorType _target{};
149 SimpleTensor<T> _reference{};
150 int _fractional_bits{};
151 DataType _data_type{};
152};
153
154template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
155class BatchNormalizationLayerValidationFixture : public BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
156{
157public:
158 template <typename...>
159 void setup(TensorShape shape0, TensorShape shape1, float epsilon, DataType dt)
160 {
161 BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, epsilon, dt, 0);
162 }
163};
164} // namespace validation
165} // namespace test
166} // namespace arm_compute
167#endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FIXTURE */