blob: d321e63d77cc33b123eefa01d4f7da5606f00d09 [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 "Validation.h"
25
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/FixedPoint.h"
Giorgio Arenafc2817d2017-06-27 17:26:37 +010029#include "arm_compute/core/IArray.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorShape.h"
31#include "arm_compute/runtime/Tensor.h"
Moritz Pflanzere49e2662017-07-21 15:55:28 +010032#include "tests/IAccessor.h"
33#include "tests/RawTensor.h"
34#include "tests/TypePrinter.h"
35#include "tests/Utils.h"
36#include "tests/validation/half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <array>
39#include <cmath>
40#include <cstddef>
41#include <cstdint>
42#include <iomanip>
Giorgio Arenafc2817d2017-06-27 17:26:37 +010043#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45namespace arm_compute
46{
47namespace test
48{
49namespace validation
50{
51namespace
52{
53/** Get the data from *ptr after casting according to @p data_type and then convert the data to double.
54 *
55 * @param[in] ptr Pointer to value.
56 * @param[in] data_type Data type of both values.
57 *
58 * @return The data from the ptr after converted to double.
59 */
60double get_double_data(const void *ptr, DataType data_type)
61{
steniu019746fd82017-06-15 10:49:37 +010062 if(ptr == nullptr)
63 {
64 ARM_COMPUTE_ERROR("Can't dereference a null pointer!");
65 }
66
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 switch(data_type)
68 {
69 case DataType::U8:
70 return *reinterpret_cast<const uint8_t *>(ptr);
71 case DataType::S8:
72 return *reinterpret_cast<const int8_t *>(ptr);
73 case DataType::QS8:
74 return *reinterpret_cast<const qint8_t *>(ptr);
75 case DataType::U16:
76 return *reinterpret_cast<const uint16_t *>(ptr);
77 case DataType::S16:
78 return *reinterpret_cast<const int16_t *>(ptr);
Michalis Spyrou0a8334c2017-06-14 18:00:05 +010079 case DataType::QS16:
80 return *reinterpret_cast<const qint16_t *>(ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 case DataType::U32:
82 return *reinterpret_cast<const uint32_t *>(ptr);
83 case DataType::S32:
84 return *reinterpret_cast<const int32_t *>(ptr);
85 case DataType::U64:
86 return *reinterpret_cast<const uint64_t *>(ptr);
87 case DataType::S64:
88 return *reinterpret_cast<const int64_t *>(ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 case DataType::F16:
Moritz Pflanzere49e2662017-07-21 15:55:28 +010090 return *reinterpret_cast<const half_float::half *>(ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 case DataType::F32:
92 return *reinterpret_cast<const float *>(ptr);
93 case DataType::F64:
94 return *reinterpret_cast<const double *>(ptr);
95 case DataType::SIZET:
96 return *reinterpret_cast<const size_t *>(ptr);
97 default:
98 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
99 }
100}
101
steniu019746fd82017-06-15 10:49:37 +0100102bool is_equal(double target, double ref, double max_absolute_error = std::numeric_limits<double>::epsilon(), double max_relative_error = 0.0001f)
103{
104 if(!std::isfinite(target) || !std::isfinite(ref))
105 {
106 return false;
107 }
108
109 // No need further check if they are equal
110 if(ref == target)
111 {
112 return true;
113 }
114
115 // Need this check for the situation when the two values close to zero but have different sign
116 if(std::abs(std::abs(ref) - std::abs(target)) <= max_absolute_error)
117 {
118 return true;
119 }
120
121 double relative_error = 0;
122
123 if(std::abs(target) > std::abs(ref))
124 {
125 relative_error = std::abs((target - ref) / target);
126 }
127 else
128 {
129 relative_error = std::abs((ref - target) / ref);
130 }
131
132 return relative_error <= max_relative_error;
133}
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135void check_border_element(const IAccessor &tensor, const Coordinates &id,
136 const BorderMode &border_mode, const void *border_value,
137 int64_t &num_elements, int64_t &num_mismatches)
138{
139 const size_t channel_size = element_size_from_data_type(tensor.data_type());
140 const auto ptr = static_cast<const uint8_t *>(tensor(id));
141
142 if(border_mode == BorderMode::REPLICATE)
143 {
144 Coordinates border_id{ id };
145 border_id.set(1, 0);
146 border_value = tensor(border_id);
147 }
148
149 // Iterate over all channels within one element
150 for(int channel = 0; channel < tensor.num_channels(); ++channel)
151 {
152 const size_t channel_offset = channel * channel_size;
153 const double target = get_double_data(ptr + channel_offset, tensor.data_type());
154 const double ref = get_double_data(static_cast<const uint8_t *>(border_value) + channel_offset, tensor.data_type());
steniu019746fd82017-06-15 10:49:37 +0100155 const bool equal = is_equal(target, ref);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
157 BOOST_TEST_INFO("id = " << id);
158 BOOST_TEST_INFO("channel = " << channel);
159 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
160 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
steniu019746fd82017-06-15 10:49:37 +0100161 BOOST_TEST_WARN(equal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162
steniu019746fd82017-06-15 10:49:37 +0100163 if(!equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 {
165 ++num_mismatches;
166 }
167
168 ++num_elements;
169 }
170}
171
172void check_single_element(const Coordinates &id, const IAccessor &tensor, const RawTensor &reference, float tolerance_value,
173 uint64_t wrap_range, int min_channels, size_t channel_size, int64_t &num_mismatches, int64_t &num_elements)
174{
175 const auto ptr = static_cast<const uint8_t *>(tensor(id));
176 const auto ref_ptr = static_cast<const uint8_t *>(reference(id));
177
178 // Iterate over all channels within one element
179 for(int channel = 0; channel < min_channels; ++channel)
180 {
181 const size_t channel_offset = channel * channel_size;
182 const double target = get_double_data(ptr + channel_offset, reference.data_type());
183 const double ref = get_double_data(ref_ptr + channel_offset, reference.data_type());
steniu019746fd82017-06-15 10:49:37 +0100184 bool equal = is_equal(target, ref, tolerance_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185
steniu019746fd82017-06-15 10:49:37 +0100186 if(wrap_range != 0 && !equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 {
steniu019746fd82017-06-15 10:49:37 +0100188 equal = is_equal(target, ref, wrap_range - tolerance_value);
189 }
190
191 if(!equal)
192 {
193 BOOST_TEST_INFO("id = " << id);
194 BOOST_TEST_INFO("channel = " << channel);
195 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
196 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
197 BOOST_TEST_WARN(equal);
steniu019746fd82017-06-15 10:49:37 +0100198 ++num_mismatches;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 }
200 ++num_elements;
201 }
202}
203} // namespace
204
205void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference)
206{
207 BOOST_TEST(region.anchor.num_dimensions() == reference.anchor.num_dimensions());
208 BOOST_TEST(region.shape.num_dimensions() == reference.shape.num_dimensions());
209
210 for(unsigned int d = 0; d < region.anchor.num_dimensions(); ++d)
211 {
212 BOOST_TEST(region.anchor[d] == reference.anchor[d]);
213 }
214
215 for(unsigned int d = 0; d < region.shape.num_dimensions(); ++d)
216 {
217 BOOST_TEST(region.shape[d] == reference.shape[d]);
218 }
219}
220
221void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference)
222{
223 BOOST_TEST(padding.top == reference.top);
224 BOOST_TEST(padding.right == reference.right);
225 BOOST_TEST(padding.bottom == reference.bottom);
226 BOOST_TEST(padding.left == reference.left);
227}
228
229void validate(const IAccessor &tensor, const RawTensor &reference, float tolerance_value, float tolerance_number, uint64_t wrap_range)
230{
231 // Validate with valid region covering the entire shape
232 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number, wrap_range);
233}
234
235void validate(const IAccessor &tensor, const RawTensor &reference, const ValidRegion &valid_region, float tolerance_value, float tolerance_number, uint64_t wrap_range)
236{
237 int64_t num_mismatches = 0;
238 int64_t num_elements = 0;
239
240 BOOST_TEST(tensor.element_size() == reference.element_size());
241 BOOST_TEST(tensor.format() == reference.format());
242 BOOST_TEST(tensor.data_type() == reference.data_type());
243 BOOST_TEST(tensor.num_channels() == reference.num_channels());
244 BOOST_TEST(compare_dimensions(tensor.shape(), reference.shape()));
245
246 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
247 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
248 const size_t channel_size = element_size_from_data_type(reference.data_type());
249
250 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
251 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
252 {
253 const Coordinates id = index2coord(reference.shape(), element_idx);
254 if(is_in_valid_region(valid_region, id))
255 {
256 check_single_element(id, tensor, reference, tolerance_value, wrap_range, min_channels, channel_size, num_mismatches, num_elements);
257 }
258 }
259
260 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
261 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
262
263 BOOST_TEST(num_mismatches <= absolute_tolerance_number,
264 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches
265 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
266}
267
Isabella Gottardi62031532017-07-04 11:21:28 +0100268void validate(const IAccessor &tensor, const RawTensor &reference, const RawTensor &valid_mask, float tolerance_value, float tolerance_number, uint64_t wrap_range)
269{
270 int64_t num_mismatches = 0;
271 int64_t num_elements = 0;
272
273 BOOST_TEST(tensor.element_size() == reference.element_size());
274 BOOST_TEST(tensor.format() == reference.format());
275 BOOST_TEST(tensor.data_type() == reference.data_type());
276 BOOST_TEST(tensor.num_channels() == reference.num_channels());
277 BOOST_TEST(compare_dimensions(tensor.shape(), reference.shape()));
278
279 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
280 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
281 const size_t channel_size = element_size_from_data_type(reference.data_type());
282
283 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
284 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
285 {
286 const Coordinates id = index2coord(reference.shape(), element_idx);
287 if(valid_mask[element_idx] == 1)
288 {
289 check_single_element(id, tensor, reference, tolerance_value, wrap_range, min_channels, channel_size, num_mismatches, num_elements);
290 }
Isabella Gottardiffc12552017-08-17 10:49:25 +0100291 else
292 {
293 ++num_elements;
294 }
Isabella Gottardi62031532017-07-04 11:21:28 +0100295 }
296
297 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
298 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
299
300 BOOST_TEST(num_mismatches <= absolute_tolerance_number,
301 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches
302 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
303}
304
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305void validate(const IAccessor &tensor, const void *reference_value)
306{
307 BOOST_TEST_REQUIRE((reference_value != nullptr));
308
309 int64_t num_mismatches = 0;
310 int64_t num_elements = 0;
311 const size_t channel_size = element_size_from_data_type(tensor.data_type());
312
313 // Iterate over all elements, e.g. U8, S16, RGB888, ...
314 for(int element_idx = 0; element_idx < tensor.num_elements(); ++element_idx)
315 {
316 const Coordinates id = index2coord(tensor.shape(), element_idx);
317
318 const auto ptr = static_cast<const uint8_t *>(tensor(id));
319
320 // Iterate over all channels within one element
321 for(int channel = 0; channel < tensor.num_channels(); ++channel)
322 {
323 const size_t channel_offset = channel * channel_size;
324 const double target = get_double_data(ptr + channel_offset, tensor.data_type());
325 const double ref = get_double_data(reference_value, tensor.data_type());
steniu019746fd82017-06-15 10:49:37 +0100326 const bool equal = is_equal(target, ref);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327
328 BOOST_TEST_INFO("id = " << id);
329 BOOST_TEST_INFO("channel = " << channel);
330 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
331 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
steniu019746fd82017-06-15 10:49:37 +0100332 BOOST_TEST_WARN(equal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333
steniu019746fd82017-06-15 10:49:37 +0100334 if(!equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 {
336 ++num_mismatches;
337 }
338
339 ++num_elements;
340 }
341 }
342
343 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
344
345 BOOST_TEST(num_mismatches == 0,
346 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched");
347}
348
349void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value)
350{
351 if(border_mode == BorderMode::UNDEFINED)
352 {
353 return;
354 }
355 else if(border_mode == BorderMode::CONSTANT)
356 {
357 BOOST_TEST((border_value != nullptr));
358 }
359
360 int64_t num_mismatches = 0;
361 int64_t num_elements = 0;
362 const int slice_size = tensor.shape()[0] * tensor.shape()[1];
363
364 for(int element_idx = 0; element_idx < tensor.num_elements(); element_idx += slice_size)
365 {
366 Coordinates id = index2coord(tensor.shape(), element_idx);
367
368 // Top border
369 for(int y = -border_size.top; y < 0; ++y)
370 {
371 id.set(1, y);
372
373 for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
374 {
375 id.set(0, x);
376
377 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
378 }
379 }
380
381 // Bottom border
382 for(int y = tensor.shape()[1]; y < static_cast<int>(tensor.shape()[1]) + static_cast<int>(border_size.bottom); ++y)
383 {
384 id.set(1, y);
385
386 for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
387 {
388 id.set(0, x);
389
390 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
391 }
392 }
393
394 // Left/right border
395 for(int y = 0; y < static_cast<int>(tensor.shape()[1]); ++y)
396 {
397 id.set(1, y);
398
399 // Left border
400 for(int x = -border_size.left; x < 0; ++x)
401 {
402 id.set(0, x);
403
404 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
405 }
406
407 // Right border
408 for(int x = tensor.shape()[0]; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
409 {
410 id.set(0, x);
411
412 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
413 }
414 }
415 }
416
417 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
418
419 BOOST_TEST(num_mismatches == 0,
420 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched");
421}
422
423void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels)
424{
Pablo Tello383deec2017-06-23 10:40:05 +0100425 ARM_COMPUTE_UNUSED(classified_labels);
426 ARM_COMPUTE_UNUSED(expected_labels);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427 BOOST_TEST(expected_labels.size() != 0);
428 BOOST_TEST(classified_labels.size() == expected_labels.size());
429
430 for(unsigned int i = 0; i < expected_labels.size(); ++i)
431 {
432 BOOST_TEST(classified_labels[i] == expected_labels[i]);
433 }
434}
steniu01423d88f2017-06-22 10:20:37 +0100435
steniu01960b0842017-06-23 11:44:34 +0100436void validate(float target, float ref, float tolerance_abs_error, float tolerance_relative_error)
steniu01423d88f2017-06-22 10:20:37 +0100437{
438 const bool equal = is_equal(target, ref, tolerance_abs_error, tolerance_relative_error);
439
440 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
441 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
442 BOOST_TEST(equal);
443}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100444
Moritz Pflanzer632925a2017-08-30 09:29:47 +0100445void validate(IArray<KeyPoint> &target, IArray<KeyPoint> &ref, int64_t tolerance)
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100446{
Moritz Pflanzer632925a2017-08-30 09:29:47 +0100447 int64_t num_mismatches = 0;
448
449 BOOST_TEST_WARN(target.num_values() == ref.num_values());
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100450
451 for(size_t i = 0; i < target.num_values(); ++i)
452 {
453 KeyPoint *ref_val = std::find_if(ref.buffer(), ref.buffer() + ref.num_values(), [&target, i](KeyPoint key)
454 {
455 return key.x == target.at(i).x && key.y == target.at(i).y;
456 });
457
Moritz Pflanzer632925a2017-08-30 09:29:47 +0100458 const KeyPoint &key = target.at(i);
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100459
Moritz Pflanzer632925a2017-08-30 09:29:47 +0100460 if((ref_val == ref.buffer() + ref.num_values()) || !(is_equal(key.strength, ref_val->strength) && is_equal(key.scale, ref_val->scale) && is_equal(key.orientation, ref_val->orientation)
461 && is_equal(key.tracking_status, ref_val->tracking_status) && is_equal(key.error, ref_val->error)))
462 {
463 ++num_mismatches;
464
465 BOOST_TEST_WARN(is_equal(key.strength, ref_val->strength));
466 BOOST_TEST_WARN(is_equal(key.scale, ref_val->scale));
467 BOOST_TEST_WARN(is_equal(key.orientation, ref_val->orientation));
468 BOOST_TEST_WARN(is_equal(key.tracking_status, ref_val->tracking_status));
469 BOOST_TEST_WARN(is_equal(key.error, ref_val->error));
470 }
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100471 }
Moritz Pflanzer632925a2017-08-30 09:29:47 +0100472
473 BOOST_TEST(num_mismatches <= tolerance);
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100474}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100475} // namespace validation
476} // namespace test
477} // namespace arm_compute