cu-tp-matrix-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-tp-matrix-test.cc
2 //
3 // Copyright 2013 Ehsan Variani
4 // Lucas Ondel
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 
12 // http://www.apache.org/licenses/LICENSE-2.0
13 
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 // UnitTests for testing cu-sp-matrix.h methods.
22 //
23 
24 #include <iostream>
25 #include <vector>
26 #include <cstdlib>
27 
28 #include "base/kaldi-common.h"
29 #include "cudamatrix/cu-device.h"
31 #include "cudamatrix/cu-vector.h"
32 #include "cudamatrix/cu-math.h"
34 
35 using namespace kaldi;
36 
37 namespace kaldi {
38 
39 
40 template<typename Real>
41 static void AssertEqual(const CuPackedMatrix<Real> &A,
42  const CuPackedMatrix<Real> &B,
43  float tol = 0.001) {
44  KALDI_ASSERT(A.NumRows() == B.NumRows());
45  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
46  for (MatrixIndexT j = 0; j <= i; j++)
47  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
48  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
49 }
50 
51 template<typename Real>
52 static void AssertEqual(const PackedMatrix<Real> &A,
53  const PackedMatrix<Real> &B,
54  float tol = 0.001) {
55  KALDI_ASSERT(A.NumRows() == B.NumRows());
56  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
57  for (MatrixIndexT j = 0; j <= i; j++)
58  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
59  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
60 }
61 
62 template<typename Real>
63 static void AssertEqual(const PackedMatrix<Real> &A,
64  const CuPackedMatrix<Real> &B,
65  float tol = 0.001) {
66  KALDI_ASSERT(A.NumRows() == B.NumRows());
67  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
68  for (MatrixIndexT j = 0; j <= i; j++)
69  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
70  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
71 }
72 
73 
74 
75 /*
76  * Unit Tests
77  */
78 template<typename Real>
79 static void UnitTestCuTpMatrixInvert() {
80  for (MatrixIndexT i = 1; i < 10; i++) {
81  MatrixIndexT dim = 5 * i + Rand() % 10;
82 
83  TpMatrix<Real> A(dim);
84  A.SetRandn();
85  CuTpMatrix<Real> B(A);
86 
87  AssertEqual<Real>(A, B, 0.005);
88  A.Invert();
89  B.Invert();
90  AssertEqual<Real>(A, B, 0.005);
91  }
92 }
93 
94 template<typename Real>
96  for (MatrixIndexT i = 1; i < 10; i++) {
97  MatrixIndexT dim = 5 * i + Rand() % 10;
98 
99  TpMatrix<Real> A(dim);
100  A.SetRandn();
101  CuTpMatrix<Real> B(dim);
102  B.CopyFromTp(A);
103  CuTpMatrix<Real> C(dim);
104  C.CopyFromTp(B);
105 
106  AssertEqual<Real>(A, B);
107  AssertEqual<Real>(B, C);
108  }
109 }
110 
111 template<typename Real>
113  for (MatrixIndexT i = 1; i < 10; i++) {
114  MatrixTransposeType trans = (i % 2 == 0 ? kNoTrans : kTrans);
115 
116  MatrixIndexT dim = 10*i + Rand() % 5;
117  CuMatrix<Real> A(dim, dim);
118  A.SetRandn();
119  Matrix<Real> A2(A);
120 
121  CuTpMatrix<Real> B(dim);
122  B.CopyFromMat(A, trans);
123  TpMatrix<Real> B2(dim);
124  B2.CopyFromMat(A2, trans);
125  TpMatrix<Real> B3(B);
126  AssertEqual(B2, B3);
127  KALDI_ASSERT(B3.Trace() != 0);
128  }
129 }
130 
131 
132 
133 template<typename Real>
135  for (MatrixIndexT i = 1; i < 10; i++) {
136  MatrixIndexT dim = 1 + Rand() % 10;
137  if (i > 4) {
138  dim += 32 * (Rand() % 5);
139  }
140 
141  Matrix<Real> M(dim, dim + 2);
142  M.SetRandn();
143  SpMatrix<Real> A(dim);
144  A.AddMat2(1.0, M, kNoTrans, 0.0); // sets A to random almost-surely +ve
145  // definite matrix.
146  CuSpMatrix<Real> B(A);
147 
148  TpMatrix<Real> C(dim);
149  C.SetRandn();
150  CuTpMatrix<Real> D(C);
151  C.Cholesky(A);
152  D.Cholesky(B);
153 
154  AssertEqual<Real>(C, D);
155  }
156 }
157 
158 template<class Real>
159 static void UnitTestCuTpMatrixIO() {
160  for (int32 i = 0; i < 3; i++) {
161  int32 dimM = Rand() % 255 + 10;
162  if (i % 5 == 0) { dimM = 0; }
163  CuTpMatrix<Real> mat(dimM);
164  mat.SetRandn();
165  std::ostringstream os;
166  bool binary = (i % 4 < 2);
167  mat.Write(os, binary);
168 
169  CuTpMatrix<Real> mat2;
170  std::istringstream is(os.str());
171  mat2.Read(is, binary);
172  AssertEqual(mat, mat2);
173  }
174 }
175 
176 template<typename Real> void CudaTpMatrixUnitTest() {
177  UnitTestCuTpMatrixIO<Real>();
178  UnitTestCuTpMatrixInvert<Real>();
179  UnitTestCuTpMatrixCopyFromTp<Real>();
180  UnitTestCuTpMatrixCholesky<Real>();
181  UnitTestCuTpMatrixCopyFromMat<Real>();
182 }
183 
184 } // namespace kaldi
185 
186 
187 int main() {
188  using namespace kaldi;
189  SetVerboseLevel(1);
190 
191  int32 loop = 0;
192 #if HAVE_CUDA == 1
193  for (; loop < 2; loop++) {
194  CuDevice::Instantiate().SetDebugStrideMode(true);
195  if (loop == 0)
196  CuDevice::Instantiate().SelectGpuId("no"); // -1 means no GPU
197  else
198  CuDevice::Instantiate().SelectGpuId("yes"); // -2 .. automatic selection
199 #endif
200  kaldi::CudaTpMatrixUnitTest<float>();
201 #if HAVE_CUDA == 1
202  if (CuDevice::Instantiate().DoublePrecisionSupported()) {
203  kaldi::CudaTpMatrixUnitTest<double>();
204  } else {
205  KALDI_WARN << "Double precision not supported";
206  }
207 #else
208  kaldi::CudaTpMatrixUnitTest<double>();
209 #endif
210 
211  if (loop == 0)
212  KALDI_LOG << "Tests without GPU use succeeded.";
213  else
214  KALDI_LOG << "Tests with GPU use (if available) succeeded.";
215 #if HAVE_CUDA == 1
216  }
217  CuDevice::Instantiate().PrintProfile();
218 #endif
219  return 0;
220 }
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
Definition: sp-matrix.cc:1110
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Packed symetric matrix class.
Definition: matrix-common.h:62
Real Trace() const
< Set to random values of a normal distribution
void CopyFromTp(const CuTpMatrix< Real > &other)
Definition: cu-tp-matrix.h:68
MatrixIndexT NumRows() const
void CudaTpMatrixUnitTest()
kaldi::int32 int32
A class for storing matrices.
Definition: kaldi-matrix.h:823
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
MatrixIndexT NumRows() const
void Cholesky(const CuSpMatrix< Real > &Orig)
Definition: cu-tp-matrix.cc:48
void SetRandn()
< Set to unit matrix.
void SetRandn()
< Set to unit matrix.
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
void Cholesky(const SpMatrix< Real > &orig)
Definition: tp-matrix.cc:88
int32 MatrixIndexT
Definition: matrix-common.h:98
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
void Read(std::istream &in, bool binary)
void SetRandn()
Sets to random values of a normal distribution.
static void UnitTestCuTpMatrixIO()
static void UnitTestCuTpMatrixCopyFromTp()
Packed symetric matrix class.
Definition: matrix-common.h:63
#define KALDI_WARN
Definition: kaldi-error.h:150
int main()
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
static void UnitTestCuTpMatrixInvert()
static void UnitTestCuTpMatrixCopyFromMat()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Matrix for CUDA computing.
Definition: matrix-common.h:75
MatrixTransposeType
Definition: matrix-common.h:32
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
void CopyFromMat(const MatrixBase< Real > &M, MatrixTransposeType Trans=kNoTrans)
CopyFromMat copies the lower triangle of M into *this (or the upper triangle, if Trans == kTrans)...
Definition: tp-matrix.cc:117
void Write(std::ostream &out, bool binary) const
void CopyFromMat(const CuMatrixBase< Real > &M, MatrixTransposeType Trans=kNoTrans)
Definition: cu-tp-matrix.cc:89
#define KALDI_LOG
Definition: kaldi-error.h:153
static void UnitTestCuTpMatrixCholesky()