MCPcopy Create free account
hub / github.com/Luce-Org/lucebox-hub / draw_from_weights

Function draw_from_weights

server/src/common/sampler.cpp:66–76  ·  view source on GitHub ↗

Draws a token from `cand`, whose .first fields are proportional probabilities (need not already sum to 1 or be sorted). `r_uniform` is a pre-drawn uniform in [0,1) supplied by the caller (drawn once per sample_logits call) so every path — GPU, GPU-assisted top_p, or CPU — consumes the same single RNG value.

Source from the content-addressed store, hash-verified

64// sample_logits call) so every path — GPU, GPU-assisted top_p, or CPU —
65// consumes the same single RNG value.
66int draw_from_weights(const std::vector<std::pair<float, int>> & cand, double r_uniform) {
67 double Z = 0.0;
68 for (auto & c : cand) Z += c.first;
69 const double r = r_uniform * Z;
70 double acc = 0.0;
71 for (auto & c : cand) {
72 acc += c.first;
73 if (r <= acc) return c.second;
74 }
75 return cand.back().second;
76}
77
78#ifdef DFLASH27B_HAVE_GPU_SAMPLER
79// Given probabilities the GPU already computed (penalties + softmax(temp)

Callers 2

sample_from_gpu_probsFunction · 0.85
sample_logitsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected