Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/TensorShape.h" |
| 25 | #include "arm_compute/core/Types.h" |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 27 | #include "tests/AssetsLibrary.h" |
| 28 | #include "tests/Globals.h" |
| 29 | #include "tests/IAccessor.h" |
| 30 | #include "tests/framework/Asserts.h" |
| 31 | #include "tests/framework/Fixture.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 32 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 33 | #include "tests/validation/reference/DeconvolutionLayer.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 34 | |
| 35 | #include <random> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
| 44 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 46 | class DeconvolutionLayerFixtureBase : public framework::Fixture |
| 47 | { |
| 48 | public: |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 49 | using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type; |
| 50 | |
| 51 | public: |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 52 | template <typename...> |
| 53 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 54 | DataType data_type, DataLayout data_layout, QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 55 | { |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 56 | _data_type = data_type; |
| 57 | _bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
| 58 | _data_layout = data_layout; |
| 59 | _input_quantization_info = input_quantization_info; |
| 60 | _output_quantization_info = output_quantization_info; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 61 | |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 62 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, add_bias); |
| 63 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, add_bias); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | protected: |
| 67 | template <typename U> |
| 68 | void fill(U &&tensor, int i) |
| 69 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 70 | switch(tensor.data_type()) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 71 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 72 | case DataType::QASYMM8: |
| 73 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 74 | std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
| 75 | std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 76 | library->fill(tensor, distribution, i); |
| 77 | break; |
| 78 | } |
| 79 | case DataType::S32: |
| 80 | { |
| 81 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 82 | library->fill(tensor, distribution, i); |
| 83 | break; |
| 84 | } |
| 85 | case DataType::F16: |
| 86 | case DataType::F32: |
| 87 | { |
| 88 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 89 | library->fill(tensor, distribution, i); |
| 90 | break; |
| 91 | } |
| 92 | default: |
| 93 | library->fill_tensor_uniform(tensor, i); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 96 | |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 97 | template <typename U> |
| 98 | void fill_zeros(U &&tensor) |
| 99 | { |
| 100 | switch(tensor.data_type()) |
| 101 | { |
| 102 | case DataType::S32: |
| 103 | { |
| 104 | const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset); |
| 105 | library->fill_tensor_value(tensor, value); |
| 106 | break; |
| 107 | } |
| 108 | case DataType::F16: |
| 109 | library->fill_tensor_value(tensor, static_cast<half>(0.0f)); |
| 110 | break; |
| 111 | case DataType::F32: |
| 112 | library->fill_tensor_value(tensor, static_cast<float>(0.0f)); |
| 113 | break; |
| 114 | default: |
| 115 | ARM_COMPUTE_ERROR("Not supported"); |
| 116 | } |
| 117 | } |
| 118 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 119 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape, |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 120 | const PadStrideInfo &info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 121 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 122 | if(_data_layout == DataLayout::NHWC) |
| 123 | { |
| 124 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 125 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 126 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 127 | } |
| 128 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 129 | // Create tensors |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 130 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _input_quantization_info, _data_layout); |
| 131 | TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _input_quantization_info, _data_layout); |
| 132 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _input_quantization_info, _data_layout); |
| 133 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _output_quantization_info, _data_layout); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 134 | |
| 135 | // Create and configure function |
| 136 | FunctionType conv; |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 137 | conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 138 | |
| 139 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 140 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 141 | if(add_bias) |
| 142 | { |
| 143 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 144 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 145 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | |
| 147 | // Allocate tensors |
| 148 | src.allocator()->allocate(); |
| 149 | weights.allocator()->allocate(); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 150 | if(add_bias) |
| 151 | { |
| 152 | bias.allocator()->allocate(); |
| 153 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 154 | dst.allocator()->allocate(); |
| 155 | |
| 156 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 157 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 158 | if(add_bias) |
| 159 | { |
| 160 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 161 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 162 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 163 | |
| 164 | // Fill tensors |
| 165 | fill(AccessorType(src), 0); |
| 166 | fill(AccessorType(weights), 1); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 167 | if(add_bias) |
| 168 | { |
| 169 | fill(AccessorType(bias), 2); |
| 170 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 171 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 172 | // Compute DeconvolutionLayer function |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 173 | conv.run(); |
| 174 | |
| 175 | return dst; |
| 176 | } |
| 177 | |
| 178 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 179 | const PadStrideInfo &info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 180 | { |
| 181 | // Create reference |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 182 | SimpleTensor<T> src{ input_shape, _data_type, 1, _input_quantization_info }; |
| 183 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _input_quantization_info }; |
| 184 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _input_quantization_info }; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 185 | |
| 186 | // Fill reference |
| 187 | fill(src, 0); |
| 188 | fill(weights, 1); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 189 | |
| 190 | if(add_bias) |
| 191 | { |
| 192 | fill(bias, 2); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | fill_zeros(bias); |
| 197 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 198 | |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 199 | return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, _output_quantization_info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 202 | TensorType _target{}; |
| 203 | SimpleTensor<T> _reference{}; |
| 204 | DataType _data_type{}; |
| 205 | DataType _bias_data_type{}; |
| 206 | DataLayout _data_layout{}; |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 207 | QuantizationInfo _input_quantization_info{}; |
| 208 | QuantizationInfo _output_quantization_info{}; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 212 | class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 213 | { |
| 214 | public: |
| 215 | template <typename...> |
| 216 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 217 | unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 218 | { |
| 219 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 220 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 221 | const TensorShape bias_shape(num_kernels); |
| 222 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 223 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info); |
| 224 | TensorInfo input_info(input_shape, 1, data_type); |
| 225 | TensorInfo weights_info(weights_shape, 1, data_type); |
| 226 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 227 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), |
| 228 | QuantizationInfo(), add_bias); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 229 | } |
| 230 | }; |
| 231 | |
| 232 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 233 | class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 234 | { |
| 235 | public: |
| 236 | template <typename...> |
| 237 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top, |
| 238 | unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias) |
| 239 | { |
| 240 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 241 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 242 | const TensorShape bias_shape(num_kernels); |
| 243 | const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL); |
| 244 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info); |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 245 | TensorInfo input_info(input_shape, 1, data_type); |
| 246 | TensorInfo weights_info(weights_shape, 1, data_type); |
| 247 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 248 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), |
| 249 | QuantizationInfo(), add_bias); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 250 | } |
| 251 | }; |
| 252 | |
| 253 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 254 | class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 255 | { |
| 256 | public: |
| 257 | template <typename...> |
| 258 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 259 | unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, bool add_bias) |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 260 | { |
| 261 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 262 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 263 | const TensorShape bias_shape(num_kernels); |
| 264 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 265 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info); |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 266 | TensorInfo input_info(input_shape, 1, data_type, input_quantization_info); |
| 267 | TensorInfo weights_info(weights_shape, 1, data_type, input_quantization_info); |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 268 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
Manuel Bottini | 279814b | 2019-10-25 10:28:28 +0100 | [diff] [blame] | 269 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, input_quantization_info, |
| 270 | output_quantization_info, add_bias); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 271 | } |
| 272 | }; |
| 273 | |
| 274 | } // namespace validation |
| 275 | } // namespace test |
| 276 | } // namespace arm_compute |