forked from Xilinx/mlir-aie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
247 lines (206 loc) · 8.17 KB
/
Copy pathtest.cpp
File metadata and controls
247 lines (206 loc) · 8.17 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//===- test.cpp -------------------------------------------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) 2023, Advanced Micro Devices, Inc.
//
//===----------------------------------------------------------------------===//
#include <bits/stdc++.h>
#include <boost/program_options.hpp>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdfloat>
#include <vector>
#include "xrt/xrt_bo.h"
#include "xrt/xrt_device.h"
#include "xrt/xrt_kernel.h"
#include "test_utils.h"
#ifndef DATATYPES_USING_DEFINED
#define DATATYPES_USING_DEFINED
// ------------------------------------------------------
// Configure this to match your buffer data type
// ------------------------------------------------------
using INOUT0_DATATYPE = std::bfloat16_t;
using INOUT1_DATATYPE = std::bfloat16_t;
#endif
namespace po = boost::program_options;
// ----------------------------------------------------------------------------
// Verify results (specific to our design example)
// ----------------------------------------------------------------------------
template <typename T>
int verify(int size, std::vector<T> A, std::vector<T> B, int verbosity) {
int errors = 0;
for (uint32_t i = 0; i < size; i++) {
// If the input is nan, lets just say its good
if (isnan(A[i]))
continue;
T ref = (T)0;
if (A[i] > (T)0)
ref = A[i];
if (!test_utils::nearly_equal(ref, B[i])) {
std::cout << "Error in output " << B[i] << " != " << ref << " from "
<< A[i] << std::endl;
errors++;
} else {
if (verbosity > 1)
std::cout << "Correct output " << B[i] << " == " << ref << std::endl;
}
}
return errors;
}
int main(int argc, const char *argv[]) {
// Program arguments parsing
po::options_description desc("Allowed options");
po::variables_map vm;
test_utils::add_default_options(desc);
test_utils::parse_options(argc, argv, desc, vm);
int verbosity = vm["verbosity"].as<int>();
int do_verify = vm["verify"].as<bool>();
int n_iterations = vm["iters"].as<int>();
int n_warmup_iterations = vm["warmup"].as<int>();
int trace_size = vm["trace_sz"].as<int>();
int INOUT0_VOLUME = 65536; // Input
int INOUT1_VOLUME = INOUT0_VOLUME; // Output
size_t INOUT0_SIZE = INOUT0_VOLUME * sizeof(INOUT0_DATATYPE);
size_t INOUT1_SIZE = INOUT1_VOLUME * sizeof(INOUT1_DATATYPE);
size_t OUT_SIZE = INOUT1_SIZE + trace_size;
srand(time(NULL));
// Load instruction sequence
std::vector<uint32_t> instr_v =
test_utils::load_instr_sequence(vm["instr"].as<std::string>());
if (verbosity >= 1)
std::cout << "Sequence instr count: " << instr_v.size() << "\n";
// ------------------------------------------------------
// Get device, load the xclbin & kernel and register them
// ------------------------------------------------------
xrt::device device;
xrt::kernel kernel;
test_utils::init_xrt_load_kernel(device, kernel, verbosity,
vm["xclbin"].as<std::string>(),
vm["kernel"].as<std::string>());
// ------------------------------------------------------
// Initialize input/ output buffer sizes and sync them
// ------------------------------------------------------
auto bo_instr = xrt::bo(device, instr_v.size() * sizeof(int),
XCL_BO_FLAGS_CACHEABLE, kernel.group_id(1));
auto bo_inout0 =
xrt::bo(device, INOUT0_SIZE, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(3));
auto bo_inout1 =
xrt::bo(device, OUT_SIZE, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(4));
// Assumes trace will only be added to inout1
if (verbosity >= 1)
std::cout << "Writing data into buffer objects.\n";
// Initialize instruction buffer
void *bufInstr = bo_instr.map<void *>();
memcpy(bufInstr, instr_v.data(), instr_v.size() * sizeof(int));
// Initialize Inout buffer 0 with ascending bfloat16 raw patterns
// All of them ...
INOUT0_DATATYPE *bufInOut0 = bo_inout0.map<INOUT0_DATATYPE *>();
std::vector<INOUT0_DATATYPE> AVec(INOUT0_VOLUME);
for (int i = 0; i < INOUT0_VOLUME; i++) {
uint16_t raw = (uint16_t)i;
AVec[i] = *(std::bfloat16_t *)(&raw);
}
memcpy(bufInOut0, AVec.data(), (AVec.size() * sizeof(INOUT0_DATATYPE)));
// Initialize Inout buffer 1 with zeros
char *bufInOut1 = bo_inout1.map<char *>();
memset(bufInOut1, 0, OUT_SIZE); // Zeroes out INOUT1_VOLUME + trace_size
// Sync buffers to update input buffer values
bo_instr.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo_inout0.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo_inout1.sync(XCL_BO_SYNC_BO_TO_DEVICE);
// ------------------------------------------------------
// Initialize run configs
// ------------------------------------------------------
unsigned num_iter = n_iterations + n_warmup_iterations;
float npu_time_total = 0;
float npu_time_min = 9999999;
float npu_time_max = 0;
int errors = 0;
// ------------------------------------------------------
// Main run loop
// ------------------------------------------------------
for (unsigned iter = 0; iter < num_iter; iter++) {
if (verbosity >= 1) {
std::cout << "Running Kernel.\n";
}
// Run kernel
if (verbosity >= 1)
std::cout << "Running Kernel.\n";
auto start = std::chrono::high_resolution_clock::now();
unsigned int opcode = 3;
auto run = kernel(opcode, bo_instr, instr_v.size(), bo_inout0, bo_inout1);
run.wait();
auto stop = std::chrono::high_resolution_clock::now();
bo_inout1.sync(XCL_BO_SYNC_BO_FROM_DEVICE);
if (iter < n_warmup_iterations) {
/* Warmup iterations do not count towards average runtime. */
continue;
}
// Copy output results and verify they are correct
std::vector<INOUT1_DATATYPE> BVec(INOUT1_VOLUME);
memcpy(BVec.data(), bufInOut1, (BVec.size() * sizeof(INOUT1_DATATYPE)));
if (do_verify) {
if (verbosity >= 1) {
std::cout << "Verifying results ..." << std::endl;
}
auto vstart = std::chrono::system_clock::now();
errors = verify(INOUT0_VOLUME, AVec, BVec, verbosity);
auto vstop = std::chrono::system_clock::now();
float vtime =
std::chrono::duration_cast<std::chrono::seconds>(vstop - vstart)
.count();
if (verbosity >= 1) {
std::cout << "Verify time: " << vtime << "secs." << std::endl;
}
} else {
if (verbosity >= 1)
std::cout << "WARNING: results not verified." << std::endl;
}
// Write trace values if trace_size > 0
if (trace_size > 0) {
test_utils::write_out_trace(((char *)bufInOut1) + INOUT1_SIZE, trace_size,
vm["trace_file"].as<std::string>());
}
// Accumulate run times
float npu_time =
std::chrono::duration_cast<std::chrono::microseconds>(stop - start)
.count();
npu_time_total += npu_time;
npu_time_min = (npu_time < npu_time_min) ? npu_time : npu_time_min;
npu_time_max = (npu_time > npu_time_max) ? npu_time : npu_time_max;
}
// ------------------------------------------------------
// Print verification and timing results
// ------------------------------------------------------
// TODO - Mac count to guide gflops
float macs = 0;
std::cout << std::endl
<< "Avg NPU time: " << npu_time_total / n_iterations << "us."
<< std::endl;
if (macs > 0)
std::cout << "Avg NPU gflops: "
<< macs / (1000 * npu_time_total / n_iterations) << std::endl;
std::cout << std::endl
<< "Min NPU time: " << npu_time_min << "us." << std::endl;
if (macs > 0)
std::cout << "Max NPU gflops: " << macs / (1000 * npu_time_min)
<< std::endl;
std::cout << std::endl
<< "Max NPU time: " << npu_time_max << "us." << std::endl;
if (macs > 0)
std::cout << "Min NPU gflops: " << macs / (1000 * npu_time_max)
<< std::endl;
if (!errors) {
std::cout << "\nPASS!\n\n";
return 0;
} else {
std::cout << "\nError count: " << errors << "\n\n";
std::cout << "\nFailed.\n\n";
return 1;
}
}