From e1f76237e019859f5560ec93b8ef2b1f158f4ff1 Mon Sep 17 00:00:00 2001 From: Jan Trmal Date: Wed, 22 Jul 2015 12:55:15 -0400 Subject: [PATCH 1/2] windows fixes -- resolving issues with incrrectly behaving expf/exp(float) under MSVC2013, plus some other improvements --- src/base/kaldi-error.h | 2 +- src/base/kaldi-math-test.cc | 21 ++-- src/base/kaldi-math.h | 102 +++++++++--------- src/bin/analyze-counts.cc | 2 +- src/bin/compute-mce-scale.cc | 2 +- src/bin/get-silence-probs.cc | 4 +- src/bin/logprob-to-post.cc | 2 +- src/bin/thresh-post.cc | 2 +- src/cudamatrix/cu-matrix-test.cc | 2 +- src/cudamatrix/cu-matrix.cc | 4 +- src/cudamatrix/cu-vector-test.cc | 4 +- src/feat/feature-fbank.cc | 4 +- src/feat/feature-functions.cc | 4 +- src/feat/feature-mfcc.cc | 4 +- src/feat/feature-plp.cc | 4 +- src/feat/feature-sdc-test.cc | 1 + src/feat/feature-spectrogram.cc | 4 +- src/feat/online-feature-test.cc | 4 +- src/feat/pitch-functions.cc | 34 +++--- src/feat/resample-test.cc | 6 +- src/featbin/interpolate-pitch.cc | 4 +- src/featbin/process-pitch-feats.cc | 4 +- src/fstext/determinize-lattice-inl.h | 6 +- src/fstext/fstext-utils-test.cc | 3 + src/fstext/push-special.cc | 13 +-- src/fstext/rescale-inl.h | 8 +- src/fstext/rescale-test.cc | 7 +- src/gmm/diag-gmm-test.cc | 14 +-- src/gmm/diag-gmm.cc | 10 +- src/gmm/ebw-diag-gmm-test.cc | 4 +- src/gmm/ebw-diag-gmm.cc | 4 +- src/gmm/full-gmm-test.cc | 4 +- src/gmm/full-gmm.cc | 6 +- src/gmm/indirect-diff-diag-gmm.cc | 2 +- src/gmm/mle-am-diag-gmm-test.cc | 2 +- src/gmm/mle-diag-gmm-test.cc | 4 +- src/gmm/mle-full-gmm-test.cc | 2 +- src/gmm/model-test-common.cc | 4 +- src/hmm/hmm-utils.cc | 4 +- src/hmm/transition-model.cc | 32 +++--- src/ivector/logistic-regression-test.cc | 2 +- src/ivector/logistic-regression.cc | 2 +- src/kws/kws-scoring.cc | 3 +- src/lat/lattice-functions.cc | 10 +- src/lat/sausages.cc | 6 +- src/latbin/lattice-combine.cc | 2 +- src/latbin/lattice-confidence.cc | 6 +- src/lm/const-arpa-lm.cc | 5 +- src/matrix/matrix-lib-test.cc | 20 ++-- src/nnet/nnet-component-test.cc | 1 - src/nnet/nnet-rbm.h | 2 +- src/nnet2/mixup-nnet.cc | 2 +- src/nnet2/nnet-component.cc | 2 +- src/nnet2/nnet-compute-discriminative.cc | 2 +- src/nnet2/nnet-compute.cc | 2 +- src/nnet2/nnet-precondition-online-test.cc | 2 +- src/nnet2/nnet-precondition-online.cc | 2 +- src/nnet2bin/nnet-adjust-priors.cc | 2 +- src/nnet2bin/nnet-modify-learning-rates.cc | 4 +- src/nnetbin/cuda-gpu-available.cc | 1 + src/online/online-audio-source.cc | 2 +- src/online2/online-gmm-decodable.cc | 2 +- src/sgmm/am-sgmm.cc | 16 +-- src/sgmm/estimate-am-sgmm-multi-test.cc | 5 +- src/sgmm/estimate-am-sgmm-test.cc | 5 +- src/sgmm/estimate-am-sgmm.cc | 6 +- src/sgmm/fmllr-sgmm-test.cc | 5 +- src/sgmm/sgmm-clusterable.cc | 2 +- src/sgmm2/am-sgmm2.cc | 12 +-- src/sgmm2/estimate-am-sgmm2-test.cc | 5 +- src/sgmm2/estimate-am-sgmm2.cc | 4 +- src/sgmm2/fmllr-sgmm2-test.cc | 5 +- .../decodable-am-diag-gmm-regtree.cc | 4 +- src/transform/fmllr-diag-gmm-test.cc | 4 +- src/transform/fmllr-diag-gmm.cc | 4 +- src/transform/fmllr-raw-test.cc | 4 +- src/transform/mllt.cc | 4 +- src/transform/regression-tree-test.cc | 2 +- src/transform/regtree-fmllr-diag-gmm-test.cc | 2 +- src/util/text-utils.cc | 66 +++++++++++- windows/generate_solution.pl | 30 ++++-- 81 files changed, 365 insertions(+), 251 deletions(-) diff --git a/src/base/kaldi-error.h b/src/base/kaldi-error.h index 644849f9963..2285edc0f1c 100644 --- a/src/base/kaldi-error.h +++ b/src/base/kaldi-error.h @@ -27,7 +27,7 @@ #include #include -#if _MSC_VER >= 0x1400 || !defined(MSC_VER) && __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) +#if _MSC_VER >= 1900 || (!defined(_MSC_VER) && __cplusplus > 199711L) || defined(__GXX_EXPERIMENTAL_CXX0X__) #define NOEXCEPT(Predicate) noexcept((Predicate)) #else #define NOEXCEPT(Predicate) diff --git a/src/base/kaldi-math-test.cc b/src/base/kaldi-math-test.cc index d002a74f48e..3026f05502f 100644 --- a/src/base/kaldi-math-test.cc +++ b/src/base/kaldi-math-test.cc @@ -18,6 +18,7 @@ // limitations under the License. #include "base/kaldi-math.h" #include "base/timer.h" +#include namespace kaldi { @@ -25,8 +26,10 @@ template void UnitTestGcdLcmTpl() { for (I a = 1; a < 15; a++) { // a is min gcd. I b = (I)(Rand() % 10); I c = (I)(Rand() % 10); - if (Rand()%2 == 0 && std::numeric_limits::is_signed) b = -b; - if (Rand()%2 == 0 && std::numeric_limits::is_signed) c = -c; + if (std::numeric_limits::is_signed) { + if (Rand() % 2 == 0) b = -b; + if (Rand() % 2 == 0) c = -c; + } if (b == 0 && c == 0) continue; // gcd not defined for such numbers. I g = Gcd(b*a, c*a); KALDI_ASSERT(g >= a); @@ -157,14 +160,14 @@ void UnitTestLogAddSub() { using namespace kaldi; for (int i = 0; i < 100; i++) { double f1 = Rand() % 10000, f2 = Rand() % 20; - double add1 = exp(LogAdd(log(f1), log(f2))); - double add2 = exp(LogAdd(log(f2), log(f1))); + double add1 = Exp(LogAdd(Log(f1), Log(f2))); + double add2 = Exp(LogAdd(Log(f2), Log(f1))); double add = f1 + f2, thresh = add*0.00001; KALDI_ASSERT(std::abs(add-add1) < thresh && std::abs(add-add2) < thresh); try { - double f2_check = exp(LogSub(log(add), log(f1))), thresh = (f2*0.01)+0.001; + double f2_check = Exp(LogSub(Log(add), Log(f1))), thresh = (f2*0.01)+0.001; KALDI_ASSERT(std::abs(f2_check-f2) < thresh); } catch(...) { KALDI_ASSERT(f2 == 0); // It will probably crash for f2=0. @@ -173,8 +176,8 @@ void UnitTestLogAddSub() { } void UnitTestDefines() { // Yes, we even unit-test the preprocessor statements. - KALDI_ASSERT(exp(kLogZeroFloat) == 0.0); - KALDI_ASSERT(exp(kLogZeroDouble) == 0.0); + KALDI_ASSERT(Exp(kLogZeroFloat) == 0.0); + KALDI_ASSERT(Exp(kLogZeroDouble) == 0.0); BaseFloat den = 0.0; KALDI_ASSERT(KALDI_ISNAN(0.0 / den)); KALDI_ASSERT(!KALDI_ISINF(0.0 / den)); @@ -194,8 +197,8 @@ void UnitTestDefines() { // Yes, we even unit-test the preprocessor statements. && "If this test fails, you can probably just comment it out-- may mean your CPU exceeds expected floating point precision"); KALDI_ASSERT(std::abs(sin(M_PI)) < 1.0e-05 && std::abs(cos(M_PI)+1.0) < 1.0e-05); KALDI_ASSERT(std::abs(sin(M_2PI)) < 1.0e-05 && std::abs(cos(M_2PI)-1.0) < 1.0e-05); - KALDI_ASSERT(std::abs(sin(exp(M_LOG_2PI))) < 1.0e-05); - KALDI_ASSERT(std::abs(cos(exp(M_LOG_2PI)) - 1.0) < 1.0e-05); + KALDI_ASSERT(std::abs(sin(Exp(M_LOG_2PI))) < 1.0e-05); + KALDI_ASSERT(std::abs(cos(Exp(M_LOG_2PI)) - 1.0) < 1.0e-05); } void UnitTestAssertFunc() { // Testing Assert** *functions diff --git a/src/base/kaldi-math.h b/src/base/kaldi-math.h index 28a02458fc6..edbc8010195 100644 --- a/src/base/kaldi-math.h +++ b/src/base/kaldi-math.h @@ -75,6 +75,51 @@ namespace kaldi { +#if !defined(_MSC_VER) || (_MSC_VER >= 1900) +inline double Exp(double x) { return exp(x); } +#ifndef KALDI_NO_EXPF +inline float Exp(float x) { return expf(x); } +#else +inline float Exp(float x) { return exp(static_cast(x)); } +#endif // KALDI_NO_EXPF +#else +inline double Exp(double x) { return exp(x); } +#if !defined(__INTEL_COMPILER) && _MSC_VER == 1800 && defined(_M_X64) +// Microsoft CL v18.0 buggy 64-bit implementation of +// expf() incorrectly returns -inf for exp(-inf). +inline float Exp(float x) { return exp(static_cast(x)); } +#else +inline float Exp(float x) { return expf(x); } +#endif // !defined(__INTEL_COMPILER) && _MSC_VER == 1800 && defined(_M_X64) +#endif // !defined(_MSC_VER) || (_MSC_VER >= 1900) + +inline double Log(double x) { return log(x); } +inline float Log(float x) { return logf(x); } + +#if !defined(_MSC_VER) || (_MSC_VER >= 1700) +inline double Log1p(double x) { return log1p(x); } +inline float Log1p(float x) { return log1pf(x); } +#else +inline double Log1p(double x) { + const double cutoff = 1.0e-08; + if (x < cutoff) + return x - 2 * x * x; + else + return Log(1.0 + x); +} + +inline float Log1p(float x) { + const float cutoff = 1.0e-07; + if (x < cutoff) + return x - 2 * x * x; + else + return Log(1.0 + x); +} +#endif + +static const double kMinLogDiffDouble = Log(DBL_EPSILON); // negative! +static const float kMinLogDiffFloat = Log(FLT_EPSILON); // negative! + // -infinity const float kLogZeroFloat = -std::numeric_limits::infinity(); const double kLogZeroDouble = -std::numeric_limits::infinity(); @@ -103,7 +148,7 @@ inline float RandUniform(struct RandomState* state = NULL) { } inline float RandGauss(struct RandomState* state = NULL) { - return static_cast(sqrtf (-2 * logf(RandUniform(state))) + return static_cast(sqrtf (-2 * Log(RandUniform(state))) * cosf(2*M_PI*RandUniform(state))); } @@ -129,8 +174,6 @@ inline Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState* s (RandUniform(state) <= fabs(post)/prune_thresh ? prune_thresh : 0.0); } -static const double kMinLogDiffDouble = std::log(DBL_EPSILON); // negative! -static const float kMinLogDiffFloat = std::log(FLT_EPSILON); // negative! inline double LogAdd(double x, double y) { double diff; @@ -144,13 +187,7 @@ inline double LogAdd(double x, double y) { if (diff >= kMinLogDiffDouble) { double res; -#if _MSC_VER >= 1800 || __cplusplus >= 201103L - res = x + std::log1p(std::exp(diff)); -#elif defined _MSC_VER - res = x + log(1.0 + exp(diff)); -#else - res = x + log1p(exp(diff)); -#endif + res = x + Log1p(Exp(diff)); return res; } else { return x; // return the larger one. @@ -170,13 +207,7 @@ inline float LogAdd(float x, float y) { if (diff >= kMinLogDiffFloat) { float res; -#if _MSC_VER >= 1800 || __cplusplus >= 201103L - res = x + std::log1p(std::exp(diff)); -#elif defined _MSC_VER - res = x + logf(1.0 + expf(diff)); -#else - res = x + log1pf(expf(diff)); -#endif + res = x + Log1p(Exp(diff)); return res; } else { return x; // return the larger one. @@ -194,7 +225,7 @@ inline double LogSub(double x, double y) { } double diff = y - x; // Will be negative. - double res = x + log(1.0 - exp(diff)); + double res = x + Log(1.0 - Exp(diff)); // res might be NAN if diff ~0.0, and 1.0-exp(diff) == 0 to machine precision if (KALDI_ISNAN(res)) @@ -213,7 +244,7 @@ inline float LogSub(float x, float y) { } float diff = y - x; // Will be negative. - float res = x + logf(1.0 - expf(diff)); + float res = x + Log(1.0f - Exp(diff)); // res might be NAN if diff ~0.0, and 1.0-exp(diff) == 0 to machine precision if (KALDI_ISNAN(res)) @@ -301,42 +332,9 @@ template void Factorize(I m, std::vector *factors) { } inline double Hypot(double x, double y) { return hypot(x, y); } - inline float Hypot(float x, float y) { return hypotf(x, y); } -#if !defined(_MSC_VER) || (_MSC_VER >= 1800) -inline double Log1p(double x) { return log1p(x); } - -inline float Log1p(float x) { return log1pf(x); } -#else -inline double Log1p(double x) { - const double cutoff = 1.0e-08; - if (x < cutoff) - return x - 2 * x * x; - else - return log(1.0 + x); -} - -inline float Log1p(float x) { - const float cutoff = 1.0e-07; - if (x < cutoff) - return x - 2 * x * x; - else - return log(1.0 + x); -} -#endif - -inline double Exp(double x) { return exp(x); } - -#ifndef KALDI_NO_EXPF -inline float Exp(float x) { return expf(x); } -#else -inline float Exp(float x) { return exp(x); } -#endif - -inline double Log(double x) { return log(x); } -inline float Log(float x) { return logf(x); } } // namespace kaldi diff --git a/src/bin/analyze-counts.cc b/src/bin/analyze-counts.cc index 6fdc06de820..60be710c79d 100644 --- a/src/bin/analyze-counts.cc +++ b/src/bin/analyze-counts.cc @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) { // sort the counts std::vector > sorted_counts; for (int32 i = 0; i < counts.size(); i++) { - sorted_counts.push_back(std::make_pair(counts[i], i)); + sorted_counts.push_back(std::make_pair(static_cast(counts[i]), i)); } std::sort(sorted_counts.begin(), sorted_counts.end()); diff --git a/src/bin/compute-mce-scale.cc b/src/bin/compute-mce-scale.cc index 2f83463c649..349d368a776 100644 --- a/src/bin/compute-mce-scale.cc +++ b/src/bin/compute-mce-scale.cc @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) { // means it's more comparable with MMI/MPE. BaseFloat den_score = den_score_reader.Value(key); BaseFloat score_difference = mce_alpha * (num_score - den_score) + mce_beta; - BaseFloat sigmoid_difference = 1.0 / (1.0 + exp(score_difference)); + BaseFloat sigmoid_difference = 1.0 / (1.0 + Exp(score_difference)); // It might be more natural to make the scale // BaseFloat scale = 4.0 * sigmoid_difference * (1 - sigmoid_difference); diff --git a/src/bin/get-silence-probs.cc b/src/bin/get-silence-probs.cc index 6aa06301f14..b440399654e 100644 --- a/src/bin/get-silence-probs.cc +++ b/src/bin/get-silence-probs.cc @@ -66,7 +66,7 @@ int main(int argc, char *argv[]) { KALDI_ASSERT(quantize >= 0.0 && quantize <= 1.0); - double sil_log_bias = log(sil_prior / (1.0 - sil_prior)); + double sil_log_bias = Log(sil_prior / (1.0 - sil_prior)); std::string silence_likes_rspecifier = po.GetArg(1), nonsilence_likes_rspecifier = po.GetArg(2), @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) { if (sil_loglike > 10) { sil_probs(f) = 1.0; // because the exp below might fail. } else { - BaseFloat e_sil_loglike = exp(sil_loglike); + BaseFloat e_sil_loglike = Exp(sil_loglike); BaseFloat sil_prob = e_sil_loglike / (1.0 + e_sil_loglike); if ( !(sil_prob >= 0.0 && sil_prob <= 1.0)) { KALDI_WARN << "Bad silence prob (NaNs found?), setting to 0.5"; diff --git a/src/bin/logprob-to-post.cc b/src/bin/logprob-to-post.cc index c9f21ee6459..f221580a484 100644 --- a/src/bin/logprob-to-post.cc +++ b/src/bin/logprob-to-post.cc @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) { for (int32 i = 0; i < logprobs.NumRows(); i++) { SubVector row(logprobs, i); for (int32 j = 0; j < row.Dim(); j++) { - BaseFloat p = exp(row(j)); + BaseFloat p = Exp(row(j)); if (p >= min_post) { post[i].push_back(std::make_pair(j, p)); } else if (random_prune && (p / min_post) >= RandUniform()) { diff --git a/src/bin/thresh-post.cc b/src/bin/thresh-post.cc index 67bf05fac40..91287306f23 100644 --- a/src/bin/thresh-post.cc +++ b/src/bin/thresh-post.cc @@ -78,7 +78,7 @@ int main(int argc, char *argv[]) { if (weight < threshold) weight *= scale; total_weight_out += weight; if (weight != 0.0) - new_post[i].push_back(std::make_pair(tid, weight)); + new_post[i].push_back(std::make_pair(tid, static_cast(weight))); } } posterior_writer.Write(posterior_reader.Key(), new_post); diff --git a/src/cudamatrix/cu-matrix-test.cc b/src/cudamatrix/cu-matrix-test.cc index a19c8b9c987..629920a1bb5 100644 --- a/src/cudamatrix/cu-matrix-test.cc +++ b/src/cudamatrix/cu-matrix-test.cc @@ -1621,7 +1621,7 @@ static void UnitTestCuDiffXent() { //cpu for(MatrixIndexT r=0; r::CompObjfAndDeriv(const std::vector >& s //KALDI_ASSERT(label >= 0 && label < nnet_.OutputDim()); Real this_prob = output(m, label); KALDI_ASSERT(this_prob >= 0.99e-20); // we floored to 1.0e-20 in SoftmaxLayer. - *tot_objf += weight * log(this_prob); + *tot_objf += weight * Log(this_prob); *tot_weight += weight; (*this)(m, label) += weight / this_prob; } @@ -1399,7 +1399,7 @@ void CuMatrixBase::DiffXent(const CuArray &tgt, for(int32 r = 0; r < num_rows; r++) { int32 col_tgt = tgt.Data()[r]; Real &value = Mat()(r, col_tgt); - log_post_tgt->Vec()(r) = log(value); + log_post_tgt->Vec()(r) = Log(value); value -= 1.0; } } diff --git a/src/cudamatrix/cu-vector-test.cc b/src/cudamatrix/cu-vector-test.cc index 99fff9ee29b..db715d75d7a 100644 --- a/src/cudamatrix/cu-vector-test.cc +++ b/src/cudamatrix/cu-vector-test.cc @@ -463,7 +463,7 @@ template void CuVectorUnitTestApplyExp() { vector.ApplyExp(); for(int32 j = 0; j < dim; j++) { //std::cout<<"diff is "< void CuVectorUnitTestApplyLog() { vector.ApplyLog(); for(int32 j = 0; j < dim; j++) { //std::cout<<"diff is "< 0.0) - log_energy_floor_ = log(opts.energy_floor); + log_energy_floor_ = Log(opts.energy_floor); int32 padded_window_size = opts.frame_opts.PaddedWindowSize(); if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two... @@ -136,7 +136,7 @@ void Fbank::ComputeInternal(const VectorBase &wave, // Compute energy after window function (not the raw one) if (opts_.use_energy && !opts_.raw_energy) - log_energy = log(std::max(VecVec(window, window), + log_energy = Log(std::max(VecVec(window, window), std::numeric_limits::min())); if (srfft_ != NULL) // Compute FFT using split-radix algorithm. diff --git a/src/feat/feature-functions.cc b/src/feat/feature-functions.cc index 2de7f49e58a..9678e909a5a 100644 --- a/src/feat/feature-functions.cc +++ b/src/feat/feature-functions.cc @@ -153,7 +153,7 @@ void ExtractWindow(const VectorBase &wave, if (log_energy_pre_window != NULL) { BaseFloat energy = std::max(VecVec(window_part, window_part), std::numeric_limits::min()); - *log_energy_pre_window = log(energy); + *log_energy_pre_window = Log(energy); } if (opts.preemph_coeff != 0.0) @@ -385,7 +385,7 @@ BaseFloat ComputeLpc(const VectorBase &autocorr_in, tmp.Data()); if (ans <= 0.0) KALDI_WARN << "Zero energy in LPC computation"; - return -log((double)1.0/ans); // forms the C0 value + return -Log((double)1.0/ans); // forms the C0 value } void SpliceFrames(const MatrixBase &input_features, diff --git a/src/feat/feature-mfcc.cc b/src/feat/feature-mfcc.cc index 2960e9ed1d5..135152733d9 100644 --- a/src/feat/feature-mfcc.cc +++ b/src/feat/feature-mfcc.cc @@ -39,7 +39,7 @@ Mfcc::Mfcc(const MfccOptions &opts) ComputeLifterCoeffs(opts.cepstral_lifter, &lifter_coeffs_); } if (opts.energy_floor > 0.0) - log_energy_floor_ = log(opts.energy_floor); + log_energy_floor_ = Log(opts.energy_floor); int32 padded_window_size = opts.frame_opts.PaddedWindowSize(); if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two... @@ -138,7 +138,7 @@ void Mfcc::ComputeInternal(const VectorBase &wave, (opts_.use_energy && opts_.raw_energy ? &log_energy : NULL)); if (opts_.use_energy && !opts_.raw_energy) - log_energy = log(std::max(VecVec(window, window), + log_energy = Log(std::max(VecVec(window, window), std::numeric_limits::min())); if (srfft_ != NULL) // Compute FFT using the split-radix algorithm. diff --git a/src/feat/feature-plp.cc b/src/feat/feature-plp.cc index a22dd3ca97b..fe439864346 100644 --- a/src/feat/feature-plp.cc +++ b/src/feat/feature-plp.cc @@ -34,7 +34,7 @@ Plp::Plp(const PlpOptions &opts) &idft_bases_); if (opts.energy_floor > 0.0) - log_energy_floor_ = log(opts.energy_floor); + log_energy_floor_ = Log(opts.energy_floor); int32 padded_window_size = opts.frame_opts.PaddedWindowSize(); if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two... @@ -193,7 +193,7 @@ void Plp::ComputeInternal(const VectorBase &wave, (opts_.use_energy && opts_.raw_energy ? &log_energy : NULL)); if (opts_.use_energy && !opts_.raw_energy) - log_energy = log(std::max(VecVec(window, window), + log_energy = Log(std::max(VecVec(window, window), std::numeric_limits::min())); if (srfft_ != NULL) // Compute FFT using split-radix algorithm. diff --git a/src/feat/feature-sdc-test.cc b/src/feat/feature-sdc-test.cc index 1ffaebc5f7e..3ecef148b87 100644 --- a/src/feat/feature-sdc-test.cc +++ b/src/feat/feature-sdc-test.cc @@ -162,6 +162,7 @@ int main() { } return 0; } catch (const std::exception &e) { + static_cast(e); return 1; } diff --git a/src/feat/feature-spectrogram.cc b/src/feat/feature-spectrogram.cc index 76a4849c7d5..df915ad90fe 100644 --- a/src/feat/feature-spectrogram.cc +++ b/src/feat/feature-spectrogram.cc @@ -27,7 +27,7 @@ namespace kaldi { Spectrogram::Spectrogram(const SpectrogramOptions &opts) : opts_(opts), feature_window_function_(opts.frame_opts), srfft_(NULL) { if (opts.energy_floor > 0.0) - log_energy_floor_ = log(opts.energy_floor); + log_energy_floor_ = Log(opts.energy_floor); int32 padded_window_size = opts.frame_opts.PaddedWindowSize(); if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two @@ -68,7 +68,7 @@ void Spectrogram::Compute(const VectorBase &wave, // Compute energy after window function (not the raw one) if (!opts_.raw_energy) - log_energy = log(std::max(VecVec(window, window), + log_energy = Log(std::max(VecVec(window, window), std::numeric_limits::min())); if (srfft_ != NULL) // Compute FFT using split-radix algorithm. diff --git a/src/feat/online-feature-test.cc b/src/feat/online-feature-test.cc index 55df9914490..7a46e837151 100644 --- a/src/feat/online-feature-test.cc +++ b/src/feat/online-feature-test.cc @@ -70,6 +70,7 @@ bool RandomSplit(int32 wav_dim, int32 num_pieces, int32 trials = 5) { piece_dim->clear(); + piece_dim->resize(num_pieces, 0); int32 dim_mean = wav_dim / (num_pieces * 2); int32 cnt = 0; @@ -182,7 +183,8 @@ void TestOnlineMfcc() { // we try to break it into from 5 pieces to 9(not essential to do so) for (int32 num_piece = 5; num_piece < 10; num_piece++) { OnlineMfcc online_mfcc(op); - std::vector piece_length(num_piece); + std::vector piece_length(num_piece, 0); + bool ret = RandomSplit(waveform.Dim(), &piece_length, num_piece); KALDI_ASSERT(ret); diff --git a/src/feat/pitch-functions.cc b/src/feat/pitch-functions.cc index c61fb5f483c..cb934a09ac2 100644 --- a/src/feat/pitch-functions.cc +++ b/src/feat/pitch-functions.cc @@ -77,10 +77,10 @@ BaseFloat NccfToPov(BaseFloat n) { BaseFloat ndash = fabs(n); if (ndash > 1.0) ndash = 1.0; // just in case it was slightly outside [-1, 1] - BaseFloat r = -5.2 + 5.4 * exp(7.5 * (ndash - 1.0)) + 4.8 * ndash - - 2.0 * exp(-10.0 * ndash) + 4.2 * exp(20.0 * (ndash - 1.0)); + BaseFloat r = -5.2 + 5.4 * Exp(7.5 * (ndash - 1.0)) + 4.8 * ndash - + 2.0 * Exp(-10.0 * ndash) + 4.2 * Exp(20.0 * (ndash - 1.0)); // r is the approximate log-prob-ratio of voicing, log(p/(1-p)). - BaseFloat p = 1.0 / (1 + exp(-1.0 * r)); + BaseFloat p = 1.0 / (1 + Exp(-1.0 * r)); KALDI_ASSERT(p - p == 0); // Check for NaN/inf return p; } @@ -210,7 +210,7 @@ class PitchFrameInfo { /// info for the final state; the iterator will be decremented inside this /// function. void SetBestState(int32 best_state, - std::vector >::iterator lag_nccf_iter); + std::vector > &lag_nccf); /// This function may be called on the last (most recent) PitchFrameInfo /// object; it computes how many frames of latency there is because the @@ -313,7 +313,7 @@ void PitchFrameInfo::ComputeBacktraces( Vector local_cost(num_states, kUndefined); ComputeLocalCost(nccf_pitch, lags, opts, &local_cost); - const BaseFloat delta_pitch_sq = pow(log(1.0 + opts.delta_pitch), 2.0), + const BaseFloat delta_pitch_sq = pow(Log(1.0 + opts.delta_pitch), 2.0), inter_frame_factor = delta_pitch_sq * opts.penalty_factor; // index local_cost, prev_forward_cost and this_forward_cost using raw pointer @@ -483,10 +483,14 @@ void PitchFrameInfo::ComputeBacktraces( void PitchFrameInfo::SetBestState( int32 best_state, - std::vector >::iterator iter) { + std::vector > &lag_nccf) { + // This function would naturally be recursive, but we have coded this to avoid // recursion, which would otherwise eat up the stack. Think of it as a static // member function, except we do use "this" right at the beginning. + + std::vector >::reverse_iterator iter = lag_nccf.rbegin(); + PitchFrameInfo *this_info = this; // it will change in the loop. while (this_info != NULL) { PitchFrameInfo *prev_info = this_info->prev_info_; @@ -501,7 +505,7 @@ void PitchFrameInfo::SetBestState( if (prev_info != NULL) // don't write anything for frame -1. iter->second = this_info->state_info_[state_info_index].pov_nccf; this_info = prev_info; - iter--; + if (this_info != NULL) ++iter; } } @@ -989,9 +993,7 @@ void OnlinePitchFeatureImpl::RecomputeBacktraces() { if (lag_nccf_.size() != static_cast(num_frames)) lag_nccf_.resize(num_frames); - std::vector >::iterator last_iter = - lag_nccf_.end() - 1; - frame_info_.back()->SetBestState(best_final_state, last_iter); + frame_info_.back()->SetBestState(best_final_state, lag_nccf_); frames_latency_ = frame_info_.back()->ComputeLatency(opts_.max_frames_latency); for (size_t i = 0; i < nccf_info_.size(); i++) @@ -1146,9 +1148,7 @@ void OnlinePitchFeatureImpl::AcceptWaveform( int32 best_final_state; forward_cost_.Min(&best_final_state); lag_nccf_.resize(frame_info_.size() - 1); // will keep any existing data. - std::vector >::iterator last_iter = - lag_nccf_.end() - 1; - frame_info_.back()->SetBestState(best_final_state, last_iter); + frame_info_.back()->SetBestState(best_final_state, lag_nccf_); frames_latency_ = frame_info_.back()->ComputeLatency(opts_.max_frames_latency); KALDI_VLOG(4) << "Latency is " << frames_latency_; @@ -1425,7 +1425,7 @@ BaseFloat OnlineProcessPitch::GetRawLogPitchFeature(int32 frame) const { src_->GetFrame(frame, &tmp); BaseFloat pitch = tmp(1); KALDI_ASSERT(pitch > 0); - return log(pitch); + return Log(pitch); } BaseFloat OnlineProcessPitch::GetNormalizedLogPitchFeature(int32 frame) { @@ -1487,7 +1487,7 @@ void OnlineProcessPitch::UpdateNormalizationStats(int32 frame) { Vector tmp(kRawFeatureDim); src_->GetFrame(prev_window_begin, &tmp); BaseFloat accurate_pov = NccfToPov(tmp(0)), - log_pitch = log(tmp(1)); + log_pitch = Log(tmp(1)); this_stats.sum_pov -= accurate_pov; this_stats.sum_log_pitch_pov -= accurate_pov * log_pitch; } @@ -1496,7 +1496,7 @@ void OnlineProcessPitch::UpdateNormalizationStats(int32 frame) { Vector tmp(kRawFeatureDim); src_->GetFrame(prev_window_end, &tmp); BaseFloat accurate_pov = NccfToPov(tmp(0)), - log_pitch = log(tmp(1)); + log_pitch = Log(tmp(1)); this_stats.sum_pov += accurate_pov; this_stats.sum_log_pitch_pov += accurate_pov * log_pitch; } @@ -1515,7 +1515,7 @@ void OnlineProcessPitch::UpdateNormalizationStats(int32 frame) { for (int32 f = this_window_begin; f < this_window_end; f++) { src_->GetFrame(f, &tmp); BaseFloat accurate_pov = NccfToPov(tmp(0)), - log_pitch = log(tmp(1)); + log_pitch = Log(tmp(1)); this_stats.sum_pov += accurate_pov; this_stats.sum_log_pitch_pov += accurate_pov * log_pitch; } diff --git a/src/feat/resample-test.cc b/src/feat/resample-test.cc index b524e73c07d..68d9f501938 100644 --- a/src/feat/resample-test.cc +++ b/src/feat/resample-test.cc @@ -133,7 +133,7 @@ void UnitTestArbitraryResample() { if (num_zeros == 3) { KALDI_ASSERT(error < 0.1); } else { - KALDI_ASSERT(error < 0.02); + KALDI_ASSERT(error < 0.025); } } else { KALDI_VLOG(1) << "[not checking since out of bounds]"; @@ -225,7 +225,7 @@ void UnitTestLinearResample() { } void UnitTestLinearResample2() { - int32 num_samp = 100 + rand() % 100; + int32 num_samp = 150 + rand() % 100; BaseFloat samp_freq = 1000, resamp_freq = 4000; int32 num_zeros = 10; // fairly accurate. @@ -257,7 +257,7 @@ void UnitTestLinearResample2() { linear_resampler2.Resample(signal_upsampled, true, &signal_downsampled); - int32 samp_discard = 20; // Discard 20 samples for edge effects. + int32 samp_discard = 30; // Discard 20 samples for edge effects. SubVector signal_middle(signal, samp_discard, signal.Dim() - (2 * samp_discard)); diff --git a/src/featbin/interpolate-pitch.cc b/src/featbin/interpolate-pitch.cc index 747f95a5824..45b67c54fc3 100644 --- a/src/featbin/interpolate-pitch.cc +++ b/src/featbin/interpolate-pitch.cc @@ -139,8 +139,8 @@ class PitchInterpolator { BaseFloat constant_prob = (1.0 - p_voicing_[t]) * opts_.interpolator_factor, specified_prob = p_voicing_[t] + constant_prob; // specified_prob adds in the extra probability mass at the observed pitch value. - BaseFloat log_constant_prob = log(constant_prob), - log_ratio = log(specified_prob / constant_prob); + BaseFloat log_constant_prob = Log(constant_prob), + log_ratio = Log(specified_prob / constant_prob); log_alpha_.Add(log_constant_prob); // add log_constant_prob to all pitches at this time. log_alpha_(pitch_[t]) += log_ratio; // corrects this to be like adding diff --git a/src/featbin/process-pitch-feats.cc b/src/featbin/process-pitch-feats.cc index 76b259532ba..72ab7ee2924 100644 --- a/src/featbin/process-pitch-feats.cc +++ b/src/featbin/process-pitch-feats.cc @@ -32,7 +32,7 @@ void ProcessPovFeatures(Matrix *mat) { for (int32 i = 0; i < num_frames; i++) { BaseFloat p = (*mat)(i, 0); KALDI_ASSERT(p >= 0.0 && p <= 1.0); - (*mat)(i, 0) = log((p + 0.0001) / (1.0001 - p)); + (*mat)(i, 0) = Log((p + 0.0001) / (1.0001 - p)); } } @@ -40,7 +40,7 @@ void TakeLogOfPitch(Matrix *mat) { int32 num_frames = mat->NumRows(); for (int32 i = 0; i < num_frames; i++) { KALDI_ASSERT((*mat)(i, 1) > 0.0); - (*mat)(i, 1) = log((*mat)(i, 1)); + (*mat)(i, 1) = Log((*mat)(i, 1)); } } diff --git a/src/fstext/determinize-lattice-inl.h b/src/fstext/determinize-lattice-inl.h index 9a3f8169dab..b41deb980ee 100644 --- a/src/fstext/determinize-lattice-inl.h +++ b/src/fstext/determinize-lattice-inl.h @@ -149,13 +149,15 @@ template class LatticeStringRepository { void ConvertToVector(const Entry *entry, vector *out) const { size_t length = Size(entry); out->resize(length); - typename vector::iterator iter = out->end() - 1; + if (entry != NULL) { + typename vector::reverse_iterator iter = out->rbegin(); while (entry != NULL) { *iter = entry->i; entry = entry->parent; - --iter; + ++iter; } } + } const Entry *ConvertFromVector(const vector &vec) { const Entry *e = NULL; diff --git a/src/fstext/fstext-utils-test.cc b/src/fstext/fstext-utils-test.cc index b697e20a913..1233491baf6 100644 --- a/src/fstext/fstext-utils-test.cc +++ b/src/fstext/fstext-utils-test.cc @@ -103,6 +103,9 @@ template void TestSafeDeterminizeWrapper() { // also tests SafeDete int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols. cout << "Testing pre-determinize with "<AddSymbol(""); + delete sptr; + sptr = new SymbolTable("my-symbol-table"); vector