5.0 KiB
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.cpphost reference unmodified (stub<cuda_runtime.h>), the Originalsampler.hpp/coupled_draft.hppheaders, 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-byteSamplerParamsABI, 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_refin the generator transcribes them and is cross-checked against the Originalmirror_pick. StrataEngine.sampling_keys(lifted verbatim fromserver.pyby AST) gives the goldenGENstrings.- Existing source-port host logic checked against the same fixtures:
cuda-driverSamplerParams(size, alignment, offsets, 64 bytes),leleka-runtimeoriginal_sampler.rshelpers.crates/sampler-ref/oracle/run_port_host_tests.shruns 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::validaterejectedtop_p == 0, which the Original accepts (Python emits it; the kernel keepsmin_keep).- Draft cell
-1(draft_firstfor a window at position 0, Philox counter 0) was refused bycoupled_draft_counterandOriginalSampler::sample_coupled. OriginalSampler::set_subsetrejected duplicate or out-of-range subset ids;setup_coupledkeeps 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_ASSETstayNone; no PTX exists, so the GPUOriginalSamplerand 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 withSTRATA_SPEC_COUPLED. Not GPU-run. The host serve-loop logic is also available asleleka_sampler_ref::servewith a whole-loop toy-model oracle (crates/sampler-ref/tests/serve_loop.rs) and the glueqwen_forward/original_sampled_serve.rs(docs/port-remaining.md, item 3). The desktop gRPC protocol still carries only-- done:temperature_milliruntime.protoGenerateRequestcarries top_p/top_k/min_p/penalties/penalty_last_n/seed (fixed point),desktop-corevalidates and forwards them, the gateway no longer refusestemperature != 0; the control plane'swire.rsbuildsGEN/GENIlines withRequestSampling::gen_keys(golden-tested against the Python server). Seedocs/api-contract.md.- Float caveat:
exp/lnuse the platform libm. Deviceexp(f64) andlogfcan differ by 1 ulp from the host and nvcc--fmad=truemay contractcount*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.