blob: f5a5420df3f7376baee717a908f1d44ba2f99f1f [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
Giorgio Arena33b103b2021-01-08 10:37:15 +00002 * Copyright (c) 2017-2021 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>
Giorgio Arenac3b2e072018-08-10 16:27:11 +010044class BatchNormalizationLayerValidationFixture : public framework::Fixture
Sanghoon Lee96883782017-09-15 14:10:48 +010045{
46public:
47 template <typename...>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010048 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 +010049 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 _data_type = dt;
51 _use_beta = use_beta;
52 _use_gamma = use_gamma;
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000053
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010054 _target = compute_target(shape0, shape1, epsilon, act_info, dt, data_layout);
55 _reference = compute_reference(shape0, shape1, epsilon, act_info, dt);
Sanghoon Lee96883782017-09-15 14:10:48 +010056 }
57
58protected:
59 template <typename U>
60 void fill(U &&src_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor)
61 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000062 static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported.");
Giorgio Arena33b103b2021-01-08 10:37:15 +000063 using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
Giorgio Arena4bdd1772020-12-17 16:47:07 +000064
65 const T min_bound = T(-1.f);
66 const T max_bound = T(1.f);
67 DistributionType distribution{ min_bound, max_bound };
68 DistributionType distribution_var{ T(0.f), max_bound };
69
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010070 library->fill(src_tensor, distribution, 0);
71 library->fill(mean_tensor, distribution, 1);
72 library->fill(var_tensor, distribution_var, 0);
73 if(_use_beta)
Sanghoon Lee96883782017-09-15 14:10:48 +010074 {
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010075 library->fill(beta_tensor, distribution, 3);
Sanghoon Lee96883782017-09-15 14:10:48 +010076 }
77 else
78 {
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010079 // Fill with default value 0.f
Giorgio Arena4bdd1772020-12-17 16:47:07 +000080 library->fill_tensor_value(beta_tensor, T(0.f));
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010081 }
82 if(_use_gamma)
83 {
84 library->fill(gamma_tensor, distribution, 4);
85 }
86 else
87 {
88 // Fill with default value 1.f
Giorgio Arena4bdd1772020-12-17 16:47:07 +000089 library->fill_tensor_value(gamma_tensor, T(1.f));
Sanghoon Lee96883782017-09-15 14:10:48 +010090 }
91 }
92
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010093 TensorType compute_target(TensorShape shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout)
Sanghoon Lee96883782017-09-15 14:10:48 +010094 {
Giorgio Arena563494c2018-04-30 17:29:41 +010095 if(data_layout == DataLayout::NHWC)
96 {
97 permute(shape0, PermutationVector(2U, 0U, 1U));
98 }
99
Sanghoon Lee96883782017-09-15 14:10:48 +0100100 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100101 TensorType src = create_tensor<TensorType>(shape0, dt, 1, QuantizationInfo(), data_layout);
102 TensorType dst = create_tensor<TensorType>(shape0, dt, 1, QuantizationInfo(), data_layout);
103 TensorType mean = create_tensor<TensorType>(shape1, dt, 1);
104 TensorType var = create_tensor<TensorType>(shape1, dt, 1);
105 TensorType beta = create_tensor<TensorType>(shape1, dt, 1);
106 TensorType gamma = create_tensor<TensorType>(shape1, dt, 1);
Sanghoon Lee96883782017-09-15 14:10:48 +0100107
108 // Create and configure function
109 FunctionType norm;
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000110 TensorType *beta_ptr = _use_beta ? &beta : nullptr;
111 TensorType *gamma_ptr = _use_gamma ? &gamma : nullptr;
112 norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon, act_info);
Sanghoon Lee96883782017-09-15 14:10:48 +0100113
114 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
116 ARM_COMPUTE_EXPECT(mean.info()->is_resizable(), framework::LogLevel::ERRORS);
117 ARM_COMPUTE_EXPECT(var.info()->is_resizable(), framework::LogLevel::ERRORS);
118 ARM_COMPUTE_EXPECT(beta.info()->is_resizable(), framework::LogLevel::ERRORS);
119 ARM_COMPUTE_EXPECT(gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
120
121 // Allocate tensors
122 src.allocator()->allocate();
123 dst.allocator()->allocate();
124 mean.allocator()->allocate();
125 var.allocator()->allocate();
126 beta.allocator()->allocate();
127 gamma.allocator()->allocate();
128
129 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
131 ARM_COMPUTE_EXPECT(!mean.info()->is_resizable(), framework::LogLevel::ERRORS);
132 ARM_COMPUTE_EXPECT(!var.info()->is_resizable(), framework::LogLevel::ERRORS);
133 ARM_COMPUTE_EXPECT(!beta.info()->is_resizable(), framework::LogLevel::ERRORS);
134 ARM_COMPUTE_EXPECT(!gamma.info()->is_resizable(), framework::LogLevel::ERRORS);
135
136 // Fill tensors
137 fill(AccessorType(src), AccessorType(mean), AccessorType(var), AccessorType(beta), AccessorType(gamma));
138
139 // Compute function
140 norm.run();
141
142 return dst;
143 }
144
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100145 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt)
Sanghoon Lee96883782017-09-15 14:10:48 +0100146 {
147 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100148 SimpleTensor<T> ref_src{ shape0, dt, 1 };
149 SimpleTensor<T> ref_mean{ shape1, dt, 1 };
150 SimpleTensor<T> ref_var{ shape1, dt, 1 };
151 SimpleTensor<T> ref_beta{ shape1, dt, 1 };
152 SimpleTensor<T> ref_gamma{ shape1, dt, 1 };
Sanghoon Lee96883782017-09-15 14:10:48 +0100153
154 // Fill reference
155 fill(ref_src, ref_mean, ref_var, ref_beta, ref_gamma);
156
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100157 return reference::batch_normalization_layer(ref_src, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, act_info);
Sanghoon Lee96883782017-09-15 14:10:48 +0100158 }
159
160 TensorType _target{};
161 SimpleTensor<T> _reference{};
Sanghoon Lee96883782017-09-15 14:10:48 +0100162 DataType _data_type{};
Michele Di Giorgio4d336302018-03-02 09:43:54 +0000163 bool _use_beta{};
164 bool _use_gamma{};
Sanghoon Lee96883782017-09-15 14:10:48 +0100165};
Sanghoon Lee96883782017-09-15 14:10:48 +0100166} // namespace validation
167} // namespace test
168} // namespace arm_compute
169#endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FIXTURE */