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