Blame view

src/matrix/sparse-matrix-test.cc 8.72 KB
8dcb6dfcb   Yannick Estève   first commit
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
  // matrix/sparse-matrix-test.cc
  
  // Copyright 2015      Guoguo Chen
  
  // 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 "matrix/matrix-lib.h"
  #include "util/stl-utils.h"
  
  namespace kaldi {
  
  template <typename Real>
  void UnitTestSparseVectorSum() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT dim = 10 + Rand() % 40;
  
      SparseVector<Real> svec(dim);
      svec.SetRandn(0.8);
  
      Vector<Real> vec(dim);
      vec.SetRandn();
      svec.CopyElementsToVec(&vec);
  
      Real sum1 = svec.Sum();
      Real sum2 = vec.Sum();
      AssertEqual(sum1, sum2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseVectorAddToVec() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT dim = 10 + Rand() % 40;
  
      SparseVector<Real> svec(dim);
      svec.SetRandn(0.8);
  
      Vector<Real> vec(dim);
      vec.SetRandn();
      svec.CopyElementsToVec(&vec);
  
      Vector<Real> other_vec1(dim);
      other_vec1.SetRandn();
      Vector<Real> other_vec2 = other_vec1;
  
      svec.AddToVec(0.7, &other_vec1);
      other_vec2.AddVec(0.7, vec);
      AssertEqual(other_vec1, other_vec2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseVectorMax() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT dim = 10 + Rand() % 40;
      if (RandInt(0, 3) == 0)
        dim = RandInt(1, 5);
  
      SparseVector<Real> svec(dim);
      if (RandInt(0, 3) != 0)
        svec.SetRandn(0.8);
  
      Vector<Real> vec(dim);
      vec.SetRandn();
      svec.CopyElementsToVec(&vec);
  
      int32 index1, index2;
      Real max1, max2;
  
      max1 = svec.Max(&index1);
      max2 = vec.Max(&index2);
  
      AssertEqual(max1, max2, 0.00001);
      AssertEqual(index1, index2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseVectorVecSvec() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT dim = 10 + Rand() % 40;
  
      SparseVector<Real> svec(dim);
      svec.SetRandn(0.8);
  
      Vector<Real> vec(dim);
      vec.SetRandn();
      svec.CopyElementsToVec(&vec);
  
      Vector<Real> other_vec(dim);
      other_vec.SetRandn();
  
      Real product1 = VecSvec(other_vec, svec);
      Real product2 = VecVec(other_vec, vec);
  
      KALDI_ASSERT(fabs(product1 - product2) < 1.0e-04);
    }
  }
  
  template <typename Real>
  void UnitTestSparseMatrixSum() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT row = 10 + Rand() % 40;
      MatrixIndexT col = 10 + Rand() % 50;
  
      SparseMatrix<Real> smat(row, col);
      smat.SetRandn(0.8);
  
      Matrix<Real> mat(row, col);
      mat.SetRandn();
      smat.CopyToMat(&mat);
  
      Real sum1 = smat.Sum();
      Real sum2 = mat.Sum();
      AssertEqual(sum1, sum2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseMatrixFrobeniusNorm() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT row = 10 + Rand() % 40;
      MatrixIndexT col = 10 + Rand() % 50;
  
      SparseMatrix<Real> smat(row, col);
      smat.SetRandn(0.8);
  
      Matrix<Real> mat(row, col);
      mat.SetRandn();
      smat.CopyToMat(&mat);
  
      Real norm1 = smat.FrobeniusNorm();
      Real norm2 = mat.FrobeniusNorm();
      AssertEqual(norm1, norm2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseMatrixAddToMat() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT row = 10 + Rand() % 40;
      MatrixIndexT col = 10 + Rand() % 50;
  
      SparseMatrix<Real> smat(row, col);
      smat.SetRandn(0.8);
  
      Matrix<Real> mat(row, col);
      mat.SetRandn();
      smat.CopyToMat(&mat);
  
      Matrix<Real> other_mat1(row, col);
      other_mat1.SetRandn();
      Matrix<Real> other_mat2 = other_mat1;
  
      smat.AddToMat(0.7, &other_mat1);
      other_mat2.AddMat(0.7, mat);
      AssertEqual(other_mat1, other_mat2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestSparseMatrixConstructor() {
    int32 num_rows = RandInt(1, 10),
        num_cols = RandInt(0, 10);
    if (num_cols == 0)
      num_rows = 0;
  
    Matrix<Real> mat(num_rows, num_cols);
  
    for (int32 r = 0; r < num_rows; r++) {
      for (int32 c = 0; c < num_cols; c++) {
        if (RandInt(0, 5) == 0)
          mat(r, c) = RandGauss();
      }
    }
    SparseMatrix<Real> smat(mat);
  
    Matrix<Real> mat2(num_rows, num_cols);
    mat2.SetRandn();
    smat.CopyToMat(&mat2);
    AssertEqual(mat, mat2);
  }
  
  template <typename Real>
  void UnitTestSparseMatrixTraceMatSmat() {
    for (int32 i = 0; i < 10; i++) {
      MatrixIndexT row = 10 + Rand() % 40;
      MatrixIndexT col = 10 + Rand() % 50;
  
      Matrix<Real> mat1(row, col);
      Matrix<Real> mat2(col, row);
      Matrix<Real> mat3(row, col);
      mat1.SetRandn();
      mat2.SetRandn();
      mat3.SetRandn();
  
      SparseMatrix<Real> smat1(row, col);
      SparseMatrix<Real> smat2(col, row);
      smat1.SetRandn(0.8);
      smat2.SetRandn(0.8);
  
      smat1.CopyToMat(&mat1);
      smat2.CopyToMat(&mat2);
  
      Real trace1 = TraceMatMat(mat3, mat1, kTrans);
      Real trace2 = TraceMatSmat(mat3, smat1, kTrans);
      AssertEqual(trace1, trace2, 0.00001);
  
      trace1 = TraceMatMat(mat3, mat2, kNoTrans);
      trace2 = TraceMatSmat(mat3, smat2, kNoTrans);
      AssertEqual(trace1, trace2, 0.00001);
    }
  }
  
  template <typename Real>
  void UnitTestMatrixAddMatSmat() {
  
    for (int32 t = 0; t < 4; t++) {
      MatrixIndexT m = RandInt(10, 20), n = RandInt(10, 20), o = RandInt(10, 20);
      MatrixTransposeType Btrans = (RandInt(0, 1) == 0 ? kTrans : kNoTrans);
  
      // we are effectively comparing trace(A B C) computed as
      // trace((A B) C) vs. trace ((C A) B)
  
      BaseFloat alpha = 0.333, beta = 1.764;
  
  
      Matrix<Real> A(m, n);
      A.SetRandn();
      SparseMatrix<Real> B(Btrans  == kNoTrans ? n : o,
                           Btrans == kNoTrans ? o : n);
      B.SetRandn(0.5);
  
      Matrix<Real> AB(m, o);
      AB.SetRandn();  // this random extra part is used in testing the 'beta'.
  
      Matrix<Real> C(o, m);
      C.SetRandn();
  
      Matrix<Real> CA(o, n);
      CA.AddMatMat(1.0, C, kNoTrans, A, kNoTrans, 0.0);
      Real trace_abc_alpha = TraceMatSmat(CA, B, Btrans);
  
      Real trace_abc_beta = TraceMatMat(AB, C, kNoTrans);
  
      AB.AddMatSmat(alpha, A, B, Btrans, beta);
  
      // next line is in case I made certain mistakes like setting matrix to
      // random.
      KALDI_ASSERT(trace_abc_alpha != 0.0 && trace_abc_beta != 0.0);
      Real result1 = TraceMatMat(AB, C, kNoTrans),
          result2 = alpha * trace_abc_alpha  + beta * trace_abc_beta;
      AssertEqual(result1, result2, 0.01);
    }
  }
  
  
  template <typename Real>
  void UnitTestMatrixAddSmatMat() {
  
    for (int32 t = 0; t < 4; t++) {
      MatrixIndexT m = RandInt(10, 20), n = RandInt(10, 20), o = RandInt(10, 20);
      MatrixTransposeType Btrans = (RandInt(0, 1) == 0 ? kTrans : kNoTrans);
  
      // we are effectively comparing trace(A B C) computed as
      // trace((A B) C) vs. trace ((C A) B)
  
      BaseFloat alpha = 0.333, beta = 1.764;
  
      Matrix<Real> A(m, n);
      A.SetRandn();
      SparseMatrix<Real> B(Btrans  == kNoTrans ? n : o,
                           Btrans == kNoTrans ? o : n);
      B.SetRandn(0.5);
  
      Matrix<Real> C(o, m);
      C.SetRandn();
  
      Matrix<Real> BC(n, m);
      BC.SetRandn();  // this random extra part is used in testing the 'beta'.
  
      Matrix<Real> CA(o, n);
      CA.AddMatMat(1.0, C, kNoTrans, A, kNoTrans, 0.0);
      Real trace_abc_alpha = TraceMatSmat(CA, B, Btrans);
  
      Real trace_abc_beta = TraceMatMat(A, BC, kNoTrans);
  
      BC.AddSmatMat(alpha, B, Btrans, C, beta);
  
      // next line is in case I made certain mistakes like setting matrix to
      // random.
      KALDI_ASSERT(trace_abc_alpha != 0.0 && trace_abc_beta != 0.0);
      Real result1 = TraceMatMat(A, BC, kNoTrans),
          result2 = alpha * trace_abc_alpha  + beta * trace_abc_beta;
      AssertEqual(result1, result2, 0.01);
    }
  }
  
  
  template <typename Real>
  void SparseMatrixUnitTest() {
    // SparseVector
    UnitTestSparseVectorSum<Real>();
    UnitTestSparseVectorAddToVec<Real>();
    UnitTestSparseVectorMax<Real>();
    UnitTestSparseVectorVecSvec<Real>();
  
    // SparseMatrix
    UnitTestSparseMatrixSum<Real>();
    UnitTestSparseMatrixFrobeniusNorm<Real>();
    UnitTestSparseMatrixAddToMat<Real>();
    UnitTestSparseMatrixTraceMatSmat<Real>();
    for (int32 i = 0; i < 30; i++)
      UnitTestSparseMatrixConstructor<Real>();
  
  
    // Matrix functions involving sparse matrices.
    UnitTestMatrixAddMatSmat<Real>();
    UnitTestMatrixAddSmatMat<Real>();
  }
  
  }  // namespace kaldi
  
  int main() {
    kaldi::SetVerboseLevel(5);
    kaldi::SparseMatrixUnitTest<float>();
    kaldi::SparseMatrixUnitTest<double>();
    KALDI_LOG << "Tests succeeded.";
    return 0;
  }