| 111 | } |
| 112 | |
| 113 | void check_sampling(const Sampling& s) { |
| 114 | auto bad = [](const std::string& what) { throw std::runtime_error("sampling: " + what); }; |
| 115 | if (!std::isfinite(s.temperature) || s.temperature < 0.f) bad("temperature must be a finite number >= 0"); |
| 116 | if (s.temperature == 0.f) return; // greedy: the other values are not used |
| 117 | if (!(s.top_p > 0.f && s.top_p <= 1.f)) bad("top_p must be in (0, 1]"); |
| 118 | if (!(s.min_p >= 0.f && s.min_p <= 1.f)) bad("min_p must be in [0, 1]"); |
| 119 | if (s.top_k < 0) bad("top_k must be >= 0"); |
| 120 | if (!(std::isfinite(s.pen_repeat) && s.pen_repeat > 0.f)) bad("penalty_repeat must be a finite number > 0"); |
| 121 | if (!std::isfinite(s.pen_freq) || !std::isfinite(s.pen_present)) bad("penalty_freq and penalty_present must be finite"); |
| 122 | if (s.pen_last_n < 0) bad("penalty_last_n must be >= 0"); |
| 123 | if (penalizes(s) && s.pen_last_n > Engine::PEN_CAP) bad("penalty_last_n above " + std::to_string(Engine::PEN_CAP)); |
| 124 | } |
| 125 | |
| 126 | int sample_token(const std::vector<std::pair<float, int>>& cand, const Sampling& s, std::mt19937_64& rng) { |
| 127 | std::uniform_real_distribution<double> unif(0.0, 1.0); |