Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 793f87d | 2018-05-18 20:08:58 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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, |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 54 | const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info) |
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 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 61 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, inner_border); |
| 62 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, inner_border); |
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 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 96 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape, |
| 97 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &inner_border) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 98 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 99 | if(_data_layout == DataLayout::NHWC) |
| 100 | { |
| 101 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 102 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 103 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 104 | } |
| 105 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 106 | // Create tensors |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 107 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info, _data_layout); |
| 108 | TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _quantization_info, _data_layout); |
| 109 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info, _data_layout); |
| 110 | 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] | 111 | |
| 112 | // Create and configure function |
| 113 | FunctionType conv; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 114 | conv.configure(&src, &weights, &bias, &dst, info, inner_border.first, inner_border.second); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 115 | |
| 116 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 117 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 118 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 119 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 120 | |
| 121 | // Allocate tensors |
| 122 | src.allocator()->allocate(); |
| 123 | weights.allocator()->allocate(); |
| 124 | bias.allocator()->allocate(); |
| 125 | dst.allocator()->allocate(); |
| 126 | |
| 127 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 128 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 129 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 130 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 131 | |
| 132 | // Fill tensors |
| 133 | fill(AccessorType(src), 0); |
| 134 | fill(AccessorType(weights), 1); |
| 135 | fill(AccessorType(bias), 2); |
| 136 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 137 | // Compute DeconvolutionLayer function |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 138 | conv.run(); |
| 139 | |
| 140 | return dst; |
| 141 | } |
| 142 | |
| 143 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 144 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> inner_border) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 145 | { |
| 146 | // Create reference |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 147 | SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info }; |
| 148 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info }; |
| 149 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info }; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 150 | |
| 151 | // Fill reference |
| 152 | fill(src, 0); |
| 153 | fill(weights, 1); |
| 154 | fill(bias, 2); |
| 155 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 156 | return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, inner_border); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 159 | TensorType _target{}; |
| 160 | SimpleTensor<T> _reference{}; |
| 161 | DataType _data_type{}; |
| 162 | DataType _bias_data_type{}; |
| 163 | DataLayout _data_layout{}; |
| 164 | QuantizationInfo _quantization_info{}; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 168 | class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 169 | { |
| 170 | public: |
| 171 | template <typename...> |
| 172 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 173 | unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type, DataLayout data_layout) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 174 | { |
| 175 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 176 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 177 | const TensorShape bias_shape(num_kernels); |
| 178 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 179 | const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 180 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, sx, sy); |
| 181 | TensorInfo input_info(input_shape, 1, data_type); |
| 182 | TensorInfo weights_info(weights_shape, 1, data_type); |
| 183 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
| 184 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, data_layout, QuantizationInfo()); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
| 188 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 189 | class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 190 | { |
| 191 | public: |
| 192 | template <typename...> |
| 193 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 194 | unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info) |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 195 | { |
| 196 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 197 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 198 | const TensorShape bias_shape(num_kernels); |
| 199 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
| 200 | const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 201 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, sx, sy); |
| 202 | TensorInfo input_info(input_shape, 1, data_type, quantization_info); |
| 203 | TensorInfo weights_info(weights_shape, 1, data_type, quantization_info); |
| 204 | TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info); |
| 205 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, data_layout, quantization_info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
| 209 | } // namespace validation |
| 210 | } // namespace test |
| 211 | } // namespace arm_compute |