Skip to content

lanes: host GPU draws select their candidates instead of sorting the whole row (#3396) - #3455

Open
ashhart wants to merge 2 commits into
mainfrom
lead/gpu-draw-select
Open

ashhart wants to merge 2 commits into
mainfrom
lead/gpu-draw-select

Conversation

@ashhart

@ashhart ashhart commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

What this changes

When two or more streams share a round, Flint draws every row's token on the CPU through lanes.gpu_full.draw. For
top_k 1 to 1,024 that calls gpu_rule.sample, which sorted all 150,144 logits of the row to keep at most 1,024
candidates. For top_k 0 or above 1,024, gpu_full.plan sorted the whole row too, for one place (top_k - 1) and,
under top_p, the first 1,024. #3396 measured the cost: sampled shared rounds 3.4 times longer at two streams and 6.4
at four, while greedy rows (one pass for the largest) scaled.

Both functions now take the same candidates without the full sort:

  • gpu_select.top: a bounded heap keeps the best n (at most 1,024) in one pass, then sorts only those.
  • gpu_select.nth: a quickselect finds place top_k - 1 for top_k above the 1,024 candidates (median of three,
    falling back to a sort of the remaining range after 2 log2(n) + 4 partitions).
  • gpu_rule.sample sums the normalizer over the whole row only when top_k is 0, the one case that reads it.

Exact by construction. rule.better is a strict total order: the key bits of the value (a bijection over every
f32, NaN and infinities included), then the lower id. So the heap's first n and the quickselect's place are the
full sort's own candidates, bit for bit, and every draw keeps its token. No switch: nothing to turn on or off.

Every host draw through gpu_rule or gpu_full gets this, not only Flint's shared rounds: MiniMax M3's and one
other Metal family's shared rounds, Kimi K3, Llama, and Flash Next's grammar draws.

Checks

  • zig build test-gpu-draw-host (new): runs the gpu draw: tests in Debug and in ReleaseFast. 16 of 16 pass in
    each.
    • Picks: every draw is compared with main's code, kept whole in the test (refSample, refPlan, the
      reference sort).
      • Row kinds: spread values, three-level ties, alternating signed zeros, rows with both NaN signs and ±inf, and
        rows that are mostly −inf (masked).
      • Lengths: 1, 2, 17, 1,023, 1,024, 1,025 and 3,001.
      • Settings: top_k 0, 1, 2, 20, 1,023, 1,024, 1,025, len−1, len, len+5 and the u32 maximum; top_p 0, 0.5,
        0.95 and 1; min_p 0, 0.05 and 0.6; temperatures 1e−7, 0.3, 1 and 2.5; a seed per row; positions varied;
        every third draw through an id map.
      • Each setting compares gpu_rule.sample, gpu_full.draw, and gpu_full.plan byte for byte. Any difference
        fails.
    • Places: top and nth equal the full sort's first n and its place k, byte for byte, on shuffled input.
    • Planted control: the same harness with a reference that breaks ties toward the higher id is caught.
    • Mutation: a heap that breaks ties by odd id instead of better fails both the pick and the place tests.
  • Lean: 341 on the base, 341 on this head, 0 new. zig fmt --check is clean. The new and changed files are at most
    232 lines.
  • test-shared-lane-regressions-host and test-cuda-host: see below.

Timing

From the timing test in test-gpu-draw-host: one row of 150,144 spread logits, temperature 1, top_p 0.95. Each
figure is the median ns per draw, main's code against this change, in the same binary. Two runs agreed within 1%.

