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