Skip to content

Commit dbd8bbf

Browse files
committed
sandbox/online: minor efficiency improvements in the basis-fMLLR.
git-svn-id: https://svn.code.sf.net/p/kaldi/code/sandbox/online@3751 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
1 parent 7000e96 commit dbd8bbf

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎src/transform/basis-fmllr-diag-gmm.cc‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ double BasisFmllrEstimate::ComputeTransform(
275275
} else {
276276
W_mat.CopyFromMat(*out_xform);
277277
}
278+
279+
// Create temporary K and G quantities. Add for efficiency,
280+
// avoid repetitions of converting the stats from double
281+
// precision to single precision
282+
Matrix<BaseFloat> stats_tmp_K(spk_stats.K_);
283+
std::vector<SpMatrix<BaseFloat> > stats_tmp_G(dim_);
284+
for (int32 d = 0; d < dim_; d++)
285+
stats_tmp_G[d] = SpMatrix<BaseFloat>(spk_stats.G_[d]);
286+
278287
// Number of bases for this speaker, according to the available
279288
// adaptation data
280289
int32 basis_size = int32 (std::min( double(basis_size_),
@@ -290,10 +299,9 @@ double BasisFmllrEstimate::ComputeTransform(
290299
// Contribution of quadratic terms to derivative
291300
// Eq. (37) s_{d} = G_{d} w_{d}
292301
Matrix<BaseFloat> S(dim_, dim_ + 1);
293-
for (int32 d = 0; d < dim_; ++d) {
294-
Matrix<BaseFloat> G_d_full(spk_stats.G_[d]);
295-
S.Row(d).AddMatVec(1.0, G_d_full, kNoTrans, W_mat.Row(d), 0.0);
296-
}
302+
for (int32 d = 0; d < dim_; ++d)
303+
S.Row(d).AddSpVec(1.0, stats_tmp_G[d], W_mat.Row(d), 0.0);
304+
297305

298306
// W_mat = [A; b]
299307
Matrix<BaseFloat> A(dim_, dim_);
@@ -308,9 +316,7 @@ double BasisFmllrEstimate::ComputeTransform(
308316
P.SetZero();
309317
P.Range(0, dim_, 0, dim_).CopyFromMat(A_inv_trans);
310318
P.Scale(spk_stats.beta_);
311-
Matrix<BaseFloat> spk_stats_Ktmp(dim_, dim_ + 1);
312-
spk_stats_Ktmp.CopyFromMat(spk_stats.K_);
313-
P.AddMat(1.0, spk_stats_Ktmp);
319+
P.AddMat(1.0, stats_tmp_K);
314320
P.AddMat(-1.0, S);
315321

316322
// Compute directional gradient restricted by bases. Here we only use
@@ -326,7 +332,8 @@ double BasisFmllrEstimate::ComputeTransform(
326332
delta_W.AddMat(delta_d(n), fmllr_basis_[n]);
327333
}
328334

329-
BaseFloat step_size = CalBasisFmllrStepSize(spk_stats, delta_W, A, S, options.step_size_iters);
335+
BaseFloat step_size = CalBasisFmllrStepSize(spk_stats, stats_tmp_K,
336+
stats_tmp_G, delta_W, A, S, options.step_size_iters);
330337
W_mat.AddMat(step_size, delta_W, kNoTrans);
331338
coefficient->AddVec(step_size, delta_d);
332339
// Check auxiliary function
@@ -347,25 +354,25 @@ double BasisFmllrEstimate::ComputeTransform(
347354

348355

349356
BaseFloat CalBasisFmllrStepSize(const AffineXformStats &spk_stats,
350-
const Matrix<BaseFloat> &delta,
351-
const Matrix<BaseFloat> &A,
352-
const Matrix<BaseFloat> &S,
353-
int32 max_iters) {
357+
const Matrix<BaseFloat> &spk_stats_tmp_K,
358+
const std::vector<SpMatrix<BaseFloat> > &spk_stats_tmp_G,
359+
const Matrix<BaseFloat> &delta,
360+
const Matrix<BaseFloat> &A,
361+
const Matrix<BaseFloat> &S,
362+
int32 max_iters) {
363+
354364
int32 dim = spk_stats.dim_;
355365
KALDI_ASSERT(dim == delta.NumRows() && dim == S.NumRows());
356366
// The first D columns of delta_W
357367
SubMatrix<BaseFloat> delta_Dim(delta, 0, dim, 0, dim);
358368
// Eq. (46): b = tr(delta K^T) - tr(delta S^T)
359-
Matrix<BaseFloat> spk_stats_Ktmp(dim, dim + 1);
360-
spk_stats_Ktmp.CopyFromMat(spk_stats.K_);
361-
BaseFloat b = TraceMatMat(delta, spk_stats_Ktmp, kTrans)
369+
BaseFloat b = TraceMatMat(delta, spk_stats_tmp_K, kTrans)
362370
- TraceMatMat(delta, S, kTrans);
363371
// Eq. (47): c = sum_d tr(delta_{d} G_{d} delta_{d})
364372
BaseFloat c = 0;
365373
Vector<BaseFloat> G_row_delta(dim + 1);
366374
for (int32 d = 0; d < dim; ++d) {
367-
SpMatrix<BaseFloat> spk_stats_Gtmp(spk_stats.G_[d]);
368-
G_row_delta.AddSpVec(1.0, spk_stats_Gtmp, delta.Row(d), 0.0);
375+
G_row_delta.AddSpVec(1.0, spk_stats_tmp_G[d], delta.Row(d), 0.0);
369376
c += VecVec(G_row_delta, delta.Row(d));
370377
}
371378

‎src/transform/basis-fmllr-diag-gmm.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ class BasisFmllrEstimate {
168168
/// the resulting step size \k should be close to 1. If \k <<1 or >>1, there
169169
/// maybe problems with preconditioning or the speaker stats.
170170
BaseFloat CalBasisFmllrStepSize(const AffineXformStats &spk_stats,
171-
const Matrix<BaseFloat> &delta,
172-
const Matrix<BaseFloat> &A,
173-
const Matrix<BaseFloat> &S,
174-
int32 max_iters);
171+
const Matrix<BaseFloat> &spk_stats_tmp_K,
172+
const std::vector<SpMatrix<BaseFloat> > &spk_stats_tmp_G,
173+
const Matrix<BaseFloat> &delta,
174+
const Matrix<BaseFloat> &A,
175+
const Matrix<BaseFloat> &S,
176+
int32 max_iters);
175177

176178
} // namespace kaldi
177179

0 commit comments

Comments
 (0)