-
Notifications
You must be signed in to change notification settings - Fork 3
/
hgemmtest.cpp
388 lines (337 loc) · 14 KB
/
hgemmtest.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include "Eigen/Dense"
#include "half.hpp"
#define CL_HPP_MINIMUM_OPENCL_VERSION 110
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_ENABLE_EXCEPTIONS
#include "cl2.hpp"
template <typename T>
using EigenMatrixMap =
Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>;
template <typename T>
using ConstEigenMatrixMap =
Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>;
std::string m_cl_args = "-cl-mad-enable -cl-fast-relaxed-math -cl-no-signed-zeros -cl-denorms-are-zero";
cl::Program m_program;
cl::Device m_device;
cl::Context m_context;
template<class T>
static std::string opencl_dev_type_to_string(T type) {
if (type == CL_DEVICE_TYPE_CPU) {
return "CPU";
} else if (type == CL_DEVICE_TYPE_GPU) {
return "GPU";
} else if (type == CL_DEVICE_TYPE_ACCELERATOR) {
return "Accelerator";
} else {
return "Unknown";
}
}
static std::string trim(std::string trim_me) {
boost::algorithm::trim(trim_me);
return trim_me;
}
static size_t next_power_of_two(const size_t x) {
return 2 << size_t(std::ceil(std::log2(x)) - 1);
}
static void sgemmBatched_ref(const std::vector<half_float::half>& a,
const std::vector<half_float::half>& b,
std::vector<half_float::half>& c,
const int m, const int n, const int k,
const int batch_size) {
std::vector<float> ar(a.size());
std::vector<float> br(b.size());
std::vector<float> cr(c.size());
std::copy(begin(a), end(a), begin(ar));
std::copy(begin(b), end(b), begin(br));
for (auto batch = 0; batch < batch_size; batch++) {
auto offset_u = batch * m * k;
auto offset_v = batch * n * k;
auto offset_m = batch * m * n;
#ifdef USE_BLAS
// Calculates C = transpose(tranpose(A) * B) in row major, or
// C = A * transpose(B) in column major.
for (auto i = 0; i < m; i++) {
for (auto j = 0; j < n; j++) {
auto acc = 0.0f;
for (auto l = 0; l < k; l++) {
acc += ar[l * m + i + offset_u] * br[l * n + j + offset_v];
}
cr[j * m + i + offset_m] = acc;
}
}
#else
auto C = EigenMatrixMap<float>(cr.data() + offset_m, m, n);
auto A = ConstEigenMatrixMap<float>(ar.data() + offset_u, m, k);
auto B = ConstEigenMatrixMap<float>(br.data() + offset_v, n, k);
C.noalias() = (A * B.transpose());
#endif
}
std::copy(begin(cr), end(cr), begin(c));
}
static void sgemm_generate_data(std::vector<half_float::half> &x,
const int m, const int n,
const int batch_size,
const int m_ceil, const int n_ceil) {
for (auto batch = 0; batch < batch_size; batch++) {
for (auto i = 0; i < n_ceil; i++) {
if (i < n) {
for (auto j = 0; j < m; j++) {
x[batch*n_ceil*m_ceil + i*m_ceil + j] =
(( (i ^ j) + batch - 128) % 256) / 256.0f;
}
for (auto j = m; j < m_ceil; j++) {
x[batch*n_ceil*m_ceil + i*m_ceil + j] = 0.0f;
}
} else {
for (auto j = 0; j < m_ceil; j++) {
x[batch*n_ceil*m_ceil + i*m_ceil + j] = 0.0f;
}
}
}
}
}
static float compare_ref(std::vector<half_float::half> &x, std::vector<half_float::half> &ref,
const int m, const int n, const int batch_size,
const int m_ceil, const int n_ceil) {
auto sum = 0.0f;
for (auto batch = 0; batch < batch_size; batch++) {
for (auto j = 0; j < m; j++) {
for (auto i = 0; i < n; i++) {
auto r = ref[batch*n*m + j*n + i];
auto y = x[batch*n_ceil*m_ceil + j*n_ceil + i];
sum += (r - y) * (r - y);
// printf("%.2f ", (float)(r-y));
}
// printf("\n");
}
// printf("\n\n");
}
return sum / (m * n * batch_size);
}
int main() {
std::ifstream t("hgemm.cl");
std::stringstream buffer;
buffer << t.rdbuf();
std::string sourceCode = buffer.str();
std::vector<cl::Platform> platforms;
try {
cl::Platform::get(&platforms);
} catch (const cl::Error &e) {
printf("OpenCL: %s\n", e.what());
throw;
}
auto best_version = 0.0f;
cl::Platform best_platform;
cl::Device best_device;
std::string best_vendor;
auto best_score = 0;
auto found_device = false;
auto id = 0;
printf("Detected %d OpenCL platforms.\n", platforms.size());
for (const auto &p : platforms) {
std::string platvers = p.getInfo<CL_PLATFORM_VERSION>();
std::string platprof = p.getInfo<CL_PLATFORM_PROFILE>();
std::string platname = p.getInfo<CL_PLATFORM_NAME>();
std::string platvend = p.getInfo<CL_PLATFORM_VENDOR>();
printf("Platform version: %s\n", platvers.c_str());;
printf("Platform profile: %s\n", platprof.c_str());
printf("Platform name: %s\n", platname.c_str());
printf("Platform vendor: %s\n", platvend.c_str());
std::istringstream versstream(platvers);
std::string tmp;
float opencl_version;
versstream >> tmp >> opencl_version;
std::vector<cl::Device> devices;
try {
p.getDevices(CL_DEVICE_TYPE_ALL, &devices);
} catch (const cl::Error &e) {
printf("Error getting device(s): %s: %d\n", e.what(), e.err());
devices.clear();
}
for (auto& d : devices) {
printf("Device ID: %d\n", id);
printf("Device name: %s\n",
trim(d.getInfo<CL_DEVICE_NAME>()).c_str());
printf("Device type: %s\n",
opencl_dev_type_to_string(
d.getInfo<CL_DEVICE_TYPE>()).c_str());
printf("Device vendor: %s\n",
d.getInfo<CL_DEVICE_VENDOR>().c_str());
printf("Device driver: %s\n",
d.getInfo<CL_DRIVER_VERSION>().c_str());
printf("Device speed: %u MHz\n",
d.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>());
printf("Device cores: %u CU\n",
d.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>());
// assign score, try to find best device
int this_score = 0;
std::string this_vendor = d.getInfo<CL_DEVICE_VENDOR>();
this_score += 1000 * boost::icontains(this_vendor, "advanced micro devices");
this_score += 1000 * boost::icontains(this_vendor, "amd");
this_score += 1000 * boost::icontains(this_vendor, "nvidia");
this_score += 500 * boost::icontains(this_vendor, "intel");
this_score += 100 * (d.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU);
this_score += opencl_version * 10;
printf("Device score: %d\n", this_score);
if ((this_score > best_score)) {
best_version = opencl_version;
best_platform = p;
best_device = d;
best_vendor = this_vendor;
best_score = this_score;
found_device = true;
}
id++;
}
}
if (!found_device) {
throw std::runtime_error("No suitable OpenCL device found.");
}
printf("Selected platform: %s\n",
best_platform.getInfo<CL_PLATFORM_NAME>().c_str());
printf("Selected device: %s\n",
trim(best_device.getInfo<CL_DEVICE_NAME>()).c_str());
printf("with OpenCL %2.1f capability.\n", best_version);
cl::Context context;
try {
context = cl::Context(best_device);
} catch (const cl::Error &e) {
printf("Error creating OpenCL context: %s: %d", e.what(), e.err());
throw std::runtime_error("Error creating OpenCL context.");
}
m_context = context;
m_device = best_device;
// Make program of the source code in the context
cl::Kernel kernel;
std::string max_option;
float min_time = 100000.0;
float min_time_error = 0.0;
auto test = [&](int mdimc, int ndimc, int mwg, int nwg, int kwg, int sa, int sb, int vwm, int vwn)
{
auto args = m_cl_args;
auto tune_args = std::string();
tune_args += " -DMDIMC="+std::to_string(mdimc);
tune_args += " -DNDIMC="+std::to_string(ndimc);
tune_args += " -DMWG="+std::to_string(mwg);
tune_args += " -DNWG="+std::to_string(nwg);
tune_args += " -DKWG="+std::to_string(kwg);
tune_args += " -DSA="+std::to_string(sa);
tune_args += " -DSB="+std::to_string(sb);
tune_args += " -DVWM="+std::to_string(vwm);
tune_args += " -DVWN="+std::to_string(vwn);
args += tune_args;
int batch_size = 36;
int m = 256;
int n = 32;
int k = 256;
// This needs to be at minimum the maximum (MNK/WG) values above.
auto m_max = std::max(64, m);
auto n_max = std::max(64, n);
auto k_max = std::max(32, k);
auto at_size = batch_size
* next_power_of_two(k_max) * next_power_of_two(m_max);
auto b_size = batch_size
* next_power_of_two(k_max) * next_power_of_two(n_max);
auto c_size = batch_size
* next_power_of_two(m_max) * next_power_of_two(n_max);
auto at = std::vector<half_float::half>(at_size);
auto b = std::vector<half_float::half>(b_size);
auto c = std::vector<half_float::half>(c_size);
auto c_ref = std::vector<half_float::half>(c_size);
auto queue = cl::CommandQueue(m_context,
m_device,
CL_QUEUE_PROFILING_ENABLE);
auto event = cl::Event();
auto aBuffer = cl::Buffer(
m_context,
CL_MEM_READ_WRITE, sizeof(half_float::half) * at_size, nullptr, nullptr);
auto bBuffer = cl::Buffer(
m_context,
CL_MEM_READ_WRITE, sizeof(half_float::half) * b_size, nullptr, nullptr);
auto cBuffer = cl::Buffer(
m_context,
CL_MEM_READ_WRITE, sizeof(half_float::half) * c_size, nullptr, nullptr);
sgemm_generate_data(at, k, m, batch_size, k, m);
sgemm_generate_data(b, n, k, batch_size, n, k);
sgemmBatched_ref(at, b, c_ref, m, n, k, batch_size);
queue.enqueueWriteBuffer(aBuffer, CL_FALSE, 0,
at_size * sizeof(half_float::half), at.data());
queue.enqueueWriteBuffer(bBuffer, CL_FALSE, 0,
b_size * sizeof(half_float::half), b.data());
queue.finish();
auto sum_time = 0.0f;
auto sum_error = 0.0f;
try {
m_program = cl::Program(m_context, sourceCode);
m_program.build(args.c_str());
kernel = cl::Kernel(m_program, "HgemmBatched");
kernel.setArg(0, m);
kernel.setArg(1, n);
kernel.setArg(2, k);
kernel.setArg(3, aBuffer);
kernel.setArg(4, bBuffer);
kernel.setArg(5, cBuffer);
for (int i=0; i<4; i++) {
cl::NDRange local_sgemm = {size_t(32 * mdimc / 16), size_t(ndimc / 16), 1};
cl::NDRange size_sgemm = {size_t(32 * m / 16 * mdimc / mwg), size_t(n / 16 * ndimc / nwg), size_t(batch_size)};
queue.enqueueNDRangeKernel(kernel, cl::NullRange,
size_sgemm, local_sgemm,
nullptr, &event);
queue.finish();
event.wait();
queue.enqueueReadBuffer(cBuffer, CL_FALSE, 0,
c_size * sizeof(half_float::half), c.data());
queue.finish();
auto this_error = compare_ref(c, c_ref, n, m, batch_size, n, m);
auto elapsed = event.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
sum_time += elapsed;
sum_error += this_error;
}
} catch (...) {
sum_error = 10000;
sum_time = 0.0f;
}
auto time = sum_time * 1e-6 / 4;
printf("%s %f %f\n", tune_args.c_str(), (double)sum_error / 4, (double) time);
if(min_time > time) {
min_time = time;
min_time_error = sum_error / 4;
max_option = tune_args;
}
};
for ( int mdimc = 16; mdimc <= 64; mdimc *= 2) {
for (int ndimc = 16; ndimc <= 32; ndimc *= 2) {
for ( int mwg = 16; mwg <= 64; mwg *= 2) {
for ( int nwg = 16; nwg <= 32; nwg *= 2) {
if(mwg < mdimc) continue;
if(nwg < ndimc) continue;
for (int kwg = 16; kwg < 64; kwg *= 2) {
for(int sa = 0; sa < 2; sa++) {
for(int sb = 0; sb < 2; sb++) {
for(int vwm = 1; vwm <= 8; vwm *= 2) {
for(int vwn = 1; vwn <= 8; vwn *= 2) {
if(sa == 0 && vwm != 1) continue;
if(sb == 0 && vwn != 1) continue;
try {
test(mdimc, ndimc, mwg, nwg, kwg, sa, sb, vwm, vwn);
} catch(...) {}
}
}
}
}
}
}
}
}
}
printf("\n\nWinner : %s %f %f\n", max_option.c_str(), (double)min_time_error, (double) min_time);
return 0;
}