build top_k 20 (Flint's default) top_k 0 (full vocabulary)
ReleaseFast 16.9 ms → 0.45 ms (37×) 55.0 ms → 40.6 ms
Debug 130.0 ms → 3.49 ms 302 ms → 181 ms

top_k 0 still makes its full-row passes (mass, race), which this change doesn't touch. Only its sort is gone.

Not run

Checklist

  • The title and description match the head commit.
  • New tests fail before the change and on a broken selection, and pass on this change. On main the timing test's
    check (at least twice as fast) fails, because both sides are the same sort; the mutation and the planted control
    fail the equality tests. They need no GPU.
  • Comments are one line each, within 120 columns.
  • No personal data, machine names or local paths.

@Spectre056

Copy link
Copy Markdown

Ran it on the M5 Max (macOS 27.2, 128 GB): #3455's head b43be39c against its base 811c3847, both built here
with Zig 0.17.0, zig build native -Dcpu=apple_m1 -Dversion=1.2.9. Same server for both:
serve flint-int4 --context 131072 --parallel 4 --prompt-cache-gib 16, FLINT_LOAD_LIMIT_GB=48. Arms ran in the
order base, PR, PR, base. Cells are flint_streams.py's (community-v2 prompts, 256 tokens, 5 reps after a
warm-up, sampled T 1.0 with top_k 20, top_p 0.95 and seeds 1234 + 10·rep + stream). Figures are aggregate tok/s,
the mean of the two arms' medians on each side; the spread between repeats is at most 2 tok/s.

T workload 1 stream 2 streams 4 streams
1.0 code 142.9 → 164.3 (+15%) 84.8 → 208.8 (+146%) 88.8 → 275.6 (+211%)
1.0 chat 163.6 → 165.0 (+1%) 89.4 → 209.1 (+134%) 94.2 → 280.6 (+198%)
0 code 168.1 → 168.7 229.8 → 229.2 326.5 → 325.1
0 chat 168.7 → 169.3 233.8 → 233.5 325.5 → 324.4

Sampled rounds now scale with streams, and 4 sampled streams reach about 85% of greedy's aggregate. Greedy is
unchanged, within 0.4%.

Tokens:

  • Sampled: every row's set of sha values is the same on base and PR (36 of 36 rows, no errors), and each
    seed gives the same sha alone and alongside other streams: seed 1234 is 599edf33 (code) and b3139cd3 (chat)
    at 1, 2 and 4 streams.
  • Greedy: one sha per workload across both builds and all widths.

The one-stream code cell gained 15% too. That fits the copy-draft verify rounds we suspected: they drew on the
host and now skip the sort.

The two local builds aren't bit-for-bit reproducible (two builds of the same tree gave different binary hashes),
so these are our own builds' numbers, not the release binary's. Binary SHA-256: base d8e3dbc4a419214a43db3441eb48f18cbbb6d4b86555a5cc0254392cf84f94a2, PR
b2304b170c88c52b02b9b89fedef7193f41dc24b3022b85c47fcd8e4bb259bb0.

…whole row (#3396)

gpu_rule.sample sorted every logit of the row to keep at most 1,024 candidates, and gpu_full.plan sorted it for one
place and the first 1,024. Both now take the same candidates by selection: a bounded heap for the first n, sorted,
and a quickselect for place top_k - 1. rule.better is a strict total order over key bits then id, so each is the
full sort's own answer and every draw keeps its token. The normalizer over the whole row is now summed only when
top_k is 0, the one case that reads it.

On a 150,144-token row (the host test, ReleaseFast), a top_k 20 draw takes 0.45 ms instead of 17 ms, and a top_k 0
draw 40 ms instead of 55 ms. Every host draw through gpu_rule or gpu_full gets this, among them Flint's shared rounds.

test-gpu-draw-host checks every pick, plan and place against main's full sort, kept in the test, across spread,
tied, signed-zero, NaN and infinity and masked rows, top_k 0/1/1023-1025/len/past len, top_p 0/0.5/0.95/1,
min_p 0/0.05/0.6, four temperatures and an id map. A control that breaks ties the other way is caught.
Keep the old planner and its helpers in a test-only snapshot. Reject a dropped top-p cut with the same plan comparison used by the parity grid.
@ashhart
ashhart force-pushed the lead/gpu-draw-select branch from 7ee2ac2 to f75f4e4 Compare November 26, 2026 22:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flint on M5 Max: sampled requests lose throughput at 2 and 4 streams (greedy scales)

2 participants