cu-math.h
Go to the documentation of this file.
1 // cudamatrix/cu-math.h
2 
3 // Copyright 2009-2012 Karel Vesely
4 // 2013 Johns Hopkins University (Author: David Snyder)
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 
22 
23 #ifndef KALDI_CUDAMATRIX_CU_MATH_H_
24 #define KALDI_CUDAMATRIX_CU_MATH_H_
25 #include "cudamatrix/cu-common.h"
26 #include "cudamatrix/cu-array.h"
27 #include "cudamatrix/cu-device.h"
28 #include "base/timer.h"
29 
30 namespace kaldi {
31 
32 namespace cu {
33 
38 template<typename Real>
39 void RegularizeL1(CuMatrixBase<Real> *weight, CuMatrixBase<Real> *gradient,
40  Real l1_penalty, Real learning_rate);
41 
46 template<typename Real>
47 void Randomize(const CuMatrixBase<Real> &src,
48  const CuArray<int32> &copy_from_idx,
49  CuMatrixBase<Real> *tgt);
50 
61 template<typename Real>
62 void Splice(const CuMatrixBase<Real> &src,
63  const CuArray<int32> &frame_offsets,
64  CuMatrixBase<Real> *tgt);
65 
71 template<typename Real>
72 void Copy(const CuMatrixBase<Real> &src,
73  const CuArray<int32> &copy_from_indices,
74  CuMatrixBase<Real> *tgt);
75 
76 
85 template <typename Real>
86 void EnsureNonzero(const CuMatrixBase<Real> &src,
87  Real epsilon,
88  CuMatrixBase<Real> *dest);
89 
91 template <typename Real>
92 void EnsureNonzero(const CuVectorBase<Real> &src,
93  Real epsilon,
94  CuVectorBase<Real> *dest);
95 
131 template<typename Real>
132 void ComputeLstmNonlinearity(const CuMatrixBase<Real> &input,
133  const CuMatrixBase<Real> &params,
134  CuMatrixBase<Real> *output);
135 // This is a version of ComputeLstmNonlinearity that only uses the CPU
136 // even if a GPU is available. It's made available for testing purposes.
137 template<typename Real>
138 void CpuComputeLstmNonlinearity(const MatrixBase<Real> &input,
139  const MatrixBase<Real> &params,
140  MatrixBase<Real> *output);
141 
142 
232 template<typename Real>
233 void BackpropLstmNonlinearity(const CuMatrixBase<Real> &input,
234  const CuMatrixBase<Real> &params,
235  const CuMatrixBase<Real> &output_deriv,
236  const CuMatrixBase<double> &deriv_sum_in,
237  const CuVectorBase<Real> &self_repair_config,
238  double count_in,
239  CuMatrixBase<Real> *input_deriv,
240  CuMatrixBase<Real> *params_deriv,
241  CuMatrixBase<double> *value_sum_out,
242  CuMatrixBase<double> *deriv_sum_out,
243  CuMatrixBase<Real> *self_repair_sum_out);
244 // This is a version of BackpropLstmNonlinearity that only uses the CPU
245 // even if a GPU is available. It's made available for testing purposes.
246 template<typename Real>
247 void CpuBackpropLstmNonlinearity(const MatrixBase<Real> &input,
248  const MatrixBase<Real> &params,
249  const MatrixBase<Real> &output_deriv,
250  const MatrixBase<double> &deriv_sum_in,
251  const VectorBase<Real> &self_repair_config,
252  double count_in,
253  MatrixBase<Real> *input_deriv,
254  MatrixBase<Real> *params_deriv,
255  MatrixBase<double> *value_sum_out,
256  MatrixBase<double> *deriv_sum_out,
257  MatrixBase<Real> *self_repair_sum_out);
258 
271 template<typename Real>
272 void NormalizePerRow(const CuMatrixBase<Real>& in, const Real target_rms,
273  const bool add_log_stddev, CuMatrixBase<Real>* out);
274 
275 // A note on the derivative of NormalizeComponent...
276 // let both row_in and row_out be vectors of dimension D.
277 // Let p = row_in^T row_in / (D * target_rms^2), and let
278 // f = 1.0 / sqrt(max(kSquaredNormFloor, p)), and we compute row_out as:
279 // row_out = f row_in.
280 // Suppose we have a quantity deriv_out which is the derivative
281 // of the objective function w.r.t. row_out. We want to compute
282 // deriv_in which is the derivative of the objective function w.r.t.
283 // row_in. Let the objective function be F. One term is obvious: we have
284 // deriv_in = f deriv_out + ....
285 // next we have to take into account the derivative that gets back-propagated
286 // through f. Obviously, dF/df = deriv_out^T row_in.
287 // And df/dp = (p <= kSquaredNormFloor ? 0.0 : -0.5 p^{-1.5}) = (f == 1.0 / sqrt(kSquaredNormFloor) ? 0.0 : -0.5 f^3),
288 // and dp/d(row_in) = 2/(D * target_rms^2) row_in. [it's vector_valued].
289 // So this term in dF/d(row_in) equals:
290 // dF/df df/dp dp/d(row_in) = 2/(D * target_rms^2) (f == 1.0 / sqrt(kSquaredNormFloor) ? 0.0 : -0.5 f^3) (deriv_out^T row_in) row_in
291 // So
292 // deriv_in = f deriv_out + (f == 1.0 ? 0.0 : -f^3 / (D * target_rms^2) ) (deriv_out^T row_in) row_in
293 // if add_log_stddev_ true, the deriv_in has another term as
294 // dF/dx_i = dF/df . df/dx_i => df/dx_i = x_i/(x^T x)
295 template<typename Real>
296 void DiffNormalizePerRow(const CuMatrixBase<Real> &in_value,
297  const CuMatrixBase<Real> &out_deriv,
298  const Real target_rms, const bool add_log_stddev,
299  CuMatrixBase<Real>* in_deriv);
300 
301 
302 } // namespace cu
303 } // namespace kaldi
304 
305 
306 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CpuComputeLstmNonlinearity(const MatrixBase< Real > &input_mat, const MatrixBase< Real > &params_mat, MatrixBase< Real > *output)
Definition: cu-math.cc:445
void Randomize(const CuMatrixBase< Real > &src, const CuArray< int32 > &copy_from_idx, CuMatrixBase< Real > *tgt)
Copies a permutation of src into tgt.
Definition: cu-math.cc:80
void BackpropLstmNonlinearity(const CuMatrixBase< Real > &input, const CuMatrixBase< Real > &params, const CuMatrixBase< Real > &output_deriv, const CuMatrixBase< double > &deriv_sum_in, const CuVectorBase< Real > &self_repair_config, double count_in, CuMatrixBase< Real > *input_deriv, CuMatrixBase< Real > *params_deriv, CuMatrixBase< double > *value_sum_out, CuMatrixBase< double > *deriv_sum_out, CuMatrixBase< Real > *self_repair_sum_out)
This function does the &#39;backward&#39; pass corresponding to the function ComputeLstmNonlinearity.
Definition: cu-math.cc:768
void DiffNormalizePerRow(const CuMatrixBase< Real > &in_value, const CuMatrixBase< Real > &out_deriv, const Real target_rms, const bool add_log_stddev, CuMatrixBase< Real > *in_deriv)
Definition: cu-math.cc:349
void EnsureNonzero(const CuMatrixBase< Real > &src, Real epsilon, CuMatrixBase< Real > *dest)
This function requires that src and dest have the same dimension and epsilon > 0. ...
Definition: cu-math.cc:209
void Splice(const CuMatrixBase< Real > &src, const CuArray< int32 > &frame_offsets, CuMatrixBase< Real > *tgt)
Splice concatenates frames of src as specified in frame_offsets into tgt.
Definition: cu-math.cc:132
void NormalizePerRow(const CuMatrixBase< Real > &in, const Real target_rms, const bool add_log_stddev, CuMatrixBase< Real > *out)
Normalize nonlinearity modifies the vector of activations by scaling it so that the root-mean-square ...
Definition: cu-math.cc:280
void RegularizeL1(CuMatrixBase< Real > *weight, CuMatrixBase< Real > *grad, Real l1, Real lr)
RegularizeL1 is a gradient step with l1 regularization added to the gradient.
Definition: cu-math.cc:37
void Copy(const CuMatrixBase< Real > &src, const CuArray< int32 > &copy_from_indices, CuMatrixBase< Real > *tgt)
Copies elements from src into tgt as given by copy_from_indices.
Definition: cu-math.cc:173
void CpuBackpropLstmNonlinearity(const MatrixBase< Real > &input, const MatrixBase< Real > &params, const MatrixBase< Real > &output_deriv, const MatrixBase< double > &deriv_sum_in, const VectorBase< Real > &self_repair_config, double count_in, MatrixBase< Real > *input_deriv, MatrixBase< Real > *params_deriv, MatrixBase< double > *value_sum_out, MatrixBase< double > *deriv_sum_out, MatrixBase< Real > *self_repair_sum_out)
Definition: cu-math.cc:543
void ComputeLstmNonlinearity(const CuMatrixBase< Real > &input, const CuMatrixBase< Real > &params, CuMatrixBase< Real > *output)
this is a special-purpose function used by class LstmNonlinearityComponent, to do its forward propaga...
Definition: cu-math.cc:489