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 | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 54 | DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 55 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +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 | _quantization_info = quantization_info; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 60 | |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 61 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, add_bias); |
| 62 | _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] | 63 | } |
| 64 | |
| 65 | protected: |
| 66 | template <typename U> |
| 67 | void fill(U &&tensor, int i) |
| 68 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 69 | switch(tensor.data_type()) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 70 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 71 | case DataType::QASYMM8: |
| 72 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 73 | std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
| 74 | std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 75 | library->fill(tensor, distribution, i); |
| 76 | break; |
| 77 | } |
| 78 | case DataType::S32: |
| 79 | { |
| 80 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 81 | library->fill(tensor, distribution, i); |
| 82 | break; |
| 83 | } |
| 84 | case DataType::F16: |
| 85 | case DataType::F32: |
| 86 | { |
| 87 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 88 | library->fill(tensor, distribution, i); |
| 89 | break; |
| 90 | } |
| 91 | default: |
| 92 | library->fill_tensor_uniform(tensor, i); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 95 | |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 96 | template <typename U> |
| 97 | void fill_zeros(U &&tensor) |
| 98 | { |
| 99 | switch(tensor.data_type()) |
| 100 | { |
| 101 | case DataType::S32: |
| 102 | { |
| 103 | const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset); |
| 104 | library->fill_tensor_value(tensor, value); |
| 105 | break; |
| 106 | } |
| 107 | case DataType::F16: |
| 108 | library->fill_tensor_value(tensor, static_cast<half>(0.0f)); |
| 109 | break; |
| 110 | case DataType::F32: |
| 111 | library->fill_tensor_value(tensor, static_cast<float>(0.0f)); |
| 112 | break; |
| 113 | default: |
| 114 | ARM_COMPUTE_ERROR("Not supported"); |
| 115 | } |
| 116 | } |
| 117 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 118 | 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] | 119 | const PadStrideInfo &info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 120 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 121 | if(_data_layout == DataLayout::NHWC) |
| 122 | { |
| 123 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 124 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 125 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 126 | } |
| 127 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 128 | // Create tensors |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 129 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info, _data_layout); |
| 130 | TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _quantization_info, _data_layout); |
| 131 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info, _data_layout); |
| 132 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _quantization_info, _data_layout); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 133 | |
| 134 | // Create and configure function |
| 135 | FunctionType conv; |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 136 | conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 137 | |
| 138 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 139 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 140 | if(add_bias) |
| 141 | { |
| 142 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 143 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 145 | |
| 146 | // Allocate tensors |
| 147 | src.allocator()->allocate(); |
| 148 | weights.allocator()->allocate(); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 149 | if(add_bias) |
| 150 | { |
| 151 | bias.allocator()->allocate(); |
| 152 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 153 | dst.allocator()->allocate(); |
| 154 | |
| 155 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 156 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 157 | if(add_bias) |
| 158 | { |
| 159 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 160 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 162 | |
| 163 | // Fill tensors |
| 164 | fill(AccessorType(src), 0); |
| 165 | fill(AccessorType(weights), 1); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 166 | if(add_bias) |
| 167 | { |
| 168 | fill(AccessorType(bias), 2); |
| 169 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 170 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 171 | // Compute DeconvolutionLayer function |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 172 | conv.run(); |
| 173 | |
| 174 | return dst; |
| 175 | } |
| 176 | |
| 177 | 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] | 178 | const PadStrideInfo &info, bool add_bias) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 179 | { |
| 180 | // Create reference |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 181 | SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info }; |
| 182 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info }; |
| 183 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info }; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 184 | |
| 185 | // Fill reference |
| 186 | fill(src, 0); |
| 187 | fill(weights, 1); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 188 | |
| 189 | if(add_bias) |
| 190 | { |
| 191 | fill(bias, 2); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | fill_zeros(bias); |
| 196 | } |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 197 | |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 198 | return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 201 | TensorType _target{}; |
| 202 | SimpleTensor<T> _reference{}; |
| 203 | DataType _data_type{}; |
| 204 | DataType _bias_data_type{}; |
| 205 | DataLayout _data_layout{}; |
| 206 | QuantizationInfo _quantization_info{}; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 210 | class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 211 | { |
| 212 | public: |
| 213 | template <typename...> |
| 214 | 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] | 215 | 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] | 216 | { |
| 217 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 218 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 219 | const TensorShape bias_shape(num_kernels); |
| 220 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 221 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info); |
| 222 | TensorInfo input_info(input_shape, 1, data_type); |
| 223 | TensorInfo weights_info(weights_shape, 1, data_type); |
| 224 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
| 225 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), add_bias); |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 230 | class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 231 | { |
| 232 | public: |
| 233 | template <typename...> |
| 234 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top, |
| 235 | unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias) |
| 236 | { |
| 237 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 238 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 239 | const TensorShape bias_shape(num_kernels); |
| 240 | const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL); |
| 241 | 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] | 242 | TensorInfo input_info(input_shape, 1, data_type); |
| 243 | TensorInfo weights_info(weights_shape, 1, data_type); |
| 244 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 245 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), add_bias); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 246 | } |
| 247 | }; |
| 248 | |
| 249 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 250 | class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 251 | { |
| 252 | public: |
| 253 | template <typename...> |
| 254 | 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] | 255 | unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info, bool add_bias) |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 256 | { |
| 257 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 258 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 259 | const TensorShape bias_shape(num_kernels); |
| 260 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 261 | 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] | 262 | TensorInfo input_info(input_shape, 1, data_type, quantization_info); |
| 263 | TensorInfo weights_info(weights_shape, 1, data_type, quantization_info); |
| 264 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
Manuel Bottini | 9f0d5ec | 2019-08-19 13:31:38 +0100 | [diff] [blame] | 265 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, quantization_info, add_bias); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 266 | } |
| 267 | }; |
| 268 | |
| 269 | } // namespace validation |
| 270 | } // namespace test |
| 271 | } // namespace arm_compute |