cu-compressed-matrix.h
Go to the documentation of this file.
1 // cudamatrix/cu-compressed-matrix.h
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 
22 #ifndef KALDI_CUDAMATRIX_CU_COMPRESSED_MATRIX_H_
23 #define KALDI_CUDAMATRIX_CU_COMPRESSED_MATRIX_H_
24 
25 #include "cudamatrix/cu-matrix.h"
26 
27 namespace kaldi {
28 
36  public:
37 
41  virtual void CopyFromMat(const CuMatrixBase<BaseFloat> &mat) = 0;
42 
45  virtual void CopyToMat(CuMatrixBase<BaseFloat> *mat) const = 0;
46 
47 
48  // The number of rows in *this.
49  virtual int32 NumRows() const = 0;
50 
51  // The number of columns in *this.
52  virtual int32 NumCols() const = 0;
53 
55 };
56 
57 
58 
71 template <typename I>
73  public:
74 
88  CuCompressedMatrix(BaseFloat range, bool truncate = true);
89 
90  virtual void CopyFromMat(const CuMatrixBase<BaseFloat> &mat);
91 
92  virtual void CopyToMat(CuMatrixBase<BaseFloat> *mat) const;
93 
94  virtual MatrixIndexT NumRows() const { return num_rows_; }
95 
96  virtual MatrixIndexT NumCols() const { return num_cols_; }
97 
98 
99  virtual ~CuCompressedMatrix() { Destroy(); }
100 
101  private:
102  // If there was data in 'data_', frees it, and sets it to NULL.
103  void Destroy();
104 
105  // The raw data.
106  I *data_;
107 
108  // scale_ affects how the raw data is interpreted as a floating point value.
109  // When uncompressing to a CuMatrix, we'll do:
110  // f = scale_ * i
111  // where f is the floating point value we're writing to, and i is the integer
112  // value.
113  //
114  // scale_ = 0 is treated specially; in this case we just take notice of the
115  // sign of the input, and when uncompressing we do it with a scale such
116  // that the output becomes -1, 0 and 1.
118 
119  // 'truncate_' affects the code that compresses data to integer values.
120  // If the data we're compressing might possibly be outside of the representable
121  // range, then you should set truncate to true (this is the default in the
122  // constructor). This way, values larger than the minimum or maximum will
123  // be set to the minimum or maximum value. If truncate_ is false, it will
124  // just wrap around, but the compression code will be slightly faster as
125  // it doesn't need to check.
126  bool truncate_;
127 
130  // stride_ is currently always equal to num_cols_; it was added mainly to
131  // point the way to possible future extension.
133 };
134 
135 
136 
137 // This enum value is used to encode the type you want to instantiate
138 // a CuCompressedMatrix with. It's used in class NnetComputation
139 // (cast to int32) as one of the arguments of kCompressMatrix.
145 };
146 
156  BaseFloat range,
157  bool truncate = true);
158 
159 
160 } // namespace kaldi
161 
162 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
virtual MatrixIndexT NumRows() const
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:98
Class CuCompressedMatrix, templated on an integer type (expected to be one of: int8, uint8, int16, uint16), this provides a way to approximate a CuMatrix in a more memory-efficient format.
virtual MatrixIndexT NumCols() const
virtual void CopyFromMat(const CuMatrixBase< BaseFloat > &mat)=0
Sets *this to an appropriately compressed copy of &#39;mat&#39;, which includes resizing *this.
Matrix for CUDA computing.
Definition: matrix-common.h:69
Class CuCompressedMatrixBase is an abstract base class that allows you to compress a matrix of type C...
virtual int32 NumRows() const =0
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 ...
virtual int32 NumCols() const =0
virtual void CopyToMat(CuMatrixBase< BaseFloat > *mat) const =0
Copies the contents of *this to &#39;mat&#39;, which should be correctly sized beforehand.