Skip to content

[Engine] Inkling 2: route guessed tokens like single tokens - #327

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/window-card-picks
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/window-card-picks

Conversation

@okada-shogo

Copy link
Copy Markdown
Contributor

Problem and result

On 8 GB cards, Inkling 2 with guesses (--window N lookup guesses, or the DSpark drafter) did not give the same greedy tokens as plain one-token decode. Which tokens changed also varied from run to run, because the draft policy picks the window length from measured times.

Cause: with experts outside VRAM, a single token is routed on the GPU (the doorbell path, route_card(), the card's expf). A window of several tokens went to moe(), which routed each row on the CPU (route(), the host's std::exp). The two can round a gate differently in the last bit. The expert weights then differ by an ulp, and at a near tie the chosen expert differs. Per-layer dumps on an RTX 2070 Super showed it: the first difference was always the experts' sum (fo) of a window row, with identical router scores.

Fix: a window's rows are now routed on the GPU too, exactly as step() already does for --batch rows (1d4d9e2). After this PR, --window 4 and the drafter's verify give the same tokens as no guesses in every case tested, with or without STRATA_SPEC_FIXED.

Greedy tokens vs no guesses (frozen experts, 128 tokens, RTX 2070 Super 8 GB and a second RTX 2070 8 GB):

Model Prompts Mode main this PR alone
IQ4_NL code/chat/prose 2048, code/chat 8192, prose 302 --window 4 3 of 6 differ 0 of 6 differ
UD-Q2_K_XL (loop) code/chat/prose 2048 --window 4 3 of 3 differ 0 of 3 differ
IQ4_NL code/chat/prose 2048 (--stage-read-mib 0, see Limits) DSpark Q8_0 drafter 3 of 3 differ 0 of 3 differ
IQ4_NL code/chat 8192, prose 302 DSpark Q8_0 drafter 1 of 3 differ 0 of 3 differ

