Files
leleka/docs/sampler-reference.md
T
Pavlo RatushnyiandClaude Sonnet 5.5 392037d83e feat(serving): port the operational HTTP surface of the Python server
control-plane serves /props, /slots, /v1/status, /api/health, /settings, /load, /unload,
/v1/load, /v1/unload, model aliases, CORS with the preflight ahead of authentication and the
own-page guard; replayed scenario by scenario against the real Python handlers. The worker
supervisor gains load/unload, lazy start, idle unload, reload gates (before_load, min free VRAM),
an outside stall watchdog and the reasoning-budget wrap-up (budget tracker golden-tested against
the Python Detokenizer/OutputParser loop). leleka-serving-policy adds CORS, idle, stall watchdog,
logits stride (golden vs the C++ header), request timings and alias rules.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:35:08 +02:00

60 lines
4.5 KiB
Markdown

# 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 (`sampling is unavailable with MTP`): coupled draft sampling is not wired into the actor.
* ~~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.