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