the chain over logits rows [row0, row0 + n) of lm_, written to tokout_[row0 ..]; row r's draw is Philox(seed, counter + r - row0)
| 3854 | |
| 3855 | // the chain over logits rows [row0, row0 + n) of lm_, written to tokout_[row0 ..]; row r's draw is Philox(seed, counter + r - row0) |
| 3856 | bool Window::sample_run(int row0, int n, const strata::kernels::SamplerParams& sp, const int32_t* hist_in, int hl, std::string& err) { |
| 3857 | const int Tn = n; |
| 3858 | const int64_t V = n_vocab_; |
| 3859 | constexpr uint32_t PARTS = 64; |
| 3860 | if (V > (int64_t) PARTS * 4096) { err = "metal sampler: the vocabulary is wider than 64 parts of 4096"; return false; } |
| 3861 | if (!scv_.handle) { |
| 3862 | scv_ = cx_->alloc(4 * (size_t) MAXT * PARTS * 64); |
| 3863 | sci_ = cx_->alloc(4 * (size_t) MAXT * PARTS * 64); |
| 3864 | } |
| 3865 | const bool greedy = sp.greedy || sp.temperature <= 0.0f; |
| 3866 | uint32_t flags = greedy ? 2u : 0u; |
| 3867 | // the logit bias: a device array of n_vocab floats (unified memory: read as it is, copied into the sampler's own buffer) |
| 3868 | if (sp.logit_bias != nullptr) { |
| 3869 | if (sbias_cap_ < 4 * (size_t) V) { // (an earlier request without a bias left the 16-byte stand-in) |
| 3870 | if (sbias_.handle) cx_->free(sbias_); |
| 3871 | sbias_ = cx_->alloc(4 * (size_t) V); |
| 3872 | sbias_cap_ = 4 * (size_t) V; |
| 3873 | } |
| 3874 | std::memcpy(sbias_.ptr, strata::gpu::host_view(sp.logit_bias), 4 * (size_t) V); // (a cudaMalloc'd array: the runtime's CPU view) |
| 3875 | flags |= 1u; |
| 3876 | } else if (!sbias_.handle) { |
| 3877 | sbias_ = cx_->alloc(16); |
| 3878 | sbias_cap_ = 16; |
| 3879 | } |
| 3880 | uint32_t pen_n = 0; |
| 3881 | if (hist_in != nullptr && hl > 0 && sp.penalty_last_n > 0) { |
| 3882 | pen_n = (uint32_t) std::min(sp.penalty_last_n, hl); |
| 3883 | const size_t need = 4 * (size_t) MAXT * (size_t) hl; |
| 3884 | if (shist_cap_ < need) { if (shist_.handle) cx_->free(shist_); shist_ = cx_->alloc(need); shist_cap_ = need; } |
| 3885 | std::memcpy(shist_.ptr, strata::gpu::host_view(hist_in), 4 * (size_t) Tn * (size_t) hl); |
| 3886 | flags |= 4u; |
| 3887 | } else if (!shist_.handle) { |
| 3888 | shist_ = cx_->alloc(16); shist_cap_ = 16; |
| 3889 | } |
| 3890 | struct SampArgs { |
| 3891 | uint32_t n_vocab, parts, k, hist_len, pen_n, min_keep, flags; |
| 3892 | float top_p, min_p, inv_t, rep, freq, pres; |
| 3893 | uint32_t seed_lo, seed_hi, ctr_lo, ctr_hi; |
| 3894 | } a{}; |
| 3895 | a.n_vocab = (uint32_t) V; |
| 3896 | a.parts = PARTS; |
| 3897 | a.k = greedy ? 1u : (uint32_t) ((sp.top_k > 0 && sp.top_k < 64) ? sp.top_k : 64); |
| 3898 | a.hist_len = (flags & 4u) ? (uint32_t) hl : 0u; |
| 3899 | a.pen_n = pen_n; |
| 3900 | a.min_keep = (uint32_t) std::max(1, sp.min_keep); |
| 3901 | a.flags = flags; |
| 3902 | a.top_p = sp.top_p; |
| 3903 | a.min_p = sp.min_p; |
| 3904 | a.inv_t = greedy ? 1.0f : 1.0f / sp.temperature; |
| 3905 | a.rep = sp.penalty_repeat; |
| 3906 | a.freq = sp.penalty_freq; |
| 3907 | a.pres = sp.penalty_present; |
| 3908 | a.seed_lo = (uint32_t) sp.seed; a.seed_hi = (uint32_t) (sp.seed >> 32); |
| 3909 | a.ctr_lo = (uint32_t) sp.counter; a.ctr_hi = (uint32_t) (sp.counter >> 32); |
| 3910 | mtl::Dispatch d1, d2; |
| 3911 | d1.p = P(lsmp_, "samp_part", {}, "samp_part"); |
| 3912 | const size_t cand_off = 4 * (size_t) row0 * PARTS * 64; |
| 3913 | d1.args = {mtl::Arg::B(lm_, 4 * (size_t) row0 * (size_t) V), mtl::Arg::B(sbias_), mtl::Arg::B(shist_), mtl::Arg::B(scv_, cand_off), mtl::Arg::B(sci_, cand_off), mtl::Arg::V(a)}; |