kaldi-math.cc
Go to the documentation of this file.
1 // base/kaldi-math.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Yanmin Qian;
4 // Saarland University; Jan Silovsky
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 #include "base/kaldi-math.h"
22 #ifndef _MSC_VER
23 #include <stdlib.h>
24 #include <unistd.h>
25 #endif
26 #include <string>
27 #include <mutex>
28 
29 namespace kaldi {
30 // These routines are tested in matrix/matrix-test.cc
31 
33  KALDI_ASSERT(n > 0);
34  n--;
35  n |= n >> 1;
36  n |= n >> 2;
37  n |= n >> 4;
38  n |= n >> 8;
39  n |= n >> 16;
40  return n+1;
41 }
42 
43 static std::mutex _RandMutex;
44 
45 int Rand(struct RandomState* state) {
46 #if !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
47  // On Windows and Cygwin, just call Rand()
48  return rand();
49 #else
50  if (state) {
51  return rand_r(&(state->seed));
52  } else {
53  std::lock_guard<std::mutex> lock(_RandMutex);
54  return rand();
55  }
56 #endif
57 }
58 
60  // we initialize it as Rand() + 27437 instead of just Rand(), because on some
61  // systems, e.g. at the very least Mac OSX Yosemite and later, it seems to be
62  // the case that rand_r when initialized with rand() will give you the exact
63  // same sequence of numbers that rand() will give if you keep calling rand()
64  // after that initial call. This can cause problems with repeated sequences.
65  // For example if you initialize two RandomState structs one after the other
66  // without calling rand() in between, they would give you the same sequence
67  // offset by one (if we didn't have the "+ 27437" in the code). 27437 is just
68  // a randomly chosen prime number.
69  seed = Rand() + 27437;
70 }
71 
72 bool WithProb(BaseFloat prob, struct RandomState* state) {
73  KALDI_ASSERT(prob >= 0 && prob <= 1.1); // prob should be <= 1.0,
74  // but we allow slightly larger values that could arise from roundoff in
75  // previous calculations.
76  KALDI_COMPILE_TIME_ASSERT(RAND_MAX > 128 * 128);
77  if (prob == 0) return false;
78  else if (prob == 1.0) return true;
79  else if (prob * RAND_MAX < 128.0) {
80  // prob is very small but nonzero, and the "main algorithm"
81  // wouldn't work that well. So: with probability 1/128, we
82  // return WithProb (prob * 128), else return false.
83  if (Rand(state) < RAND_MAX / 128) { // with probability 128...
84  // Note: we know that prob * 128.0 < 1.0, because
85  // we asserted RAND_MAX > 128 * 128.
86  return WithProb(prob * 128.0);
87  } else {
88  return false;
89  }
90  } else {
91  return (Rand(state) < ((RAND_MAX + static_cast<BaseFloat>(1.0)) * prob));
92  }
93 }
94 
95 int32 RandInt(int32 min_val, int32 max_val, struct RandomState* state) {
96  // This is not exact.
97  KALDI_ASSERT(max_val >= min_val);
98  if (max_val == min_val) return min_val;
99 
100 #ifdef _MSC_VER
101  // RAND_MAX is quite small on Windows -> may need to handle larger numbers.
102  if (RAND_MAX > (max_val-min_val)*8) {
103  // *8 to avoid large inaccuracies in probability, from the modulus...
104  return min_val +
105  ((unsigned int)Rand(state) % (unsigned int)(max_val+1-min_val));
106  } else {
107  if ((unsigned int)(RAND_MAX*RAND_MAX) >
108  (unsigned int)((max_val+1-min_val)*8)) {
109  // *8 to avoid inaccuracies in probability, from the modulus...
110  return min_val + ( (unsigned int)( (Rand(state)+RAND_MAX*Rand(state)))
111  % (unsigned int)(max_val+1-min_val));
112  } else {
113  KALDI_ERR << "rand_int failed because we do not support such large "
114  "random numbers. (Extend this function).";
115  }
116  }
117 #else
118  return min_val +
119  (static_cast<int32>(Rand(state)) % static_cast<int32>(max_val+1-min_val));
120 #endif
121 }
122 
123 // Returns poisson-distributed random number.
124 // Take care: this takes time proportional
125 // to lambda. Faster algorithms exist but are more complex.
126 int32 RandPoisson(float lambda, struct RandomState* state) {
127  // Knuth's algorithm.
128  KALDI_ASSERT(lambda >= 0);
129  float L = expf(-lambda), p = 1.0;
130  int32 k = 0;
131  do {
132  k++;
133  float u = RandUniform(state);
134  p *= u;
135  } while (p > L);
136  return k-1;
137 }
138 
139 void RandGauss2(float *a, float *b, RandomState *state) {
140  KALDI_ASSERT(a);
141  KALDI_ASSERT(b);
142  float u1 = RandUniform(state);
143  float u2 = RandUniform(state);
144  u1 = sqrtf(-2.0f * logf(u1));
145  u2 = 2.0f * M_PI * u2;
146  *a = u1 * cosf(u2);
147  *b = u1 * sinf(u2);
148 }
149 
150 void RandGauss2(double *a, double *b, RandomState *state) {
151  KALDI_ASSERT(a);
152  KALDI_ASSERT(b);
153  float a_float, b_float;
154  // Just because we're using doubles doesn't mean we need super-high-quality
155  // random numbers, so we just use the floating-point version internally.
156  RandGauss2(&a_float, &b_float, state);
157  *a = a_float;
158  *b = b_float;
159 }
160 
161 
162 } // end namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
#define M_PI
Definition: kaldi-math.h:44
bool WithProb(BaseFloat prob, struct RandomState *state)
Definition: kaldi-math.cc:72
kaldi::int32 int32
int32 RoundUpToNearestPowerOfTwo(int32 n)
Definition: kaldi-math.cc:32
int32 RandPoisson(float lambda, struct RandomState *state)
Definition: kaldi-math.cc:126
static std::mutex _RandMutex
Definition: kaldi-math.cc:43
float BaseFloat
Definition: kaldi-types.h:29
void RandGauss2(float *a, float *b, RandomState *state)
Definition: kaldi-math.cc:139
struct rnnlm::@11::@12 n
#define KALDI_ERR
Definition: kaldi-error.h:147
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95
#define KALDI_COMPILE_TIME_ASSERT(b)
Definition: kaldi-utils.h:131