blob: 4b4919fd28201f496f324fc8ee1ac69322bed54b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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#include "Reference.h"
25
26#include "Globals.h"
27#include "Helpers.h"
28#include "ReferenceCPP.h"
29#include "TensorLibrary.h"
30#include "validation/Helpers.h"
31
32#include <random>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010033#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35using namespace arm_compute::test;
36
Isabella Gottardib797fa22017-06-23 15:02:11 +010037#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Giorgio Arena50f9fd72017-06-19 17:05:30 +010044std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
45{
46 // Create reference
47 RawTensor ref_src = library->get(shape, Format::U8);
48 RawTensor ref_dst_x = library->get(shape, Format::S16);
49 RawTensor ref_dst_y = library->get(shape, Format::S16);
50
51 // Fill reference
52 library->fill_tensor_uniform(ref_src, 0);
53
54 // Compute reference
55 ReferenceCPP::sobel_3x3(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value);
56
57 return std::make_pair(ref_dst_x, ref_dst_y);
58}
59std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
60{
61 // Create reference
62 RawTensor ref_src = library->get(shape, Format::U8);
63 RawTensor ref_dst_x = library->get(shape, Format::S16);
64 RawTensor ref_dst_y = library->get(shape, Format::S16);
65
66 // Fill reference
67 library->fill_tensor_uniform(ref_src, 0);
68
69 // Compute reference
70 ReferenceCPP::sobel_5x5(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value);
71
72 return std::make_pair(ref_dst_x, ref_dst_y);
73}
Giorgio Arenaf7959862017-06-13 15:19:51 +010074std::pair<float, float> Reference::compute_reference_mean_and_standard_deviation(const TensorShape &shape)
75{
76 // Create reference
77 RawTensor ref_src = library->get(shape, DataType::U8);
78
79 // Create output variables
80 float mean;
81 float std_dev;
82
83 // Fill reference
84 library->fill_tensor_uniform(ref_src, 0);
85
86 // Compute reference
87 ReferenceCPP::mean_and_standard_deviation(ref_src, mean, std_dev);
88
89 return std::make_pair(mean, std_dev);
90}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091RawTensor Reference::compute_reference_integral_image(const TensorShape &shape)
92{
93 // Create reference
94 RawTensor ref_src = library->get(shape, DataType::U8);
95 RawTensor ref_dst = library->get(shape, DataType::U32);
96
97 // Fill reference
98 library->fill_tensor_uniform(ref_src, 0);
99
100 // Compute reference
101 ReferenceCPP::integral_image(ref_src, ref_dst);
102
103 return ref_dst;
104}
105RawTensor Reference::compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out)
106{
107 // Create reference
108 RawTensor ref_src1 = library->get(shape, dt_in0);
109 RawTensor ref_src2 = library->get(shape, dt_in1);
110 RawTensor ref_dst = library->get(shape, dt_out);
111
112 // Fill reference
113 library->fill_tensor_uniform(ref_src1, 0);
114 library->fill_tensor_uniform(ref_src2, 1);
115
116 // Compute reference
117 ReferenceCPP::absolute_difference(ref_src1, ref_src2, ref_dst);
118
119 return ref_dst;
120}
121
122RawTensor Reference::compute_reference_accumulate(const TensorShape &shape)
123{
124 // Create reference
125 RawTensor ref_src = library->get(shape, DataType::U8);
126 RawTensor ref_dst = library->get(shape, DataType::S16);
127
128 // Fill reference
129 library->fill_tensor_uniform(ref_src, 0);
130 library->fill_tensor_uniform(ref_dst, 1);
131
132 // Compute reference
133 ReferenceCPP::accumulate(ref_src, ref_dst);
134
135 return ref_dst;
136}
137
138RawTensor Reference::compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift)
139{
140 // Create reference
141 RawTensor ref_src = library->get(shape, DataType::U8);
142 RawTensor ref_dst = library->get(shape, DataType::S16);
143
144 // Fill reference
145 // ref_dst tensor filled with non-negative values
146 library->fill_tensor_uniform(ref_src, 0);
147 library->fill_tensor_uniform(ref_dst, 1, static_cast<int16_t>(0), std::numeric_limits<int16_t>::max());
148
149 // Compute reference
150 ReferenceCPP::accumulate_squared(ref_src, ref_dst, shift);
151
152 return ref_dst;
153}
154
155RawTensor Reference::compute_reference_accumulate_weighted(const TensorShape &shape, float alpha)
156{
157 // Create reference
158 RawTensor ref_src = library->get(shape, DataType::U8);
159 RawTensor ref_dst = library->get(shape, DataType::U8);
160
161 // Fill reference
162 library->fill_tensor_uniform(ref_src, 0);
163 library->fill_tensor_uniform(ref_dst, 1);
164
165 // Compute reference
166 ReferenceCPP::accumulate_weighted(ref_src, ref_dst, alpha);
167
168 return ref_dst;
169}
170
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100171RawTensor Reference::compute_reference_arithmetic_addition(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy, int fixed_point_position)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172{
173 // Create reference
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100174 RawTensor ref_src1 = library->get(shape, dt_in0, 1, fixed_point_position);
175 RawTensor ref_src2 = library->get(shape, dt_in1, 1, fixed_point_position);
176 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178 // Fill reference
179 library->fill_tensor_uniform(ref_src1, 0);
180 library->fill_tensor_uniform(ref_src2, 1);
181
182 // Compute reference
183 ReferenceCPP::arithmetic_addition(ref_src1, ref_src2, ref_dst, convert_policy);
184
185 return ref_dst;
186}
187
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100188RawTensor Reference::compute_reference_arithmetic_subtraction(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy, int fixed_point_position)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189{
190 // Create reference
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100191 RawTensor ref_src1 = library->get(shape, dt_in0, 1, fixed_point_position);
192 RawTensor ref_src2 = library->get(shape, dt_in1, 1, fixed_point_position);
193 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194
195 // Fill reference
196 library->fill_tensor_uniform(ref_src1, 0);
197 library->fill_tensor_uniform(ref_src2, 1);
198
199 // Compute reference
200 ReferenceCPP::arithmetic_subtraction(ref_src1, ref_src2, ref_dst, convert_policy);
201
202 return ref_dst;
203}
204
205RawTensor Reference::compute_reference_bitwise_and(const TensorShape &shape)
206{
207 // Create reference
208 RawTensor ref_src1 = library->get(shape, DataType::U8);
209 RawTensor ref_src2 = library->get(shape, DataType::U8);
210 RawTensor ref_dst = library->get(shape, DataType::U8);
211
212 // Fill reference
213 library->fill_tensor_uniform(ref_src1, 0);
214 library->fill_tensor_uniform(ref_src2, 1);
215
216 // Compute reference
217 ReferenceCPP::bitwise_and(ref_src1, ref_src2, ref_dst);
218
219 return ref_dst;
220}
221
222RawTensor Reference::compute_reference_bitwise_or(const TensorShape &shape)
223{
224 // Create reference
225 RawTensor ref_src1 = library->get(shape, DataType::U8);
226 RawTensor ref_src2 = library->get(shape, DataType::U8);
227 RawTensor ref_dst = library->get(shape, DataType::U8);
228
229 // Fill reference
230 library->fill_tensor_uniform(ref_src1, 0);
231 library->fill_tensor_uniform(ref_src2, 1);
232
233 // Compute reference
234 ReferenceCPP::bitwise_or(ref_src1, ref_src2, ref_dst);
235
236 return ref_dst;
237}
238
239RawTensor Reference::compute_reference_bitwise_xor(const TensorShape &shape)
240{
241 // Create reference
242 RawTensor ref_src1 = library->get(shape, DataType::U8);
243 RawTensor ref_src2 = library->get(shape, DataType::U8);
244 RawTensor ref_dst = library->get(shape, DataType::U8);
245
246 // Fill reference
247 library->fill_tensor_uniform(ref_src1, 0);
248 library->fill_tensor_uniform(ref_src2, 1);
249
250 // Compute reference
251 ReferenceCPP::bitwise_xor(ref_src1, ref_src2, ref_dst);
252
253 return ref_dst;
254}
255
256RawTensor Reference::compute_reference_bitwise_not(const TensorShape &shape)
257{
258 // Create reference
259 RawTensor ref_src = library->get(shape, DataType::U8);
260 RawTensor ref_dst = library->get(shape, DataType::U8);
261
262 // Fill reference
263 library->fill_tensor_uniform(ref_src, 0);
264
265 // Compute reference
266 ReferenceCPP::bitwise_not(ref_src, ref_dst);
267
268 return ref_dst;
269}
270
SiCong Libacaf9a2017-06-19 13:41:45 +0100271RawTensor Reference::compute_reference_box3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272{
273 // Create reference
274 RawTensor ref_src = library->get(shape, DataType::U8);
275 RawTensor ref_dst = library->get(shape, DataType::U8);
276
277 // Fill reference
278 library->fill_tensor_uniform(ref_src, 0);
279
280 // Compute reference
SiCong Libacaf9a2017-06-19 13:41:45 +0100281 ReferenceCPP::box3x3(ref_src, ref_dst, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282
283 return ref_dst;
284}
285
Georgios Pinitase2229412017-07-12 12:30:40 +0100286RawTensor Reference::compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy,
287 uint32_t shift, uint32_t fixed_point_position_in, uint32_t fixed_point_position_out)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288{
Georgios Pinitase2229412017-07-12 12:30:40 +0100289 RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position_in);
290 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291
292 // Fill reference
293 library->fill_tensor_uniform(ref_src, 0);
294
295 // Compute reference
296 ReferenceCPP::depth_convert(ref_src, ref_dst, policy, shift);
297
298 return ref_dst;
299}
300
SiCong Li5a536642017-06-19 14:47:05 +0100301RawTensor Reference::compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
302{
303 // Create reference
304 RawTensor ref_src = library->get(shape, DataType::U8);
305 RawTensor ref_dst = library->get(shape, DataType::U8);
306
307 // Fill reference
308 library->fill_tensor_uniform(ref_src, 0);
309
310 // Compute reference
311 ReferenceCPP::gaussian3x3(ref_src, ref_dst, border_mode, constant_border_value);
312
313 return ref_dst;
314}
315
SiCong Li3eb263e2017-06-19 15:31:43 +0100316RawTensor Reference::compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
317{
318 // Create reference
319 RawTensor ref_src = library->get(shape, DataType::U8);
320 RawTensor ref_dst = library->get(shape, DataType::U8);
321
322 // Fill reference
323 library->fill_tensor_uniform(ref_src, 0);
324
325 // Compute reference
326 ReferenceCPP::gaussian5x5(ref_src, ref_dst, border_mode, constant_border_value);
327
328 return ref_dst;
329}
330
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331RawTensor Reference::compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3,
332 const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position)
333{
334 RawTensor src1 = library->get(src_shape1, dt, 1, fixed_point_position);
335 RawTensor src2 = library->get(src_shape2, dt, 1, fixed_point_position);
336 RawTensor src3 = library->get(src_shape3, dt, 1, fixed_point_position);
337 RawTensor dst = library->get(dst_shape, dt, 1, fixed_point_position);
338
339 // Fill reference
Pablo Tello221f3812017-06-28 17:27:56 +0100340 if(dt == DataType::F16 || dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341 {
342 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
343 library->fill(src1, distribution, 0);
344 library->fill(src2, distribution, 1);
345 library->fill(src3, distribution, 2);
346 }
347 else
348 {
349 library->fill_tensor_uniform(src1, 0);
350 library->fill_tensor_uniform(src2, 1);
351 library->fill_tensor_uniform(src3, 2);
352 }
353
354 // Compute reference
355 ReferenceCPP::gemm(src1, src2, src3, dst, alpha, beta);
356
357 return dst;
358}
359
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100360RawTensor Reference::compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
361 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value)
362{
363 // Create reference
364 RawTensor ref_src = library->get(shape, DataType::U8);
365 RawTensor ref_dst = library->get(shape, DataType::U8);
366
367 // Fill reference
368 library->fill_tensor_uniform(ref_src, 0);
369
370 // Compute reference
371 ReferenceCPP::non_linear_filter(ref_src, ref_dst, function, mask_size, pattern, mask, border_mode, constant_border_value);
372
373 return ref_dst;
374}
375
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376RawTensor Reference::compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
377 RoundingPolicy rounding_policy)
378{
379 // Create reference
380 RawTensor ref_src1 = library->get(shape, dt_in0);
381 RawTensor ref_src2 = library->get(shape, dt_in1);
382 RawTensor ref_dst = library->get(shape, dt_out);
383
384 // Fill reference
385 library->fill_tensor_uniform(ref_src1, 0);
386 library->fill_tensor_uniform(ref_src2, 1);
387
388 // Compute reference
389 ReferenceCPP::pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
390
391 return ref_dst;
392}
393
394RawTensor Reference::compute_reference_fixed_point_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, int fixed_point_position,
395 ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
396{
397 // Create reference
398 RawTensor ref_src1 = library->get(shape, dt_in0, 1, fixed_point_position);
399 RawTensor ref_src2 = library->get(shape, dt_in1, 1, fixed_point_position);
400 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
401
402 // Fill reference
403 library->fill_tensor_uniform(ref_src1, 0);
404 library->fill_tensor_uniform(ref_src2, 1);
405
406 // Compute reference
407 ReferenceCPP::fixed_point_pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
408
409 return ref_dst;
410}
411
Isabella Gottardib797fa22017-06-23 15:02:11 +0100412template <typename T>
413RawTensor Reference::compute_reference_table_lookup(const TensorShape &shape, DataType dt_inout, std::map<T, T> &lut)
414{
415 // Create reference
416 RawTensor ref_src = library->get(shape, dt_inout);
417 RawTensor ref_dst = library->get(shape, dt_inout);
418 // Fill reference
419 library->fill_tensor_uniform(ref_src, 0);
420
421 // Compute reference
422 ReferenceCPP::table_lookup(ref_src, ref_dst, lut);
423
424 return ref_dst;
425}
426template RawTensor arm_compute::test::validation::Reference::compute_reference_table_lookup<uint8_t>(const TensorShape &shape, DataType dt_inout, std::map<uint8_t, uint8_t> &lut);
427template RawTensor arm_compute::test::validation::Reference::compute_reference_table_lookup<int16_t>(const TensorShape &shape, DataType dt_inout, std::map<int16_t, int16_t> &lut);
428
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429RawTensor Reference::compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
430{
431 // Create reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100432 RawTensor ref_src = library->get(shape, DataType::U8);
433 RawTensor ref_dst = library->get(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100434
435 // Fill reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100436 library->fill_tensor_uniform(ref_src, 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437
438 // Compute reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100439 ReferenceCPP::threshold(ref_src, ref_dst, threshold, false_value, true_value, type, upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100440
441 return ref_dst;
442}
443
444RawTensor Reference::compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position)
445{
446 // Create reference
447 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
448 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
449
450 // Fill reference
451 if(dt == DataType::F32)
452 {
453 float min_bound = 0;
454 float max_bound = 0;
455 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation());
456 std::uniform_real_distribution<> distribution(min_bound, max_bound);
457 library->fill(ref_src, distribution, 0);
458 }
459 else
460 {
461 int min_bound = 0;
462 int max_bound = 0;
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100463 if(dt == DataType::QS8)
464 {
465 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position);
466 }
467 else
468 {
469 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int16_t>(act_info.activation(), fixed_point_position);
470 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100471 std::uniform_int_distribution<> distribution(min_bound, max_bound);
472 library->fill(ref_src, distribution, 0);
473 }
474
475 // Compute reference
476 ReferenceCPP::activation_layer(ref_src, ref_dst, act_info);
477
478 return ref_dst;
479}
480
481RawTensor Reference::compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position)
482{
483 // Create reference
484 RawTensor ref_src = library->get(shape0, dt, 1, fixed_point_position);
485 RawTensor ref_dst = library->get(shape0, dt, 1, fixed_point_position);
486 RawTensor ref_mean = library->get(shape1, dt, 1, fixed_point_position);
487 RawTensor ref_var = library->get(shape1, dt, 1, fixed_point_position);
488 RawTensor ref_beta = library->get(shape1, dt, 1, fixed_point_position);
489 RawTensor ref_gamma = library->get(shape1, dt, 1, fixed_point_position);
490
491 // Fill tensors with values from -1 to 1.
492 if(dt == DataType::F32)
493 {
494 float min_bound = 0.f;
495 float max_bound = 0.f;
496 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<float>();
497 std::uniform_real_distribution<> distribution(min_bound, max_bound);
498 std::uniform_real_distribution<> distribution_var(0, max_bound);
499 library->fill(ref_src, distribution, 0);
500 library->fill(ref_mean, distribution, 1);
501 library->fill(ref_var, distribution_var, 0);
502 library->fill(ref_beta, distribution, 3);
503 library->fill(ref_gamma, distribution, 4);
504 }
505 else
506 {
507 int min_bound = 0;
508 int max_bound = 0;
Michalis Spyrou172e5702017-06-26 14:18:47 +0100509 if(dt == DataType::QS8)
510 {
511 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<int8_t>(fixed_point_position);
512 }
513 else
514 {
515 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<int16_t>(fixed_point_position);
516 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517 std::uniform_int_distribution<> distribution(min_bound, max_bound);
518 std::uniform_int_distribution<> distribution_var(0, max_bound);
519 library->fill(ref_src, distribution, 0);
520 library->fill(ref_mean, distribution, 1);
521 library->fill(ref_var, distribution_var, 0);
522 library->fill(ref_beta, distribution, 3);
523 library->fill(ref_gamma, distribution, 4);
524 }
525
526 // Compute reference
527 ReferenceCPP::batch_normalization_layer(ref_src, ref_dst, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, fixed_point_position);
528
529 return ref_dst;
530}
531
532RawTensor Reference::compute_reference_convolution_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt,
533 const PadStrideInfo &conv_info, int fixed_point_position)
534{
535 // Create reference
536 RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position);
537 RawTensor ref_weights = library->get(weights_shape, dt, 1, fixed_point_position);
538 RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position);
539 RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position);
540
541 // Fill reference
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100542 switch(dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100543 {
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100544 case DataType::F32:
545 case DataType::F16:
546 {
547 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
548 library->fill(ref_src, distribution, 0);
549 library->fill(ref_weights, distribution, 1);
550 library->fill(ref_bias, distribution, 2);
551 break;
552 }
553 case DataType::QS16:
554 case DataType::QS8:
555 {
556 library->fill_tensor_uniform(ref_src, 0);
557 library->fill_tensor_uniform(ref_weights, 1);
558 library->fill_tensor_uniform(ref_bias, 2);
559 break;
560 }
561 default:
562 {
563 ARM_COMPUTE_ERROR("Not supported");
564 break;
565 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100566 }
567
568 // Compute reference
569 ReferenceCPP::convolution_layer(ref_src, ref_weights, ref_bias, ref_dst, conv_info);
570
571 return ref_dst;
572}
573
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100574RawTensor Reference::compute_reference_depth_concatenate_layer(const std::vector<TensorShape> &shapes, DataType dt, int fixed_point_position)
575{
576 std::vector<std::unique_ptr<RawTensor>> ref_srcs{};
577 TensorShape dst_shape = calculate_depth_concatenate_shape(shapes);
578
579 // Create tensors
580 for(unsigned int i = 0; i < shapes.size(); ++i)
581 {
582 ref_srcs.push_back(support::cpp14::make_unique<RawTensor>(RawTensor(shapes[i], dt, 1, fixed_point_position)));
583 }
584 RawTensor ref_dst = library->get(dst_shape, dt, 1, fixed_point_position);
585
586 // Fill references
587 for(unsigned int i = 0; i < ref_srcs.size(); ++i)
588 {
589 library->fill_tensor_uniform(*ref_srcs[i], i);
590 }
591
592 // Compute reference
593 ReferenceCPP::depth_concatenate_layer(ref_srcs, ref_dst);
594
595 return ref_dst;
596}
597
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100598RawTensor Reference::compute_reference_fully_connected_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
599 DataType dt, bool transpose_weights, int fixed_point_position)
600{
601 // Create reference
602 RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position);
603 RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position);
604 RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position);
605
606 // Swap the first and second dimension of weights' shape if transpose_weights is true
607 TensorShape ws = weights_shape;
608 if(transpose_weights)
609 {
610 const size_t dimx = ws.x();
611 ws.set(0, ws.y());
612 ws.set(1, dimx);
613 }
614
615 RawTensor ref_weights = library->get(ws, dt, 1, fixed_point_position);
616
617 // Fill reference
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100618 if(dt == DataType::F16 || dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100619 {
620 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
621 library->fill(ref_src, distribution, 0);
622 library->fill(ref_weights, distribution, 1);
623 library->fill(ref_bias, distribution, 2);
624 }
625 else
626 {
627 library->fill_tensor_uniform(ref_src, 0);
628 library->fill_tensor_uniform(ref_weights, 1);
629 library->fill_tensor_uniform(ref_bias, 2);
630 }
631
632 // Compute reference
633 ReferenceCPP::fully_connected_layer(ref_src, ref_weights, ref_bias, ref_dst);
634
635 return ref_dst;
636}
637
638RawTensor Reference::compute_reference_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position)
639{
640 // Create reference
641 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
642 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
643
644 // Fill reference
645 if(dt == DataType::QS8)
646 {
647 const int8_t one_fixed_point = 1 << fixed_point_position;
648 const int8_t minus_one_fixed_point = -one_fixed_point;
649 library->fill_tensor_uniform(ref_src, 0, minus_one_fixed_point, one_fixed_point);
650 }
651 else
652 {
653 library->fill_tensor_uniform(ref_src, 0);
654 }
655
656 // Compute reference
657 ReferenceCPP::normalization_layer(ref_src, ref_dst, norm_info);
658
659 return ref_dst;
660}
661
662RawTensor Reference::compute_reference_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position)
663{
664 // Create reference
665 RawTensor ref_src = library->get(shape_in, dt, 1, fixed_point_position);
666 RawTensor ref_dst = library->get(shape_out, dt, 1, fixed_point_position);
667
668 // Fill reference
669 int min = 0;
670 int max = 0;
671 switch(dt)
672 {
673 case DataType::F32:
674 min = -1;
675 max = 1;
676 break;
677 case DataType::QS8:
678 min = -(1 << fixed_point_position);
679 max = (1 << fixed_point_position);
680 break;
681 default:
682 ARM_COMPUTE_ERROR("DataType not supported.");
683 }
684 std::uniform_real_distribution<> distribution(min, max);
685 library->fill(ref_src, distribution, 0.0);
686
687 // Compute reference
688 ReferenceCPP::pooling_layer(ref_src, ref_dst, pool_info, fixed_point_position);
689
690 return ref_dst;
691}
692
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100693RawTensor Reference::compute_reference_roi_pooling_layer(const TensorShape &shape, DataType dt, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info)
694{
695 TensorShape shape_dst;
696 shape_dst.set(0, pool_info.pooled_width());
697 shape_dst.set(1, pool_info.pooled_height());
698 shape_dst.set(2, shape.z());
699 shape_dst.set(3, rois.size());
700
701 // Create reference
702 RawTensor ref_src = library->get(shape, dt);
703 RawTensor ref_dst = library->get(shape_dst, dt);
704
705 // Fill reference
706 std::uniform_real_distribution<> distribution(-1, 1);
707 library->fill(ref_src, distribution, 0.0);
708
709 // Compute reference
710 ReferenceCPP::roi_pooling_layer(ref_src, ref_dst, rois, pool_info);
711
712 return ref_dst;
713}
714
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100715RawTensor Reference::compute_reference_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position)
716{
717 // Create reference
718 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
719 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
720
721 // Fill reference
722 if(arm_compute::is_data_type_float(dt))
723 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100724 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100725 library->fill(ref_src, distribution, 0);
726 }
727 else
728 {
729 int one_fixed = 1 << fixed_point_position;
730 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
731 library->fill(ref_src, distribution, 0);
732 }
733
734 // Compute reference
735 ReferenceCPP::softmax_layer(ref_src, ref_dst);
736
737 return ref_dst;
738}
739
740RawTensor Reference::compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position)
741{
742 // Create reference
743 RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position);
744 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
745
746 // Fill reference
747 int min = 0;
748 int max = 0;
749 switch(op)
750 {
751 case(FixedPointOp::INV_SQRT):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100752 min = 1;
753 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100754 break;
755 case(FixedPointOp::LOG):
756 min = (1 << (fixed_point_position - 1));
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100757 max = (dt_in == DataType::QS8) ? 0x3F : 0x3FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100758 break;
759 case(FixedPointOp::EXP):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100760 min = -(1 << (fixed_point_position - 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100761 max = (1 << (fixed_point_position - 1));
762 break;
763 case(FixedPointOp::RECIPROCAL):
764 min = 15;
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100765 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100766 break;
767 default:
768 ARM_COMPUTE_ERROR("Fixed point operation not supported");
769 }
770 std::uniform_int_distribution<> distribution(min, max);
771 library->fill(ref_src, distribution, 0);
772
773 // Compute reference
774 ReferenceCPP::fixed_point_operation(ref_src, ref_dst, op);
775
776 return ref_dst;
777}
778
779} // namespace validation
780} // namespace test
781} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100782#endif /* DOXYGEN_SKIP_THIS */