Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 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 Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 34 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/BatchNormalizationLayer.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 44 | class BatchNormalizationLayerValidationFixedPointFixture : public framework::Fixture |
| 45 | { |
| 46 | public: |
| 47 | template <typename...> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 48 | void setup(TensorShape shape0, TensorShape shape1, float epsilon, bool use_beta, bool use_gamma, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 49 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 50 | _data_type = dt; |
| 51 | _use_beta = use_beta; |
| 52 | _use_gamma = use_gamma; |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 53 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 54 | _target = compute_target(shape0, shape1, epsilon, act_info, dt, data_layout); |
| 55 | _reference = compute_reference(shape0, shape1, epsilon, act_info, dt); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&src_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor) |
| 61 | { |
| 62 | if(is_data_type_float(_data_type)) |
| 63 | { |
| 64 | float min_bound = 0.f; |
| 65 | float max_bound = 0.f; |
| 66 | std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>(); |
| 67 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 68 | std::uniform_real_distribution<> distribution_var(0, max_bound); |
| 69 | library->fill(src_tensor, distribution, 0); |
| 70 | library->fill(mean_tensor, distribution, 1); |
| 71 | library->fill(var_tensor, distribution_var, 0); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 72 | if(_use_beta) |
| 73 | { |
| 74 | library->fill(beta_tensor, distribution, 3); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | // Fill with default value 0.f |
| 79 | library->fill_tensor_value(beta_tensor, 0.f); |
| 80 | } |
| 81 | if(_use_gamma) |
| 82 | { |
| 83 | library->fill(gamma_tensor, distribution, 4); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | // Fill with default value 1.f |
| 88 | library->fill_tensor_value(gamma_tensor, 1.f); |
| 89 | } |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
| 93 | int min_bound = 0; |
| 94 | int max_bound = 0; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 95 | std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<T>(); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 96 | std::uniform_int_distribution<> distribution(min_bound, max_bound); |
| 97 | std::uniform_int_distribution<> distribution_var(0, max_bound); |
| 98 | library->fill(src_tensor, distribution, 0); |
| 99 | library->fill(mean_tensor, distribution, 1); |
| 100 | library->fill(var_tensor, distribution_var, 0); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 101 | if(_use_beta) |
| 102 | { |
| 103 | library->fill(beta_tensor, distribution, 3); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | // Fill with default value 0 |
| 108 | library->fill_tensor_value(beta_tensor, static_cast<T>(0)); |
| 109 | } |
| 110 | if(_use_gamma) |
| 111 | { |
| 112 | library->fill(gamma_tensor, distribution, 4); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | // Fill with default value 1 |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 117 | library->fill_tensor_value(gamma_tensor, static_cast<T>(1)); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 118 | } |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 122 | TensorType compute_target(TensorShape shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 123 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 124 | if(data_layout == DataLayout::NHWC) |
| 125 | { |
| 126 | permute(shape0, PermutationVector(2U, 0U, 1U)); |
| 127 | } |
| 128 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 129 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 130 | TensorType src = create_tensor<TensorType>(shape0, dt, 1, QuantizationInfo(), data_layout); |
| 131 | TensorType dst = create_tensor<TensorType>(shape0, dt, 1, QuantizationInfo(), data_layout); |
| 132 | TensorType mean = create_tensor<TensorType>(shape1, dt, 1); |
| 133 | TensorType var = create_tensor<TensorType>(shape1, dt, 1); |
| 134 | TensorType beta = create_tensor<TensorType>(shape1, dt, 1); |
| 135 | TensorType gamma = create_tensor<TensorType>(shape1, dt, 1); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 136 | |
| 137 | // Create and configure function |
| 138 | FunctionType norm; |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 139 | TensorType *beta_ptr = _use_beta ? &beta : nullptr; |
| 140 | TensorType *gamma_ptr = _use_gamma ? &gamma : nullptr; |
| 141 | norm.configure(&src, &dst, &mean, &var, beta_ptr, gamma_ptr, epsilon, act_info); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 142 | |
| 143 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 144 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 145 | ARM_COMPUTE_EXPECT(mean.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | ARM_COMPUTE_EXPECT(var.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 147 | ARM_COMPUTE_EXPECT(beta.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 148 | ARM_COMPUTE_EXPECT(gamma.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 149 | |
| 150 | // Allocate tensors |
| 151 | src.allocator()->allocate(); |
| 152 | dst.allocator()->allocate(); |
| 153 | mean.allocator()->allocate(); |
| 154 | var.allocator()->allocate(); |
| 155 | beta.allocator()->allocate(); |
| 156 | gamma.allocator()->allocate(); |
| 157 | |
| 158 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 159 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 160 | ARM_COMPUTE_EXPECT(!mean.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 161 | ARM_COMPUTE_EXPECT(!var.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 162 | ARM_COMPUTE_EXPECT(!beta.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 163 | ARM_COMPUTE_EXPECT(!gamma.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 164 | |
| 165 | // Fill tensors |
| 166 | fill(AccessorType(src), AccessorType(mean), AccessorType(var), AccessorType(beta), AccessorType(gamma)); |
| 167 | |
| 168 | // Compute function |
| 169 | norm.run(); |
| 170 | |
| 171 | return dst; |
| 172 | } |
| 173 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 174 | SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, float epsilon, ActivationLayerInfo act_info, DataType dt) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 175 | { |
| 176 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 177 | SimpleTensor<T> ref_src{ shape0, dt, 1 }; |
| 178 | SimpleTensor<T> ref_mean{ shape1, dt, 1 }; |
| 179 | SimpleTensor<T> ref_var{ shape1, dt, 1 }; |
| 180 | SimpleTensor<T> ref_beta{ shape1, dt, 1 }; |
| 181 | SimpleTensor<T> ref_gamma{ shape1, dt, 1 }; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 182 | |
| 183 | // Fill reference |
| 184 | fill(ref_src, ref_mean, ref_var, ref_beta, ref_gamma); |
| 185 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 186 | return reference::batch_normalization_layer(ref_src, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, act_info); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TensorType _target{}; |
| 190 | SimpleTensor<T> _reference{}; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 191 | DataType _data_type{}; |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 192 | bool _use_beta{}; |
| 193 | bool _use_gamma{}; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 197 | class BatchNormalizationLayerValidationFixture : public BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T> |
| 198 | { |
| 199 | public: |
| 200 | template <typename...> |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 201 | void setup(TensorShape shape0, TensorShape shape1, float epsilon, bool use_beta, bool use_gamma, ActivationLayerInfo act_info, DataType dt, DataLayout data_layout) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 202 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 203 | BatchNormalizationLayerValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, epsilon, use_beta, use_gamma, act_info, dt, data_layout); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 204 | } |
| 205 | }; |
| 206 | } // namespace validation |
| 207 | } // namespace test |
| 208 | } // namespace arm_compute |
| 209 | #endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FIXTURE */ |