cu-compressed-matrix-test.cc
5.02 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
// cudamatrix/cu-compressed-matrix-test.cc
// Copyright 2018 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-lib.h"
using namespace kaldi;
namespace kaldi {
void CuCompressedMatrixTestSign() {
int32 num_rows = RandInt(80, 100),
num_cols = RandInt(80, 100);
CuMatrix<BaseFloat> M(num_rows, num_cols);
M.SetRandn();
CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
CuCompressedMatrixBase *cm = NewCuCompressedMatrix(kCompressedMatrixUint8, 0.0);
// this just stores (M(i, j) > 0 ? 1 : 0).
cm->CopyFromMat(M);
cm->CopyToMat(&M2);
M.Heaviside(M);
AssertEqual(M, M2);
delete cm;
}
void CuCompressedMatrixTestNonnegative() {
int32 num_rows = RandInt(80, 100),
num_cols = RandInt(80, 100);
CuMatrix<BaseFloat> M(num_rows, num_cols);
M.SetRandUniform();
BaseFloat range = 0.5 * RandInt(1, 5);
M.Scale(range);
CuCompressedMatrixType t = (RandInt(0, 1) == 0 ?
kCompressedMatrixUint8 :
kCompressedMatrixUint16);
// since the input is in the correct range, truncating or not should make no
// difference.
bool truncate = (RandInt(0, 1) == 0);
BaseFloat extra_error = 0.0;
if (truncate && (RandInt(0, 1) == 0)) {
// this tests that with truncate == true, adding a small offset, which would
// take us outside the representable range, will not add too much extra
// error. (with truncate == false this would not be true because we wouldn't
// round to the edges of the range, it would wrap around).
extra_error = -0.01 * (RandInt(0, 1) == 0 ? 1.0 : -1.0);
M.Add(extra_error);
}
CuCompressedMatrixBase *cm = NewCuCompressedMatrix(t, range, truncate);
CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
cm->CopyFromMat(M);
cm->CopyToMat(&M2);
M2.AddMat(-1.0, M);
BaseFloat diff_max = M2.Max(),
diff_min = M2.Min();
BaseFloat
headroom = 1.1,
max_expected_error = fabs(extra_error) + headroom * 0.5 *
range / (t == kCompressedMatrixUint8 ? 255 : 65535);
KALDI_ASSERT(diff_max < max_expected_error &&
diff_min > -1.0 * max_expected_error);
delete cm;
}
// this is like CuCompressedMatrixTestNonnegative but
// with signed integers, and input in the range [-range, +range].
void CuCompressedMatrixTestSymmetric() {
int32 num_rows = RandInt(80, 100),
num_cols = RandInt(80, 100);
CuMatrix<BaseFloat> M(num_rows, num_cols);
M.SetRandUniform();
M.Scale(2.0);
M.Add(-1.0);
BaseFloat range = 0.5 * RandInt(1, 5);
M.Scale(range);
CuCompressedMatrixType t = (RandInt(0, 1) == 0 ?
kCompressedMatrixInt8 :
kCompressedMatrixInt16);
// since the input is in the correct range, truncating or not should make no
// difference.
bool truncate = (RandInt(0, 1) == 0);
BaseFloat extra_error = 0.0;
if (truncate && (RandInt(0, 1) == 0)) {
// this tests that with truncate == true, adding a small offset, which would
// take us outside the representable range, will not add too much extra
// error. (with truncate == false this would not be true because we wouldn't
// round to the edges of the range, it would wrap around).
extra_error = -0.01 * (RandInt(0, 1) == 0 ? 1.0 : -1.0);
M.Add(extra_error);
}
CuCompressedMatrixBase *cm = NewCuCompressedMatrix(t, range, truncate);
CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
cm->CopyFromMat(M);
cm->CopyToMat(&M2);
M2.AddMat(-1.0, M);
BaseFloat diff_max = M2.Max(),
diff_min = M2.Min();
BaseFloat
headroom = 1.1,
max_expected_error = fabs(extra_error) + headroom * 0.5 *
range / (t == kCompressedMatrixInt8 ? 127 : 32767);
KALDI_ASSERT(diff_max < max_expected_error &&
diff_min > -1.0 * max_expected_error);
delete cm;
}
} // namespace kaldi
int main() {
SetVerboseLevel(1);
// we don't run this test if CUDA is not compiled in, since
// you can't instantiate class CuCompressedMatrix in that case.
#if HAVE_CUDA == 1
CuDevice::Instantiate().SelectGpuId("yes");
for (int32 i = 1; i < 10; i++) {
CuCompressedMatrixTestSign();
CuCompressedMatrixTestNonnegative();
CuCompressedMatrixTestSymmetric();
}
#endif
return 0;
}