23#if defined(__STDCPP_BFLOAT16_T__)
42void parse_options(
int argc,
const char *argv[], cxxopts::Options &options,
43 cxxopts::ParseResult &result);
54 const std::string &name_prefix,
int verbosity = 0);
57 int verbosity, std::string xclbinFileName,
58 std::string kernelNameInXclbin);
60static inline std::int16_t random_int16_t(int32_t range = 0x10000) {
61 return (std::int16_t)rand() % range;
64static inline std::int32_t random_int32_t(int32_t range = 0x10000) {
65 return (std::int32_t)rand() % range;
72#if defined(__STDCPP_BFLOAT16_T__)
75static inline bfloat16_t bfloat16_from_float(
float value) {
79static inline bfloat16_t bfloat16_from_bits(std::uint16_t bits) {
81 std::memcpy(&value, &bits,
sizeof(value));
85static inline float bfloat16_to_float(
bfloat16_t value) {
86 return static_cast<float>(value);
91static inline bfloat16_t bfloat16_from_bits(std::uint16_t bits) {
return bits; }
93static inline float bfloat16_to_float(
bfloat16_t bits) {
94 const std::uint32_t expanded_bits =
static_cast<std::uint32_t
>(bits) << 16;
96 std::memcpy(&value, &expanded_bits,
sizeof(value));
100static inline bfloat16_t bfloat16_from_float(
float value) {
101 std::uint32_t bits = 0;
102 std::memcpy(&bits, &value,
sizeof(bits));
105 const std::uint32_t lsb = (bits >> 16) & 1U;
106 const std::uint32_t rounding_bias = 0x7FFFU + lsb;
107 return static_cast<bfloat16_t>((bits + rounding_bias) >> 16);
112 const float scale_value = bfloat16_to_float(scale);
113 const float bias_value = bfloat16_to_float(bias);
114 return bfloat16_from_float((scale_value * (
float)rand() / (
float)(RAND_MAX)) +
118bool nearly_equal(
float a,
float b,
float epsilon = 128 * FLT_EPSILON,
119 float abs_th = FLT_MIN);
122 float epsilon = 128 * FLT_EPSILON,
123 float abs_th = FLT_MIN) {
124 return nearly_equal(bfloat16_to_float(a), bfloat16_to_float(b), epsilon,
129 return bfloat16_from_float(bfloat16_to_float(lhs) + bfloat16_to_float(rhs));
133 return bfloat16_from_float(bfloat16_to_float(lhs) * bfloat16_to_float(rhs));
137 return bfloat16_from_float(bfloat16_to_float(lhs) / bfloat16_to_float(rhs));
141 return bfloat16_from_float(std::tanh(bfloat16_to_float(value)));
146 int n_printable_rows = 10,
int n_printable_cols = 10,
147 std::ostream &ostream = std::cout,
148 const char col_sep[] =
" ",
const char elide_sym[] =
" ... ",
150 assert(matrix.size() % n_cols == 0);
152 auto maxima = std::minmax_element(matrix.begin(), matrix.end());
153 T max_val = std::max(*maxima.first, std::abs(*maxima.second));
154 size_t n_digits = log10(max_val);
158 int n_rows = matrix.size() / n_cols;
160 n_printable_rows = std::min(n_rows, n_printable_rows);
161 n_printable_cols = std::min(n_cols, n_printable_cols);
163 const bool elide_rows = n_printable_rows < n_rows;
164 const bool elide_cols = n_printable_cols < n_cols;
166 if (elide_rows || elide_cols) {
167 w = std::max((
int)w, (
int)strlen(elide_sym));
171 ostream << std::fixed << std::setprecision(2);
173#define print_row(what) \
174 for (int col = 0; col < n_printable_cols / 2; col++) { \
175 ostream << std::right << std::setw(w) << (what); \
176 ostream << std::setw(0) << col_sep; \
179 ostream << std::setw(0) << elide_sym; \
181 for (int col = n_printable_cols / 2 + 1; col < n_printable_cols; col++) { \
182 ostream << std::right << std::setw(w) << (what); \
183 ostream << std::setw(0) << col_sep; \
186 for (
int row = 0; row < n_printable_rows / 2; row++) {
188 ostream << std::endl;
192 ostream << std::endl;
194 for (
int row = n_printable_rows / 2 + 1; row < n_printable_rows; row++) {
196 ostream << std::endl;
202void write_out_trace(
char *traceOutPtr,
size_t trace_size, std::string path);
bool nearly_equal(float a, float b, float epsilon=128 *FLT_EPSILON, float abs_th=FLT_MIN)
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)
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 parse_options(int argc, const char *argv[], cxxopts::Options &options, cxxopts::ParseResult &result)
std::string get_kernel_name(const xrt::xclbin &xclbin, const std::string &name_prefix, int verbosity=0)
void write_out_trace(char *traceOutPtr, size_t trace_size, std::string path)
void check_arg_file_exists(const cxxopts::ParseResult &result, std::string name)
std::vector< uint32_t > load_instr_sequence(std::string instr_path)
void add_default_options(cxxopts::Options &options)