forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcu-vector-speed-test.cc
More file actions
300 lines (247 loc) · 8.6 KB
/
cu-vector-speed-test.cc
File metadata and controls
300 lines (247 loc) · 8.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
// cudamatrix/cu-vector-speed-test.cc
// Copyright 2013 Johns Hopkins University (author: Daniel Povey)
// 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.";
}
#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 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 CudaVectorSpeedTest() {
std::vector<int32> sizes;
sizes.push_back(16);
sizes.push_back(32);
sizes.push_back(64);
sizes.push_back(128);
sizes.push_back(256);
sizes.push_back(1024);
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++) {
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++) {
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);
}
}
} // namespace kaldi
int main() {
//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
std::cout << "Tests succeeded.\n";
}