Files
leleka/docs/sampler-reference.md
T

5.0 KiB

Sampler reference, request plumbing and serving path

Status: source-level work, no GPU run, no speed or quality claim.

What exists

Piece Where
Bit-exact CPU reference: 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 crates/sampler-ref
Request validation/normalisation into one SamplingParams (Original GEN defaults, ranges, GEN wire keys) crates/sampler-ref/src/params.rs
Golden fixtures from the Original's host logic crates/sampler-ref/tests/fixtures/*.json, generators in crates/sampler-ref/oracle/
Per-request host sampler on the plain decode path leleka-runtime qwen_forward/step_sampler.rs, hooked in output.rs, armed by backend.rs
Backend request carries the sampling controls backend-api SamplingOptions

What is verified (macOS, no CUDA)

  • The fixture generator compiles the Original sampler_parity.cpp host reference unmodified (stub <cuda_runtime.h>), the Original sampler.hpp/coupled_draft.hpp headers, with -ffp-contract=off. 322 cases: Philox words, full chain (random, ties, NaN/inf/denormal rows, penalties, top_p exact-boundary cases, full 248,320-vocabulary rows with up to 4,096 history slots), the 64-byte SamplerParams ABI, counter/ring arithmetic, penalty_rows. Rust matches bitwise (token, selection list, uniform bits). Mutating >=->> in top_p, the repeat rule, tie order and the min_p comparison each fail the suite.
  • The coupled-draft kernels have no host twin in the Original; coupled_ref in the generator transcribes them and is cross-checked against the Original mirror_pick.
  • StrataEngine.sampling_keys (lifted verbatim from server.py by AST) gives the golden GEN strings.
  • Existing source-port host logic checked against the same fixtures: cuda-driver SamplerParams (size, alignment, offsets, 64 bytes), leleka-runtime original_sampler.rs helpers. crates/sampler-ref/oracle/run_port_host_tests.sh runs these on macOS; on Linux they are ordinary crate tests.
  • Linux gated code type-checks: cargo check --target x86_64-unknown-linux-gnu -p leleka-cuda-driver --features cuda-driver -p leleka-runtime --all-targets.

Defects found in the existing source-port and fixed

  • SamplerParams::validate rejected top_p == 0, which the Original accepts (Python emits it; the kernel keeps min_keep).
  • Draft cell -1 (draft_first for a window at position 0, Philox counter 0) was refused by coupled_draft_counter and OriginalSampler::sample_coupled.
  • OriginalSampler::set_subset rejected duplicate or out-of-range subset ids; setup_coupled keeps the first index and ignores out-of-range entries.

Serving path

temperature_milli != 0 is no longer refused in backend.rs / desktop runtime_rpc.rs. A request is planned by request_sampler: all-default greedy keeps the compact device head; any temperature or penalty arms StepSampler on the output stage, which forces the complete logit vector (no compact argmax) and draws with the CPU reference at counter = sequence index of the fed token, penalty history = last min(penalty_last_n, 4096) tokens of prompt + generated. Requests without a seed take a clock seed (the Original's seed = 0).

Open

  • nvcc/PTX: ASSET/SAMPLER_ASSET stay None; no PTX exists, so the GPU OriginalSampler and the NativeHead sampler have never run. The serving path therefore samples on the host from the Q6 head's full logits. Switching it to the GPU sampler is a later step once the asset is pinned; the token contract is the same.
  • Sampling with MTP is refused on the preview path (sampling is unavailable with MTP). The Original serve path (--backend-mode original|auto, docs/original-serve.md) accepts the sampled request: the request parameters reach the native head sampler with the literal penalty-history rows, picks are verified by equality, and the drafter samples coupled only with STRATA_SPEC_COUPLED. Not GPU-run. The host serve-loop logic is also available as leleka_sampler_ref::serve with a whole-loop toy-model oracle (crates/sampler-ref/tests/serve_loop.rs) and the glue qwen_forward/original_sampled_serve.rs (docs/port-remaining.md, item 3).
  • The desktop gRPC protocol still carries only temperature_milli -- done: runtime.proto GenerateRequest carries top_p/top_k/min_p/penalties/penalty_last_n/seed (fixed point), desktop-core validates and forwards them, the gateway no longer refuses temperature != 0; the control plane's wire.rs builds GEN/GENI lines with RequestSampling::gen_keys (golden-tested against the Python server). See docs/api-contract.md.
  • Float caveat: exp/ln use the platform libm. Device exp (f64) and logf can differ by 1 ulp from the host and nvcc --fmad=true may contract count*freq + flag*present; these can change a pick only on cumulative-boundary or threshold ties. Compare GPU output with the fixtures and report disagreements; do not treat them as noise.