Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 9fb1159 | 2018-04-26 20:34:58 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018 ARM Limited. |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [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_WINOGRAD_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 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" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 35 | #include "tests/validation/Helpers.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/ConvolutionLayer.h" |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 38 | #include "tests/validation/reference/GEMM.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 39 | #include "tests/validation/reference/Utils.h" |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 40 | #include "tests/validation/reference/Winograd.h" |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 41 | |
| 42 | #include <random> |
| 43 | |
| 44 | namespace arm_compute |
| 45 | { |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 46 | namespace test |
| 47 | { |
| 48 | namespace validation |
| 49 | { |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 50 | using namespace arm_compute::misc::shape_calculator; |
| 51 | |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 52 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool use_bias = true> |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 53 | class WinogradConvolutionLayerValidationFixture : public framework::Fixture |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 54 | { |
| 55 | public: |
| 56 | template <typename...> |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 57 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, DataType data_type, ActivationLayerInfo act_info) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 58 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_UNUSED(dilation); |
| 60 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 61 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info); |
| 62 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | protected: |
| 66 | template <typename U> |
| 67 | void fill(U &&tensor, int i, float min, float max) |
| 68 | { |
| 69 | switch(tensor.data_type()) |
| 70 | { |
| 71 | case DataType::F32: |
| 72 | { |
| 73 | std::uniform_real_distribution<> distribution(min, max); |
| 74 | library->fill(tensor, distribution, i); |
| 75 | break; |
| 76 | } |
| 77 | default: |
| 78 | { |
| 79 | ARM_COMPUTE_ERROR("Not supported"); |
| 80 | library->fill_tensor_uniform(tensor, i); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 86 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 87 | DataType data_type, ActivationLayerInfo act_info) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 88 | { |
| 89 | // Create tensors |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 90 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1); |
| 91 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1); |
| 92 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 93 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 94 | |
| 95 | // Create and configure function |
| 96 | FunctionType conv; |
Vidhya Sudhan Loganathan | 84ce1f9 | 2018-04-25 13:00:09 +0100 | [diff] [blame] | 97 | ARM_COMPUTE_EXPECT(static_cast<bool>(conv.validate(src.info(), weights.info(), (use_bias) ? bias.info() : nullptr, dst.info(), info, act_info)), framework::LogLevel::ERRORS); |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 98 | conv.configure(&src, &weights, (use_bias) ? &bias : nullptr, &dst, info, act_info); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 99 | |
| 100 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | |
| 105 | // Allocate tensors |
| 106 | src.allocator()->allocate(); |
| 107 | weights.allocator()->allocate(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 108 | dst.allocator()->allocate(); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 109 | bias.allocator()->allocate(); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 110 | |
| 111 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 112 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 113 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 114 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 115 | |
| 116 | // Fill tensors |
| 117 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 118 | fill(AccessorType(weights), 1, -1.f, 1.f); |
Pablo Tello | d6ca478 | 2018-01-23 09:36:04 +0000 | [diff] [blame] | 119 | fill(AccessorType(bias), 2, -1.f, 1.f); |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 120 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 121 | // Compute Winograd Convolution function |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 122 | conv.run(); |
| 123 | |
| 124 | return dst; |
| 125 | } |
| 126 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 127 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 128 | DataType data_type, ActivationLayerInfo act_info) |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 129 | { |
| 130 | // Create reference |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 131 | SimpleTensor<T> src{ input_shape, data_type, 1 }; |
| 132 | SimpleTensor<T> weights{ weights_shape, data_type, 1 }; |
| 133 | SimpleTensor<T> bias{ bias_shape, data_type, 1 }; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 134 | |
| 135 | // Fill reference |
| 136 | fill(src, 0, -1.f, 1.f); |
| 137 | fill(weights, 1, -1.f, 1.f); |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 138 | if(use_bias) |
| 139 | { |
| 140 | fill(bias, 2, -1.f, 1.f); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | fill(bias, 2, 0.f, 0.f); |
| 145 | } |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 146 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 147 | SimpleTensor<T> conv_out = reference::convolution_layer<T>(src, weights, bias, output_shape, info); |
| 148 | |
| 149 | return (act_info.enabled()) ? reference::activation_layer<T>(conv_out, act_info) : conv_out; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TensorType _target{}; |
| 153 | SimpleTensor<T> _reference{}; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 156 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 157 | class WinogradConvolutionLayerFastMathValidationFixture : public framework::Fixture |
| 158 | { |
| 159 | public: |
| 160 | template <typename...> |
| 161 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, DataType data_type, ActivationLayerInfo act_info) |
| 162 | { |
| 163 | ARM_COMPUTE_UNUSED(dilation); |
| 164 | |
| 165 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info); |
| 166 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, act_info); |
| 167 | } |
| 168 | |
| 169 | protected: |
| 170 | template <typename U> |
| 171 | void fill(U &&tensor, int i, float min, float max) |
| 172 | { |
| 173 | switch(tensor.data_type()) |
| 174 | { |
| 175 | case DataType::F32: |
| 176 | { |
| 177 | std::uniform_real_distribution<> distribution(min, max); |
| 178 | library->fill(tensor, distribution, i); |
| 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | { |
| 183 | ARM_COMPUTE_ERROR("Not supported"); |
| 184 | library->fill_tensor_uniform(tensor, i); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
| 191 | DataType data_type, ActivationLayerInfo act_info) |
| 192 | { |
| 193 | // Create tensors |
| 194 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1); |
| 195 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1); |
| 196 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 197 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1); |
| 198 | |
| 199 | // Create and configure function |
| 200 | FunctionType conv; |
| 201 | ARM_COMPUTE_EXPECT(static_cast<bool>(conv.validate(src.info(), weights.info(), bias.info(), dst.info(), info, act_info, true /* Enable fast math */)), framework::LogLevel::ERRORS); |
| 202 | conv.configure(&src, &weights, &bias, &dst, info, act_info, true /* Enable fast math */); |
| 203 | |
| 204 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 205 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 206 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 207 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 208 | |
| 209 | // Allocate tensors |
| 210 | src.allocator()->allocate(); |
| 211 | weights.allocator()->allocate(); |
| 212 | dst.allocator()->allocate(); |
| 213 | bias.allocator()->allocate(); |
| 214 | |
| 215 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 216 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 217 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 218 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 219 | |
| 220 | // Fill tensors |
| 221 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 222 | fill(AccessorType(weights), 1, -1.f, 1.f); |
| 223 | fill(AccessorType(bias), 2, -1.f, 1.f); |
| 224 | |
| 225 | // Compute Winograd Convolution function |
| 226 | conv.run(); |
| 227 | |
| 228 | return dst; |
| 229 | } |
| 230 | |
| 231 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
| 232 | DataType data_type, ActivationLayerInfo act_info) |
| 233 | { |
| 234 | // Create reference |
| 235 | SimpleTensor<T> src{ input_shape, data_type, 1 }; |
| 236 | SimpleTensor<T> weights{ weights_shape, data_type, 1 }; |
| 237 | SimpleTensor<T> bias{ bias_shape, data_type, 1 }; |
| 238 | |
| 239 | // Fill reference |
| 240 | fill(src, 0, -1.f, 1.f); |
| 241 | fill(weights, 1, -1.f, 1.f); |
| 242 | fill(bias, 2, -1.f, 1.f); |
| 243 | |
| 244 | WinogradInfo winograd_info(Size2D(4U, 4U), |
| 245 | Size2D(weights_shape[0], weights_shape[1]), |
| 246 | Size2D(input_shape[0], input_shape[1]), |
| 247 | info, |
| 248 | src.data_layout()); |
| 249 | |
| 250 | // Compute tensor shapes for input, filter and output transforms |
| 251 | TensorShape input_transform_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
| 252 | TensorShape filter_transform_shape = compute_winograd_filter_transform_shape(TensorInfo(weights_shape, 1, data_type), winograd_info); |
| 253 | TensorShape batched_gemm_shape = input_transform_shape; |
| 254 | batched_gemm_shape[0] = filter_transform_shape[0]; |
| 255 | TensorShape output_transform_shape = compute_winograd_output_transform_shape(TensorInfo(batched_gemm_shape, 1, data_type), winograd_info); |
| 256 | |
| 257 | // Dummy matrix C to perform matrix multiplication |
| 258 | SimpleTensor<T> dummy_c{ batched_gemm_shape, data_type, 1 }; |
| 259 | |
| 260 | // Compute Winograd-based convolution |
| 261 | SimpleTensor<T> input_transform_out = reference::winograd_input_transform<T>(src, input_transform_shape, winograd_info); |
| 262 | SimpleTensor<T> filter_transform_out = reference::winograd_filter_transform<T>(weights, filter_transform_shape, winograd_info); |
| 263 | SimpleTensor<T> batched_gemm = reference::gemm<T>(input_transform_out, filter_transform_out, dummy_c, 1.0f, 0.0f); |
| 264 | SimpleTensor<T> conv_out = reference::winograd_output_transform<T>(batched_gemm, bias, output_transform_shape, winograd_info); |
| 265 | |
| 266 | return (act_info.enabled()) ? reference::activation_layer<T>(conv_out, act_info) : conv_out; |
| 267 | } |
| 268 | |
| 269 | TensorType _target{}; |
| 270 | SimpleTensor<T> _reference{}; |
| 271 | }; |
| 272 | |
| 273 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 274 | class WinogradInputTransformValidationFixture : public framework::Fixture |
| 275 | { |
| 276 | public: |
| 277 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 278 | void setup(TensorShape input_shape, WinogradInfo winograd_info, DataLayout data_layout, DataType data_type) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 279 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 280 | TensorShape output_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 281 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 282 | _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); |
| 283 | _reference = compute_reference(input_shape, output_shape, winograd_info, data_layout, data_type); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | protected: |
| 287 | template <typename U> |
| 288 | void fill(U &&tensor, int i, float min, float max) |
| 289 | { |
| 290 | switch(tensor.data_type()) |
| 291 | { |
| 292 | case DataType::F32: |
| 293 | { |
| 294 | std::uniform_real_distribution<> distribution(min, max); |
| 295 | library->fill(tensor, distribution, i); |
| 296 | break; |
| 297 | } |
| 298 | default: |
| 299 | { |
| 300 | ARM_COMPUTE_ERROR("Not supported"); |
| 301 | library->fill_tensor_uniform(tensor, i); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 307 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 308 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 309 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, 0, QuantizationInfo(), data_layout); |
| 310 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), data_layout); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 311 | |
| 312 | // Create and configure function |
| 313 | FunctionType transf; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 314 | transf.configure(&src, &dst, winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 315 | |
| 316 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 317 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 318 | |
| 319 | // Allocate tensors |
| 320 | src.allocator()->allocate(); |
| 321 | dst.allocator()->allocate(); |
| 322 | |
| 323 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 324 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 325 | |
| 326 | // Fill tensors |
| 327 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 328 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 329 | // Compute Winograd input transform function |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 330 | transf.run(); |
| 331 | |
| 332 | return dst; |
| 333 | } |
| 334 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 335 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 336 | { |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 337 | // Create reference |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 338 | SimpleTensor<T> src{ input_shape, data_type, 1, 0, QuantizationInfo(), data_layout }; |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 339 | |
| 340 | // Fill reference |
| 341 | fill(src, 0, -1.f, 1.f); |
| 342 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 343 | return reference::winograd_input_transform<T>(src, output_shape, winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | TensorType _target{}; |
| 347 | SimpleTensor<T> _reference{}; |
| 348 | }; |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 349 | |
| 350 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 351 | class WinogradFilterTransformValidationFixture : public framework::Fixture |
| 352 | { |
| 353 | public: |
| 354 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 355 | void setup(TensorShape input_shape, Size2D output_tile, DataLayout data_layout, DataType data_type) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 356 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 357 | WinogradInfo winograd_info(output_tile, Size2D(input_shape[0], input_shape[1]), Size2D() /* Not needed */, PadStrideInfo() /* Not needed */, DataLayout::NCHW /* Not needed */); |
| 358 | TensorShape output_shape = compute_winograd_filter_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 359 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 360 | _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); |
| 361 | _reference = compute_reference(input_shape, output_shape, winograd_info, data_layout, data_type); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | protected: |
| 365 | template <typename U> |
| 366 | void fill(U &&tensor, int i, float min, float max) |
| 367 | { |
| 368 | switch(tensor.data_type()) |
| 369 | { |
| 370 | case DataType::F32: |
| 371 | { |
| 372 | std::uniform_real_distribution<> distribution(min, max); |
| 373 | library->fill(tensor, distribution, i); |
| 374 | break; |
| 375 | } |
| 376 | default: |
| 377 | { |
| 378 | ARM_COMPUTE_ERROR("Not supported"); |
| 379 | library->fill_tensor_uniform(tensor, i); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 385 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 386 | { |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 387 | // Create tensors |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 388 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, 0, QuantizationInfo(), data_layout); |
| 389 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), data_layout); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 390 | |
| 391 | // Create and configure function |
| 392 | FunctionType filter_transform; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 393 | filter_transform.configure(&src, &dst, winograd_info); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 394 | |
| 395 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 396 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 397 | |
| 398 | // Allocate tensors |
| 399 | src.allocator()->allocate(); |
| 400 | dst.allocator()->allocate(); |
| 401 | |
| 402 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 403 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 404 | |
| 405 | // Fill tensors |
| 406 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 407 | |
| 408 | filter_transform.run(); |
| 409 | |
| 410 | return dst; |
| 411 | } |
| 412 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 413 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataLayout data_layout, DataType data_type) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 414 | { |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 415 | // Create reference |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 416 | SimpleTensor<T> src{ input_shape, data_type, 1, 0, QuantizationInfo(), data_layout }; |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 417 | |
| 418 | // Fill reference |
| 419 | fill(src, 0, -1.f, 1.f); |
| 420 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 421 | return reference::winograd_filter_transform<T>(src, output_shape, winograd_info); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TensorType _target{}; |
| 425 | SimpleTensor<T> _reference{}; |
| 426 | }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 427 | |
| 428 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 429 | class WinogradOutputTransformValidationFixture : public framework::Fixture |
| 430 | { |
| 431 | public: |
| 432 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 433 | void setup(TensorShape input_shape, WinogradInfo winograd_info, DataType data_type) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 434 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 435 | TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 436 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 437 | _target = compute_target(input_shape, output_shape, winograd_info, data_type); |
| 438 | _reference = compute_reference(input_shape, output_shape, winograd_info, data_type); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | protected: |
| 442 | template <typename U> |
| 443 | void fill(U &&tensor, int i, float min, float max) |
| 444 | { |
| 445 | switch(tensor.data_type()) |
| 446 | { |
| 447 | case DataType::F32: |
| 448 | { |
| 449 | std::uniform_real_distribution<> distribution(min, max); |
| 450 | library->fill(tensor, distribution, i); |
| 451 | break; |
| 452 | } |
| 453 | default: |
| 454 | { |
| 455 | ARM_COMPUTE_ERROR("Not supported"); |
| 456 | library->fill_tensor_uniform(tensor, i); |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 462 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 463 | { |
| 464 | // Create tensors |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 465 | TensorType src = create_tensor<TensorType>(input_shape, data_type); |
| 466 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, 0, QuantizationInfo(), winograd_info.output_data_layout); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 467 | |
| 468 | // Create and configure function |
| 469 | FunctionType output_transform; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 470 | output_transform.configure(&src, nullptr, &dst, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 471 | |
| 472 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 473 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 474 | |
| 475 | // Allocate tensors |
| 476 | src.allocator()->allocate(); |
| 477 | dst.allocator()->allocate(); |
| 478 | |
| 479 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 480 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 481 | |
| 482 | // Fill tensors |
| 483 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 484 | |
| 485 | output_transform.run(); |
| 486 | |
| 487 | return dst; |
| 488 | } |
| 489 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 490 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, const WinogradInfo &winograd_info, DataType data_type) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 491 | { |
| 492 | // Create reference |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 493 | SimpleTensor<T> src{ input_shape, data_type }; |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 494 | SimpleTensor<T> bias{ TensorShape(input_shape[0]), data_type }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 495 | |
| 496 | // Fill reference |
| 497 | fill(src, 0, -1.f, 1.f); |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 498 | fill(bias, 1, 0.0f, 0.0f); // Fill with zeros as we validate just the output transform without bias contribution |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 499 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 500 | return reference::winograd_output_transform<T>(src, bias, output_shape, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | TensorType _target{}; |
| 504 | SimpleTensor<T> _reference{}; |
| 505 | }; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 506 | } // namespace validation |
| 507 | } // namespace test |
| 508 | } // namespace arm_compute |
| 509 | #endif /* ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE */ |