diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index 2055fb22..f82b423d 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -62,7 +62,10 @@ public: /// The sampling the verify window's head applies (temperature / top_p / top_k / seed). Set per /// request; greedy by default. The sampling itself runs OUTSIDE the captured graph - its /// parameters would otherwise be baked forever - so this can change between requests freely. - void set_sampling(const strata::kernels::SamplerParams& sp) { sampling_ = sp; } + void set_sampling(const strata::kernels::SamplerParams& sp) { + sampling_ = sp; + draw_counter_ = 0; // each request's sampling starts its own draw sequence + } /// Keep the first `n_keep` (1..T) tokens of the last window; advances `ss.ple_prev` by them. bool commit(int n_keep, std::string& err); @@ -94,6 +97,7 @@ private: s.temperature = 0.0f; return s; }(); ///< greedy by default; per-request via set_sampling + uint64_t draw_counter_ = 0; ///< advanced by T on every sampled window; reset by set_sampling bool capture_commit(std::string& err); bool record_window(int T, cudaStream_t cs, std::string& err); diff --git a/src/core/verify.cpp b/src/core/verify.cpp index 35a73a21..7e7010cf 100644 --- a/src/core/verify.cpp +++ b/src/core/verify.cpp @@ -111,6 +111,9 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& hits_ = hits; head_ = head; max_t_ = max_t; + sampling_.greedy = true; // a fresh verifier samples greedily until set_sampling says otherwise + sampling_.temperature = 0.0f; + draw_counter_ = 0; if (max_t < 2 || max_t > strata::kernels::kVerifyMaxT || max_t > strata::kernels::cpu::MAXT) { err = "verify: the window must hold 2.." + std::to_string(strata::kernels::kVerifyMaxT) + " tokens"; return false; @@ -595,10 +598,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { return false; } } - SamplerParams sp; - sp.greedy = true; - sp.temperature = 0.0f; - sample_tokens(head_logits_, T, (int) n_vocab_, nullptr, 0, sp, m_out_, cs); + // the sampling is NOT recorded here: it runs host-side after the graph (run()), because + // its parameters change per request and a captured kernel would bake them forever } return true; } @@ -794,6 +795,18 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, const cudaError_t se = cudaStreamSynchronize(cs_); if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; } cudaStreamSynchronize(copy_); // no host function of this window may raise flag B in the next one + // ---- the head's sampling, host-side so its parameters are this call's own (a captured kernel + // would replay the same draws forever). The counter advances by the window's rows. + SamplerParams sp = sampling_; + sp.counter = draw_counter_; + draw_counter_ += (uint64_t) T; + sample_tokens(head_logits_, T, (int) n_vocab_, nullptr, 0, sp, m_out_, cs_); + const cudaError_t sm = cudaMemcpyAsync(h_out_, m_out_, (size_t) T * sizeof(int32_t), + cudaMemcpyDeviceToHost, cs_); + if (sm != cudaSuccess || cudaStreamSynchronize(cs_) != cudaSuccess) { + err = "verify: the head sampling failed"; + return false; + } for (int t = 0; t < T; ++t) out[t] = ((volatile int32_t*) h_out_)[t]; VDBG("window done\n"); ++windows; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index f730f90a..059cffd6 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2154,6 +2154,29 @@ int main(int argc, char** argv) { } char* endp = nullptr; const long long max_new = std::strtoll(line.c_str() + (geni ? 5 : 4), &endp, 10); + // optional sampling keys between max_new and the ids: temperature=F, top_p=F, top_k=N, + // seed=N (text requests only). Absent keys keep today's behavior: greedy. + float req_temperature = 0.0f, req_top_p = 1.0f; + int req_top_k = 20; // the sampler's own default; the sampled path REQUIRES top_k in 1..64 + unsigned long long req_seed = 0; + if (!geni && endp != nullptr) { + for (;;) { + while (*endp == ' ') ++endp; + const char* start = endp; + while (*endp != '\0' && *endp != ' ') ++endp; + if (endp == start) break; + const std::string tok(start, (size_t) (endp - start)); + const size_t eq = tok.find('='); + if (eq == std::string::npos) { endp = const_cast(start); break; } + const std::string key = tok.substr(0, eq); + const float fv = std::strtof(tok.c_str() + eq + 1, nullptr); + if (key == "temperature") req_temperature = fv; + else if (key == "top_p") req_top_p = fv; + else if (key == "top_k") req_top_k = std::atoi(tok.c_str() + eq + 1); + else if (key == "seed") req_seed = std::strtoull(tok.c_str() + eq + 1, nullptr, 10); + // unknown keys are skipped: the ids start at the first token without '=' + } + } std::string emb_path; if (geni && endp != nullptr) { while (*endp == ' ') ++endp; @@ -2386,6 +2409,16 @@ int main(int argc, char** argv) { return true; }; apply_pending(true); + // per-request sampling for the verify window's head (greedy when temperature is absent) + strata::kernels::SamplerParams req_sp; + req_sp.greedy = req_temperature <= 0.0f; + req_sp.temperature = req_temperature; + req_sp.top_p = req_top_p; + req_sp.top_k = req_top_k; + req_sp.seed = req_seed ? req_seed + : (unsigned long long) std::chrono::steady_clock::now().time_since_epoch().count(); + req_sp.counter = 0; + ver.set_sampling(req_sp); bool cancelled = false; tr("prompt start", n - 1); // The prompt is read in two parts when it has a turn boundary past `resume`: up to the last <|im_start|>