cu-compressed-matrix-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-compressed-matrix-test.cc
2 
3 // Copyright 2018 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include <iostream>
22 #include <vector>
23 #include <cstdlib>
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
28 
29 using namespace kaldi;
30 
31 
32 namespace kaldi {
33 
35  int32 num_rows = RandInt(80, 100),
36  num_cols = RandInt(80, 100);
37  CuMatrix<BaseFloat> M(num_rows, num_cols);
38  M.SetRandn();
39 
40  CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
41 
43 
44  // this just stores (M(i, j) > 0 ? 1 : 0).
45  cm->CopyFromMat(M);
46  cm->CopyToMat(&M2);
47 
48  M.Heaviside(M);
49 
50  AssertEqual(M, M2);
51  delete cm;
52 }
53 
55  int32 num_rows = RandInt(80, 100),
56  num_cols = RandInt(80, 100);
57  CuMatrix<BaseFloat> M(num_rows, num_cols);
58  M.SetRandUniform();
59 
60  BaseFloat range = 0.5 * RandInt(1, 5);
61  M.Scale(range);
62 
63  CuCompressedMatrixType t = (RandInt(0, 1) == 0 ?
66 
67  // since the input is in the correct range, truncating or not should make no
68  // difference.
69  bool truncate = (RandInt(0, 1) == 0);
70 
71  BaseFloat extra_error = 0.0;
72  if (truncate && (RandInt(0, 1) == 0)) {
73  // this tests that with truncate == true, adding a small offset, which would
74  // take us outside the representable range, will not add too much extra
75  // error. (with truncate == false this would not be true because we wouldn't
76  // round to the edges of the range, it would wrap around).
77  extra_error = -0.01 * (RandInt(0, 1) == 0 ? 1.0 : -1.0);
78  M.Add(extra_error);
79  }
80 
81  CuCompressedMatrixBase *cm = NewCuCompressedMatrix(t, range, truncate);
82 
83  CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
84 
85  cm->CopyFromMat(M);
86  cm->CopyToMat(&M2);
87 
88 
89  M2.AddMat(-1.0, M);
90 
91  BaseFloat diff_max = M2.Max(),
92  diff_min = M2.Min();
93 
94  BaseFloat
95  headroom = 1.1,
96  max_expected_error = fabs(extra_error) + headroom * 0.5 *
97  range / (t == kCompressedMatrixUint8 ? 255 : 65535);
98 
99  KALDI_ASSERT(diff_max < max_expected_error &&
100  diff_min > -1.0 * max_expected_error);
101 
102  delete cm;
103 }
104 
105 // this is like CuCompressedMatrixTestNonnegative but
106 // with signed integers, and input in the range [-range, +range].
108  int32 num_rows = RandInt(80, 100),
109  num_cols = RandInt(80, 100);
110  CuMatrix<BaseFloat> M(num_rows, num_cols);
111  M.SetRandUniform();
112  M.Scale(2.0);
113  M.Add(-1.0);
114 
115  BaseFloat range = 0.5 * RandInt(1, 5);
116  M.Scale(range);
117 
118  CuCompressedMatrixType t = (RandInt(0, 1) == 0 ?
121 
122  // since the input is in the correct range, truncating or not should make no
123  // difference.
124  bool truncate = (RandInt(0, 1) == 0);
125 
126  BaseFloat extra_error = 0.0;
127  if (truncate && (RandInt(0, 1) == 0)) {
128  // this tests that with truncate == true, adding a small offset, which would
129  // take us outside the representable range, will not add too much extra
130  // error. (with truncate == false this would not be true because we wouldn't
131  // round to the edges of the range, it would wrap around).
132  extra_error = -0.01 * (RandInt(0, 1) == 0 ? 1.0 : -1.0);
133  M.Add(extra_error);
134  }
135 
136  CuCompressedMatrixBase *cm = NewCuCompressedMatrix(t, range, truncate);
137 
138  CuMatrix<BaseFloat> M2(num_rows, num_cols, kUndefined);
139 
140  cm->CopyFromMat(M);
141  cm->CopyToMat(&M2);
142 
143 
144  M2.AddMat(-1.0, M);
145 
146  BaseFloat diff_max = M2.Max(),
147  diff_min = M2.Min();
148 
149  BaseFloat
150  headroom = 1.1,
151  max_expected_error = fabs(extra_error) + headroom * 0.5 *
152  range / (t == kCompressedMatrixInt8 ? 127 : 32767);
153 
154  KALDI_ASSERT(diff_max < max_expected_error &&
155  diff_min > -1.0 * max_expected_error);
156 
157  delete cm;
158 }
159 
160 
161 
162 } // namespace kaldi
163 
164 
165 int main() {
166  SetVerboseLevel(1);
167  // we don't run this test if CUDA is not compiled in, since
168  // you can't instantiate class CuCompressedMatrix in that case.
169 #if HAVE_CUDA == 1
170  CuDevice::Instantiate().SelectGpuId("yes");
171  for (int32 i = 1; i < 10; i++) {
175  }
176 
177 #endif
178  return 0;
179 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
void CuCompressedMatrixTestSign()
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:954
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
void Min(const CuMatrixBase< Real > &A)
Do, elementwise, *this = min(*this, A).
Definition: cu-matrix.cc:740
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 Scale(Real value)
Definition: cu-matrix.cc:644
void Max(const CuMatrixBase< Real > &A)
Do, elementwise, *this = max(*this, A).
Definition: cu-matrix.cc:715
void Add(Real value)
Definition: cu-matrix.cc:582
virtual void CopyFromMat(const CuMatrixBase< BaseFloat > &mat)=0
Sets *this to an appropriately compressed copy of &#39;mat&#39;, which includes resizing *this.
void Heaviside(const CuMatrixBase< Real > &src)
Set each element to the Heaviside function of the corresponding element of "src", which we define as ...
Definition: cu-matrix.cc:2435
void CuCompressedMatrixTestSymmetric()
void CuCompressedMatrixTestNonnegative()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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
Class CuCompressedMatrixBase is an abstract base class that allows you to compress a matrix of type C...
CuCompressedMatrixBase * NewCuCompressedMatrix(CuCompressedMatrixType t, BaseFloat range, bool truncat)
This function allocates a new CuCompressedMatrix with type determined by t, and with the &#39;range&#39; and ...
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95
virtual void CopyToMat(CuMatrixBase< BaseFloat > *mat) const =0
Copies the contents of *this to &#39;mat&#39;, which should be correctly sized beforehand.