MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / LogSumExp

Function LogSumExp

external/sentencepiece/src/unigram_model.cc:47–59  ·  view source on GitHub ↗

Returns log(exp(x) + exp(y)). if init_mode is true, returns log(exp(y)) == y. log(\sum_i exp(a[i])) can be computed as for (int i = 0; i < a.size(); ++i) x = LogSumExp(x, a[i], i == 0);

Source from the content-addressed store, hash-verified

45// for (int i = 0; i < a.size(); ++i)
46// x = LogSumExp(x, a[i], i == 0);
47inline float LogSumExp(float x, float y, bool init_mode) {
48 if (init_mode) {
49 return y;
50 }
51 const float vmin = std::min(x, y);
52 const float vmax = std::max(x, y);
53 constexpr float kMinusLogEpsilon = 50;
54 if (vmax > vmin + kMinusLogEpsilon) {
55 return vmax;
56 } else {
57 return vmax + log(std::exp(static_cast<double>(vmin - vmax)) + 1.0);
58 }
59}
60
61// Returns a sample from a standard Gumbel distribution.
62// If U ~ U[0, 1], -log(-log U) ~ G(0,1)

Callers 2

ForwardAlgorithmMethod · 0.85
BackwardAlgorithmMethod · 0.85

Calls 2

minFunction · 0.85
maxFunction · 0.85

Tested by

no test coverage detected