blob: 0a57fc0ea532faebfeecc343b4264fe8fe1f6cfe [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
171RawTensor Reference::compute_reference_arithmetic_addition(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy)
172{
173 // Create reference
174 RawTensor ref_src1 = library->get(shape, dt_in0);
175 RawTensor ref_src2 = library->get(shape, dt_in1);
176 RawTensor ref_dst = library->get(shape, dt_out);
177
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
188RawTensor Reference::compute_reference_arithmetic_subtraction(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy)
189{
190 // Create reference
191 RawTensor ref_src1 = library->get(shape, dt_in0);
192 RawTensor ref_src2 = library->get(shape, dt_in1);
193 RawTensor ref_dst = library->get(shape, dt_out);
194
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
286RawTensor Reference::compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy, uint32_t shift, uint32_t fixed_point_position)
287{
288 RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position);
289 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
290
291 // Fill reference
292 library->fill_tensor_uniform(ref_src, 0);
293
294 // Compute reference
295 ReferenceCPP::depth_convert(ref_src, ref_dst, policy, shift);
296
297 return ref_dst;
298}
299
SiCong Li5a536642017-06-19 14:47:05 +0100300RawTensor Reference::compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
301{
302 // Create reference
303 RawTensor ref_src = library->get(shape, DataType::U8);
304 RawTensor ref_dst = library->get(shape, DataType::U8);
305
306 // Fill reference
307 library->fill_tensor_uniform(ref_src, 0);
308
309 // Compute reference
310 ReferenceCPP::gaussian3x3(ref_src, ref_dst, border_mode, constant_border_value);
311
312 return ref_dst;
313}
314
SiCong Li3eb263e2017-06-19 15:31:43 +0100315RawTensor Reference::compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
316{
317 // Create reference
318 RawTensor ref_src = library->get(shape, DataType::U8);
319 RawTensor ref_dst = library->get(shape, DataType::U8);
320
321 // Fill reference
322 library->fill_tensor_uniform(ref_src, 0);
323
324 // Compute reference
325 ReferenceCPP::gaussian5x5(ref_src, ref_dst, border_mode, constant_border_value);
326
327 return ref_dst;
328}
329
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330RawTensor Reference::compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3,
331 const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position)
332{
333 RawTensor src1 = library->get(src_shape1, dt, 1, fixed_point_position);
334 RawTensor src2 = library->get(src_shape2, dt, 1, fixed_point_position);
335 RawTensor src3 = library->get(src_shape3, dt, 1, fixed_point_position);
336 RawTensor dst = library->get(dst_shape, dt, 1, fixed_point_position);
337
338 // Fill reference
Pablo Tello221f3812017-06-28 17:27:56 +0100339 if(dt == DataType::F16 || dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340 {
341 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
342 library->fill(src1, distribution, 0);
343 library->fill(src2, distribution, 1);
344 library->fill(src3, distribution, 2);
345 }
346 else
347 {
348 library->fill_tensor_uniform(src1, 0);
349 library->fill_tensor_uniform(src2, 1);
350 library->fill_tensor_uniform(src3, 2);
351 }
352
353 // Compute reference
354 ReferenceCPP::gemm(src1, src2, src3, dst, alpha, beta);
355
356 return dst;
357}
358
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100359RawTensor Reference::compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
360 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value)
361{
362 // Create reference
363 RawTensor ref_src = library->get(shape, DataType::U8);
364 RawTensor ref_dst = library->get(shape, DataType::U8);
365
366 // Fill reference
367 library->fill_tensor_uniform(ref_src, 0);
368
369 // Compute reference
370 ReferenceCPP::non_linear_filter(ref_src, ref_dst, function, mask_size, pattern, mask, border_mode, constant_border_value);
371
372 return ref_dst;
373}
374
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375RawTensor Reference::compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
376 RoundingPolicy rounding_policy)
377{
378 // Create reference
379 RawTensor ref_src1 = library->get(shape, dt_in0);
380 RawTensor ref_src2 = library->get(shape, dt_in1);
381 RawTensor ref_dst = library->get(shape, dt_out);
382
383 // Fill reference
384 library->fill_tensor_uniform(ref_src1, 0);
385 library->fill_tensor_uniform(ref_src2, 1);
386
387 // Compute reference
388 ReferenceCPP::pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
389
390 return ref_dst;
391}
392
393RawTensor 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,
394 ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
395{
396 // Create reference
397 RawTensor ref_src1 = library->get(shape, dt_in0, 1, fixed_point_position);
398 RawTensor ref_src2 = library->get(shape, dt_in1, 1, fixed_point_position);
399 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
400
401 // Fill reference
402 library->fill_tensor_uniform(ref_src1, 0);
403 library->fill_tensor_uniform(ref_src2, 1);
404
405 // Compute reference
406 ReferenceCPP::fixed_point_pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
407
408 return ref_dst;
409}
410
Isabella Gottardib797fa22017-06-23 15:02:11 +0100411template <typename T>
412RawTensor Reference::compute_reference_table_lookup(const TensorShape &shape, DataType dt_inout, std::map<T, T> &lut)
413{
414 // Create reference
415 RawTensor ref_src = library->get(shape, dt_inout);
416 RawTensor ref_dst = library->get(shape, dt_inout);
417 // Fill reference
418 library->fill_tensor_uniform(ref_src, 0);
419
420 // Compute reference
421 ReferenceCPP::table_lookup(ref_src, ref_dst, lut);
422
423 return ref_dst;
424}
425template 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);
426template 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);
427
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428RawTensor Reference::compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
429{
430 // Create reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100431 RawTensor ref_src = library->get(shape, DataType::U8);
432 RawTensor ref_dst = library->get(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433
434 // Fill reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100435 library->fill_tensor_uniform(ref_src, 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100436
437 // Compute reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100438 ReferenceCPP::threshold(ref_src, ref_dst, threshold, false_value, true_value, type, upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439
440 return ref_dst;
441}
442
443RawTensor Reference::compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position)
444{
445 // Create reference
446 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
447 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
448
449 // Fill reference
450 if(dt == DataType::F32)
451 {
452 float min_bound = 0;
453 float max_bound = 0;
454 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(act_info.activation());
455 std::uniform_real_distribution<> distribution(min_bound, max_bound);
456 library->fill(ref_src, distribution, 0);
457 }
458 else
459 {
460 int min_bound = 0;
461 int max_bound = 0;
462 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<int8_t>(act_info.activation(), fixed_point_position);
463 std::uniform_int_distribution<> distribution(min_bound, max_bound);
464 library->fill(ref_src, distribution, 0);
465 }
466
467 // Compute reference
468 ReferenceCPP::activation_layer(ref_src, ref_dst, act_info);
469
470 return ref_dst;
471}
472
473RawTensor Reference::compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position)
474{
475 // Create reference
476 RawTensor ref_src = library->get(shape0, dt, 1, fixed_point_position);
477 RawTensor ref_dst = library->get(shape0, dt, 1, fixed_point_position);
478 RawTensor ref_mean = library->get(shape1, dt, 1, fixed_point_position);
479 RawTensor ref_var = library->get(shape1, dt, 1, fixed_point_position);
480 RawTensor ref_beta = library->get(shape1, dt, 1, fixed_point_position);
481 RawTensor ref_gamma = library->get(shape1, dt, 1, fixed_point_position);
482
483 // Fill tensors with values from -1 to 1.
484 if(dt == DataType::F32)
485 {
486 float min_bound = 0.f;
487 float max_bound = 0.f;
488 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<float>();
489 std::uniform_real_distribution<> distribution(min_bound, max_bound);
490 std::uniform_real_distribution<> distribution_var(0, max_bound);
491 library->fill(ref_src, distribution, 0);
492 library->fill(ref_mean, distribution, 1);
493 library->fill(ref_var, distribution_var, 0);
494 library->fill(ref_beta, distribution, 3);
495 library->fill(ref_gamma, distribution, 4);
496 }
497 else
498 {
499 int min_bound = 0;
500 int max_bound = 0;
501 std::tie(min_bound, max_bound) = get_batchnormalization_layer_test_bounds<int8_t>(fixed_point_position);
502 std::uniform_int_distribution<> distribution(min_bound, max_bound);
503 std::uniform_int_distribution<> distribution_var(0, max_bound);
504 library->fill(ref_src, distribution, 0);
505 library->fill(ref_mean, distribution, 1);
506 library->fill(ref_var, distribution_var, 0);
507 library->fill(ref_beta, distribution, 3);
508 library->fill(ref_gamma, distribution, 4);
509 }
510
511 // Compute reference
512 ReferenceCPP::batch_normalization_layer(ref_src, ref_dst, ref_mean, ref_var, ref_beta, ref_gamma, epsilon, fixed_point_position);
513
514 return ref_dst;
515}
516
517RawTensor Reference::compute_reference_convolution_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt,
518 const PadStrideInfo &conv_info, int fixed_point_position)
519{
520 // Create reference
521 RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position);
522 RawTensor ref_weights = library->get(weights_shape, dt, 1, fixed_point_position);
523 RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position);
524 RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position);
525
526 // Fill reference
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100527 switch(dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100528 {
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100529 case DataType::F32:
530 case DataType::F16:
531 {
532 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
533 library->fill(ref_src, distribution, 0);
534 library->fill(ref_weights, distribution, 1);
535 library->fill(ref_bias, distribution, 2);
536 break;
537 }
538 case DataType::QS16:
539 case DataType::QS8:
540 {
541 library->fill_tensor_uniform(ref_src, 0);
542 library->fill_tensor_uniform(ref_weights, 1);
543 library->fill_tensor_uniform(ref_bias, 2);
544 break;
545 }
546 default:
547 {
548 ARM_COMPUTE_ERROR("Not supported");
549 break;
550 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100551 }
552
553 // Compute reference
554 ReferenceCPP::convolution_layer(ref_src, ref_weights, ref_bias, ref_dst, conv_info);
555
556 return ref_dst;
557}
558
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100559RawTensor Reference::compute_reference_depth_concatenate_layer(const std::vector<TensorShape> &shapes, DataType dt, int fixed_point_position)
560{
561 std::vector<std::unique_ptr<RawTensor>> ref_srcs{};
562 TensorShape dst_shape = calculate_depth_concatenate_shape(shapes);
563
564 // Create tensors
565 for(unsigned int i = 0; i < shapes.size(); ++i)
566 {
567 ref_srcs.push_back(support::cpp14::make_unique<RawTensor>(RawTensor(shapes[i], dt, 1, fixed_point_position)));
568 }
569 RawTensor ref_dst = library->get(dst_shape, dt, 1, fixed_point_position);
570
571 // Fill references
572 for(unsigned int i = 0; i < ref_srcs.size(); ++i)
573 {
574 library->fill_tensor_uniform(*ref_srcs[i], i);
575 }
576
577 // Compute reference
578 ReferenceCPP::depth_concatenate_layer(ref_srcs, ref_dst);
579
580 return ref_dst;
581}
582
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100583RawTensor Reference::compute_reference_fully_connected_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
584 DataType dt, bool transpose_weights, int fixed_point_position)
585{
586 // Create reference
587 RawTensor ref_src = library->get(input_shape, dt, 1, fixed_point_position);
588 RawTensor ref_bias = library->get(bias_shape, dt, 1, fixed_point_position);
589 RawTensor ref_dst = library->get(output_shape, dt, 1, fixed_point_position);
590
591 // Swap the first and second dimension of weights' shape if transpose_weights is true
592 TensorShape ws = weights_shape;
593 if(transpose_weights)
594 {
595 const size_t dimx = ws.x();
596 ws.set(0, ws.y());
597 ws.set(1, dimx);
598 }
599
600 RawTensor ref_weights = library->get(ws, dt, 1, fixed_point_position);
601
602 // Fill reference
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100603 if(dt == DataType::F16 || dt == DataType::F32)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100604 {
605 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
606 library->fill(ref_src, distribution, 0);
607 library->fill(ref_weights, distribution, 1);
608 library->fill(ref_bias, distribution, 2);
609 }
610 else
611 {
612 library->fill_tensor_uniform(ref_src, 0);
613 library->fill_tensor_uniform(ref_weights, 1);
614 library->fill_tensor_uniform(ref_bias, 2);
615 }
616
617 // Compute reference
618 ReferenceCPP::fully_connected_layer(ref_src, ref_weights, ref_bias, ref_dst);
619
620 return ref_dst;
621}
622
623RawTensor Reference::compute_reference_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position)
624{
625 // Create reference
626 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
627 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
628
629 // Fill reference
630 if(dt == DataType::QS8)
631 {
632 const int8_t one_fixed_point = 1 << fixed_point_position;
633 const int8_t minus_one_fixed_point = -one_fixed_point;
634 library->fill_tensor_uniform(ref_src, 0, minus_one_fixed_point, one_fixed_point);
635 }
636 else
637 {
638 library->fill_tensor_uniform(ref_src, 0);
639 }
640
641 // Compute reference
642 ReferenceCPP::normalization_layer(ref_src, ref_dst, norm_info);
643
644 return ref_dst;
645}
646
647RawTensor Reference::compute_reference_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position)
648{
649 // Create reference
650 RawTensor ref_src = library->get(shape_in, dt, 1, fixed_point_position);
651 RawTensor ref_dst = library->get(shape_out, dt, 1, fixed_point_position);
652
653 // Fill reference
654 int min = 0;
655 int max = 0;
656 switch(dt)
657 {
658 case DataType::F32:
659 min = -1;
660 max = 1;
661 break;
662 case DataType::QS8:
663 min = -(1 << fixed_point_position);
664 max = (1 << fixed_point_position);
665 break;
666 default:
667 ARM_COMPUTE_ERROR("DataType not supported.");
668 }
669 std::uniform_real_distribution<> distribution(min, max);
670 library->fill(ref_src, distribution, 0.0);
671
672 // Compute reference
673 ReferenceCPP::pooling_layer(ref_src, ref_dst, pool_info, fixed_point_position);
674
675 return ref_dst;
676}
677
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100678RawTensor Reference::compute_reference_roi_pooling_layer(const TensorShape &shape, DataType dt, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info)
679{
680 TensorShape shape_dst;
681 shape_dst.set(0, pool_info.pooled_width());
682 shape_dst.set(1, pool_info.pooled_height());
683 shape_dst.set(2, shape.z());
684 shape_dst.set(3, rois.size());
685
686 // Create reference
687 RawTensor ref_src = library->get(shape, dt);
688 RawTensor ref_dst = library->get(shape_dst, dt);
689
690 // Fill reference
691 std::uniform_real_distribution<> distribution(-1, 1);
692 library->fill(ref_src, distribution, 0.0);
693
694 // Compute reference
695 ReferenceCPP::roi_pooling_layer(ref_src, ref_dst, rois, pool_info);
696
697 return ref_dst;
698}
699
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100700RawTensor Reference::compute_reference_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position)
701{
702 // Create reference
703 RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position);
704 RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position);
705
706 // Fill reference
707 if(arm_compute::is_data_type_float(dt))
708 {
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100709 std::uniform_real_distribution<> distribution(-1000.f, 1000.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100710 library->fill(ref_src, distribution, 0);
711 }
712 else
713 {
714 int one_fixed = 1 << fixed_point_position;
715 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
716 library->fill(ref_src, distribution, 0);
717 }
718
719 // Compute reference
720 ReferenceCPP::softmax_layer(ref_src, ref_dst);
721
722 return ref_dst;
723}
724
725RawTensor Reference::compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position)
726{
727 // Create reference
728 RawTensor ref_src = library->get(shape, dt_in, 1, fixed_point_position);
729 RawTensor ref_dst = library->get(shape, dt_out, 1, fixed_point_position);
730
731 // Fill reference
732 int min = 0;
733 int max = 0;
734 switch(op)
735 {
736 case(FixedPointOp::INV_SQRT):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100737 min = 1;
738 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100739 break;
740 case(FixedPointOp::LOG):
741 min = (1 << (fixed_point_position - 1));
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100742 max = (dt_in == DataType::QS8) ? 0x3F : 0x3FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100743 break;
744 case(FixedPointOp::EXP):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100745 min = -(1 << (fixed_point_position - 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100746 max = (1 << (fixed_point_position - 1));
747 break;
748 case(FixedPointOp::RECIPROCAL):
749 min = 15;
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100750 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100751 break;
752 default:
753 ARM_COMPUTE_ERROR("Fixed point operation not supported");
754 }
755 std::uniform_int_distribution<> distribution(min, max);
756 library->fill(ref_src, distribution, 0);
757
758 // Compute reference
759 ReferenceCPP::fixed_point_operation(ref_src, ref_dst, op);
760
761 return ref_dst;
762}
763
764} // namespace validation
765} // namespace test
766} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100767#endif /* DOXYGEN_SKIP_THIS */