MCPcopy Create free account
hub / github.com/Niko1221/Strata / make_dist

Function make_dist

engine/src/split/src/session.cpp:40–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38}
39
40Dist make_dist(const std::vector<std::pair<float, int>>& cand, const Sampling& s) {
41 const int k = (s.top_k <= 0 || s.top_k > (int) cand.size()) ? (int) cand.size() : s.top_k;
42 int keep = k;
43 const double mx = cand[0].first;
44 if (s.top_p < 1.f) {
45 double sum = 0.0;
46 for (int i = 0; i < k; ++i) sum += std::exp((double) cand[i].first - mx);
47 double cum = 0.0;
48 for (int i = 0; i < k; ++i) {
49 cum += std::exp((double) cand[i].first - mx) / sum;
50 if (cum >= (double) s.top_p) {
51 keep = i + 1;
52 break;
53 }
54 }
55 }
56 if (s.min_p > 0.f) {
57 const float thresh = cand[0].first + std::log(s.min_p);
58 for (int i = 1; i < keep; ++i)
59 if (cand[i].first < thresh) {
60 keep = i;
61 break;
62 }
63 }
64 Dist d;
65 const double inv_t = 1.0 / s.temperature, smx = cand[0].first * inv_t;
66 double sum = 0.0;
67 for (int i = 0; i < keep; ++i) {
68 d.ids.push_back(cand[i].second);
69 d.p.push_back(std::exp(cand[i].first * inv_t - smx));
70 sum += d.p.back();
71 }
72 for (double& v : d.p) v /= sum;
73 return d;
74}
75
76int draw(const Dist& d, double u) {
77 double cum = 0.0;

Callers 5

draft_oneMethod · 0.70
windowMethod · 0.70
draft_batchedMethod · 0.70
sample_tokenFunction · 0.70
generateMethod · 0.70

Calls 4

push_backMethod · 0.80
logFunction · 0.50
sizeMethod · 0.45
backMethod · 0.45

Tested by

no test coverage detected