-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathtest_utils.h
131 lines (104 loc) · 4.42 KB
/
test_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//===- test_utils.h ----------------------------000---*- C++ -*-===//
//
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (C) 2024, Advanced Micro Devices, Inc.
//
//===----------------------------------------------------------------------===//
// This file contains common helper functions for the generic host code
#ifndef _TEST_UTILS_H_
#define _TEST_UTILS_H_
#include <boost/program_options.hpp>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#if defined(__STDCPP_BFLOAT16_T__)
#include <stdfloat>
#endif
#include <string>
#include <vector>
#include "xrt/xrt_device.h"
#include "xrt/xrt_kernel.h"
namespace po = boost::program_options;
namespace test_utils {
void check_arg_file_exists(po::variables_map &vm_in, std::string name);
void add_default_options(po::options_description &desc);
void parse_options(int argc, const char *argv[], po::options_description &desc,
po::variables_map &vm);
std::vector<uint32_t> load_instr_sequence(std::string instr_path);
std::vector<uint32_t> load_instr_binary(std::string instr_path);
void init_xrt_load_kernel(xrt::device &device, xrt::kernel &kernel,
int verbosity, std::string xclbinFileName,
std::string kernelNameInXclbin);
static inline std::int16_t random_int16_t(int32_t range = 0x10000) {
return (std::int16_t)rand() % range;
}
static inline std::int32_t random_int32_t(int32_t range = 0x10000) {
return (std::int32_t)rand() % range;
}
#if defined(__STDCPP_BFLOAT16_T__)
static inline std::bfloat16_t random_bfloat16_t(std::bfloat16_t scale,
std::bfloat16_t bias) {
return std::bfloat16_t((scale * (float)rand() / (float)(RAND_MAX)) + bias);
}
#endif
bool nearly_equal(float a, float b, float epsilon = 128 * FLT_EPSILON,
float abs_th = FLT_MIN);
template <typename T>
void print_matrix(const std::vector<T> matrix, int n_cols,
int n_printable_rows = 10, int n_printable_cols = 10,
std::ostream &ostream = std::cout,
const char col_sep[] = " ", const char elide_sym[] = " ... ",
int w = -1) {
assert(matrix.size() % n_cols == 0);
auto maxima = std::minmax_element(matrix.begin(), matrix.end());
T max_val = std::max(*maxima.first, std::abs(*maxima.second));
size_t n_digits = log10(max_val);
if (w == -1) {
w = n_digits;
}
int n_rows = matrix.size() / n_cols;
n_printable_rows = std::min(n_rows, n_printable_rows);
n_printable_cols = std::min(n_cols, n_printable_cols);
const bool elide_rows = n_printable_rows < n_rows;
const bool elide_cols = n_printable_cols < n_cols;
if (elide_rows || elide_cols) {
w = std::max((int)w, (int)strlen(elide_sym));
}
w += 3; // for decimal point and two decimal digits
ostream << std::fixed << std::setprecision(2);
#define print_row(what) \
for (int col = 0; col < n_printable_cols / 2; col++) { \
ostream << std::right << std::setw(w) << (what); \
ostream << std::setw(0) << col_sep; \
} \
if (elide_cols) { \
ostream << std::setw(0) << elide_sym; \
} \
for (int col = n_printable_cols / 2 + 1; col < n_printable_cols; col++) { \
ostream << std::right << std::setw(w) << (what); \
ostream << std::setw(0) << col_sep; \
}
for (int row = 0; row < n_printable_rows / 2; row++) {
print_row(matrix[row * n_rows + col]);
ostream << std::endl;
}
if (elide_rows) {
print_row(elide_sym);
ostream << std::endl;
}
for (int row = n_printable_rows / 2 + 1; row < n_printable_rows; row++) {
print_row(matrix[row * n_rows + col]);
ostream << std::endl;
}
#undef print_row
}
void write_out_trace(char *traceOutPtr, size_t trace_size, std::string path);
} // namespace test_utils
#endif // _TEST_UTILS_H_