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
320 lines (272 loc) · 10.6 KB
/
test.cpp
File metadata and controls
320 lines (272 loc) · 10.6 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//===- 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 "cxxopts.hpp"
#include <bits/stdc++.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdfloat>
#include "xrt/xrt_bo.h"
#include "xrt/xrt_device.h"
#include "xrt/xrt_kernel.h"
#include "common.h"
#ifndef DATATYPES_USING_DEFINED
#define DATATYPES_USING_DEFINED
#ifndef DTYPE_IN
#define DTYPE_IN std::bfloat16_t
#endif
#ifndef DTYPE_OUT
#define DTYPE_OUT std::bfloat16_t
#endif
#ifndef DTYPE_ACC
#define DTYPE_ACC float
#endif
using A_DATATYPE = DTYPE_IN;
using B_DATATYPE = DTYPE_IN;
using C_DATATYPE = DTYPE_OUT;
using ACC_DATATYPE = DTYPE_ACC;
#endif
#define XSTR(X) STR(X)
#define STR(X) #X
constexpr long long verify_stochastic_threshold = 1024 * 1024 * 1024;
constexpr int verify_stochastic_n_samples = 1000;
// Verification tolerance
// See "Note on Numerical Tolerances" in README.md
float abs_tol = matmul_common::get_abs_tol<C_DATATYPE>();
float rel_tol = matmul_common::get_rel_tol<C_DATATYPE>();
int main(int argc, const char *argv[]) {
// Program arguments parsing
cxxopts::Options options("Matrix Matrix Multiplication Test");
cxxopts::ParseResult vm;
matmul_common::add_default_options(options);
matmul_common::parse_options(argc, argv, options, 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 b_col_maj = vm["b_col_maj"].as<int>();
int c_col_maj = vm["c_col_maj"].as<int>();
// Fix the seed to ensure reproducibility in CI.
srand(1726250518); // srand(time(NULL));
int M = vm["M"].as<int>();
int K = vm["K"].as<int>();
int N = vm["N"].as<int>();
bool do_verify_stochastic =
(long long)M * N * K > verify_stochastic_threshold;
if (verbosity >= 1) {
std::cout << "Matrix size " << M << "x" << K << "x" << N << std::endl;
}
int A_VOLUME = M * K;
int B_VOLUME = N * K;
int C_VOLUME = M * N;
size_t A_SIZE = (A_VOLUME * sizeof(A_DATATYPE));
size_t B_SIZE = (B_VOLUME * sizeof(B_DATATYPE));
size_t C_SIZE = (C_VOLUME * sizeof(C_DATATYPE));
std::vector<uint32_t> instr_v =
test_utils::load_instr_binary(vm["instr"].as<std::string>());
if (verbosity >= 1)
std::cout << "Sequence instr count: " << instr_v.size() << "\n";
// Start the XRT test code
// Get a device handle
unsigned int device_index = 0;
auto device = xrt::device(device_index);
// Load the xclbin
if (verbosity >= 1)
std::cout << "Loading xclbin: " << vm["xclbin"].as<std::string>() << "\n";
auto xclbin = xrt::xclbin(vm["xclbin"].as<std::string>());
if (verbosity >= 1)
std::cout << "Kernel opcode: " << vm["kernel"].as<std::string>() << "\n";
std::string Node = vm["kernel"].as<std::string>();
// Get the kernel from the xclbin
auto xkernels = xclbin.get_kernels();
auto xkernel = *std::find_if(xkernels.begin(), xkernels.end(),
[Node, verbosity](xrt::xclbin::kernel &k) {
auto name = k.get_name();
if (verbosity >= 1) {
std::cout << "Name: " << name << std::endl;
}
return name.rfind(Node, 0) == 0;
});
auto kernelName = xkernel.get_name();
if (verbosity >= 1)
std::cout << "Registering xclbin: " << vm["xclbin"].as<std::string>()
<< "\n";
device.register_xclbin(xclbin);
// get a hardware context
if (verbosity >= 1)
std::cout << "Getting hardware context.\n";
xrt::hw_context context(device, xclbin.get_uuid());
// get a kernel handle
if (verbosity >= 1)
std::cout << "Getting handle to kernel:" << kernelName << "\n";
auto kernel = xrt::kernel(context, kernelName);
auto bo_instr = xrt::bo(device, instr_v.size() * sizeof(int),
XCL_BO_FLAGS_CACHEABLE, kernel.group_id(1));
auto bo_a =
xrt::bo(device, A_SIZE, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(3));
auto bo_b =
xrt::bo(device, B_SIZE, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(4));
auto bo_out =
xrt::bo(device, C_SIZE, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(5));
auto bo_tmp1 = xrt::bo(device, 1, XRT_BO_FLAGS_HOST_ONLY, kernel.group_id(6));
// Workaround so we declare a really small trace buffer when one is not used
int tmp_trace_size = (trace_size > 0) ? trace_size : 1;
auto bo_trace = xrt::bo(device, tmp_trace_size * 4, XRT_BO_FLAGS_HOST_ONLY,
kernel.group_id(7));
if (verbosity >= 1) {
std::cout << "Writing data into buffer objects.\n";
}
A_DATATYPE *bufA = bo_a.map<A_DATATYPE *>();
std::vector<A_DATATYPE> AVec(A_VOLUME);
for (int i = 0; i < A_VOLUME; i++) {
AVec[i] = matmul_common::get_random<A_DATATYPE>();
}
memcpy(bufA, AVec.data(), (AVec.size() * sizeof(A_DATATYPE)));
B_DATATYPE *bufB = bo_b.map<B_DATATYPE *>();
std::vector<B_DATATYPE> BVec(B_VOLUME);
for (int i = 0; i < B_VOLUME; i++) {
BVec[i] = matmul_common::get_random<B_DATATYPE>() * i;
// Diagonal:
// if(i % N == i / N) {
// BVec[i] = 1.0;
// } else {
// BVec[i] = 0.0;
// }
}
memcpy(bufB, BVec.data(), (BVec.size() * sizeof(B_DATATYPE)));
// Initialize outputs; bufOut is results matrix plus tracing info
char *bufOut = bo_out.map<char *>();
std::vector<C_DATATYPE> CVec(C_VOLUME);
memset(bufOut, 0, C_SIZE);
char *bufTrace = bo_trace.map<char *>();
if (trace_size > 0)
memset(bufTrace, 0, trace_size);
if (verbosity >= 2) {
std::cout << "DTYPE_IN = " XSTR(DTYPE_IN) "\n";
std::cout << "DTYPE_OUT = " XSTR(DTYPE_OUT) "\n";
std::cout << "Verification tolerance " << abs_tol << " absolute, "
<< rel_tol << " relative.\n";
std::cout << "A = \n";
matmul_common::print_matrix(AVec, K);
std::cout << "B = \n";
matmul_common::print_matrix(BVec, N);
}
// Instruction buffer for DMA configuration
void *bufInstr = bo_instr.map<void *>();
memcpy(bufInstr, instr_v.data(), instr_v.size() * sizeof(int));
bo_instr.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo_a.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo_b.sync(XCL_BO_SYNC_BO_TO_DEVICE);
bo_out.sync(XCL_BO_SYNC_BO_TO_DEVICE);
if (trace_size > 0)
bo_trace.sync(XCL_BO_SYNC_BO_TO_DEVICE);
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;
float macs = 2.0 * float(M) * float(K) * float(N);
for (unsigned iter = 0; iter < num_iter; iter++) {
if (verbosity >= 1) {
std::cout << "Running Kernel (iteration " << iter << ").\n";
}
auto start = std::chrono::high_resolution_clock::now();
unsigned int opcode = 3;
auto run = kernel(opcode, bo_instr, instr_v.size(), bo_a, bo_b, bo_out,
bo_tmp1, bo_trace);
ert_cmd_state r = run.wait();
if (r != ERT_CMD_STATE_COMPLETED) {
std::cout << "Kernel did not complete. Returned status: " << r << "\n";
return 1;
}
auto stop = std::chrono::high_resolution_clock::now();
bo_out.sync(XCL_BO_SYNC_BO_FROM_DEVICE);
if (trace_size > 0)
bo_trace.sync(XCL_BO_SYNC_BO_FROM_DEVICE);
if (iter < n_warmup_iterations) {
/* Warmup iterations do not count towards average runtime. */
continue;
}
if (do_verify) {
memcpy(CVec.data(), bufOut, (CVec.size() * sizeof(C_DATATYPE)));
if (verbosity >= 1) {
if (do_verify_stochastic) {
std::cout << "Verifying " << verify_stochastic_n_samples
<< " random samples against reference matmul ..."
<< std::endl;
} else {
std::cout << "Verifying against reference matmul ..." << std::endl;
}
}
auto vstart = std::chrono::system_clock::now();
if (do_verify_stochastic) {
errors = matmul_common::verify_stochastic<A_DATATYPE, C_DATATYPE,
ACC_DATATYPE>(
M, N, K, AVec, BVec, CVec, verify_stochastic_n_samples, verbosity,
abs_tol, rel_tol, b_col_maj, c_col_maj);
} else {
errors = matmul_common::verify<A_DATATYPE, C_DATATYPE, ACC_DATATYPE>(
M, N, K, AVec, BVec, CVec, verbosity, abs_tol, rel_tol, b_col_maj,
c_col_maj);
}
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 << " s." << std::endl;
}
} else {
if (verbosity >= 1)
std::cout << "WARNING: matmul results not verified." << std::endl;
}
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;
}
// Only write out trace of last iteration.
if (trace_size > 0) {
matmul_common::write_out_trace((char *)bufTrace, trace_size,
vm["trace_file"].as<std::string>());
}
std::cout << std::endl
<< "Avg NPU matmul time: " << npu_time_total / n_iterations << "us."
<< std::endl;
std::cout << "Avg NPU gflops: "
<< macs / (1000 * npu_time_total / n_iterations) << std::endl;
std::cout << std::endl
<< "Min NPU matmul time: " << npu_time_min << "us." << std::endl;
std::cout << "Max NPU gflops: " << macs / (1000 * npu_time_min) << std::endl;
std::cout << std::endl
<< "Max NPU matmul time: " << npu_time_max << "us." << std::endl;
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;
if (do_verify_stochastic) {
std::cout << " (out of " << verify_stochastic_n_samples
<< " random samples)";
}
std::cout << "\n\n";
std::cout << "\nFailed.\n\n";
return 1;
}
}