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.
| 64 | // sample_logits call) so every path — GPU, GPU-assisted top_p, or CPU — |
| 65 | // consumes the same single RNG value. |
| 66 | int 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) |
no outgoing calls
no test coverage detected