cu-vector.h
Go to the documentation of this file.
1 // cudamatrix/cu-vector.h
2 
3 // Copyright 2009-2012 Karel Vesely
4 // Johns Hopkins University (author: Daniel Povey)
5 // Lucas Ondel
6 // 2013 Xiaohui Zhang
7 // 2015 Guoguo Chen
8 // 2017 Daniel Galvez
9 // 2019 Yiwen Shao
10 
11 // See ../../COPYING for clarification regarding multiple authors
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
21 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
22 // MERCHANTABLITY OR NON-INFRINGEMENT.
23 // See the Apache 2 License for the specific language governing permissions and
24 // limitations under the License.
25 
26 
27 
28 #ifndef KALDI_CUDAMATRIX_CU_VECTOR_H_
29 #define KALDI_CUDAMATRIX_CU_VECTOR_H_
30 
31 #include "matrix/kaldi-vector.h"
32 #include "cudamatrix/cu-common.h"
33 #include "cudamatrix/cu-value.h"
34 #include "cudamatrix/cu-math.h"
35 
36 namespace kaldi {
37 
38 template<typename Real> class CuMatrixBase;
39 
40 template<typename Real>
41 Real VecVec(const CuVectorBase<Real> &v1, const CuVectorBase<Real> &v2);
42 
43 template<typename Real, typename OtherReal>
44 Real VecVec(const CuVectorBase<Real> &v1, const CuVectorBase<OtherReal> &v2);
45 
49 template<typename Real>
50 class CuVectorBase {
51  public:
52  friend class CuVectorBase<float>;
53  friend class CuVectorBase<double>;
54  friend class CuMatrixBase<Real>;
55  friend class MatrixBase<Real>;
56  friend class CuPackedMatrix<Real>;
57  friend class CuSpMatrix<Real>;
58  friend class CuTpMatrix<Real>;
59 
60  template <typename OtherReal>
61  friend OtherReal VecVec(const CuVectorBase<OtherReal> &v1,
62  const CuVectorBase<OtherReal> &v2);
63  friend void cu::Splice<Real>(const CuMatrixBase<Real> &src,
64  const CuArray<int32> &frame_offsets,
65  CuMatrixBase<Real> *tgt);
66  friend class CuRand<Real>;
67 
69  MatrixIndexT Dim() const { return dim_; }
70 
72  inline Real* Data() { return data_; }
74  inline const Real* Data() const { return data_; }
75 
79  void CopyFromVec(const CuVectorBase<Real> &src);
80 
81  template<typename OtherReal>
82  void CopyFromVec(const CuVectorBase<OtherReal> &M);
83 
84  template<typename OtherReal>
85  void CopyFromVec(const VectorBase<OtherReal> &src);
86 
87  template<typename OtherReal>
88  void CopyToVec(VectorBase<OtherReal> *dst) const;
89 
90  void CopyRowsFromMat(const CuMatrixBase<Real> &M);
91 
92  void CopyRowsFromMat(const MatrixBase<Real> &M);
93 
95  void SetZero();
96  void Set(Real value);
97  void Add(Real value);
98  void Scale(Real value);
99 
100  void AddVec(Real alpha, const CuVectorBase<Real> &vec, Real beta = 1.0);
101 
102  template<typename OtherReal>
103  void AddVec(Real alpha, const CuVectorBase<OtherReal> &vec, Real beta = 1.0);
104 
106  void AddRowSumMat(Real alpha, const CuMatrixBase<Real> &mat, Real beta = 1.0);
108  void AddColSumMat(Real alpha, const CuMatrixBase<Real> &mat, Real beta = 1.0);
109 
112  void AddTpVec(const Real alpha, const CuTpMatrix<Real>&M,
113  const MatrixTransposeType trans, const CuVectorBase<Real> &v,
114  const Real beta); // **beta previously defaulted to 0.0**
115 
117  void MulTp(const CuTpMatrix<Real> &M, const MatrixTransposeType trans);
118 
119  bool ApproxEqual(const CuVectorBase<Real> &other, float tol = 0.01) const;
120 
121  void InvertElements();
122 
123 
131  void CopyElements(const CuMatrixBase<Real> &mat,
132  const MatrixTransposeType trans,
133  const CuArrayBase<int32> &elements);
134 
135  void Floor(const CuVectorBase<Real> &src, Real floor_val, MatrixIndexT *floored_count = NULL);
136  void Ceiling(const CuVectorBase<Real> &src, Real ceiling_val, MatrixIndexT *ceiled_count = NULL);
137  void Pow(const CuVectorBase<Real> &src, Real power);
138 
139  inline void ApplyFloor(Real floor_val, MatrixIndexT *floored_count = NULL) {
140  this -> Floor(*this, floor_val, floored_count);
141  };
142 
143  inline void ApplyCeiling(Real ceiling_val, MatrixIndexT *ceiled_count = NULL) {
144  this -> Ceiling(*this, ceiling_val, ceiled_count);
145  };
146 
147  inline void ApplyPow(Real power) {
148  this -> Pow(*this, power);
149  };
150 
151  void ApplySoftMax();
152  void ApplyLogSoftMax();
153  void ApplyExp();
154  void ApplyLog();
155  Real Sum() const;
156 
157  void SetRandn();
158  void SetRandUniform();
159 
161  return CuSubVector<Real>(*this, o, l);
162  }
163 
165  const MatrixIndexT l) const {
166  return CuSubVector<Real>(*this, o, l);
167  }
168 
169  void CopyColFromMat(const CuMatrixBase<Real> &mat, MatrixIndexT col);
170 
171  template<typename OtherReal>
173 
174  void AddMatVec(const Real alpha, const CuMatrixBase<Real> &M,
175  MatrixTransposeType trans, const CuVectorBase<Real> &v,
176  const Real beta);
177  void AddVecVec(Real alpha, const CuVectorBase<Real> &v,
178  const CuVectorBase<Real> &r, Real beta);
179 
180  void AddSpVec(const Real alpha, const CuSpMatrix<Real> &S,
181  const CuVectorBase<Real> &v, const Real beta);
182 
186  void AddDiagMat2(Real alpha, const CuMatrixBase<Real> &M,
187  MatrixTransposeType trans, Real beta);
188 
192  void AddDiagMatMat(Real alpha, const CuMatrixBase<Real> &M, MatrixTransposeType transM,
193  const CuMatrixBase<Real> &N, MatrixTransposeType transN,
194  Real beta = 1.0);
195 
197  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
198  static_cast<UnsignedMatrixIndexT>(dim_));
199  return CuValue<Real>(data_ + i);
200  }
201 
202  Real Norm(Real p); // Only works for p = 1 and p = 2.
203 
204  inline Real operator() (MatrixIndexT i) const {
205  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
206  static_cast<UnsignedMatrixIndexT>(dim_));
207  return CuValue<Real>(data_ + i); // will be casted to Real.
208  }
209 
212 
214  void CopyDiagFromMat(const CuMatrix<Real> &M);
215 
217  Real Max() const;
218 
220  Real Min() const;
221 
222  // Set each element to y = (x == orig ? changed : x).
223  void ReplaceValue(Real orig, Real changed);
224 
225  // Multiplies (*this) by v elementwise: (*this)[i] *= v
226  void MulElements(const CuVectorBase<Real> &v);
227 
228  // Divides (*this) by v elementwise: (*this)[i] /= v
229  void DivElements(const CuVectorBase<Real> &v);
230 
231  // The following two functions should only be called if we did not compile
232  // with CUDA or could not get a CUDA card; in that case the contents are
233  // interpreted the same as a regular vector.
234  // Do not use the following functions unless you know what you are doing!
235  inline const VectorBase<Real> &Vec() const {
236  return *(reinterpret_cast<const VectorBase<Real>* >(this));
237  }
238  inline VectorBase<Real> &Vec() {
239  return *(reinterpret_cast<VectorBase<Real>* >(this));
240  }
241 
242  protected:
243 
246  CuVectorBase<Real>(): data_(NULL), dim_(0) { }
247 
248  Real *data_;
251 
252  private:
254 };
255 
256 template<typename Real>
257 class CuVector: public CuVectorBase<Real> {
258  friend class CuVectorBase<float>;
259  friend class CuVectorBase<double>;
260  friend class CuMatrixBase<Real>;
261  friend class CuPackedMatrix<Real>;
262  friend class CuSpMatrix<Real>;
263  friend class CuTpMatrix<Real>;
264 
265  public:
266  CuVector() { }
267  CuVector(MatrixIndexT dim, MatrixResizeType t = kSetZero) { Resize(dim, t); }
268 
269  CuVector(const CuVectorBase<Real> &v);
270 
271  CuVector(const VectorBase<Real> &v);
272  explicit CuVector(const CuVector<Real> &v) : CuVectorBase<Real>() {
273  Resize(v.Dim(), kUndefined);
274  this->CopyFromVec(v);
275  }
276 
277  template<typename OtherReal>
278  explicit CuVector(const CuVectorBase<OtherReal> &v) : CuVectorBase<Real>() {
279  Resize(v.Dim(), kUndefined);
280  this->CopyFromVec(v);
281  }
282 
283  template<typename OtherReal>
284  explicit CuVector(const VectorBase<OtherReal> &v) : CuVectorBase<Real>() {
285  Resize(v.Dim(), kUndefined);
286  this->CopyFromVec(Vector<Real>(v));
287  }
288 
290  void Resize(MatrixIndexT dim, MatrixResizeType t = kSetZero);
291 
292  ~CuVector() { Destroy(); }
293 
294  CuVector<Real> &operator = (const CuVectorBase<Real> &other) {
295  Resize(other.Dim(), kUndefined);
296  this->CopyFromVec(other);
297  return *this;
298  }
299 
300  CuVector<Real> &operator = (const CuVector<Real> &other) {
301  Resize(other.Dim(), kUndefined);
302  this->CopyFromVec(other);
303  return *this;
304  }
305  CuVector<Real> &operator = (const VectorBase<Real> &other) {
306  Resize(other.Dim());
307  this->CopyFromVec(other);
308  return *this;
309  }
310 
311  void Swap(CuVector<Real> *vec);
312  void Swap(Vector<Real> *vec);
313 
315  void Read(std::istream &is, bool binary);
316  void Write(std::ostream &is, bool binary) const;
317 
318 
319  private:
320  void Destroy();
321 };
322 
323 // We'll fill out the following class if it's needed.
324 template<typename Real>
325 class CuSubVector: public CuVectorBase<Real> {
326  public:
328  const MatrixIndexT length) : CuVectorBase<Real>() {
329  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(origin)+
330  static_cast<UnsignedMatrixIndexT>(length) <=
331  static_cast<UnsignedMatrixIndexT>(t.Dim()));
332  this->data_ = const_cast<Real*>(t.Data()+origin);
333  this->dim_ = length;
334  }
337  CuSubVector(const CuSubVector &other) : CuVectorBase<Real> () {
338  this->data_ = other.data_;
339  this->dim_ = other.dim_;
340  }
341 
342  CuSubVector(const Real* data, MatrixIndexT length) : CuVectorBase<Real> () {
343  // Yes, we're evading C's restrictions on const here, and yes, it can be used
344  // to do wrong stuff; unfortunately the workaround would be very difficult.
345  this->data_ = const_cast<Real*>(data);
346  this->dim_ = length;
347  }
348 
351  this->data_ = const_cast<Real*>(matrix.RowData(row));
352  this->dim_ = matrix.NumCols();
353  }
354 
355 
356 };
357 
359 template<typename Real>
360 std::ostream &operator << (std::ostream &out, const CuVectorBase<Real> &vec);
361 
362 
363 template<typename Real>
365  const CuVectorBase<Real> &b, Real tol = 0.01) {
366  return a.ApproxEqual(b, tol);
367 }
368 
369 template<typename Real>
370 inline void AssertEqual(const CuVectorBase<Real> &a,
371  const CuVectorBase<Real> &b, Real tol = 0.01) {
372  KALDI_ASSERT(a.ApproxEqual(b, tol));
373 }
374 
375 template<typename Real>
376 template<typename OtherReal>
378  v.CopyToVec(&this);
379 }
380 
381 template<typename Real>
382 template<typename OtherReal>
384  cu.CopyToVec(this);
385 }
386 
387 // declare template specializations.
388 template <>
389 template <>
391 
392 template<>
393 template <>
395 
396 template<typename Real>
397 template<typename OtherReal>
399  Init(cu.Dim());
400  cu.CopyToVec(this);
401 }
402 
404 template<typename Real>
405 Real VecMatVec(const CuVectorBase<Real> &v1, const CuMatrixBase<Real> &M,
406  const CuVectorBase<Real> &v2);
407 
408 
409 } // namespace
410 
411 #endif
void MulElements(const CuVectorBase< Real > &v)
Definition: cu-vector.cc:838
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
friend OtherReal VecVec(const CuVectorBase< OtherReal > &v1, const CuVectorBase< OtherReal > &v2)
This class provides a way for switching between double and float types.
Definition: matrix-common.h:84
VectorBase< Real > & Vec()
Definition: cu-vector.h:238
void SetZero()
Math operations.
Definition: cu-vector.cc:1098
void CopyDiagFromPacked(const CuPackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
Definition: cu-vector.cc:1177
MatrixResizeType
Definition: matrix-common.h:37
void MulTp(const CuTpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular marix: *this <– *this *M.
Definition: cu-vector.cc:727
const CuSubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l) const
Definition: cu-vector.h:164
void ApplyCeiling(Real ceiling_val, MatrixIndexT *ceiled_count=NULL)
Definition: cu-vector.h:143
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
void CopyColFromMat(const CuMatrixBase< Real > &mat, MatrixIndexT col)
Definition: cu-vector.cc:103
void Add(Real value)
Definition: cu-vector.cc:1157
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:248
Real Sum() const
Definition: cu-vector.cc:297
KALDI_DISALLOW_COPY_AND_ASSIGN(CuVectorBase)
void Set(Real value)
Definition: cu-vector.cc:1135
void CopyDiagFromMat(const CuMatrix< Real > &M)
Extracts the diagonal of a matrix.
Definition: cu-vector.cc:1198
void AddDiagMat2(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, Real beta)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
Definition: cu-vector.cc:595
CuValue< Real > operator()(MatrixIndexT i)
Definition: cu-vector.h:196
Real VecMatVec(const VectorBase< Real > &v1, const MatrixBase< Real > &M, const VectorBase< Real > &v2)
Returns .
CuVector(const VectorBase< OtherReal > &v)
Definition: cu-vector.h:284
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 ApplyFloor(Real floor_val, MatrixIndexT *floored_count=NULL)
Definition: cu-vector.h:139
The following class is used to simulate non-const references to Real, e.g.
Definition: cu-value.h:34
void ReplaceValue(Real orig, Real changed)
Definition: cu-vector.cc:820
void AddDiagMatMat(Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const CuMatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoT...
Definition: cu-vector.cc:611
void CopyElements(const CuMatrixBase< Real > &mat, const MatrixTransposeType trans, const CuArrayBase< int32 > &elements)
Copies selected elements from &#39;mat&#39; to *this.
Definition: cu-vector.cc:1340
const Real * Data() const
Returns a pointer to the start of the vector&#39;s data (const).
Definition: cu-vector.h:74
void Pow(const CuVectorBase< Real > &src, Real power)
Definition: cu-vector.cc:419
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void Ceiling(const CuVectorBase< Real > &src, Real ceiling_val, MatrixIndexT *ceiled_count=NULL)
Definition: cu-vector.cc:385
void AddMatVec(const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
Definition: cu-vector.cc:506
int32 MatrixIndexT
Definition: matrix-common.h:98
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void AddColSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the columns of the matrix, add to vector.
Definition: cu-vector.cc:1298
void DivElements(const CuVectorBase< Real > &v)
Definition: cu-vector.cc:857
CuSubVector(const CuVectorBase< Real > &t, const MatrixIndexT origin, const MatrixIndexT length)
Definition: cu-vector.h:327
void AddSpVec(const Real alpha, const CuSpMatrix< Real > &S, const CuVectorBase< Real > &v, const Real beta)
Definition: cu-vector.cc:535
void ApplyPow(Real power)
Definition: cu-vector.h:147
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
CuSubVector(const CuMatrixBase< Real > &matrix, MatrixIndexT row)
This operation does not preserve const-ness, so be careful.
Definition: cu-vector.h:350
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
Definition: cu-vector.cc:782
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
Definition: cu-vector.cc:1237
CuSubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Definition: cu-vector.h:160
Vector()
Constructor that takes no arguments. Initializes to empty.
Definition: kaldi-vector.h:409
Matrix for CUDA computing.
Definition: matrix-common.h:69
CuVector(const CuVectorBase< OtherReal > &v)
Definition: cu-vector.h:278
MatrixIndexT NumCols() const
Definition: cu-matrix.h:216
CuSubVector(const CuSubVector &other)
Copy constructor this constructor needed for Range() to work in base class.
Definition: cu-vector.h:337
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250
A class representing a vector.
Definition: kaldi-vector.h:406
void Floor(const CuVectorBase< Real > &src, Real floor_val, MatrixIndexT *floored_count=NULL)
Definition: cu-vector.cc:353
const VectorBase< Real > & Vec() const
Definition: cu-vector.h:235
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Real Norm(Real p)
Definition: cu-vector.cc:193
Matrix for CUDA computing.
Definition: matrix-common.h:75
void AddVecVec(Real alpha, const CuVectorBase< Real > &v, const CuVectorBase< Real > &r, Real beta)
Definition: cu-vector.cc:560
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
Real * Data()
Returns a pointer to the start of the vector&#39;s data.
Definition: cu-vector.h:72
bool ApproxEqual(const CuVectorBase< Real > &other, float tol=0.01) const
Definition: cu-vector.cc:583
void Scale(Real value)
Definition: cu-vector.cc:1216
CuVector(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Definition: cu-vector.h:267
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
void AddTpVec(const Real alpha, const CuTpMatrix< Real > &M, const MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
Add triangular matrix times vector: this <– beta*this + alpha*M*v.
Definition: cu-vector.cc:698
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:938
CuSubVector(const Real *data, MatrixIndexT length)
Definition: cu-vector.h:342
void CopyRowsFromMat(const CuMatrixBase< Real > &M)
Definition: cu-vector.cc:164
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
Definition: cu-vector.cc:744
void AddRowSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the rows of the matrix, add to vector.
Definition: cu-vector.cc:1277
CuVector(const CuVector< Real > &v)
Definition: cu-vector.h:272
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:69
Vector for CUDA computing.
Definition: matrix-common.h:72
const Real * RowData(MatrixIndexT r) const
Get raw row pointer (const).
Definition: cu-matrix.h:740