Compute Library
 22.08
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef TESTS_VALIDATION_CL_DYNAMICFUSION_UTILS
25 #define TESTS_VALIDATION_CL_DYNAMICFUSION_UTILS
26 
27 #include "tests/AssetsLibrary.h"
28 #include "utils/Utils.h"
29 
30 #include <chrono>
31 #include <limits>
32 #include <type_traits>
33 
34 namespace arm_compute
35 {
36 namespace test
37 {
38 namespace validation
39 {
40 namespace utils
41 {
42 /** A pair of macros which measures the wall clock time, and records it into a map measurement_map with name clock_name
43  *
44  */
45 #define TICK(clock_name) \
46  auto clock_name##_tick = std::chrono::high_resolution_clock::now();
47 #define TOCK(clock_name, measurement_map) \
48  auto clock_name##_tock = std::chrono::high_resolution_clock::now(); \
49  measurement_map["\"" #clock_name "\""] = duration_cast<microseconds>(clock_name##_tock - clock_name##_tick);
50 #define TOCK_AVG(clock_name, measurement_map, num_iterations) \
51  auto clock_name##_tock = std::chrono::high_resolution_clock::now(); \
52  measurement_map["\"" #clock_name "\""] = duration_cast<microseconds>((clock_name##_tock - clock_name##_tick) / (num_iterations));
53 
54 template <typename T, typename U>
55 void fill(U &&tensor, int seed, AssetsLibrary *library)
56 {
57  static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported.");
58  using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
59 
60  DistributionType distribution{ T(-1.0f), T(1.0f) };
61  library->fill(tensor, distribution, seed);
62 
63  // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0)
64  DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) };
65  library->fill_borders_with_garbage(tensor, distribution_inf, seed);
66 }
67 } // namespace utils
68 } // namespace validation
69 } // namespace test
70 } // namespace arm_compute
71 #endif //TESTS_VALIDATION_CL_DYNAMICFUSION_UTILS
Factory class to create and fill tensors.
Definition: AssetsLibrary.h:59
void fill(U &&tensor, int seed, AssetsLibrary *library)
Definition: Utils.h:55
decltype(strategy::transforms) typedef type
Copyright (c) 2017-2022 Arm Limited.
Specialized class to generate random non-zero FP16 values.
Definition: Utils.h:254
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
void fill(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Fills the specified tensor with random values drawn from distribution.
std::uniform_real_distribution< float > distribution(-5.f, 5.f)
void fill_borders_with_garbage(T &&tensor, D &&distribution, std::random_device::result_type seed_offset) const
Puts garbage values all around the tensor for testing purposes.