Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 26 | #include "tests/AssetsLibrary.h" |
| 27 | #include "tests/Globals.h" |
| 28 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 29 | #include "tests/framework/Asserts.h" |
| 30 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 31 | #include "tests/validation/Helpers.h" |
| 32 | #include "tests/validation/fixtures/ConvolutionLayerFixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 33 | #include "tests/validation/reference/ConvolutionLayer.h" |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 34 | |
| 35 | #include <random> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 44 | class DirectConvolutionValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 45 | { |
| 46 | public: |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 47 | using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type; |
| 48 | |
| 49 | public: |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 50 | template <typename...> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 51 | void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, |
| 52 | DataType data_type, int fractional_bits, QuantizationInfo quantization_info) |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 53 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 54 | _fractional_bits = fractional_bits; |
| 55 | _quantization_info = quantization_info; |
| 56 | _data_type = data_type; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 57 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 58 | const TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels); |
| 59 | const TensorShape bias_shape(num_kernels); |
| 60 | const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR); |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 61 | const TensorShape output_shape = get_output_shape(input_shape, weights_shape, info); |
| 62 | const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 63 | |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 64 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info); |
| 65 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 68 | template <typename...> |
| 69 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, |
| 70 | DataType data_type, int fractional_bits, QuantizationInfo quantization_info) |
| 71 | { |
| 72 | _fractional_bits = fractional_bits; |
| 73 | _quantization_info = quantization_info; |
| 74 | _data_type = data_type; |
| 75 | |
| 76 | const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
| 77 | |
| 78 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info); |
| 79 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info); |
| 80 | } |
| 81 | |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 82 | protected: |
| 83 | template <typename U> |
| 84 | void fill(U &&tensor, int i) |
| 85 | { |
| 86 | switch(tensor.data_type()) |
| 87 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 88 | case DataType::QASYMM8: |
| 89 | { |
Georgios Pinitas | 6fdfaa8 | 2017-11-29 14:27:24 +0000 | [diff] [blame] | 90 | std::uniform_int_distribution<uint8_t> distribution(0, 50); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 91 | library->fill(tensor, distribution, i); |
| 92 | break; |
| 93 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 94 | case DataType::F16: |
| 95 | case DataType::F32: |
| 96 | { |
| 97 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 98 | library->fill(tensor, distribution, i); |
| 99 | break; |
| 100 | } |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 101 | case DataType::S32: |
| 102 | { |
Georgios Pinitas | 6fdfaa8 | 2017-11-29 14:27:24 +0000 | [diff] [blame] | 103 | std::uniform_int_distribution<int32_t> distribution(-5, 5); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 104 | library->fill(tensor, distribution, i); |
| 105 | break; |
| 106 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 107 | default: |
| 108 | library->fill_tensor_uniform(tensor, i); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 113 | DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 114 | { |
| 115 | // Create tensors |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 116 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info); |
| 117 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info); |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 118 | TensorType bias = create_tensor<TensorType>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 119 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position, quantization_info); |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 120 | |
| 121 | // Create and configure function |
| 122 | FunctionType conv; |
| 123 | conv.configure(&src, &weights, &bias, &dst, info); |
| 124 | |
| 125 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 126 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 127 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 128 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 129 | |
| 130 | // Allocate tensors |
| 131 | src.allocator()->allocate(); |
| 132 | weights.allocator()->allocate(); |
| 133 | bias.allocator()->allocate(); |
| 134 | dst.allocator()->allocate(); |
| 135 | |
| 136 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 137 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 138 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 139 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 140 | |
| 141 | // Fill tensors |
| 142 | fill(AccessorType(src), 0); |
| 143 | fill(AccessorType(weights), 1); |
| 144 | fill(AccessorType(bias), 2); |
| 145 | |
| 146 | // Compute NEConvolutionLayer function |
| 147 | conv.run(); |
| 148 | |
| 149 | return dst; |
| 150 | } |
| 151 | |
| 152 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 153 | DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 154 | { |
| 155 | // Create reference |
Georgios Pinitas | 540d008 | 2017-11-17 10:55:00 +0000 | [diff] [blame] | 156 | SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info }; |
| 157 | SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info }; |
| 158 | SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, fixed_point_position, quantization_info }; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 159 | |
| 160 | // Fill reference |
| 161 | fill(src, 0); |
| 162 | fill(weights, 1); |
| 163 | fill(bias, 2); |
| 164 | |
| 165 | return reference::convolution_layer<T>(src, weights, bias, output_shape, info); |
| 166 | } |
| 167 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 168 | TensorType _target{}; |
| 169 | SimpleTensor<T> _reference{}; |
| 170 | int _fractional_bits{}; |
| 171 | QuantizationInfo _quantization_info{}; |
| 172 | DataType _data_type{}; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 173 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 174 | private: |
| 175 | TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info) |
| 176 | { |
| 177 | TensorShape out_shape(in_shape); |
| 178 | const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(), |
| 179 | in_shape.y(), |
| 180 | kernel_shape.x(), |
| 181 | kernel_shape.y(), |
| 182 | info); |
| 183 | out_shape.set(0, scaled_dims.first); |
| 184 | out_shape.set(1, scaled_dims.second); |
| 185 | out_shape.set(2, kernel_shape[3]); |
| 186 | return out_shape; |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 191 | class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 192 | { |
| 193 | public: |
| 194 | template <typename...> |
| 195 | void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type) |
| 196 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 197 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, QuantizationInfo()); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 198 | } |
| 199 | }; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 200 | |
| 201 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 202 | class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 203 | { |
| 204 | public: |
| 205 | template <typename...> |
| 206 | void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, int fractional_bits) |
| 207 | { |
| 208 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits, |
| 209 | QuantizationInfo()); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 214 | class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 215 | { |
| 216 | public: |
| 217 | template <typename...> |
| 218 | void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, QuantizationInfo quantization_info) |
| 219 | { |
| 220 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, quantization_info); |
| 221 | } |
| 222 | }; |
| 223 | |
Jaroslaw Rzepecki | 2ecbada | 2017-11-29 13:51:34 +0000 | [diff] [blame] | 224 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 225 | class DirectConvolutionValidationWithTensorShapesQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 226 | { |
| 227 | public: |
| 228 | template <typename...> |
| 229 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, |
| 230 | DataType data_type, QuantizationInfo quantization_info) |
| 231 | { |
| 232 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, 0, quantization_info); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 237 | class DirectConvolutionValidationWithTensorShapesFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 238 | { |
| 239 | public: |
| 240 | template <typename...> |
| 241 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, |
| 242 | DataType data_type) |
| 243 | { |
| 244 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, 0, QuantizationInfo()); |
| 245 | } |
| 246 | }; |
| 247 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 248 | } // namespace validation |
| 249 | } // namespace test |
| 250 | } // namespace arm_compute |