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); |
Pablo Tello | 96e922e | 2018-09-26 11:25:15 +0100 | [diff] [blame] | 266 | if(weights_shape[0] == 7 && weights_shape[1] == 1) |
| 267 | { |
| 268 | output_tile.width = 2; |
| 269 | output_tile.height = 1; |
| 270 | } |
| 271 | else if(weights_shape[0] == 1 && weights_shape[1] == 7) |
| 272 | { |
| 273 | output_tile.width = 1; |
| 274 | output_tile.height = 2; |
| 275 | } |
| 276 | else if(weights_shape[0] == 1) |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 277 | { |
| 278 | output_tile.width = 1; |
| 279 | } |
| 280 | else if(weights_shape[1] == 1) |
| 281 | { |
| 282 | output_tile.height = 1; |
| 283 | } |
| 284 | |
| 285 | WinogradInfo winograd_info(output_tile, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 286 | Size2D(weights_shape[0], weights_shape[1]), |
| 287 | Size2D(input_shape[0], input_shape[1]), |
| 288 | info, |
| 289 | src.data_layout()); |
| 290 | |
| 291 | // Compute tensor shapes for input, filter and output transforms |
| 292 | TensorShape input_transform_shape = compute_winograd_input_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
| 293 | TensorShape filter_transform_shape = compute_winograd_filter_transform_shape(TensorInfo(weights_shape, 1, data_type), winograd_info); |
| 294 | TensorShape batched_gemm_shape = input_transform_shape; |
| 295 | batched_gemm_shape[0] = filter_transform_shape[0]; |
| 296 | TensorShape output_transform_shape = compute_winograd_output_transform_shape(TensorInfo(batched_gemm_shape, 1, data_type), winograd_info); |
| 297 | |
| 298 | // Dummy matrix C to perform matrix multiplication |
| 299 | SimpleTensor<T> dummy_c{ batched_gemm_shape, data_type, 1 }; |
| 300 | |
| 301 | // Compute Winograd-based convolution |
| 302 | SimpleTensor<T> input_transform_out = reference::winograd_input_transform<T>(src, input_transform_shape, winograd_info); |
| 303 | SimpleTensor<T> filter_transform_out = reference::winograd_filter_transform<T>(weights, filter_transform_shape, winograd_info); |
| 304 | SimpleTensor<T> batched_gemm = reference::gemm<T>(input_transform_out, filter_transform_out, dummy_c, 1.0f, 0.0f); |
| 305 | SimpleTensor<T> conv_out = reference::winograd_output_transform<T>(batched_gemm, bias, output_transform_shape, winograd_info); |
| 306 | |
| 307 | return (act_info.enabled()) ? reference::activation_layer<T>(conv_out, act_info) : conv_out; |
| 308 | } |
| 309 | |
| 310 | TensorType _target{}; |
| 311 | SimpleTensor<T> _reference{}; |
| 312 | }; |
| 313 | |
| 314 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 315 | class WinogradInputTransformValidationFixture : public framework::Fixture |
| 316 | { |
| 317 | public: |
| 318 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 319 | 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] | 320 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 321 | 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] | 322 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 323 | _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); |
| 324 | _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] | 325 | } |
| 326 | |
| 327 | protected: |
| 328 | template <typename U> |
| 329 | void fill(U &&tensor, int i, float min, float max) |
| 330 | { |
| 331 | switch(tensor.data_type()) |
| 332 | { |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 333 | case DataType::F16: |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 334 | case DataType::F32: |
| 335 | { |
| 336 | std::uniform_real_distribution<> distribution(min, max); |
| 337 | library->fill(tensor, distribution, i); |
| 338 | break; |
| 339 | } |
| 340 | default: |
| 341 | { |
| 342 | ARM_COMPUTE_ERROR("Not supported"); |
| 343 | library->fill_tensor_uniform(tensor, i); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 349 | 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] | 350 | { |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 351 | if(data_layout == DataLayout::NHWC) |
| 352 | { |
| 353 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 354 | } |
| 355 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 356 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 357 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo()); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 358 | |
| 359 | // Create and configure function |
| 360 | FunctionType transf; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 361 | transf.configure(&src, &dst, winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 362 | |
| 363 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 364 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 365 | |
| 366 | // Allocate tensors |
| 367 | src.allocator()->allocate(); |
| 368 | dst.allocator()->allocate(); |
| 369 | |
| 370 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 371 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 372 | |
| 373 | // Fill tensors |
| 374 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 375 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 376 | // Compute Winograd input transform function |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 377 | transf.run(); |
| 378 | |
| 379 | return dst; |
| 380 | } |
| 381 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 382 | 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] | 383 | { |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 384 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 385 | SimpleTensor<T> src{ input_shape, data_type, 1, QuantizationInfo() }; |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 386 | |
| 387 | // Fill reference |
| 388 | fill(src, 0, -1.f, 1.f); |
| 389 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 390 | return reference::winograd_input_transform<T>(src, output_shape, winograd_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TensorType _target{}; |
| 394 | SimpleTensor<T> _reference{}; |
| 395 | }; |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 396 | |
| 397 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 398 | class WinogradFilterTransformValidationFixture : public framework::Fixture |
| 399 | { |
| 400 | public: |
| 401 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 402 | 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] | 403 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 404 | WinogradInfo winograd_info(output_tile, Size2D(input_shape[0], input_shape[1]), Size2D() /* Not needed */, PadStrideInfo() /* Not needed */, DataLayout::NCHW /* Not needed */); |
| 405 | 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] | 406 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 407 | _target = compute_target(input_shape, output_shape, winograd_info, data_layout, data_type); |
| 408 | _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] | 409 | } |
| 410 | |
| 411 | protected: |
| 412 | template <typename U> |
| 413 | void fill(U &&tensor, int i, float min, float max) |
| 414 | { |
| 415 | switch(tensor.data_type()) |
| 416 | { |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 417 | case DataType::F16: |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 418 | case DataType::F32: |
| 419 | { |
| 420 | std::uniform_real_distribution<> distribution(min, max); |
| 421 | library->fill(tensor, distribution, i); |
| 422 | break; |
| 423 | } |
| 424 | default: |
| 425 | { |
| 426 | ARM_COMPUTE_ERROR("Not supported"); |
| 427 | library->fill_tensor_uniform(tensor, i); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Giorgio Arena | dcb5b28 | 2018-04-25 12:07:29 +0100 | [diff] [blame] | 433 | 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] | 434 | { |
Giorgio Arena | dcb5b28 | 2018-04-25 12:07:29 +0100 | [diff] [blame] | 435 | if(data_layout == DataLayout::NHWC) |
| 436 | { |
| 437 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 438 | } |
| 439 | |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 440 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 441 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 442 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, QuantizationInfo()); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 443 | |
| 444 | // Create and configure function |
| 445 | FunctionType filter_transform; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 446 | filter_transform.configure(&src, &dst, winograd_info); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 447 | |
| 448 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 449 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 450 | |
| 451 | // Allocate tensors |
| 452 | src.allocator()->allocate(); |
| 453 | dst.allocator()->allocate(); |
| 454 | |
| 455 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 456 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 457 | |
| 458 | // Fill tensors |
| 459 | fill(AccessorType(src), 0, -1.f, 1.f); |
| 460 | |
| 461 | filter_transform.run(); |
| 462 | |
| 463 | return dst; |
| 464 | } |
| 465 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 466 | 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] | 467 | { |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 468 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 469 | SimpleTensor<T> src{ input_shape, data_type, 1, QuantizationInfo() }; |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 470 | |
| 471 | // Fill reference |
| 472 | fill(src, 0, -1.f, 1.f); |
| 473 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 474 | return reference::winograd_filter_transform<T>(src, output_shape, winograd_info); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | TensorType _target{}; |
| 478 | SimpleTensor<T> _reference{}; |
| 479 | }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 480 | |
| 481 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 482 | class WinogradOutputTransformValidationFixture : public framework::Fixture |
| 483 | { |
| 484 | public: |
| 485 | template <typename...> |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 486 | void setup(TensorShape input_shape, WinogradInfo winograd_info, DataType data_type) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 487 | { |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 488 | _target = compute_target(input_shape, winograd_info, data_type); |
| 489 | _reference = compute_reference(input_shape, winograd_info, data_type); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | protected: |
| 493 | template <typename U> |
| 494 | void fill(U &&tensor, int i, float min, float max) |
| 495 | { |
| 496 | switch(tensor.data_type()) |
| 497 | { |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 498 | case DataType::F16: |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 499 | case DataType::F32: |
| 500 | { |
| 501 | std::uniform_real_distribution<> distribution(min, max); |
| 502 | library->fill(tensor, distribution, i); |
| 503 | break; |
| 504 | } |
| 505 | default: |
| 506 | { |
| 507 | ARM_COMPUTE_ERROR("Not supported"); |
| 508 | library->fill_tensor_uniform(tensor, i); |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 514 | 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] | 515 | { |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 516 | TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
| 517 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 518 | // Create tensors |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 519 | TensorType src = create_tensor<TensorType>(input_shape, data_type); |
| 520 | TensorType bias = create_tensor<TensorType>(output_shape[get_data_layout_dimension_index(winograd_info.output_data_layout, DataLayoutDimension::CHANNEL)], data_type); |
| 521 | 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] | 522 | |
| 523 | // Create and configure function |
| 524 | FunctionType output_transform; |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 525 | output_transform.configure(&src, &bias, &dst, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 526 | |
| 527 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 528 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 529 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 530 | |
| 531 | // Allocate tensors |
| 532 | src.allocator()->allocate(); |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 533 | bias.allocator()->allocate(); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 534 | dst.allocator()->allocate(); |
| 535 | |
| 536 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 537 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 538 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 539 | |
| 540 | // Fill tensors |
| 541 | fill(AccessorType(src), 0, -1.f, 1.f); |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 542 | fill(AccessorType(bias), 1, -1.f, 1.f); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 543 | |
| 544 | output_transform.run(); |
| 545 | |
| 546 | return dst; |
| 547 | } |
| 548 | |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 549 | 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] | 550 | { |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 551 | winograd_info.output_data_layout = DataLayout::NCHW; |
| 552 | TensorShape output_shape = compute_winograd_output_transform_shape(TensorInfo(input_shape, 1, data_type), winograd_info); |
| 553 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 554 | // Create reference |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 555 | SimpleTensor<T> src{ input_shape, data_type }; |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 556 | SimpleTensor<T> bias{ TensorShape(input_shape[0]), data_type }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 557 | |
| 558 | // Fill reference |
| 559 | fill(src, 0, -1.f, 1.f); |
Giorgio Arena | ea55f91 | 2018-07-12 15:41:35 +0100 | [diff] [blame] | 560 | fill(bias, 1, -1.f, 1.f); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 561 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 562 | 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] | 563 | } |
| 564 | |
| 565 | TensorType _target{}; |
| 566 | SimpleTensor<T> _reference{}; |
| 567 | }; |
Pablo Tello | 8951933 | 2017-11-17 11:52:36 +0000 | [diff] [blame] | 568 | } // namespace validation |
| 569 | } // namespace test |
| 570 | } // namespace arm_compute |
| 571 | #endif /* ARM_COMPUTE_TEST_WINOGRAD_LAYER_FIXTURE */ |