diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 3c7ee34d..3fa7e0f3 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -215,8 +215,15 @@ assistant turn and every 16K prompt tokens). A checkpoint is used only when the and pictures. Engine options: `--prompt-cache N` (0 = off), `--prompt-cache-every N`, `--turn-token ID`. **Current limits (v1):** one request at a time, and one conversation cached at a time (switching between two chats -re-reads the other one); greedy decoding (temperature is ignored); images only when set up with them (below); no -video. +re-reads the other one); images only when set up with them (below); no video. **Temperature / top_p / top_k / min_p / +seed** are honored per request (OpenAI and Anthropic fields); with the default adaptive expert tier a sampled result +is not reproducible run to run - for seed-reproducible output add `--adapt-every 100000` (static residency) to the +engine arguments. The run config's optional `sampling` block sets the defaults for requests that leave the fields out +(`"sampling": {"temperature": 1.0, "top_p": 0.95, "top_k": 20}`); a request's own fields always win, and with no +block at all a request without sampling keys decodes greedy. The penalties (`presence_penalty`, `frequency_penalty`, +`repetition_penalty`, with `penalty_last_n` capping how many recent tokens they count over, default 64 when any +penalty is set) ride the same path; they count the tokens the request has consumed, so a repetition penalty +suppresses what the model itself just said, not the prompt alone. --- diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp index 384ccc08..7bdf6ae1 100644 --- a/include/strata/core/verify.hpp +++ b/include/strata/core/verify.hpp @@ -24,6 +24,7 @@ #include "strata/core/expert_source.hpp" #include "strata/core/layer.hpp" #include "strata/core/session.hpp" +#include "strata/kernels/sampler.hpp" #include @@ -58,6 +59,22 @@ public: /// One window: `tokens[0..T)` at positions pos0.., the pool served per layer; `out[t]` = argmax after token t. /// The PLE rows are gathered here from `ss.ple_prev` and the tokens. Captures the T-token graph on first use. bool run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out, std::string& err); + /// 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; + draw_counter_ = 0; // each request's sampling starts its own draw sequence + } + + /// The penalty-history row for `sampling_.penalty_last_n`: ONE row of `history_len` int32 slots, most + /// recent token LAST, unused front slots -1 (the kernel reads only the tail window). Null disables the + /// penalties entirely - the neutral run's sampling call is byte-for-byte what it was. The engine + /// re-uploads the request's tail before every window; the pointer must stay alive across the request. + void set_history(const int32_t* history, int history_len) { + hist_d_ = history; + hist_len_ = history_len; + } /// 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); @@ -83,6 +100,15 @@ public: private: bool capture(int T, std::string& err); + strata::kernels::SamplerParams sampling_ = [] { + strata::kernels::SamplerParams s; + s.greedy = true; + 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 + const int32_t* hist_d_ = nullptr; ///< penalty-history row (set_history); null = no penalties apply + int hist_len_ = 0; bool capture_commit(std::string& err); bool record_window(int T, cudaStream_t cs, std::string& err); diff --git a/include/strata/kernels/sampler.hpp b/include/strata/kernels/sampler.hpp index 32a5ded9..d528e363 100644 --- a/include/strata/kernels/sampler.hpp +++ b/include/strata/kernels/sampler.hpp @@ -1,12 +1,10 @@ // include/strata/kernels/sampler.hpp - the sampler chain, host-callable (P2.S2). // -// penalties -> top_k -> top_p -> temperature -> pick +// penalties -> top_k -> min_p -> top_p -> temperature -> penalties -> pick // -// The ORDER is specified in docs/sampling.md, transcribed from llama.cpp's own chain. Two facts there are -// easy to get backwards: temperature comes AFTER the truncation filters, and penalties come AFTER temperature. -// -// PENALTIES ARE NOT IMPLEMENTED YET: they need the token history, which is not in this signature, so a caller -// passing a non-zero `penalty_last_n` gets a loud failure rather than a silently unpenalised sample. +// The ORDER is specified in docs/sampling.md, transcribed from llama.cpp's own chain. Three facts there are +// easy to get backwards: temperature comes AFTER the truncation filters, penalties come AFTER temperature as +// well as before the selection, and min_p cuts BEFORE top_p (both on the pre-temperature logits). #pragma once #include @@ -16,6 +14,7 @@ namespace strata::kernels { struct SamplerParams { int top_k = 20; // 0 disables the filter float top_p = 0.95f; // 1.0 disables the filter + float min_p = 0.0f; // 0 disables; keeps tokens with p >= min_p * p_max (a prefix of the top_k list) float temperature = 1.0f; // <= 0 means greedy int min_keep = 1; // top_p keeps at least this many int penalty_last_n = 0; // 0 disables; the window over `history` to count occurrences in diff --git a/serve/server.py b/serve/server.py index 3e5755d2..4f968094 100644 --- a/serve/server.py +++ b/serve/server.py @@ -73,7 +73,12 @@ class MockEngine: class StrataEngine: """The resident engine: `strata --serve` reads `GEN ` lines and streams `T ` lines, then - `DONE ...`. Requests are serialized by the service's FIFO, so one pipe is enough.""" + `DONE ...`. Requests are serialized by the service's FIFO, so one pipe is enough. + + Per-request sampling rides the same line as engine-side keys between max_new and the ids + (`temperature=F top_p=F top_k=N seed=N`, the engine's own spelling). An absent temperature keeps the + engine's default, which is greedy; `temperature=0` means the same thing, so it is not forwarded. + """ def __init__(self, exe: str, args: list[str], cwd: str | None = None, log: str | None = None, env: dict | None = None): @@ -105,11 +110,51 @@ class StrataEngine: self.last = {"generated": int(f[1]), "prompt_tokens": int(f[2]), "prompt_ms": float(f[3]), "decode_ms": float(f[4]), "finish": f[5]} + @staticmethod + def sampling_keys(sampling: dict) -> str: + keys = "" + t = sampling.get("temperature") + if isinstance(t, (int, float)) and float(t) > 0.0: + keys += f" temperature={float(t)!r}" + tp = sampling.get("top_p") + if isinstance(tp, (int, float)) and float(tp) < 1.0: + keys += f" top_p={float(tp)!r}" + tk = sampling.get("top_k") + if isinstance(tk, int) and 1 <= tk <= 64: + keys += f" top_k={tk}" # the engine's sampled path takes 1..64; outside it keeps its 20 + mp = sampling.get("min_p") + if isinstance(mp, (int, float)) and 0.0 < float(mp) <= 1.0: + keys += f" min_p={float(mp)!r}" + rp = sampling.get("repetition_penalty") + rp_on = isinstance(rp, (int, float)) and float(rp) != 1.0 + pf = sampling.get("frequency_penalty") + pf_on = isinstance(pf, (int, float)) and float(pf) != 0.0 + pp = sampling.get("presence_penalty") + pp_on = isinstance(pp, (int, float)) and float(pp) != 0.0 + if rp_on: + keys += f" penalty_repeat={float(rp)!r}" + if pf_on: + keys += f" penalty_freq={float(pf)!r}" + if pp_on: + keys += f" penalty_present={float(pp)!r}" + if rp_on or pf_on or pp_on: + # a penalty without a window counts over nothing: the engine's default is the last 64 tokens + pln = sampling.get("penalty_last_n") + if isinstance(pln, int) and not isinstance(pln, bool) and pln > 0: + keys += f" penalty_last_n={pln}" + else: + keys += " penalty_last_n=64" + seed = sampling.get("seed") + if isinstance(seed, int) and seed > 0: + keys += f" seed={seed}" + return keys + def generate(self, ids, max_new, sampling, cancel, embeddings=None): """Yields token ids, and None as a heartbeat every 10 s while the engine is quiet (reading a long prompt): the HTTP layer turns it into an SSE comment, which keeps clients' watchdogs calm and notices a client that has gone. A consumer that stops early (or `cancel`) makes the engine STOP, so it does not run to max_new.""" - head = f"GENI {int(max_new)} {embeddings}" if embeddings else f"GEN {int(max_new)}" + head = f"GENI {int(max_new)} {embeddings}" if embeddings else \ + f"GEN {int(max_new)}{self.sampling_keys(sampling or {}) if not embeddings else ''}" self.proc.stdin.write(f"{head} {','.join(str(int(t)) for t in ids)}\n") self.proc.stdin.flush() done = False @@ -313,8 +358,9 @@ class Detokenizer: class Service: def __init__(self, engine: Engine, tokenizer, template: ChatTemplate, model_name: str = "qwen3.8-flash-next", - vision: Vision | None = None): + vision: Vision | None = None, sampling_defaults: dict | None = None): self.engine, self.tok, self.template, self.model, self.vision = engine, tokenizer, template, model_name, vision + self.sampling_defaults = dict(sampling_defaults or {}) # the run config's `sampling` block self.fifo = threading.Lock() self.embeddings = threading.local() # the current request's image embeddings file (GENI) self.api_key = "" # when set, /v1/* needs it (Bearer or x-api-key) @@ -402,6 +448,9 @@ class Service: def run(self, ids, thinking, tools, max_new, sampling, cancel) -> Iterator[tuple[str, object]]: """Yields ("event", Event) as text arrives, then ("done", {"finish": .., "completion_tokens": ..}).""" + if self.sampling_defaults: # config defaults; the request's own fields win (explicit 0 stays greedy) + req_values = {k: v for k, v in (sampling or {}).items() if v is not None} + sampling = {**self.sampling_defaults, **req_values} parser = OutputParser(thinking=thinking, tools=tools, stream_tools=True) detok, n, finish = Detokenizer(self.tok), 0, "length" raw_ids = [] # every generated id (STRATA_DEBUG: dump raw model text) @@ -771,6 +820,59 @@ def serve(svc: Service, host="127.0.0.1", port=8095) -> ThreadingHTTPServer: return httpd +def sampling_defaults_from_config(cfg: dict) -> dict: + """The run config's optional `sampling` block: defaults for the sampling fields a request leaves out, so + a plain client gets configured sampling instead of greedy. Supported: temperature, top_p, top_k, min_p, + presence_penalty, repetition_penalty, frequency_penalty, penalty_last_n, seed. The request's own fields + always win - an explicit temperature=0 still means greedy, a field set to null falls back to the default. + A bad value refuses to start the server (a typo'd config should not quietly change sampling); unknown keys + are named at startup and ignored.""" + out = {} + for key, value in (cfg.get("sampling") or {}).items(): + if value is None: + continue + number = isinstance(value, (int, float)) and not isinstance(value, bool) + if key == "temperature": + if not number or value < 0: + raise SystemExit(f"[strata] config sampling.temperature={value!r}: expected a number >= 0 (0 = greedy)") + out[key] = float(value) + elif key == "top_p": + if not number or not 0 < value <= 1: + raise SystemExit(f"[strata] config sampling.top_p={value!r}: expected 0 < top_p <= 1") + out[key] = float(value) + elif key == "min_p": + if not number or not 0 <= value <= 1: + raise SystemExit(f"[strata] config sampling.min_p={value!r}: expected 0 <= min_p <= 1") + out[key] = float(value) + elif key == "top_k": + if not number or value != int(value) or not 1 <= value <= 64: + raise SystemExit(f"[strata] config sampling.top_k={value!r}: the sampled path takes an integer 1..64") + out[key] = int(value) + elif key == "presence_penalty": + if not number or value < 0: + raise SystemExit(f"[strata] config sampling.presence_penalty={value!r}: expected a number >= 0") + out[key] = float(value) + elif key == "frequency_penalty": + if not number or value < 0: + raise SystemExit(f"[strata] config sampling.frequency_penalty={value!r}: expected a number >= 0") + out[key] = float(value) + elif key == "repetition_penalty": + if not number or value <= 0: + raise SystemExit(f"[strata] config sampling.repetition_penalty={value!r}: expected a number > 0 (1 = off)") + out[key] = float(value) + elif key == "penalty_last_n": + if not number or value != int(value) or value < 0: + raise SystemExit(f"[strata] config sampling.penalty_last_n={value!r}: expected a non-negative integer") + out[key] = int(value) + elif key == "seed": + if not number or value != int(value) or value <= 0: + raise SystemExit(f"[strata] config sampling.seed={value!r}: expected a positive integer") + out[key] = int(value) + else: + print(f"[strata] config sampling.{key}={value!r}: unknown key, ignored", flush=True) + return out + + def main() -> int: ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--engine", choices=["mock", "strata"], default="mock") @@ -811,6 +913,10 @@ def main() -> int: ap.error("--engine strata needs --config") vision = None env = child_env(cfg) + sampling_defaults = sampling_defaults_from_config(cfg) + if sampling_defaults: + pretty = ", ".join(f"{k}={v}" for k, v in sampling_defaults.items()) + print(f"[strata] sampling defaults from the config: {pretty}", flush=True) if cfg.get("vision"): print("loading the vision encoder ...", flush=True) vision = Vision(cfg["vision"], log=open(cfg["log"], "a", encoding="utf-8") if cfg.get("log") else None, @@ -818,11 +924,12 @@ def main() -> int: print("loading the model (the first start takes a minute or two) ...", flush=True) engine = StrataEngine(cfg["exe"], cfg["args"], cwd=cfg.get("cwd"), log=cfg.get("log"), env=env) else: - engine, vision = MockEngine(tok, a.script), None + engine, vision, sampling_defaults = MockEngine(tok, a.script), None, {} # the model's own chat template (exported with its tokenizer), else the original model's tpl = tpath / "chat_template.jinja" svc = Service(engine, tok, ChatTemplate(tpl if tpl.exists() else ROOT / "serve/chat_template.jinja"), - model_name=cfg.get("model_name", "qwen3.8-flash-next"), vision=vision) + model_name=cfg.get("model_name", "qwen3.8-flash-next"), vision=vision, + sampling_defaults=sampling_defaults) svc.api_key = a.api_key or cfg.get("api_key", "") httpd = serve(svc, host=a.host, port=a.port) print(f"ready: http://{a.host}:{a.port}/v1 (OpenAI: /v1/chat/completions, Anthropic: /v1/messages, " diff --git a/src/core/verify.cpp b/src/core/verify.cpp index abb22e4a..e4f8bc63 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; @@ -594,10 +597,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; } @@ -793,6 +794,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_, hist_d_, hist_len_, 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/kernels/cuda/sampler.cu b/src/kernels/cuda/sampler.cu index 3e1daaf2..9a4eecde 100644 --- a/src/kernels/cuda/sampler.cu +++ b/src/kernels/cuda/sampler.cu @@ -1,6 +1,6 @@ // src/kernels/cuda/sampler.cu - P2.S2: the sampler chain, in the order docs/sampling.md settles. // -// penalties -> top_k -> top_p -> temperature -> pick +// penalties -> top_k -> min_p -> top_p -> temperature -> penalties -> pick // // THE ORDER IS THE WHOLE CONTENT OF THIS FILE. `docs/sampling.md` transcribes it from llama.cpp's own chain // (`common/sampling.cpp` L357/360/375/381/399) and the two facts that are easy to get backwards are that @@ -9,10 +9,9 @@ // valid token, so only a comparison at the distribution level can tell them apart; the parity test does that // explicitly by running the wrong order and requiring it to differ. // -// Everything happens on the logits of ONE token in one thread. The vocab is 248,320, which is far too large -// to sort per token on a naive path, so `top_k` uses `k` passes of a maximum scan - 20 x 248,320 = 5.0M -// comparisons per token. Phase 3's note ("sort-free sampler: top-k = 20 makes this easy") is the optimisation; -// this is the correctness version. +// Both kernels put ONE BLOCK per token over the vocabulary: `sampler_greedy_kernel` is the plain argmax, +// `sampler_kernel` runs the sampled chain as `top_k` block-argmax rounds followed by the top_p / temperature / +// draw chain (its header says why the selection must be parallel and why the tie rule keeps the semantics). #include "strata/kernels/sampler.hpp" #include @@ -102,12 +101,32 @@ __global__ void sampler_greedy_kernel(const float* __restrict__ logits, int n_vo hrow += history_len - hlen; // the window is the TAIL } + // PENALTY MEMBERSHIP AS A BITMAP. The history touches at most `hlen` tokens of a quarter-million + // vocabulary, but the naive `history_count` per candidate per argmax round costs O(k x n_vocab x hlen) + // integer compares (~318 M per token at k=20, hlen=64 - measured 45 -> 31 tok/s on a real workload). + // A shared bitmap gives an O(1) membership test, and only the (at most hlen) hits pay the count scan; + // the counts - and therefore every sampled value - are exactly what the per-candidate scan produced. + extern __shared__ unsigned int penal_bits[]; + const int bits_words = (int) ((n_vocab + 31) / 32); + const bool use_bits = hrow != nullptr && bits_words > 0; + if (use_bits) { + for (int w = threadIdx.x; w < bits_words; w += blockDim.x) penal_bits[w] = 0u; + __syncthreads(); + for (int i = threadIdx.x; i < hlen; i += blockDim.x) + if (hrow[i] >= 0) atomicOr(&penal_bits[hrow[i] >> 5], 1u << (hrow[i] & 31)); + __syncthreads(); + } + auto hit_count = [&](int v) -> int { + if (!use_bits || !(penal_bits[v >> 5] & (1u << (v & 31)))) return 0; + return history_count(hrow, hlen, v); + }; + // `n_vocab` is the "no candidate" index: it loses every comparison to a real one, so a thread with no // elements contributes nothing rather than contributing a bogus zero. float bv = __int_as_float(0xff800000); // -inf int best = n_vocab; for (int v = threadIdx.x; v < n_vocab; v += blockDim.x) { - const float s = apply_penalties(l[v], hrow ? history_count(hrow, hlen, v) : 0, p); + const float s = apply_penalties(l[v], hit_count(v), p); if (s > bv) { bv = s; best = v; } } for (int off = 16; off > 0; off >>= 1) { @@ -134,47 +153,32 @@ __global__ void sampler_greedy_kernel(const float* __restrict__ logits, int n_vo } } +/// **THE SAMPLED PATH, ONE BLOCK PER TOKEN.** The kernel below replaced a version that ran the whole chain +/// in ONE THREAD per token (`<<>>`, so a 4-token window fielded four threads): `top_k` alone +/// was `k` sequential scans of the vocabulary with an inner sweep over the already-taken list - 20 x 248,320 +/// iterations of dependent work on one SM - and a verify window measured **1.6 s in the sampler**, which made +/// every temperature-bearing request ~30x slower than a greedy one. The selection is `k` argmax rounds, and +/// an argmax over the vocabulary parallelises exactly like `sampler_greedy_kernel` (block over the vocab), so +/// the rounds run back to back inside a block-per-token launch: the per-token cost falls to +/// `k x n_vocab / 1024` plus `k` block reductions. +/// +/// THE SEMANTICS ARE THE SERIAL ONES, EXACTLY. Each round's argmax resolves ties to the LOWEST index (the +/// serial scan's strict `>` keeps the first maximum it meets), so the kept sequence - both its set and its +/// order - is unchanged; `top_p`'s cut reads that order in double arithmetic as before; temperature and the +/// Philox draw apply after the cut. `sampler_parity` pins all of it against the host reference. __global__ void sampler_kernel(const float* __restrict__ logits, int n_vocab, int n_tokens, const int* __restrict__ history, int history_len, const SamplerParams p, int* __restrict__ out) { - const int t = blockIdx.x * blockDim.x + threadIdx.x; + const int t = blockIdx.x; if (t >= n_tokens) return; - - // Work on a private copy: the chain mutates the distribution and the caller's logits are shared with the - // next stage. A 248,320-float allocation per thread is far too much for shared memory, so this uses a - // two-pass approach instead - first find the keep set, then normalise over it - and never materialises a - // second full-size buffer. const float* l = logits + (size_t) t * n_vocab; - // Temperature is needed by BOTH paths below, so it is computed here; the chain still APPLIES it after + // Temperature is needed by BOTH stages below, so it is computed here; the chain still APPLIES it after // the truncation filters - the survivors are chosen on the raw logits and only then scaled. const float inv_t = p.temperature > 0.0f ? 1.0f / p.temperature : 0.0f; - // ---- penalties: NOT IMPLEMENTED, and the ORDER they must take is transcribed here so the next person - // does not have to guess it. `llama_sampler_penalties_apply` (src/llama-sampler.cpp) runs AFTER - // temperature in the chain (L381 against L375), and for every token seen in the last `penalty_last_n`: - // - // if (logit <= 0) logit *= penalty_repeat; else logit /= penalty_repeat; - // logit -= float(count) * penalty_freq + float(count > 0) * penalty_present; - // - // TWO details there are counter-intuitive. The repeat penalty MULTIPLIES for non-positive logits and - // DIVIDES for positive ones - the source notes the paper only ever divided, "but that would cause tokens - // with negative logits to become more likely, which is obviously wrong". And the presence penalty rides - // on `float(count > 0)`, a BOOLEAN cast to float, not the count - so it applies once however many times - // the token appeared. Both are the kind of thing a plausible implementation gets wrong while still - // producing valid tokens. - // - // It needs the token history, which is not in this signature, so a caller passing a non-zero - // `penalty_last_n` gets a loud failure rather than a silently unpenalised sample. - // ---- top_k: k passes of a maximum scan, skipping the already-taken - // ---- top_p: cumulative mass over the survivors in descending order - // The two are done together in one selection pass so the survivors and their mass are consistent. - // GREEDY IS THE GLOBAL ARGMAX, WHATEVER THE FILTERS SAY - so it is computed directly and the filter chain - // is skipped entirely. No filter can remove the maximum: top_k keeps the k largest, top_p keeps a prefix - // of the descending order that always contains the largest (min_keep >= 1), and temperature is monotonic so - // it cannot reorder anything. That also makes greedy INDEPENDENT OF THE ORDER, which is why the ordering - // test cannot use it - see sampler_parity.cpp. - // The penalty window is the last `penalty_last_n` entries of this row's history. + // The penalty window is the last `penalty_last_n` entries of this row's history (disabled at this + // launch: `sample_tokens` refuses a non-zero `penalty_last_n` without a history buffer). const int* hrow = history ? history + (size_t) t * history_len : nullptr; int hlen = 0; if (hrow) { @@ -183,109 +187,117 @@ __global__ void sampler_kernel(const float* __restrict__ logits, int n_vocab, in hrow += history_len - hlen; // the window is the TAIL } - if (p.greedy || p.temperature <= 0.0f) { - // Greedy is the global argmax ONLY when no penalty is active: a penalty can lower a token enough to - // change the maximum, so the argmax has to be taken over the penalised logits, not the raw ones. - // - // *** TEMPERATURE MUST NOT BE APPLIED HERE. THIS WAS A REAL BUG. *** - // This branch used to read `apply_penalties(l[v] * inv_t, ...)`. `inv_t` is 0.0f whenever - // temperature <= 0 (see above), so at temperature 0 EVERY logit became 0.0f and the argmax returned - // index 0 - the sampler emitted token 0 forever, whatever the model predicted. OpenAI clients send - // `temperature: 0` for greedy decoding, so this was reachable from any ordinary client. - // It is also pointless even when inv_t is non-zero: scaling by a positive constant is monotonic and - // cannot reorder the argmax, which is exactly what the note above this block already says. - int best = 0; - float bv = apply_penalties(l[0], hrow ? history_count(hrow, hlen, 0) : 0, p); - for (int v = 1; v < n_vocab; ++v) { - const float s = apply_penalties(l[v], hrow ? history_count(hrow, hlen, v) : 0, p); - if (s > bv) { bv = s; best = v; } - } - out[t] = best; - return; + // the membership bitmap, as in `sampler_greedy_kernel` - see the cost note there + extern __shared__ unsigned int penal_bits[]; + const int bits_words = (int) ((n_vocab + 31) / 32); + const bool use_bits = hrow != nullptr && bits_words > 0; + if (use_bits) { + for (int w = threadIdx.x; w < bits_words; w += blockDim.x) penal_bits[w] = 0u; + __syncthreads(); + for (int i = threadIdx.x; i < hlen; i += blockDim.x) + if (hrow[i] >= 0) atomicOr(&penal_bits[hrow[i] >> 5], 1u << (hrow[i] & 31)); + __syncthreads(); } + auto hit_count = [&](int v) -> int { + if (!use_bits || !(penal_bits[v >> 5] & (1u << (v & 31)))) return 0; + return history_count(hrow, hlen, v); + }; const int KMAX = 64; - int keep_ids[KMAX]; - float keep_logit[KMAX]; - int k = p.top_k > 0 ? (p.top_k < KMAX ? p.top_k : KMAX) : KMAX; - if (p.top_k <= 0) k = 0; // 0 means "no top-k filter" -> handled by the mask below - + int k = p.top_k > 0 ? (p.top_k < KMAX ? p.top_k : KMAX) : 0; if (k <= 0) { - std::printf("sampler: the sampled path needs top_k in 1..%d (got %d); greedy needs no filters\n", KMAX, p.top_k); + if (threadIdx.x == 0) + std::printf("sampler: the sampled path needs top_k in 1..%d (got %d); greedy needs no filters\n", + KMAX, p.top_k); return; // leave out[t] unwritten rather than returning an uninitialised token } - int n_keep = 0; - { - for (int i = 0; i < k; ++i) { - int best = -1; - float bv = 0.0f; - for (int v = 0; v < n_vocab; ++v) { - bool taken = false; - for (int j = 0; j < i; ++j) if (keep_ids[j] == v) { taken = true; break; } - if (taken) continue; - if (best < 0 || l[v] > bv) { best = v; bv = l[v]; } + + // ---- top_k: k rounds of a block argmax over the not-yet-taken. `sel_*` holds the kept ids and their + // raw logits in selection order: descending by value, ties to the lower index, which is the order the + // top_p cut below is defined over. + __shared__ int sel_ids[KMAX]; + __shared__ float sel_logit[KMAX]; + __shared__ float sv[32]; + __shared__ int si[32]; + for (int i = 0; i < k; ++i) { + // `n_vocab` is the "no candidate" index: it loses every comparison to a real one (same convention as + // the greedy kernel, whose tie rule this reduction shares). + float bv = __int_as_float(0xff800000); // -inf + int best = n_vocab; + for (int v = threadIdx.x; v < n_vocab; v += blockDim.x) { + bool taken = false; + for (int j = 0; j < i; ++j) if (sel_ids[j] == v) { taken = true; break; } + if (taken) continue; + const float s = apply_penalties(l[v], hit_count(v), p); + if (s > bv) { bv = s; best = v; } + } + for (int off = 16; off > 0; off >>= 1) { + const float ov = __shfl_down_sync(0xFFFFFFFFu, bv, off); + const int oi = __shfl_down_sync(0xFFFFFFFFu, best, off); + if (ov > bv || (ov == bv && oi < best)) { bv = ov; best = oi; } + } + const int warp = (int) (threadIdx.x >> 5), lane = (int) (threadIdx.x & 31); + if (lane == 0) { sv[warp] = bv; si[warp] = best; } + __syncthreads(); + if (warp == 0) { + const int nw = (int) ((blockDim.x + 31) >> 5); + float wv = lane < nw ? sv[lane] : __int_as_float(0xff800000); + int wi = lane < nw ? si[lane] : n_vocab; + for (int off = 16; off > 0; off >>= 1) { + const float ov = __shfl_down_sync(0xFFFFFFFFu, wv, off); + const int oi = __shfl_down_sync(0xFFFFFFFFu, wi, off); + if (ov > wv || (ov == wv && oi < wi)) { wv = ov; wi = oi; } } - keep_ids[i] = best; - keep_logit[i] = bv; - } - n_keep = k; - - // ---- top_p over the survivors, in descending order (which the selection above already produced) - if (p.top_p < 1.0f) { - // softmax over the keep set for the cumulative mass - float mx = keep_logit[0]; - for (int i = 1; i < n_keep; ++i) mx = fmaxf(mx, keep_logit[i]); - double sum = 0.0; - for (int i = 0; i < n_keep; ++i) sum += exp((double) keep_logit[i] - (double) mx); - double cum = 0.0; - int cut = n_keep; - for (int i = 0; i < n_keep; ++i) { - cum += exp((double) keep_logit[i] - (double) mx) / sum; - if (cum >= (double) p.top_p) { cut = i + 1; break; } - } - if (cut < p.min_keep) cut = p.min_keep < n_keep ? p.min_keep : n_keep; - n_keep = cut; + if (lane == 0) { sel_ids[i] = (wi < n_vocab) ? wi : 0; sel_logit[i] = wv; } } + __syncthreads(); } + // ---- min_p: keep the descending prefix whose probability is at least `min_p` of the top token's. The + // kept list is in selection order (descending), so the survivors are a PREFIX and the cut composes with + // top_p's below. In logit space the threshold is `sel_logit[0] + logf(min_p)` - equivalent to + // `p >= min_p * p_max` without the overflow an exp of raw logits risks. 0 disables, and the head itself + // always survives (`expf(0) == 1 >= min_p` for min_p in 0..1), so the count never reaches zero. + int n_minp = k; + if (p.min_p > 0.0f) { + const float thresh = sel_logit[0] + logf(p.min_p); + for (int i = 0; i < k; ++i) + if (sel_logit[i] < thresh) { n_minp = i; break; } + } - if (p.greedy || p.temperature <= 0.0f) { - // DEAD CODE AS WRITTEN: the identical condition at the top of this function already returned, so this - // block cannot be reached. It is kept and corrected rather than deleted because if that early return - // is ever narrowed, this is the path that would run - and it carried the same `* inv_t` bug (every - // logit 0.0f at temperature 0, so `best` would always be `keep_ids[0]`). Temperature is monotonic and - // cannot reorder a maximum, so it must not appear here at all. - // argmax over the SURVIVORS, ties by smallest id (the same convention as the router and top_k) - int best = keep_ids[0]; - float bv = keep_logit[0]; - for (int i = 1; i < n_keep; ++i) { - const float v = keep_logit[i]; - if (v > bv) { bv = v; best = keep_ids[i]; } + // ---- top_p over the survivors, in descending order (which the selection produced), then temperature and + // one Philox draw. Every thread computes the same chain redundantly over `sel_*` - the arithmetic is the + // serial kernel's, instruction for instruction - so they agree on `pick` and thread 0 writes it. + int n_keep = n_minp; + float mx = sel_logit[0]; + for (int i = 1; i < n_minp; ++i) mx = fmaxf(mx, sel_logit[i]); + if (p.top_p < 1.0f) { + double sum = 0.0; + for (int i = 0; i < n_minp; ++i) sum += exp((double) sel_logit[i] - (double) mx); + double cum = 0.0; + int cut = n_minp; + for (int i = 0; i < n_minp; ++i) { + cum += exp((double) sel_logit[i] - (double) mx) / sum; + if (cum >= (double) p.top_p) { cut = i + 1; break; } } - out[t] = best; - return; + if (cut < p.min_keep) cut = p.min_keep < n_minp ? p.min_keep : n_minp; + n_keep = cut; } - - // ---- pick: temperature, then penalties, then softmax over the survivors, then one uniform draw. - // The ORDER is the chain's: the survivors were chosen on the RAW logits, temperature scales them, and the - // penalty is applied to the scaled value - which is why the `logit <= 0` branch inside the penalty sees a - // temperature-scaled logit and not the model's own. - for (int i = 0; i < n_keep; ++i) { - keep_logit[i] = apply_penalties(keep_logit[i] * inv_t, - hrow ? history_count(hrow, hlen, keep_ids[i]) : 0, p); - } - float mx = keep_logit[0]; - for (int i = 1; i < n_keep; ++i) mx = fmaxf(mx, keep_logit[i]); + auto scaled = [&](int i) { + return apply_penalties(sel_logit[i] * inv_t, hit_count(sel_ids[i]), p); + }; + float smx = scaled(0); + for (int i = 1; i < n_keep; ++i) smx = fmaxf(smx, scaled(i)); double sum = 0.0; - for (int i = 0; i < n_keep; ++i) sum += exp((double) keep_logit[i] - (double) mx); + for (int i = 0; i < n_keep; ++i) sum += exp((double) scaled(i) - (double) smx); const float u = philox_uniform(p.seed, p.counter + (uint64_t) t); double cum = 0.0; - int pick = keep_ids[n_keep - 1]; + int pick = sel_ids[n_keep - 1]; for (int i = 0; i < n_keep; ++i) { - cum += exp((double) keep_logit[i] - (double) mx) / sum; - if ((double) u < cum) { pick = keep_ids[i]; break; } + cum += exp((double) scaled(i) - (double) smx) / sum; + if ((double) u < cum) { pick = sel_ids[i]; break; } } - out[t] = pick; + if (threadIdx.x == 0) out[t] = pick; } } // namespace @@ -298,16 +310,19 @@ void sample_tokens(const float* logits, int n_tokens, int n_vocab, const int* hi p.penalty_last_n, (const void*) history, history_len); std::exit(1); } - const int threads = 64; - const unsigned grid = (unsigned) ((n_tokens + threads - 1) / threads); + const unsigned shmem = (history != nullptr && history_len > 0 && p.penalty_last_n > 0) + ? (unsigned) ((n_vocab + 31) / 32) * sizeof(unsigned) // the penalty bitmap + : 0; if (p.greedy || p.temperature <= 0.0f) { // One block per token, 1,024 threads over the vocabulary. See `sampler_greedy_kernel`. const int gthreads = 1024; - sampler_greedy_kernel<<<(unsigned) n_tokens, gthreads, 0, (cudaStream_t) stream>>>( + sampler_greedy_kernel<<<(unsigned) n_tokens, gthreads, shmem, (cudaStream_t) stream>>>( logits, n_vocab, history, history_len, p, p.penalty_last_n, p.penalty_last_n, out); } else { - sampler_kernel<<>>(logits, n_vocab, n_tokens, history, - history_len, p, out); + // The same block-per-token shape: the selection's k argmax rounds reduce inside the block. See + // `sampler_kernel`'s header for what the old one-thread-per-token launch cost. + sampler_kernel<<<(unsigned) n_tokens, 1024, shmem, (cudaStream_t) stream>>>( + logits, n_vocab, n_tokens, history, history_len, p, out); } const cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) { diff --git a/src/kernels/sampler_parity.cpp b/src/kernels/sampler_parity.cpp index 6eb7df06..2b56ba51 100644 --- a/src/kernels/sampler_parity.cpp +++ b/src/kernels/sampler_parity.cpp @@ -12,6 +12,7 @@ #include +#include #include #include #include @@ -126,6 +127,139 @@ int run(const char* name, const std::vector& logits, int n_tokens, const return bad; } +// The Philox draw, host side - a transcription of the kernel's `philox_uniform` so the SAMPLED pick (not +// just the greedy argmax) can be pinned against a reference. `__umulhi(a, b)` is the high half of a 32x32 +// multiply, spelled `(uint32_t)(((uint64_t) a * b) >> 32)` here. +struct PhiloxRound { + uint32_t& c0; uint32_t& c1; uint32_t& c2; uint32_t& c3; + void step(uint32_t k0, uint32_t k1) const { + const uint32_t hi0 = (uint32_t) (((uint64_t) 0x9E3779B9u * c0) >> 32); + const uint32_t hi1 = (uint32_t) (((uint64_t) 0xBB67AE85u * c2) >> 32); + const uint32_t lo0 = 0x9E3779B9u * c0; + const uint32_t lo1 = 0xBB67AE85u * c2; + const uint32_t n0 = hi1 ^ c1 ^ k0; + const uint32_t n1 = lo1; + const uint32_t n2 = hi0 ^ c3 ^ k1; + const uint32_t n3 = lo0; + c0 = n0; c1 = n1; c2 = n2; c3 = n3; + } +}; + +float host_philox_uniform(uint64_t seed, uint64_t counter) { + uint32_t c0 = (uint32_t) counter, c1 = (uint32_t) (counter >> 32); + uint32_t c2 = (uint32_t) seed, c3 = (uint32_t) (seed >> 32); + PhiloxRound r{c0, c1, c2, c3}; + for (int i = 0; i < 10; ++i) r.step((uint32_t) i, 0u); + return (float) (c0 >> 8) * (1.0f / 16777216.0f); +} + +// The full SAMPLED chain, host side - the kernel's `sampler_kernel` in serial form: penalties on the raw +// logits during the top_k selection (ties to the lowest index), the min_p prefix cut, top_p's cut in double, +// then penalties AGAIN on the temperature-scaled survivors (docs/sampling.md's after-temperature pass), and +// one Philox draw at (seed, counter + row). +int sampled_reference(const std::vector& l, const std::vector& hist, + const strata::kernels::SamplerParams& p, int row) { + auto penal = [&](float logit, int count) { + if (count <= 0) return logit; + if (logit <= 0.0f) logit *= p.penalty_repeat; else logit /= p.penalty_repeat; + logit -= (float) count * p.penalty_freq + (count > 0 ? 1.0f : 0.0f) * p.penalty_present; + return logit; + }; + auto count = [&](int v) { int c = 0; for (int h : hist) if (h == v) ++c; return c; }; + const int nv = (int) l.size(); + const int KMAX = 64; + const int k = p.top_k > 0 ? (p.top_k < KMAX ? p.top_k : KMAX) : 0; + std::vector sel_ids; + std::vector sel_logit; + std::vector taken((size_t) nv, 0); + for (int i = 0; i < k; ++i) { + int best = -1; float bv = 0; + for (int v = 0; v < nv; ++v) { + if (taken[(size_t) v]) continue; + const float s = penal(l[(size_t) v], count(v)); + if (best < 0 || s > bv) { best = v; bv = s; } + } + taken[(size_t) best] = 1; + sel_ids.push_back(best); sel_logit.push_back(bv); + } + int n_minp = (int) sel_ids.size(); + if (p.min_p > 0.0f) { + const float thresh = sel_logit[0] + std::log(p.min_p); + for (int i = 0; i < (int) sel_ids.size(); ++i) + if (sel_logit[(size_t) i] < thresh) { n_minp = i; break; } + } + int n_keep = n_minp; + if (p.top_p < 1.0f) { + double sum = 0.0; + for (int i = 0; i < n_minp; ++i) sum += std::exp((double) sel_logit[(size_t) i] - (double) sel_logit[0]); + double cum = 0.0; + int cut = n_minp; + for (int i = 0; i < n_minp; ++i) { + cum += std::exp((double) sel_logit[(size_t) i] - (double) sel_logit[0]) / sum; + if (cum >= (double) p.top_p) { cut = i + 1; break; } + } + if (cut < p.min_keep) cut = p.min_keep < n_minp ? p.min_keep : n_minp; + n_keep = cut; + } + const float inv_t = p.temperature > 0.0f ? 1.0f / p.temperature : 0.0f; + auto scaled = [&](int i) { return penal(sel_logit[(size_t) i] * inv_t, count(sel_ids[(size_t) i])); }; + float smx = scaled(0); + for (int i = 1; i < n_keep; ++i) smx = std::fmax(smx, scaled(i)); + double sum = 0.0; + for (int i = 0; i < n_keep; ++i) sum += std::exp((double) scaled(i) - (double) smx); + const float u = host_philox_uniform(p.seed, p.counter + (uint64_t) row); + double cum = 0.0; + int pick = sel_ids[(size_t) (n_keep - 1)]; + for (int i = 0; i < n_keep; ++i) { + cum += std::exp((double) scaled(i) - (double) smx) / sum; + if ((double) u < cum) { pick = sel_ids[(size_t) i]; break; } + } + return pick; +} + +// Survivors after the min_p + top_p cuts, in the sampled chain - the quantity an order or a threshold +// actually changes, used to assert a fixture can SEE the feature before asserting the kernel matches. +int sampled_cut(const std::vector& l, const std::vector& hist, const strata::kernels::SamplerParams& p) { + auto penal = [&](float logit, int count) { + if (count <= 0) return logit; + if (logit <= 0.0f) logit *= p.penalty_repeat; else logit /= p.penalty_repeat; + logit -= (float) count * p.penalty_freq + (count > 0 ? 1.0f : 0.0f) * p.penalty_present; + return logit; + }; + auto count = [&](int v) { int c = 0; for (int h : hist) if (h == v) ++c; return c; }; + const int nv = (int) l.size(); + const int KMAX = 64; + const int k = p.top_k > 0 ? (p.top_k < KMAX ? p.top_k : KMAX) : 0; + std::vector sel; + std::vector taken((size_t) nv, 0); + for (int i = 0; i < k; ++i) { + int best = -1; float bv = 0; + for (int v = 0; v < nv; ++v) { + if (taken[(size_t) v]) continue; + const float s = penal(l[(size_t) v], count(v)); + if (best < 0 || s > bv) { best = v; bv = s; } + } + taken[(size_t) best] = 1; sel.push_back(bv); + } + int n_minp = (int) sel.size(); + if (p.min_p > 0.0f) { + const float thresh = sel[0] + std::log(p.min_p); + for (int i = 0; i < (int) sel.size(); ++i) + if (sel[(size_t) i] < thresh) { n_minp = i; break; } + } + if (p.top_p >= 1.0f) return n_minp; + double sum = 0.0; + for (int i = 0; i < n_minp; ++i) sum += std::exp((double) sel[(size_t) i] - (double) sel[0]); + double cum = 0.0; + int cut = n_minp; + for (int i = 0; i < n_minp; ++i) { + cum += std::exp((double) sel[(size_t) i] - (double) sel[0]) / sum; + if (cum >= (double) p.top_p) { cut = i + 1; break; } + } + if (cut < p.min_keep) cut = p.min_keep < n_minp ? p.min_keep : n_minp; + return cut; +} + } // namespace int main(int argc, char** argv) { @@ -326,6 +460,116 @@ int main(int argc, char** argv) { bad += run("T=1 greedy=true is the argmax", l, NT3, p2, want); } + // ---- fixture 6: PENALTIES IN THE SAMPLED CHAIN. Fixture 4 pins the greedy (argmax) path; the sampled + // chain applies penalties a SECOND time - after the temperature scaling, on the survivors - so it gets its + // own reference (the full chain with the host Philox) and its own observability check: with the penalties + // on, the history row's favourite must LOSE a pick it would win penalty-free. + { + const int NV2 = 8, NT2 = 2; + strata::kernels::SamplerParams p; + p.top_k = 5; p.top_p = 0.9f; p.temperature = 0.8f; p.seed = 9; p.counter = 0; + p.penalty_last_n = 4; p.penalty_repeat = 3.0f; p.penalty_freq = 0.2f; p.penalty_present = 0.6f; + + std::vector l((size_t) NV2 * NT2, -8.0f); + std::vector hist((size_t) NT2 * 4, -1); + for (int t = 0; t < NT2; ++t) { + float* row = l.data() + (size_t) t * NV2; + row[0] = 9.0f; row[1] = 4.5f; row[2] = 4.4f; row[3] = 4.3f; // token 0 leads clean (9 vs 4.5) + hist[(size_t) t * 4 + 0] = 0; // and falls to 2.2/2.0 penalised + hist[(size_t) t * 4 + 1] = t == 1 ? 0 : -1; // (repeat 3, freq, presence) + } + std::vector want((size_t) NT2), clean((size_t) NT2); + strata::kernels::SamplerParams clean_p = p; + clean_p.penalty_last_n = 0; clean_p.penalty_repeat = 1.0f; + clean_p.penalty_freq = 0.0f; clean_p.penalty_present = 0.0f; + for (int t = 0; t < NT2; ++t) { + const std::vector row(l.begin() + (size_t) t * NV2, l.begin() + (size_t) (t + 1) * NV2); + const std::vector h(hist.begin() + (size_t) t * 4, hist.begin() + (size_t) (t + 1) * 4); + want[(size_t) t] = sampled_reference(row, h, p, t); + clean[(size_t) t] = sampled_reference(row, h, clean_p, t); + } + const bool visible = want[0] != clean[0] || want[1] != clean[1]; + std::printf(" %-34s %s (penalised picks %d/%d, clean %d/%d)\n", + "sampled penalties are observable", visible ? "yes" : "*** NO ***", want[0], want[1], + clean[0], clean[1]); + if (!visible) ++bad; + else bad += run("sampled chain: penalties + top_k/p", l, NT2, p, want, hist, 4); + } + + // ---- fixture 7: MIN_P. The cut is a PREFIX of the descending top_k list (logit >= max + log(min_p)), + // so the fixture asserts the survivor count moves with the threshold (the observability half) and that + // the kernel's pick equals the reference's through the full sampled chain (the correctness half). + { + const int NV3 = 8, NT3 = 2; + std::vector l((size_t) NV3 * NT3, -8.0f); + for (int t = 0; t < NT3; ++t) { + float* row = l.data() + (size_t) t * NV3; + row[0] = 4.0f; row[1] = 3.5f; row[2] = 3.2f; row[3] = 3.1f; // gaps keep the cut off the + row[4] = 2.0f; // logf/rounding knife edge + } + strata::kernels::SamplerParams base; + base.top_k = 6; base.top_p = 1.0f; base.temperature = 0.9f; base.seed = 77; + + int c0 = 0, c05 = 0, c09 = 0; + for (int t = 0; t < NT3; ++t) { + const std::vector row(l.begin() + (size_t) t * NV3, l.begin() + (size_t) (t + 1) * NV3); + strata::kernels::SamplerParams q = base; q.min_p = 0.0f; + c0 += sampled_cut(row, {}, q); + q.min_p = 0.5f; c05 += sampled_cut(row, {}, q); + q.min_p = 0.9f; c09 += sampled_cut(row, {}, q); + } + const bool visible = c0 > c05 && c05 > c09 && c09 >= NT3; + std::printf(" %-34s %s (survivors: min_p 0 -> %d, 0.5 -> %d, 0.9 -> %d)\n", + "min_p cut is observable", visible ? "yes" : "*** NO ***", c0, c05, c09); + if (!visible) ++bad; + + for (float mp : {0.0f, 0.5f, 0.9f}) { + strata::kernels::SamplerParams q = base; q.min_p = mp; + std::vector want((size_t) NT3); + for (int t = 0; t < NT3; ++t) { + const std::vector row(l.begin() + (size_t) t * NV3, l.begin() + (size_t) (t + 1) * NV3); + want[(size_t) t] = sampled_reference(row, {}, q, t); + } + char name[64]; + std::snprintf(name, sizeof name, "sampled chain: min_p=%.1f", (double) mp); + bad += run(name, l, NT3, q, want); + } + } + + // ---- fixture 8: THE PENALTY WINDOW IS THE TAIL. With an 8-entry history and penalty_last_n = 4, only + // the LAST four entries count: a token punished in the old half must come back to full strength, and one + // punished in the tail half stays down. The reference counts the same tail; the observability check runs + // the reference once more WITHOUT the clamp (counting all 8) and requires the picks to differ. + { + const int NV4 = 8; + strata::kernels::SamplerParams p; + p.top_k = 0; p.top_p = 1.0f; p.temperature = 1.0f; p.greedy = true; + p.penalty_last_n = 4; p.penalty_repeat = 3.0f; p.penalty_freq = 0.3f; p.penalty_present = 0.5f; + + std::vector l((size_t) NV4, -8.0f); + l[0] = 6.0f; l[3] = 6.5f; // token 0 leads clean; token 3 is the tail offender + std::vector hist = {0, 0, 0, 0, 3, 3, 3, 3}; // token 0 old (out), token 3 in the tail + + auto pick_clamped = [&](bool clamp) { + int best = 0; float bv = 0; bool first = true; + for (int v = 0; v < NV4; ++v) { + int c = 0; + for (int i = 0; i < (clamp ? 4 : 8); ++i) if (hist[(size_t) (8 - (clamp ? 4 : 8) + i)] == v) ++c; + float logit = l[(size_t) v]; + if (c > 0) { logit = logit <= 0.0f ? logit * p.penalty_repeat : logit / p.penalty_repeat; + logit -= (float) c * p.penalty_freq + p.penalty_present; } + if (first || logit > bv) { bv = logit; best = v; first = false; } + } + return best; + }; + const int want = pick_clamped(true), unclamped = pick_clamped(false); + const bool visible = want != unclamped; + std::printf(" %-34s %s (clamped pick %d, full-history pick %d)\n", + "penalty window clamp is observable", visible ? "yes" : "*** NO ***", want, unclamped); + if (!visible) ++bad; + else bad += run("penalty window: tail only", {l.begin(), l.end()}, 1, p, {want}, hist, 8); + } + // A continuous stream and individual decode calls consume the same draw counters. { constexpr int count = 32, vocab = 16; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index d9099e3e..ffd8cc14 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1991,6 +1991,16 @@ int main(int argc, char** argv) { return 1; } mem_mark("the head and the prompt path"); + // the penalty-history buffer: ONE row, the request's last `penalty_last_n` tokens, -1 padded in + // front. Allocated once at the cap; a request without penalties gets a null row and takes the + // byte-for-byte neutral path (no upload, no buffer handed to the sampler). + constexpr int kPenaltyWindowCap = 4096; + int32_t* d_hist = nullptr; + std::vector hist_stage((size_t) kPenaltyWindowCap, -1); + if (cudaMalloc(&d_hist, (size_t) kPenaltyWindowCap * sizeof(int32_t)) != cudaSuccess) { + std::fprintf(stderr, "strata serve: the penalty-history allocation failed\n"); + return 1; + } strata::core::Verifier ver; strata::core::VerifyHits vh; vh.d_res = thits.d_res; @@ -2182,6 +2192,37 @@ 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, min_p=F, + // penalty_last_n=N, penalty_repeat=F, penalty_freq=F, penalty_present=F, seed=N (text requests + // only). Absent keys keep today's behavior: greedy, no penalties. + 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; + float req_min_p = 0.0f, req_penalty_repeat = 1.0f, req_penalty_freq = 0.0f, req_penalty_present = 0.0f; + int req_penalty_last_n = 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 == "min_p") req_min_p = fv; + else if (key == "penalty_last_n") req_penalty_last_n = std::atoi(tok.c_str() + eq + 1); + else if (key == "penalty_repeat") req_penalty_repeat = fv; + else if (key == "penalty_freq") req_penalty_freq = fv; + else if (key == "penalty_present") req_penalty_present = fv; + 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; @@ -2417,6 +2458,23 @@ 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.min_p = std::clamp(req_min_p, 0.0f, 1.0f); + req_sp.penalty_last_n = std::max(req_penalty_last_n, 0); + req_sp.penalty_repeat = req_penalty_repeat; + req_sp.penalty_freq = req_penalty_freq; + req_sp.penalty_present = req_penalty_present; + req_sp.counter = 0; + ver.set_sampling(req_sp); + const int hist_n = std::min(req_sp.penalty_last_n, kPenaltyWindowCap); + ver.set_history(hist_n > 0 ? d_hist : nullptr, hist_n); 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|> @@ -2508,6 +2566,19 @@ int main(int argc, char** argv) { drive.d.experts = 0; drive.d.failed = false; apply_pending(false); + if (hist_n > 0) { + // the tail the penalties count over: the tokens the state has consumed plus the fed-back + // head `x` (it joins `consumed` only after this window commits). Most recent LAST, + // -1 in the unused front slots - the sampler's row layout. + const int64_t avail = (int64_t) consumed.size() + 1; + const int take = (int) std::min(hist_n, avail); + std::fill(hist_stage.begin(), hist_stage.begin() + hist_n, -1); + for (int j = 0; j < take - 1; ++j) + hist_stage[(size_t) (hist_n - take + j)] = + consumed[(size_t) ((int64_t) consumed.size() - (take - 1) + j)]; + hist_stage[(size_t) (hist_n - 1)] = (int32_t) x; + cudaMemcpy(d_hist, hist_stage.data(), (size_t) hist_n * sizeof(int32_t), cudaMemcpyHostToDevice); + } tr("window", p, T); if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err) || drive.d.failed) { std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());