17#ifdef TEST_UTILS_USE_XRT
18#include "xrt/xrt_device.h"
19#include "xrt/xrt_kernel.h"
28 if (!result.
count(name)) {
29 throw std::runtime_error(
"Missing required argument: " + name);
31 std::string path = result[name].as<std::string>();
32 if (!std::filesystem::exists(path)) {
33 throw std::runtime_error(
"File does not exist: " + path);
38 options.
add_options()(
"help,h",
"produce help message")(
39 "xclbin,x",
"the input xclbin path", cxxopts::value<std::string>())(
40 "kernel,k",
"the kernel name in the XCLBIN (for instance PP_PRE_FD)",
41 cxxopts::value<std::string>())(
"verbosity,v",
42 "the verbosity of the output",
43 cxxopts::value<int>()->default_value(
"0"))(
45 "path of file containing userspace instructions sent to the NPU",
46 cxxopts::value<std::string>())(
47 "verify",
"whether to verify the AIE computed output",
48 cxxopts::value<bool>()->default_value(
"true"))(
49 "iters",
"number of iterations",
50 cxxopts::value<int>()->default_value(
"1"))(
51 "warmup",
"number of warmup iterations",
52 cxxopts::value<int>()->default_value(
"0"))(
53 "trace_sz,t",
"trace size", cxxopts::value<int>()->default_value(
"0"))(
54 "trace_file",
"where to store trace output",
55 cxxopts::value<std::string>()->default_value(
"trace.txt"));
62 vm = options.
parse(argc, argv);
64 if (vm.
count(
"help")) {
65 std::cout << options.
help() <<
"\n";
69 std::cerr << e.
what() <<
"\n\n";
70 std::cerr <<
"Usage:\n" << options.
help() <<
"\n";
77 }
catch (
const std::exception &ex) {
78 std::cerr << ex.what() <<
"\n\n";
87 std::ifstream instr_file(instr_path);
89 std::vector<uint32_t> instr_v;
90 while (std::getline(instr_file, line)) {
91 std::istringstream iss(line);
93 if (!(iss >> std::hex >> a)) {
94 throw std::runtime_error(
"Unable to parse instruction file\n");
103 std::ifstream instr_file(instr_path, std::ios::binary);
104 if (!instr_file.is_open()) {
105 throw std::runtime_error(
"Unable to open instruction file\n");
109 instr_file.seekg(0, std::ios::end);
110 std::streamsize size = instr_file.tellg();
111 instr_file.seekg(0, std::ios::beg);
115 throw std::runtime_error(
"File size is not a multiple of 4 bytes\n");
119 std::vector<uint32_t> instr_v(size / 4);
120 if (!instr_file.read(
reinterpret_cast<char *
>(instr_v.data()), size)) {
121 throw std::runtime_error(
"Failed to read instruction file\n");
126#ifdef TEST_UTILS_USE_XRT
132 int verbosity, std::string xclbinFileName,
133 std::string kernelNameInXclbin) {
135 unsigned int device_index = 0;
136 device = xrt::device(device_index);
140 std::cout <<
"Loading xclbin: " << xclbinFileName <<
"\n";
141 auto xclbin = xrt::xclbin(xclbinFileName);
144 std::cout <<
"Kernel opcode: " << kernelNameInXclbin <<
"\n";
147 auto xkernels = xclbin.get_kernels();
149 *std::find_if(xkernels.begin(), xkernels.end(),
150 [kernelNameInXclbin, verbosity](xrt::xclbin::kernel &k) {
151 auto name = k.get_name();
152 if (verbosity >= 1) {
153 std::cout <<
"Name: " << name << std::endl;
155 return name.rfind(kernelNameInXclbin, 0) == 0;
157 auto kernelName = xkernel.get_name();
160 std::cout <<
"Registering xclbin: " << xclbinFileName <<
"\n";
162 device.register_xclbin(xclbin);
166 std::cout <<
"Getting hardware context.\n";
167 xrt::hw_context context(device, xclbin.get_uuid());
171 std::cout <<
"Getting handle to kernel:" << kernelName <<
"\n";
172 kernel = xrt::kernel(context, kernelName);
189 assert(std::numeric_limits<float>::epsilon() <= epsilon);
190 assert(epsilon < 1.f);
195 auto diff = std::abs(a - b);
197 std::min((std::abs(a) + std::abs(b)), std::numeric_limits<float>::max());
201 return diff < std::max(abs_th, epsilon * norm);
209 std::ofstream fout(path);
210 uint32_t *traceOut = (uint32_t *)traceOutPtr;
211 for (
int i = 0; i < trace_size /
sizeof(traceOut[0]); i++) {
212 fout << std::setfill(
'0') << std::setw(8) << std::hex << (int)traceOut[i];
std::string help(const std::vector< std::string > &groups={}, bool print_usage=true) const
ParseResult parse(int argc, const char *const *argv)
OptionAdder add_options(std::string group="")
std::size_t count(const std::string &o) const
CXXOPTS_NODISCARD const char * what() const noexcept override
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)
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)