cu-packed-matrix-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-packed-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"
33 
34 using namespace kaldi;
35 
36 namespace kaldi {
37 
38 /*
39  * INITIALIZERS
40  */
41 
42 /*
43  * ASSERTS
44  */
45 template<typename Real>
46 static void AssertEqual(const CuPackedMatrix<Real> &A,
47  const CuPackedMatrix<Real> &B,
48  float tol = 0.001) {
49  KALDI_ASSERT(A.NumRows() == B.NumRows());
50  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
51  for (MatrixIndexT j = 0; j <= i; j++)
52  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
53  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
54 }
55 
56 template<typename Real>
57 static void AssertEqual(const PackedMatrix<Real> &A,
58  const PackedMatrix<Real> &B,
59  float tol = 0.001) {
60  KALDI_ASSERT(A.NumRows() == B.NumRows());
61  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
62  for (MatrixIndexT j = 0; j <= i; j++)
63  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
64  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
65 }
66 
67 template<typename Real>
68 static void AssertDiagEqual(const PackedMatrix<Real> &A,
69  const CuPackedMatrix<Real> &B,
70  float value,
71  float tol = 0.001) {
72  for (MatrixIndexT i = 0; i < A.NumRows(); i++) {
73  KALDI_ASSERT(std::abs((A(i, i)+value) - B(i, i))
74  < tol * std::max(1.0, (double) (std::abs(A(i, i)) + std::abs(B(i, i) + value))));
75  }
76 }
77 template<typename Real>
78 static void AssertDiagEqual(const PackedMatrix<Real> &A,
79  const PackedMatrix<Real> &B,
80  float value,
81  float tol = 0.001) {
82  for (MatrixIndexT i = 0; i < A.NumRows(); i++) {
83  KALDI_ASSERT(std::abs((A(i, i)+value) - B(i, i))
84  < tol * std::max(1.0, (double) (std::abs(A(i, i)) + std::abs(B(i, i) + value))));
85  }
86 }
87 
88 template<typename Real>
89 static void AssertEqual(const PackedMatrix<Real> &A,
90  const CuPackedMatrix<Real> &B,
91  float tol = 0.001) {
92  KALDI_ASSERT(A.NumRows() == B.NumRows());
93  for (MatrixIndexT i = 0; i < A.NumRows(); i++)
94  for (MatrixIndexT j = 0; j <= i; j++)
95  KALDI_ASSERT(std::abs(A(i, j) - B(i, j))
96  < tol * std::max(1.0, (double) (std::abs(A(i, j)) + std::abs(B(i, j)))));
97 }
98 
99 template<typename Real>
100 static bool ApproxEqual(const PackedMatrix<Real> &A,
101  const PackedMatrix<Real> &B, Real tol = 0.001) {
102  KALDI_ASSERT(A.NumRows() == B.NumRows());
103  PackedMatrix<Real> diff(A);
104  diff.AddPacked(1.0, B);
105  Real a = std::max(A.Max(), -A.Min()), b = std::max(B.Max(), -B.Min()),
106  d = std::max(diff.Max(), -diff.Min());
107  return (d <= tol * std::max(a, b));
108 }
109 
110 /*
111  * Unit Tests
112  */
113 template<typename Real>
115  for (MatrixIndexT i = 1; i < 10; i++) {
116  MatrixIndexT dim = 10 * i;
117 
118  PackedMatrix<Real> A(dim);
119  A.SetRandn();
122  AssertEqual(B, C);
123  }
124 }
125 
126 template<typename Real>
128  for (MatrixIndexT i = 1; i < 10; i++) {
129  MatrixIndexT dim = 10 * i;
130 
131  PackedMatrix<Real> A(dim);
132  A.SetRandn();
134 
135  CuPackedMatrix<Real> C(dim);
136  C.CopyFromPacked(A);
137  CuPackedMatrix<Real> D(dim);
138  D.CopyFromPacked(B);
139  AssertEqual(C, D);
140 
141  PackedMatrix<Real> E(dim);
142  D.CopyToPacked(&E);
143  AssertEqual(A, E);
144  }
145 }
146 
147 template<typename Real>
149  for (MatrixIndexT i = 1; i < 10; i++) {
150  MatrixIndexT dim = 5 * i + Rand() % 10;
151 
152  PackedMatrix<Real> A(dim);
153  A.SetRandn();
155 
156  AssertEqual(A.Trace(), B.Trace());
157  }
158 }
159 
160 template<typename Real>
162  for (MatrixIndexT i = 1; i < 10; i++) {
163  MatrixIndexT dim = 5 * i + Rand() % 10;
164 
165  PackedMatrix<Real> A(dim);
166  A.SetRandn();
168 
169  Real scale_factor = 23.5896223;
170  A.Scale(scale_factor);
171  B.Scale(scale_factor);
172  AssertEqual(A, B);
173  }
174 }
175 
176 template<typename Real>
178  for (MatrixIndexT i = 1; i < 10; i++) {
179  MatrixIndexT dim = 5 * i + Rand() % 10;
180 
181  PackedMatrix<Real> A(dim);
182  A.SetRandn();
184 
185  Real scale_factor = 23.5896223;
186  A.ScaleDiag(scale_factor);
187  B.ScaleDiag(scale_factor);
188  AssertEqual(A, B);
189  }
190 }
191 
192 
193 
194 template<typename Real>
196  for (MatrixIndexT i = 1; i < 10; i++) {
197  MatrixIndexT dim = 5 * i + Rand() % 10;
198 
199  PackedMatrix<Real> A(dim);
200  A.SetRandn();
202 
203  Real value = Rand() % 50;
204  B.AddToDiag(value);
205 
206  AssertDiagEqual(A, B, value);
207  }
208 }
209 
210 template<typename Real>
212  for (MatrixIndexT i = 1; i < 10; i++) {
213  MatrixIndexT dim = 5 * i + Rand() % 10;
214 
215  CuPackedMatrix<Real> A(dim);
216  A.SetUnit();
217 
218  for (MatrixIndexT i = 0; i < A.NumRows(); i++) {
219  for (MatrixIndexT j = 0; j < A.NumRows(); j++) {
220  if (i != j) {
221  KALDI_ASSERT(A(i, j) == 0);
222  } else {
223  KALDI_ASSERT(A(i, j) == 1.0);
224  }
225  }
226  }
227  }
228 }
229 
230 
231 template<typename Real> void CudaPackedMatrixUnitTest() {
232  UnitTestCuPackedMatrixConstructor<Real>();
233  //UnitTestCuPackedMatrixCopy<Real>();
234  UnitTestCuPackedMatrixTrace<Real>();
235  UnitTestCuPackedMatrixScale<Real>();
236  UnitTestCuPackedMatrixAddToDiag<Real>();
237  UnitTestCuPackedMatrixSetUnit<Real>();
238 }
239 
240 } // namespace kaldi
241 
242 
243 int main() {
244  using namespace kaldi;
245 #if HAVE_CUDA == 1
246  CuDevice::Instantiate().SetDebugStrideMode(true);
247  // Select the GPU
248  CuDevice::Instantiate().SelectGpuId("yes");
249 #endif
250  kaldi::CudaPackedMatrixUnitTest<float>();
251 #if HAVE_CUDA == 1
252  if (CuDevice::Instantiate().DoublePrecisionSupported()) {
253  kaldi::CudaPackedMatrixUnitTest<double>();
254  } else {
255  KALDI_WARN << "Double precision not supported";
256  }
257 #else
258  kaldi::CudaPackedMatrixUnitTest<double>();
259 #endif
260 
261  KALDI_LOG << "Tests succeeded";
262 #if HAVE_CUDA == 1
263  CuDevice::Instantiate().PrintProfile();
264 #endif
265  return 0;
266 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static void UnitTestCuPackedMatrixScale()
static void UnitTestCuPackedMatrixConstructor()
Real Trace() const
< Set to random values of a normal distribution
void Scale(Real c)
static void UnitTestCuPackedMatrixTrace()
MatrixIndexT NumRows() const
void CopyToPacked(PackedMatrix< Real > *dst) const
void AddToDiag(Real r)
< Set the diagonal value to alpha
void ScaleDiag(const Real alpha)
MatrixIndexT NumRows() const
int main()
static void AssertDiagEqual(const PackedMatrix< Real > &A, const CuPackedMatrix< Real > &B, float value, float tol=0.001)
void SetRandn()
< Set to unit matrix.
int32 MatrixIndexT
Definition: matrix-common.h:98
static void UnitTestCuPackedMatrixAddToDiag()
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
void Scale(Real alpha)
void SetUnit()
< Set to zero
#define KALDI_WARN
Definition: kaldi-error.h:150
static void UnitTestCuPackedMatrixCopy()
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void ScaleDiag(Real alpha)
void CudaPackedMatrixUnitTest()
static void UnitTestCuPackedMatrixSetUnit()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Matrix for CUDA computing.
Definition: matrix-common.h:75
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
static void UnitTestCuPackedMatrixScaleDiag()
#define KALDI_LOG
Definition: kaldi-error.h:153
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265
void CopyFromPacked(const CuPackedMatrix< Real > &src)