New pure-Rust crate leleka-sampler-ref: Philox draw, penalties -> top_k -> top_p -> min_p -> temperature -> inverse-CDF pick, greedy argmax, coupled-draft counter/ring math, penalty_rows, exact-match acceptance, and the request-level SamplingParams validation with the Original's GEN defaults and wire keys. Golden fixtures come from compiling the Original's sampler_parity.cpp host reference (included unmodified) and sampler/coupled_draft headers with clang++ (-ffp-contract=off), plus the verbatim StrataEngine.sampling_keys lifted from server.py. No GPU, no speed or quality claim. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
79 lines
3.5 KiB
Python
79 lines
3.5 KiB
Python
"""Offline fixture generator (Python is allowed for experiments/fixtures, never for serving).
|
|
|
|
Runs the Original's `StrataEngine.sampling_keys` (engine/strata/serve/server.py) over a table of requests and
|
|
records the exact GEN key string it emits, plus the ranges `clean_shared_defaults` /
|
|
`sampling_defaults_from_config` enforce. Usage:
|
|
|
|
python3 -I crates/sampler-ref/oracle/gen_sampling_keys.py crates/sampler-ref/tests/fixtures/sampling_keys.json
|
|
|
|
The two pure functions are lifted verbatim from the Original's AST; nothing else of server.py is imported.
|
|
"""
|
|
import ast
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
server_py = os.path.normpath(os.path.join(here, "..", "..", "..", "engine", "strata", "serve", "server.py"))
|
|
|
|
# server.py imports the whole serving stack (jinja2, sockets, ...). The two functions under test are pure, so lift
|
|
# their source verbatim out of the AST instead of importing the module.
|
|
tree = ast.parse(open(server_py).read(), server_py)
|
|
engine = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "StrataEngine")
|
|
wanted = [n for n in engine.body if isinstance(n, ast.FunctionDef) and n.name in ("sampling_keys", "projection_key")]
|
|
assert {n.name for n in wanted} == {"sampling_keys", "projection_key"}
|
|
lifted = ast.Module(body=[ast.ClassDef(name="StrataEngine", bases=[], keywords=[], body=wanted, decorator_list=[])],
|
|
type_ignores=[])
|
|
ast.fix_missing_locations(lifted)
|
|
ns = {}
|
|
exec(compile(lifted, server_py, "exec"), ns)
|
|
|
|
|
|
class server: # noqa: N801 - keeps the call below readable
|
|
StrataEngine = ns["StrataEngine"]
|
|
|
|
|
|
keys = server.StrataEngine.sampling_keys
|
|
|
|
cases = [
|
|
{},
|
|
{"temperature": 0.0},
|
|
{"temperature": 0.7},
|
|
{"temperature": 1},
|
|
{"temperature": 2.0},
|
|
{"temperature": 0.1 + 0.2},
|
|
{"temperature": 1e-7},
|
|
{"temperature": 0.7, "top_p": 0.95},
|
|
{"temperature": 0.7, "top_p": 1.0},
|
|
{"top_p": 0.0},
|
|
{"top_p": 0.5},
|
|
{"top_k": 0}, {"top_k": 1}, {"top_k": 20}, {"top_k": 64}, {"top_k": 65}, {"top_k": 100000},
|
|
{"min_p": 0.0}, {"min_p": 0.05}, {"min_p": 1.0}, {"min_p": 1e-5},
|
|
{"repetition_penalty": 1.0},
|
|
{"repetition_penalty": 1.1},
|
|
{"repetition_penalty": 1.1, "penalty_last_n": 256},
|
|
{"repetition_penalty": 1.1, "penalty_last_n": 0},
|
|
{"repetition_penalty": 1.1, "penalty_last_n": 5000},
|
|
{"frequency_penalty": 0.5},
|
|
{"frequency_penalty": -0.5},
|
|
{"presence_penalty": 0.25},
|
|
{"presence_penalty": 1.5, "frequency_penalty": 0.5, "repetition_penalty": 1.05},
|
|
{"presence_penalty": 0.0, "frequency_penalty": 0.0, "repetition_penalty": 1.0, "penalty_last_n": 33},
|
|
{"penalty_last_n": 33},
|
|
{"seed": 1}, {"seed": 42}, {"seed": 18446744073709551615},
|
|
{"temperature": 0.6, "top_p": 0.95, "top_k": 20, "min_p": 0.02, "repetition_penalty": 1.1,
|
|
"presence_penalty": 0.5, "frequency_penalty": 0.25, "penalty_last_n": 128, "seed": 7},
|
|
{"temperature": 1e16}, {"temperature": 1.5e-5},
|
|
]
|
|
# the request-boundary rule (Rust) is stricter than the Original's silent skipping: those are tested in Rust only
|
|
out = {
|
|
"meta": {"generator": "crates/sampler-ref/oracle/gen_sampling_keys.py",
|
|
"oracle": "engine/strata/serve/server.py StrataEngine.sampling_keys"},
|
|
"cases": [{"request": c, "keys": keys(dict(c))} for c in cases],
|
|
}
|
|
with open(sys.argv[1], "w") as f:
|
|
f.write('{"meta":' + json.dumps(out["meta"]) + ',\n"cases":[\n')
|
|
f.write(",\n".join(json.dumps(c) for c in out["cases"]))
|
|
f.write("\n]}\n")
|
|
print(f"wrote {len(cases)} cases")
|