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" |
| 31 | #include "tests/validation/CPP/ConvolutionLayer.h" |
| 32 | #include "tests/validation/Helpers.h" |
| 33 | #include "tests/validation/fixtures/ConvolutionLayerFixture.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: |
| 47 | template <typename...> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 48 | 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, |
| 49 | DataType data_type, int fractional_bits, QuantizationInfo quantization_info) |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 50 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 51 | _fractional_bits = fractional_bits; |
| 52 | _quantization_info = quantization_info; |
| 53 | _data_type = data_type; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 54 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 55 | const TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels); |
| 56 | const TensorShape bias_shape(num_kernels); |
| 57 | const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR); |
| 58 | const TensorShape output_shape = get_output_shape(input_shape, weights_shape, info); |
| 59 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 60 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits, quantization_info); |
| 61 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits, quantization_info); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 64 | protected: |
| 65 | template <typename U> |
| 66 | void fill(U &&tensor, int i) |
| 67 | { |
| 68 | switch(tensor.data_type()) |
| 69 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 70 | case DataType::QASYMM8: |
| 71 | { |
| 72 | std::uniform_int_distribution<uint8_t> distribution(0, 10); |
| 73 | library->fill(tensor, distribution, i); |
| 74 | break; |
| 75 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 76 | case DataType::F16: |
| 77 | case DataType::F32: |
| 78 | { |
| 79 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 80 | library->fill(tensor, distribution, i); |
| 81 | break; |
| 82 | } |
| 83 | default: |
| 84 | library->fill_tensor_uniform(tensor, i); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 89 | DataType data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 90 | { |
| 91 | // Create tensors |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 92 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info); |
| 93 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info); |
| 94 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position, quantization_info); |
| 95 | 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] | 96 | |
| 97 | // Create and configure function |
| 98 | FunctionType conv; |
| 99 | conv.configure(&src, &weights, &bias, &dst, info); |
| 100 | |
| 101 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | |
| 106 | // Allocate tensors |
| 107 | src.allocator()->allocate(); |
| 108 | weights.allocator()->allocate(); |
| 109 | bias.allocator()->allocate(); |
| 110 | dst.allocator()->allocate(); |
| 111 | |
| 112 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 113 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 114 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 115 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 116 | |
| 117 | // Fill tensors |
| 118 | fill(AccessorType(src), 0); |
| 119 | fill(AccessorType(weights), 1); |
| 120 | fill(AccessorType(bias), 2); |
| 121 | |
| 122 | // Compute NEConvolutionLayer function |
| 123 | conv.run(); |
| 124 | |
| 125 | return dst; |
| 126 | } |
| 127 | |
| 128 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 129 | DataType data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 130 | { |
| 131 | // Create reference |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 132 | SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info }; |
| 133 | SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info }; |
| 134 | SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position, quantization_info }; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 135 | |
| 136 | // Fill reference |
| 137 | fill(src, 0); |
| 138 | fill(weights, 1); |
| 139 | fill(bias, 2); |
| 140 | |
| 141 | return reference::convolution_layer<T>(src, weights, bias, output_shape, info); |
| 142 | } |
| 143 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 144 | TensorType _target{}; |
| 145 | SimpleTensor<T> _reference{}; |
| 146 | int _fractional_bits{}; |
| 147 | QuantizationInfo _quantization_info{}; |
| 148 | DataType _data_type{}; |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 149 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 150 | private: |
| 151 | TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info) |
| 152 | { |
| 153 | TensorShape out_shape(in_shape); |
| 154 | const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(), |
| 155 | in_shape.y(), |
| 156 | kernel_shape.x(), |
| 157 | kernel_shape.y(), |
| 158 | info); |
| 159 | out_shape.set(0, scaled_dims.first); |
| 160 | out_shape.set(1, scaled_dims.second); |
| 161 | out_shape.set(2, kernel_shape[3]); |
| 162 | return out_shape; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 167 | class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 168 | { |
| 169 | public: |
| 170 | template <typename...> |
| 171 | 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) |
| 172 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 173 | 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] | 174 | } |
| 175 | }; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 176 | |
| 177 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 178 | class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 179 | { |
| 180 | public: |
| 181 | template <typename...> |
| 182 | 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) |
| 183 | { |
| 184 | DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits, |
| 185 | QuantizationInfo()); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 190 | class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 191 | { |
| 192 | public: |
| 193 | template <typename...> |
| 194 | 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) |
| 195 | { |
| 196 | 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); |
| 197 | } |
| 198 | }; |
| 199 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 200 | } // namespace validation |
| 201 | } // namespace test |
| 202 | } // namespace arm_compute |