Repository navigation
lanes sampling: a keyed draw that cuts nothing by rank finds the sorted draw's token in one pass - #1337
lanes sampling: a keyed draw that cuts nothing by rank finds the sorted draw's token in one pass#1337ajensenwaud wants to merge 1 commit into
Conversation
…ed draw's token in one pass over the row With top_k off (or at least the row width) and top_p off, choose no longer sorts the whole vocabulary. One pass draws the same Gumbel race in id order, a tied score going to the candidate the sort would put first; min_p keeps the same prefix of the sorted order, and a candidate whose noise cannot reach the lead's score skips its logs. Nonfinite rows, rank cuts and top_p cuts keep the full sort, and the partial top-k path is unchanged. Tests compare the draw with the full-sort reference over widths, shapes with ties and duplicate ids, temperatures, filters and keys, and check that the draw allocates nothing.
|
M5 Ultra Metal validation of head
Environment: Apple M5 Ultra, 256 GiB, macOS 27.0 (26A425), Zig 0.17.0. Same This is a validation backport, not a full build/test of the PR branch: baseline is the existing DeepSeek Metal engine Method: A0 baseline → B0 candidate → B1 candidate → A1 restored baseline, one resident model at a time. Two observations per cell per binary, three configured lanes, warm approximately 8k-token synthetic code/prose prompts, 256 output-token cap, thinking off, fixed seeds. Sampled requests use temperature 1, top-k 0, min-p 0, top-p 1; the filtered control uses top-p 0.95. Code task is a Python LRU implementation/tests; prose is an engineering update from fixed synthetic facts. Total decode counts output tokens minus the first token per request over the shared streamed-output window, averaged over two arms. These are short controlled fixtures, not coding-quality or capacity qualification. Exactness: all eight cell/fixture groups have identical request payload SHA-256, full Checks run on the Mac:
Not run: full upstream host/golden/kernel suites, CUDA, other Apple chips, cold 2k/32k/64k prefill gates, or broad agent-quality evaluation. Prompt-processing arithmetic is unchanged, but I am not claiming a measured cold-prefill improvement. The original baseline was restored and verified afterward; this is not a production rollout. |
|
Landed on main in e929a71, thanks @ajensenwaud. We added 4577a1a on top: a host test that pins |
|
Thanks a lot. |
What this changes
lanes.sampling.choosealready selects only the top k candidates when0 < top_k <= width / 2. In every othercase it still runs a stable
std.mem.sortover the whole row: withtop_koff, which is the server default(
top_k0,top_p1.0), and withtop_kat or above the width. On a 200K-entry vocabulary that sort is most of thecost of a host draw.
When neither
top_knortop_pcuts anything,choosenow runsrace(incore/lanes/sampling.zig). It is onepass over the row in id order and returns exactly the token
chooseOrderedwould draw from the sorted order:-log(-log(u))andvalue / t, witht = max(temperature, 1e-6).value, then the lower id).
min_pcut. The floor ismax / t + ln(min_p), aschooseOrderedcomputes it. Everyvalue / tat orabove the floor is a prefix of the sorted order, so filtering in id order keeps the same set. When
min_pkeepsnothing, the result is the greedy first candidate, as the sorted loop returns
order[0].min_pon costs oneextra max pass, still with no sort.
Such a candidate scores strictly below the lead, so skipping it changes no result. The proof: a uniform below
exp(-exp(-(d - 0.5)))has Gumbel noise underd - 0.5, and with both scores within 1e6, rounding stays farinside that 0.5 margin.
<= 0or NaN (the greedy path is unchanged);0 < top_k < width;0 < top_p < 1;value / tthat is not finite. This includes NaN and infinite logits, matching howselection.topKreturns null for nonfinite rows.
The partial top-k path,
chooseOrdered,sampling_selection.zigand the reference are unchanged.keyBits/uniformare refactored to sharekeyBase/uniformOf; the operations and bits are the same.This replaces the old patch
0060(chooseAll/race). The Muse-specific f32chooseRowentry is not part of thisPR.
Receipt
up/main2ff0191 + this commit, Zig 0.17.0, x86_64 Linux host.sampling_reference.choose(the full stable sort pluschooseOrdered). No bits change.temperature 1.0, top_k 0) took decode from 14.7 to 23.2 tok/s with its drafter and from 13.3 to 18.2 without it,
same tokens. Device samplers are untouched. A cross-family A/B on Metal and CUDA is welcome.
zig/src/core/lanes/sampling_race_test.zig, 94 lines, imported fromsampling.zig's test block):±0.0, wide (×40, which exercises the skip), huge (×1e303,overflowing at tiny temperatures, which exercises the fallback) and repeated ids;
inf;min_p0, 0.05, 0.5, 1, 1.5 and NaN;top_p0, 1, 1.5 and NaN;top_k0, width andmaxInt(u32);FailingAllocatorwithfail_index = 0). This fails before thechange, when the sort allocated. A rank cut (
top_k = width - 1), atop_pcut and a NaN row still sort,which shows up as OOM under the failing allocator.
sampling_selection_test.zigdraws (reference.choosevschooseover filters includingtop_k0/maxIntandtop_p0/1) now also exercise the new path.python3 tools/zig/lean_check.pyreports 435 problems onup/mainand 435 on this branch, so thereare 0 new findings, all inherited.
git diff --check: clean.zig test -lc zig/src/core/lanes/sampling.zig14/14 passed (4 new);zig build test-cuda-hostpassed on a branch carrying this commit.tests/lanes_units.zig's Python-golden draws.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Thedrawn token is unchanged for every key.)
test is the before/after one.)