Decode tok/s on the same tokens (both builds --follow this PR's greedy answer, so they check the same guesses), frozen experts, 256 tokens, mean of 6 texts (code/chat/prose x 2048/8192) x 3 runs, RTX 2070 8 GB + Ryzen 7 5800X:

Card Model Measure main this PR alone Change
RTX 2070 8 GB IQ4_NL decode, --window 4 (tok/s) 19.83 19.85 +0.1%
RTX 2070 8 GB IQ4_NL decode, DSpark Q8_0 drafter (tok/s) 18.92 18.94 +0.1%

No change beyond noise. Prompt speed does not change (no new code on that path).

Decode tok/s of each run, per text (W = `--window 4`, D = drafter)
Mode Text main this PR Change main, each run this PR, each run
W chat2048 18.30 18.37 +0.4% 18.35 18.20 18.34 18.46 18.41 18.24
W chat8192 17.84 17.87 +0.1% 17.82 17.72 17.98 17.88 17.89 17.83
W code2048 19.06 19.05 -0.1% 19.22 19.01 18.96 19.04 19.01 19.10
W code8192 20.27 20.41 +0.7% 20.17 20.41 20.23 20.40 20.41 20.42
W prose2048 21.83 21.78 -0.2% 21.77 21.78 21.94 21.95 21.67 21.72
W prose8192 21.66 21.60 -0.3% 21.67 21.70 21.61 21.44 21.68 21.68
D chat2048 17.24 17.32 +0.5% 17.28 17.20 17.25 17.26 17.41 17.30
D chat8192 16.84 16.91 +0.4% 16.78 16.91 16.84 16.81 16.95 16.97
D code2048 17.69 17.69 +0.0% 17.64 17.76 17.66 17.62 17.76 17.68
D code8192 19.73 19.57 -0.8% 19.75 19.70 19.73 19.51 19.47 19.74
D prose2048 20.81 20.88 +0.3% 20.72 20.95 20.77 20.95 20.96 20.73
D prose8192 21.18 21.26 +0.4% 21.11 21.30 21.13 21.43 21.37 20.97

An earlier version copied the picks through d_ids_ with one more launch per layer: -0.3% / -0.1% on the same card, and -0.4% / +0.3% on the RTX 2070 Super 8 GB. Writing the picks straight into pinned memory removed that launch.

Related discussion

none

Approach

  • run(): when a single token would be routed on the card (picks_like_alone()), up() calls route_card() for the window's rows before the router's event and writes the picks and weights straight to pinned memory (h_ids_, h_wts_). moe() uses them through step_ids_, the same path step() uses.
  • step()'s two conditions moved into picks_like_alone() and picks_like_alone(const Layer&), shared by both. step() behaves as before.
  • h_wts_ is now also allocated when the window is over 1 (rows_ * n_used_ floats of pinned memory).
  • Not used for a window's halves (they need reads_by_layer(), which Inkling 2 does not have), with --cache-bias, or on HIP and Metal (no doorbell there: a single token is routed on the CPU like the window).
  • Nothing is skipped. The routing kernel gives each row its own warp, so a row's bits do not depend on the window length.

Validation

Passed:

  • Per-layer dumps (scratch build, --no-graphs, frozen cap 600, code and prose 2048, 64 tokens): main has the first difference in fo of a 2- or 3-row window. This PR: every tensor of every position and the logits are bit-identical between window 1 and --window 4.
  • Greedy identity, STRATA_FREEZE_EXPERTS=1 STRATA_GPU_EXPERTS=600 (Q2: 1000), 128 tokens, --serve: table above. Also with the timing-adaptive policy (no STRATA_SPEC_FIXED): 0 differ.
  • No guesses: this PR = main in all 9 cases.
  • The final version (picks written straight to pinned memory) against the earlier one that copied them through d_ids_: same tokens, and --window 4 and the drafter = no guesses, in code/chat 2048 and chat 8192 (RTX 2070, --stage-read-mib 0).
  • HIP compile (ROCm 7.2.4, gfx1201/gfx1100).
  • Independent code review: no bugs found.
  • HIP on main: no doorbell, so nothing changes there. With the HIP doorbell of [Engine] Inkling 2 on AMD: GPU doorbell for experts in RAM #306 the same split shows on an RX 9070 XT, and this change ported onto it closes it (code 2048, chat and prose 8192, --window 4 and drafter: 6 of 6 differ before, 0 of 6 after, same expert placement in every mode). That port goes with [Engine] Inkling 2 on AMD: GPU doorbell for experts in RAM #306.

Failed:

  • none

Not run:

  • Cards with every expert in VRAM (A100, 24 GB): they never reach the new code (no doorbell).

Limits

  • A 2048-token prompt can be read with other bits when the drafter is loaded, before any guess. Without the drafter, read() takes the two 1,024-row calls through each layer together, and the second call (1,023 rows, under --stage-ahead) uses the first call's staged experts, so its sums are split as a larger call's (as read()'s comment says). With the drafter loaded, the read gives the same bits as --stage-read-mib 0, where each call reads on its own. read_groups() sizes its groups from free VRAM, which the drafter lowers. This PR does not change it.
  • With unfrozen experts, vram_ahead() uploads can still send a window's miss to the GPU where a single token computes it on the CPU. That depends on upload timing, as before this PR.
  • Lossy flags (--tail-skip, --route-top, --skip-weight) still act differently on a window than on a single token, as before.

🤖 Generated with Claude Code

🤖 Generated with Claude Code

With the doorbell (bell_on(): Inkling 2 with experts outside VRAM) a
token alone is routed on the card: route_card(), whose gate sigmoid uses
the card's expf.  A window of several tokens (--window N lookup guesses,
the drafter's verify) went to moe(), which routed each row on the host:
route(), the same formula with the host's std::exp.  The two need not
round alike in the last bit, so a guessed token's expert weights (and,
at a near tie, an expert) could differ from the ones it got alone.
Greedy answers with guesses then parted from the answers without them
after some tokens, and since the policy picks the window length from
measured times, from run to run as well.

run() now does for a window what step() does for its rows since
1d4d9e2: where a token alone routes on the card, up() routes the
window's rows there before the router's event, with the picks and
weights written straight to pinned memory (h_ids_, h_wts_, now also allocated for
bells() with a window over 1), and moe() takes them (step_ids_).
step()'s tests move into picks_like_alone() and picks_like_alone(L),
shared by both; step() behaves as before.  Not in a window's halves
(they need reads_by_layer(), which no family with the doorbell has),
not with --cache-bias, and not on HIP or Metal (no doorbell there).

Class: same bits as one token at a time (frozen experts, 2070 Super:
per-layer states and logits of every verified row equal the token
alone's); other families, HIP and Metal do not reach the new code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant