blob: f65f22935f9448ecca669d014b0070b66bb832d1 [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
26#include "IAccessor.h"
27#include "RawTensor.h"
28#include "TypePrinter.h"
29#include "Utils.h"
30
31#include "arm_compute/core/Coordinates.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/FixedPoint.h"
34#include "arm_compute/core/TensorShape.h"
35#include "arm_compute/runtime/Tensor.h"
36
37#include <array>
38#include <cmath>
39#include <cstddef>
40#include <cstdint>
41#include <iomanip>
42
Anthony Barbierac69aa12017-07-03 17:39:37 +010043#ifdef ARM_COMPUTE_ENABLE_FP16
Pablo Tello383deec2017-06-23 10:40:05 +010044#include <arm_fp16.h> // needed for float16_t
Anthony Barbierac69aa12017-07-03 17:39:37 +010045#endif /* ARM_COMPUTE_ENABLE_FP16 */
Pablo Tello383deec2017-06-23 10:40:05 +010046
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047namespace arm_compute
48{
49namespace test
50{
51namespace validation
52{
53namespace
54{
55/** Get the data from *ptr after casting according to @p data_type and then convert the data to double.
56 *
57 * @param[in] ptr Pointer to value.
58 * @param[in] data_type Data type of both values.
59 *
60 * @return The data from the ptr after converted to double.
61 */
62double get_double_data(const void *ptr, DataType data_type)
63{
steniu019746fd82017-06-15 10:49:37 +010064 if(ptr == nullptr)
65 {
66 ARM_COMPUTE_ERROR("Can't dereference a null pointer!");
67 }
68
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 switch(data_type)
70 {
71 case DataType::U8:
72 return *reinterpret_cast<const uint8_t *>(ptr);
73 case DataType::S8:
74 return *reinterpret_cast<const int8_t *>(ptr);
75 case DataType::QS8:
76 return *reinterpret_cast<const qint8_t *>(ptr);
77 case DataType::U16:
78 return *reinterpret_cast<const uint16_t *>(ptr);
79 case DataType::S16:
80 return *reinterpret_cast<const int16_t *>(ptr);
Michalis Spyrou0a8334c2017-06-14 18:00:05 +010081 case DataType::QS16:
82 return *reinterpret_cast<const qint16_t *>(ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 case DataType::U32:
84 return *reinterpret_cast<const uint32_t *>(ptr);
85 case DataType::S32:
86 return *reinterpret_cast<const int32_t *>(ptr);
87 case DataType::U64:
88 return *reinterpret_cast<const uint64_t *>(ptr);
89 case DataType::S64:
90 return *reinterpret_cast<const int64_t *>(ptr);
Anthony Barbierac69aa12017-07-03 17:39:37 +010091#ifdef ARM_COMPUTE_ENABLE_FP16
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 case DataType::F16:
93 return *reinterpret_cast<const float16_t *>(ptr);
Anthony Barbierac69aa12017-07-03 17:39:37 +010094#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 case DataType::F32:
96 return *reinterpret_cast<const float *>(ptr);
97 case DataType::F64:
98 return *reinterpret_cast<const double *>(ptr);
99 case DataType::SIZET:
100 return *reinterpret_cast<const size_t *>(ptr);
101 default:
102 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
103 }
104}
105
steniu019746fd82017-06-15 10:49:37 +0100106bool is_equal(double target, double ref, double max_absolute_error = std::numeric_limits<double>::epsilon(), double max_relative_error = 0.0001f)
107{
108 if(!std::isfinite(target) || !std::isfinite(ref))
109 {
110 return false;
111 }
112
113 // No need further check if they are equal
114 if(ref == target)
115 {
116 return true;
117 }
118
119 // Need this check for the situation when the two values close to zero but have different sign
120 if(std::abs(std::abs(ref) - std::abs(target)) <= max_absolute_error)
121 {
122 return true;
123 }
124
125 double relative_error = 0;
126
127 if(std::abs(target) > std::abs(ref))
128 {
129 relative_error = std::abs((target - ref) / target);
130 }
131 else
132 {
133 relative_error = std::abs((ref - target) / ref);
134 }
135
136 return relative_error <= max_relative_error;
137}
138
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139void check_border_element(const IAccessor &tensor, const Coordinates &id,
140 const BorderMode &border_mode, const void *border_value,
141 int64_t &num_elements, int64_t &num_mismatches)
142{
143 const size_t channel_size = element_size_from_data_type(tensor.data_type());
144 const auto ptr = static_cast<const uint8_t *>(tensor(id));
145
146 if(border_mode == BorderMode::REPLICATE)
147 {
148 Coordinates border_id{ id };
149 border_id.set(1, 0);
150 border_value = tensor(border_id);
151 }
152
153 // Iterate over all channels within one element
154 for(int channel = 0; channel < tensor.num_channels(); ++channel)
155 {
156 const size_t channel_offset = channel * channel_size;
157 const double target = get_double_data(ptr + channel_offset, tensor.data_type());
158 const double ref = get_double_data(static_cast<const uint8_t *>(border_value) + channel_offset, tensor.data_type());
steniu019746fd82017-06-15 10:49:37 +0100159 const bool equal = is_equal(target, ref);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 BOOST_TEST_INFO("id = " << id);
162 BOOST_TEST_INFO("channel = " << channel);
163 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
164 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
steniu019746fd82017-06-15 10:49:37 +0100165 BOOST_TEST_WARN(equal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166
steniu019746fd82017-06-15 10:49:37 +0100167 if(!equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 {
169 ++num_mismatches;
170 }
171
172 ++num_elements;
173 }
174}
175
176void check_single_element(const Coordinates &id, const IAccessor &tensor, const RawTensor &reference, float tolerance_value,
177 uint64_t wrap_range, int min_channels, size_t channel_size, int64_t &num_mismatches, int64_t &num_elements)
178{
179 const auto ptr = static_cast<const uint8_t *>(tensor(id));
180 const auto ref_ptr = static_cast<const uint8_t *>(reference(id));
181
182 // Iterate over all channels within one element
183 for(int channel = 0; channel < min_channels; ++channel)
184 {
185 const size_t channel_offset = channel * channel_size;
186 const double target = get_double_data(ptr + channel_offset, reference.data_type());
187 const double ref = get_double_data(ref_ptr + channel_offset, reference.data_type());
steniu019746fd82017-06-15 10:49:37 +0100188 bool equal = is_equal(target, ref, tolerance_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
steniu019746fd82017-06-15 10:49:37 +0100190 if(wrap_range != 0 && !equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 {
steniu019746fd82017-06-15 10:49:37 +0100192 equal = is_equal(target, ref, wrap_range - tolerance_value);
193 }
194
195 if(!equal)
196 {
197 BOOST_TEST_INFO("id = " << id);
198 BOOST_TEST_INFO("channel = " << channel);
199 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
200 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
201 BOOST_TEST_WARN(equal);
202
203 ++num_mismatches;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 }
205 ++num_elements;
206 }
207}
208} // namespace
209
210void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference)
211{
212 BOOST_TEST(region.anchor.num_dimensions() == reference.anchor.num_dimensions());
213 BOOST_TEST(region.shape.num_dimensions() == reference.shape.num_dimensions());
214
215 for(unsigned int d = 0; d < region.anchor.num_dimensions(); ++d)
216 {
217 BOOST_TEST(region.anchor[d] == reference.anchor[d]);
218 }
219
220 for(unsigned int d = 0; d < region.shape.num_dimensions(); ++d)
221 {
222 BOOST_TEST(region.shape[d] == reference.shape[d]);
223 }
224}
225
226void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference)
227{
228 BOOST_TEST(padding.top == reference.top);
229 BOOST_TEST(padding.right == reference.right);
230 BOOST_TEST(padding.bottom == reference.bottom);
231 BOOST_TEST(padding.left == reference.left);
232}
233
234void validate(const IAccessor &tensor, const RawTensor &reference, float tolerance_value, float tolerance_number, uint64_t wrap_range)
235{
236 // Validate with valid region covering the entire shape
237 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number, wrap_range);
238}
239
240void validate(const IAccessor &tensor, const RawTensor &reference, const ValidRegion &valid_region, float tolerance_value, float tolerance_number, uint64_t wrap_range)
241{
242 int64_t num_mismatches = 0;
243 int64_t num_elements = 0;
244
245 BOOST_TEST(tensor.element_size() == reference.element_size());
246 BOOST_TEST(tensor.format() == reference.format());
247 BOOST_TEST(tensor.data_type() == reference.data_type());
248 BOOST_TEST(tensor.num_channels() == reference.num_channels());
249 BOOST_TEST(compare_dimensions(tensor.shape(), reference.shape()));
250
251 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
252 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
253 const size_t channel_size = element_size_from_data_type(reference.data_type());
254
255 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
256 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
257 {
258 const Coordinates id = index2coord(reference.shape(), element_idx);
259 if(is_in_valid_region(valid_region, id))
260 {
261 check_single_element(id, tensor, reference, tolerance_value, wrap_range, min_channels, channel_size, num_mismatches, num_elements);
262 }
263 }
264
265 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
266 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
267
268 BOOST_TEST(num_mismatches <= absolute_tolerance_number,
269 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches
270 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
271}
272
273void validate(const IAccessor &tensor, const void *reference_value)
274{
275 BOOST_TEST_REQUIRE((reference_value != nullptr));
276
277 int64_t num_mismatches = 0;
278 int64_t num_elements = 0;
279 const size_t channel_size = element_size_from_data_type(tensor.data_type());
280
281 // Iterate over all elements, e.g. U8, S16, RGB888, ...
282 for(int element_idx = 0; element_idx < tensor.num_elements(); ++element_idx)
283 {
284 const Coordinates id = index2coord(tensor.shape(), element_idx);
285
286 const auto ptr = static_cast<const uint8_t *>(tensor(id));
287
288 // Iterate over all channels within one element
289 for(int channel = 0; channel < tensor.num_channels(); ++channel)
290 {
291 const size_t channel_offset = channel * channel_size;
292 const double target = get_double_data(ptr + channel_offset, tensor.data_type());
293 const double ref = get_double_data(reference_value, tensor.data_type());
steniu019746fd82017-06-15 10:49:37 +0100294 const bool equal = is_equal(target, ref);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295
296 BOOST_TEST_INFO("id = " << id);
297 BOOST_TEST_INFO("channel = " << channel);
298 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
299 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
steniu019746fd82017-06-15 10:49:37 +0100300 BOOST_TEST_WARN(equal);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301
steniu019746fd82017-06-15 10:49:37 +0100302 if(!equal)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 {
304 ++num_mismatches;
305 }
306
307 ++num_elements;
308 }
309 }
310
311 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
312
313 BOOST_TEST(num_mismatches == 0,
314 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched");
315}
316
317void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value)
318{
319 if(border_mode == BorderMode::UNDEFINED)
320 {
321 return;
322 }
323 else if(border_mode == BorderMode::CONSTANT)
324 {
325 BOOST_TEST((border_value != nullptr));
326 }
327
328 int64_t num_mismatches = 0;
329 int64_t num_elements = 0;
330 const int slice_size = tensor.shape()[0] * tensor.shape()[1];
331
332 for(int element_idx = 0; element_idx < tensor.num_elements(); element_idx += slice_size)
333 {
334 Coordinates id = index2coord(tensor.shape(), element_idx);
335
336 // Top border
337 for(int y = -border_size.top; y < 0; ++y)
338 {
339 id.set(1, y);
340
341 for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
342 {
343 id.set(0, x);
344
345 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
346 }
347 }
348
349 // Bottom border
350 for(int y = tensor.shape()[1]; y < static_cast<int>(tensor.shape()[1]) + static_cast<int>(border_size.bottom); ++y)
351 {
352 id.set(1, y);
353
354 for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
355 {
356 id.set(0, x);
357
358 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
359 }
360 }
361
362 // Left/right border
363 for(int y = 0; y < static_cast<int>(tensor.shape()[1]); ++y)
364 {
365 id.set(1, y);
366
367 // Left border
368 for(int x = -border_size.left; x < 0; ++x)
369 {
370 id.set(0, x);
371
372 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
373 }
374
375 // Right border
376 for(int x = tensor.shape()[0]; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x)
377 {
378 id.set(0, x);
379
380 check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches);
381 }
382 }
383 }
384
385 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
386
387 BOOST_TEST(num_mismatches == 0,
388 num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched");
389}
390
391void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels)
392{
Pablo Tello383deec2017-06-23 10:40:05 +0100393 ARM_COMPUTE_UNUSED(classified_labels);
394 ARM_COMPUTE_UNUSED(expected_labels);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100395 BOOST_TEST(expected_labels.size() != 0);
396 BOOST_TEST(classified_labels.size() == expected_labels.size());
397
398 for(unsigned int i = 0; i < expected_labels.size(); ++i)
399 {
400 BOOST_TEST(classified_labels[i] == expected_labels[i]);
401 }
402}
steniu01423d88f2017-06-22 10:20:37 +0100403
steniu01960b0842017-06-23 11:44:34 +0100404void validate(float target, float ref, float tolerance_abs_error, float tolerance_relative_error)
steniu01423d88f2017-06-22 10:20:37 +0100405{
406 const bool equal = is_equal(target, ref, tolerance_abs_error, tolerance_relative_error);
407
408 BOOST_TEST_INFO("reference = " << std::setprecision(5) << ref);
409 BOOST_TEST_INFO("target = " << std::setprecision(5) << target);
410 BOOST_TEST(equal);
411}
412
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413} // namespace validation
414} // namespace test
415} // namespace arm_compute