cu-vector-speed-test.cc
14.3 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// cudamatrix/cu-vector-speed-test.cc
// Copyright 2013 Johns Hopkins University (author: Daniel Povey)
// 2017 Daniel Galvez
// 2016-2018 Shiyin Kang
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <vector>
#include <cstdlib>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "cudamatrix/cu-matrix.h"
#include "cudamatrix/cu-vector.h"
#include "cudamatrix/cu-math.h"
using namespace kaldi;
namespace kaldi {
template<typename Real>
std::string NameOf() {
return (sizeof(Real) == 8 ? "<double>" : "<float>");
}
template<typename Real> void TestCuVectorSoftmax(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> M(dim);
M.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
M.ApplySoftMax();
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::Softmax" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorSum(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> M(dim);
M.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
M.Sum();
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::Sum" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real, typename OtherReal> void TestCuVectorCopyFromVec(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> M(dim);
M.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
CuVector<OtherReal> v(dim);
v.CopyFromVec(M);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::CopyFromVec" << NameOf<Real>() << " to "
<< NameOf<OtherReal>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
#if HAVE_CUDA == 1
// This test choose the min length of vectors to be reduced on GPU.
// Smaller vector will be copied to RAM and reduced on CPU.
template<typename Real> void TestCuVectorSumChooseMinLength() {
BaseFloat time_in_secs = 0.02;
for (int dim = 100; dim < 1000000; dim = dim * 1.5 + 1 ) {
CuVector<Real> M(dim);
BaseFloat gflops, gflops_cpu;
Real result = 0, result_cpu = 0;
M.SetRandn();
{
Timer tim;
int32 iter = 0;
for (; tim.Elapsed() < time_in_secs; iter++) {
// Force GPU reduction
int dimBlock = CU1DBLOCK;
int dimGrid = n_blocks(M.Dim(), dimBlock);
if (dimGrid > 256) {
dimGrid = 256;
}
CuVector<Real> ans(dimGrid, kUndefined);
cuda_vec_sum(dimGrid, dimBlock, M.Data(), ans.Data(), M.Dim(), 1);
CU_SAFE_CALL(cudaGetLastError());
Vector<Real> ans_cpu(ans);
result = ans_cpu.Sum();
}
BaseFloat fdim = dim;
gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
}
{
Timer tim;
int32 iter = 0;
for (; tim.Elapsed() < time_in_secs; iter++) {
Vector<Real> M_cpu(M);
result_cpu = M_cpu.Sum();
}
BaseFloat fdim = dim;
gflops_cpu = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
}
KALDI_LOG << "CuVector::Sum" << NameOf<Real>() << ", dim: " << dim
<< ", speed: GPU " << (gflops > gflops_cpu ? ">" : "<")
<< " CPU, GPU speed: " << gflops << " Gflops. CPU speed: "
<< gflops_cpu << " Gflops. Result diff: " << (result - result_cpu);
}
}
#endif
template<typename Real> void TestCuVectorVecVecOne(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> M(dim);
M.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
CuVector<Real> ones(dim);
ones.Set(1.0);
VecVec(M, ones);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::VecVecOne" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorAddDiagMatMat(int32 dim,
MatrixTransposeType transN,
MatrixTransposeType transO) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
CuMatrix<Real> N(dim, dim), O(dim, dim);
N.SetRandn();
O.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.AddDiagMatMat(1.0, N, transN, O, transO, 1.0);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddDiagMatMat" << NameOf<Real>()
<< (transN == kNoTrans ? "[no-trans],":"[trans],")
<< (transO == kNoTrans ? "[no-trans],":"[trans],")
<< " for dim = "<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorAddDiagMat2OnVariousShapes(
int32 dim, MatrixTransposeType trans) {
BaseFloat time_in_secs = 0.02;
int32 size = 1024 * 32;
CuVector<Real> v(trans == kNoTrans ? size / dim : dim);
v.SetRandn();
CuMatrix<Real> N(size / dim, dim);
N.SetRandn();
Timer tim;
int32 iter = 0;
for (; tim.Elapsed() < time_in_secs; iter++) {
v.AddDiagMat2(1.0, N, trans, 0.0);
}
BaseFloat fdim = size;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddDiagMat2Shapes" << NameOf<Real>()
<< (trans == kTrans ? "[trans]" : "[no-trans]") << ", for dim = ("
<< size / dim << ", " << dim << "), speed was " << gflops
<< " gigaflops.";
}
template<typename Real> void TestCuVectorAddDiagMat2(int32 dim, MatrixTransposeType trans) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
CuMatrix<Real> N(dim, dim);
N.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.AddDiagMat2(1.0, N, trans, 0.0);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddDiagMat2" << NameOf<Real>()
<< (trans == kTrans ? "[trans]" : "[no-trans]") << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorAddRowSumMat(int32 dim, MatrixTransposeType trans) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
CuMatrix<Real> N(dim, dim);
N.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.AddRowSumMat(1.0, N, 0.5);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddRowSumMat" << NameOf<Real>()
<< (trans == kTrans ? "[trans]" : "[no-trans]") << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorAddColSumMat(int32 dim, MatrixTransposeType trans) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
CuMatrix<Real> N(dim, dim);
N.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.AddColSumMat(1.0, N, 0.5);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddColSumMat" << NameOf<Real>()
<< (trans == kTrans ? "[trans]" : "[no-trans]") << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorApplyFloor(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
Real threshold = RandInt(-35000, 35000) / Real(100);
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
MatrixIndexT dummy_count;
v.ApplyFloor(threshold, &dummy_count);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::ApplyFloor" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorApplyFloorNoCount(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
Real threshold = RandInt(-35000, 35000) / Real(100);
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.ApplyFloor(threshold, nullptr);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::ApplyFloor (no count variety)" << NameOf<Real>()
<< ", for dim = " << dim << ", speed was " << gflops
<< " gigaflops.";
}
template<typename Real> void TestCuVectorApplyCeiling(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
Real threshold = RandInt(-35000, 35000) / Real(100);
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
MatrixIndexT dummy_count;
v.ApplyCeiling(threshold, &dummy_count);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::ApplyCeiling" << NameOf<Real>() << ", for dim = "
<< dim << ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void TestCuVectorApplyCeilingNoCount(int32 dim) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(dim);
v.SetRandn();
Real threshold = RandInt(-35000, 35000) / Real(100);
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.ApplyCeiling(threshold, nullptr);
}
BaseFloat fdim = dim;
BaseFloat gflops = (fdim * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::ApplyCeiling (no count variety)" << NameOf<Real>()
<< ", for dim = " << dim << ", speed was " << gflops
<< " gigaflops.";
}
template<typename Real> void TestCuVectorAddDiagMatMatShape(
int32 num_rows, int32 num_cols, MatrixTransposeType transM,
MatrixTransposeType transN) {
BaseFloat time_in_secs = 0.02;
CuVector<Real> v(transM == kTrans ? num_cols : num_rows);
v.SetRandn();
CuMatrix<Real> M(num_rows, num_cols);
CuMatrix<Real> N(transM != transN ? num_rows : num_cols,
transM != transN ? num_cols : num_rows);
M.SetRandn();
N.SetRandn();
Timer tim;
int32 iter = 0;
for (;tim.Elapsed() < time_in_secs; iter++) {
v.AddDiagMatMat(1.0, M, transM, N, transN, 1.0);
}
BaseFloat fnr = num_rows;
BaseFloat fnc = num_cols;
BaseFloat gflops = (fnr * fnc * iter) / (tim.Elapsed() * 1.0e+09);
KALDI_LOG << "For CuVector::AddDiagMatMat" << NameOf<Real>()
<< (transM == kNoTrans ? "[no-trans],":"[trans],")
<< (transN == kNoTrans ? "[no-trans],":"[trans],")
<< " for dim = "<< num_rows << ", " << num_cols
<< ", speed was " << gflops << " gigaflops.";
}
template<typename Real> void CudaVectorSpeedTest() {
const size_t a = 1 << 5;
const size_t b = 1 << 8;
for (size_t i = a; i <= b; i *= 2) {
for (size_t j = a; j <= b; j *= 2) {
if (i * j <= a * b) {
TestCuVectorAddDiagMatMatShape<Real>(i, j, kNoTrans, kNoTrans);
TestCuVectorAddDiagMatMatShape<Real>(i, j, kNoTrans, kTrans);
TestCuVectorAddDiagMatMatShape<Real>(i, j, kTrans, kNoTrans);
TestCuVectorAddDiagMatMatShape<Real>(i, j, kTrans, kTrans);
}
}
}
std::vector<int32> sizes;
for (int i = 32; i <= 1024; i *= 2) {
sizes.push_back(i);
}
int32 ns = sizes.size();
for (int32 s = 0; s < ns; s++)
TestCuVectorSoftmax<Real>(sizes[s]);
#if HAVE_CUDA == 1
TestCuVectorSumChooseMinLength<Real>();
#endif
for (int32 s = 0; s < ns; s++)
TestCuVectorSum<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuVectorVecVecOne<Real>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuVectorCopyFromVec<Real, float>(sizes[s]);
for (int32 s = 0; s < ns; s++)
TestCuVectorCopyFromVec<Real, double>(sizes[s]);
for (int32 s = 0; s < ns; s++) {
TestCuVectorAddDiagMatMat<Real>(sizes[s], kNoTrans, kNoTrans);
TestCuVectorAddDiagMatMat<Real>(sizes[s], kNoTrans, kTrans);
TestCuVectorAddDiagMatMat<Real>(sizes[s], kTrans, kNoTrans);
TestCuVectorAddDiagMatMat<Real>(sizes[s], kTrans, kTrans);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorAddDiagMat2OnVariousShapes<Real>(sizes[s], kNoTrans);
TestCuVectorAddDiagMat2OnVariousShapes<Real>(sizes[s], kTrans);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorAddDiagMat2<Real>(sizes[s], kNoTrans);
TestCuVectorAddDiagMat2<Real>(sizes[s], kTrans);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorAddRowSumMat<Real>(sizes[s], kNoTrans);
TestCuVectorAddRowSumMat<Real>(sizes[s], kTrans);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorAddColSumMat<Real>(sizes[s], kNoTrans);
TestCuVectorAddColSumMat<Real>(sizes[s], kTrans);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorApplyFloor<Real>(sizes[s]);
TestCuVectorApplyFloorNoCount<Real>(sizes[s]);
}
for (int32 s = 0; s < ns; s++) {
TestCuVectorApplyCeiling<Real>(sizes[s]);
TestCuVectorApplyCeilingNoCount<Real>(sizes[s]);
}
}
} // namespace kaldi
int main() {
kaldi::SetVerboseLevel(1);
//Select the GPU
#if HAVE_CUDA == 1
CuDevice::Instantiate().SelectGpuId("yes"); //-2 .. automatic selection
#endif
kaldi::CudaVectorSpeedTest<float>();
#if HAVE_CUDA == 1
if (CuDevice::Instantiate().DoublePrecisionSupported()) {
kaldi::CudaVectorSpeedTest<double>();
} else {
KALDI_WARN << "Double precision not supported";
}
#else
kaldi::CudaVectorSpeedTest<double>();
#endif
KALDI_LOG << "Tests succeeded.";
}