blob: 48b4665fe7145bf79162bd598f44dcb7dedfd2e7 [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +01001/*
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#ifndef ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010035#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/ConvolutionLayer.h"
37#include "tests/validation/reference/Utils.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010038
39#include <random>
40
41namespace arm_compute
42{
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043class NEConvolutionLayer;
44
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010045namespace test
46{
47namespace validation
48{
49template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosov5124be52017-11-22 20:42:13 +070050class ConvolutionValidationGenericFixture : public framework::Fixture
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010051{
52public:
Chunosov5124be52017-11-22 20:42:13 +070053 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010054
Chunosov5124be52017-11-22 20:42:13 +070055public:
56 template <typename...>
57 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights,
58 DataType data_type, int fractional_bits, QuantizationInfo quantization_info)
59 {
60 _data_type = data_type;
61 _is_quantized = is_data_type_quantized_asymmetric(data_type);
62 _bias_data_type = _is_quantized ? DataType::S32 : data_type;
63 _fractional_bits = fractional_bits;
64 _quantization_info = quantization_info;
65
66 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights);
67 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010068 }
69
70protected:
71 template <typename U>
72 void fill(U &&tensor, int i)
73 {
74 switch(tensor.data_type())
75 {
Chunosov5124be52017-11-22 20:42:13 +070076 case DataType::QASYMM8:
77 {
78 std::uniform_int_distribution<uint8_t> distribution(0, 3);
79 library->fill(tensor, distribution, i);
80 break;
81 }
82 case DataType::S32:
83 {
84 std::uniform_int_distribution<int32_t> distribution(-100, 100);
85 library->fill(tensor, distribution, i);
86 break;
87 }
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010088 case DataType::F16:
89 case DataType::F32:
90 {
91 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
92 library->fill(tensor, distribution, i);
93 break;
94 }
95 default:
96 library->fill_tensor_uniform(tensor, i);
97 }
98 }
99
100 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Chunosov5124be52017-11-22 20:42:13 +0700101 bool reshape_weights)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100102 {
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100103 WeightsInfo weights_info(!reshape_weights, weights_shape.x(), weights_shape.y(), weights_shape[3]);
104 TensorShape reshaped_weights_shape(weights_shape);
105
106 if(!reshape_weights)
107 {
108 // Check if its a "fully connected" convolution
109 const bool is_fully_connected_convolution = (output_shape.x() == 1 && output_shape.y() == 1);
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100110 bool is_optimised = false;
111#if defined(__arm__)
Chunosov5124be52017-11-22 20:42:13 +0700112 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && _data_type == DataType::F32;
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100113#elif defined(__aarch64__)
Chunosov5124be52017-11-22 20:42:13 +0700114 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && _data_type == DataType::F32;
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100115#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100116
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100117 reshaped_weights_shape.collapse(3);
118
Chunosov5124be52017-11-22 20:42:13 +0700119 if(bias_shape.total_size() > 0 && !_is_quantized)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100120 {
Chunosov5124be52017-11-22 20:42:13 +0700121 // Add bias to the weights reshaped matrix
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100122 reshaped_weights_shape.set(0, reshaped_weights_shape.x() + 1);
123 }
124
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100125 if(is_fully_connected_convolution || is_optimised)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100126 {
127 const size_t shape_x = reshaped_weights_shape.x();
128 reshaped_weights_shape.set(0, reshaped_weights_shape.y());
129 reshaped_weights_shape.set(1, shape_x);
130 }
131 else
132 {
Chunosov5124be52017-11-22 20:42:13 +0700133 const int interleave_width = 16 / data_size_from_type(_data_type);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100134 reshaped_weights_shape.set(0, reshaped_weights_shape.x() * interleave_width);
135 reshaped_weights_shape.set(1, static_cast<unsigned int>(std::ceil(reshaped_weights_shape.y() / static_cast<float>(interleave_width))));
136 }
137 }
138
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100139 // Create tensors
Chunosov5124be52017-11-22 20:42:13 +0700140 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _fractional_bits, _quantization_info);
141 TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type, 1, _fractional_bits, _quantization_info);
142 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _fractional_bits, _quantization_info);
143 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _fractional_bits, _quantization_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100144
145 // Create and configure function
146 FunctionType conv;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100147 conv.configure(&src, &weights, &bias, &dst, info, weights_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100148
149 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
150 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
151 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
152 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
153
154 // Allocate tensors
155 src.allocator()->allocate();
156 weights.allocator()->allocate();
157 bias.allocator()->allocate();
158 dst.allocator()->allocate();
159
160 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
161 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
162 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
163 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
164
165 // Fill tensors
166 fill(AccessorType(src), 0);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100167
168 if(!reshape_weights)
169 {
170 const bool is_fully_connected_convolution = (output_shape.x() == 1 && output_shape.y() == 1);
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100171 bool is_optimised = false;
172#if defined(__arm__)
Chunosov5124be52017-11-22 20:42:13 +0700173 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && _data_type == DataType::F32;
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100174#elif defined(__aarch64__)
Chunosov5124be52017-11-22 20:42:13 +0700175 is_optimised = std::is_same<FunctionType, NEConvolutionLayer>::value && NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && _data_type == DataType::F32;
Gian Marco Iodiceece307b2017-10-03 13:17:02 +0100176#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100177
178 TensorShape tmp_weights_shape(weights_shape);
Chunosov5124be52017-11-22 20:42:13 +0700179 SimpleTensor<T> tmp_weights(tmp_weights_shape, _data_type, 1, _fractional_bits, _quantization_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100180
181 // Fill with original shape
182 fill(tmp_weights, 1);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100183
Chunosov5124be52017-11-22 20:42:13 +0700184 if(_is_quantized)
185 {
186 fill(AccessorType(bias), 2);
187 tmp_weights = linearise_weights(tmp_weights);
188 }
189 else
190 {
191 SimpleTensor<T> tmp_bias(bias_shape, _bias_data_type, 1, _fractional_bits, _quantization_info);
192 fill(tmp_bias, 2);
193 tmp_weights = linearise_weights(tmp_weights, &tmp_bias);
194 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100195
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100196 if(!is_fully_connected_convolution && !is_optimised)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100197 {
198 // Transpose with interleave
199 const int interleave_size = 16 / tmp_weights.element_size();
200 tmp_weights = transpose(std::move(tmp_weights), interleave_size);
201 }
202
203 AccessorType weights_accessor(weights);
204
205 for(int i = 0; i < tmp_weights.num_elements(); ++i)
206 {
207 Coordinates coord = index2coord(tmp_weights.shape(), i);
208 std::copy_n(static_cast<const T *>(tmp_weights(coord)), 1, static_cast<T *>(weights_accessor(coord)));
209 }
210 }
211 else
212 {
213 fill(AccessorType(weights), 1);
214 fill(AccessorType(bias), 2);
215 }
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100216
217 // Compute NEConvolutionLayer function
218 conv.run();
219
220 return dst;
221 }
222
Chunosov5124be52017-11-22 20:42:13 +0700223 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100224 {
225 // Create reference
Chunosov5124be52017-11-22 20:42:13 +0700226 SimpleTensor<T> src{ input_shape, _data_type, 1, _fractional_bits, _quantization_info };
227 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _fractional_bits, _quantization_info };
228 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _fractional_bits, _quantization_info };
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100229
230 // Fill reference
231 fill(src, 0);
232 fill(weights, 1);
233 fill(bias, 2);
234
235 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
236 }
237
Chunosov5124be52017-11-22 20:42:13 +0700238 TensorType _target{};
239 SimpleTensor<T> _reference{};
240 DataType _data_type{};
241 DataType _bias_data_type{};
242 int _fractional_bits{};
243 QuantizationInfo _quantization_info{};
244 bool _is_quantized = false;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100245
246private:
247 template <typename U>
248 SimpleTensor<U> linearise_weights(const SimpleTensor<U> &weights, const SimpleTensor<U> *biases = nullptr)
249 {
250 TensorShape dst_shape(weights.shape());
251 dst_shape.collapse(3);
252
253 if(biases != nullptr)
254 {
255 dst_shape.set(0, dst_shape.x() + 1);
256 }
257
258 const size_t shape_x = dst_shape.x();
259 dst_shape.set(0, dst_shape.y());
260 dst_shape.set(1, shape_x);
261
262 SimpleTensor<U> dst(dst_shape, weights.data_type());
263
264 // Don't iterate over biases yet
265 for(int weights_idx = 0; weights_idx < weights.num_elements(); ++weights_idx)
266 {
267 Coordinates weights_coord = index2coord(weights.shape(), weights_idx);
268 const int dst_row = weights_idx % weights.shape().total_size_lower(3);
269 Coordinates dst_coord{ weights_coord[3], dst_row, weights_coord[4] };
270 const int dst_idx = coord2index(dst.shape(), dst_coord);
271
272 dst[dst_idx] = weights[weights_idx];
273 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100274 if(biases != nullptr)
275 {
276 // Fill last row with biases
277 for(int bias_idx = 0; bias_idx < biases->num_elements(); ++bias_idx)
278 {
279 Coordinates bias_coord = index2coord(biases->shape(), bias_idx);
280 Coordinates dst_coord{ bias_coord.x(), static_cast<int>(dst.shape().y()) - 1, bias_coord.y() };
281 int dst_idx = coord2index(dst.shape(), dst_coord);
282
283 dst[dst_idx] = (*biases)[bias_idx];
284 }
285 }
286
287 return dst;
288 }
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100289};
290
291template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosov5124be52017-11-22 20:42:13 +0700292class ConvolutionValidationFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100293{
294public:
295 template <typename...>
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100296 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights, DataType data_type)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100297 {
Chunosov5124be52017-11-22 20:42:13 +0700298 ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, data_type, 0, QuantizationInfo());
299 }
300};
301
302template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
303class ConvolutionValidationFixedPointFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
304{
305public:
306 template <typename...>
307 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights, DataType data_type, int fractional_bits)
308 {
309 ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, data_type, fractional_bits,
310 QuantizationInfo());
311 }
312};
313
314template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
315class ConvolutionValidationQuantizedFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
316{
317public:
318 template <typename...>
319 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, bool reshape_weights, DataType data_type,
320 QuantizationInfo quantization_info)
321 {
322 ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, data_type, 0, quantization_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100323 }
324};
325} // namespace validation
326} // namespace test
327} // namespace arm_compute
328#endif /* ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE */