14#ifdef TEST_UTILS_USE_XRT
15#include "xrt/xrt_device.h"
16#include "xrt/xrt_kernel.h"
25 if (!result.count(name)) {
26 throw std::runtime_error(
"Missing required argument: " + name);
28 std::string path = result[name].as<std::string>();
29 if (!std::filesystem::exists(path)) {
30 throw std::runtime_error(
"File does not exist: " + path);
35 options.add_options()(
"help,h",
"produce help message")(
36 "xclbin,x",
"the input xclbin path", cxxopts::value<std::string>())(
37 "kernel,k",
"the kernel name in the XCLBIN (for instance PP_PRE_FD)",
38 cxxopts::value<std::string>())(
"verbosity,v",
39 "the verbosity of the output",
40 cxxopts::value<int>()->default_value(
"0"))(
42 "path of file containing userspace instructions sent to the NPU",
43 cxxopts::value<std::string>())(
44 "verify",
"whether to verify the AIE computed output",
45 cxxopts::value<bool>()->default_value(
"true"))(
46 "iters",
"number of iterations",
47 cxxopts::value<int>()->default_value(
"1"))(
48 "warmup",
"number of warmup iterations",
49 cxxopts::value<int>()->default_value(
"0"))(
50 "trace_sz,t",
"trace size", cxxopts::value<int>()->default_value(
"0"))(
51 "trace_file",
"where to store trace output",
52 cxxopts::value<std::string>()->default_value(
"trace.txt"));
56 cxxopts::Options &options,
57 cxxopts::ParseResult &vm) {
59 vm = options.parse(argc, argv);
61 if (vm.count(
"help")) {
62 std::cout << options.help() <<
"\n";
65 }
catch (
const cxxopts::exceptions::parsing &e) {
66 std::cerr << e.what() <<
"\n\n";
67 std::cerr <<
"Usage:\n" << options.help() <<
"\n";
74 }
catch (
const std::exception &ex) {
75 std::cerr << ex.what() <<
"\n\n";
84 std::ifstream instr_file(instr_path);
86 std::vector<uint32_t> instr_v;
87 while (std::getline(instr_file, line)) {
88 std::istringstream iss(line);
90 if (!(iss >> std::hex >> a)) {
91 throw std::runtime_error(
"Unable to parse instruction file\n");
100 std::ifstream instr_file(instr_path, std::ios::binary);
101 if (!instr_file.is_open()) {
102 throw std::runtime_error(
"Unable to open instruction file\n");
106 instr_file.seekg(0, std::ios::end);
107 std::streamsize size = instr_file.tellg();
108 instr_file.seekg(0, std::ios::beg);
112 throw std::runtime_error(
"File size is not a multiple of 4 bytes\n");
116 std::vector<uint32_t> instr_v(size / 4);
117 if (!instr_file.read(
reinterpret_cast<char *
>(instr_v.data()), size)) {
118 throw std::runtime_error(
"Failed to read instruction file\n");
123#ifdef TEST_UTILS_USE_XRT
129 const std::string &name_prefix,
131 std::string available;
132 for (
auto &k : xclbin.get_kernels()) {
133 auto name = k.get_name();
135 std::cout <<
"Name: " << name << std::endl;
136 if (name.rfind(name_prefix, 0) == 0)
138 if (!available.empty())
142 throw std::runtime_error(
143 "No kernel in the xclbin starts with '" + name_prefix +
144 "'. Available kernels: " + (available.empty() ?
"(none)" : available));
148 int verbosity, std::string xclbinFileName,
149 std::string kernelNameInXclbin) {
151 unsigned int device_index = 0;
152 device = xrt::device(device_index);
156 std::cout <<
"Loading xclbin: " << xclbinFileName <<
"\n";
157 auto xclbin = xrt::xclbin(xclbinFileName);
160 std::cout <<
"Kernel opcode: " << kernelNameInXclbin <<
"\n";
163 auto kernelName =
get_kernel_name(xclbin, kernelNameInXclbin, verbosity);
167 std::cout <<
"Registering xclbin: " << xclbinFileName <<
"\n";
169 device.register_xclbin(xclbin);
173 std::cout <<
"Getting hardware context.\n";
174 xrt::hw_context context(device, xclbin.get_uuid());
178 std::cout <<
"Getting handle to kernel:" << kernelName <<
"\n";
179 kernel = xrt::kernel(context, kernelName);
196 assert(std::numeric_limits<float>::epsilon() <= epsilon);
197 assert(epsilon < 1.f);
202 auto diff = std::abs(a - b);
204 std::min((std::abs(a) + std::abs(b)), std::numeric_limits<float>::max());
208 return diff < std::max(abs_th, epsilon * norm);
216 std::ofstream fout(path);
217 uint32_t *traceOut = (uint32_t *)traceOutPtr;
218 for (
int i = 0; i < trace_size /
sizeof(traceOut[0]); i++) {
219 fout << std::setfill(
'0') << std::setw(8) << std::hex << (int)traceOut[i];
bool nearly_equal(float a, float b, float epsilon=128 *FLT_EPSILON, float abs_th=FLT_MIN)
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)