Skip to content

inkling2: enable SuffixLookup prompt copy drafting and DSpark copy_first - #5023

Open
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-4-suffix-lookup-rebased
Open

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-4-suffix-lookup-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, lanes.Model already defaults .copy_drafts = true (with .copy_match = 8) and dspark.Drafter.factsFn() already sets .copy_first = true; however, backend.Metal.facts() did not wire the CLI --drafts / --no-drafts switch through backend.Options or lower the plain threshold to .copy_match = 4, and dspark.Drafter.factsFn() kept the default match threshold rather than explicitly setting .copy_match = 12.

After:

  • zig/src/families/inkling2/metal/backend.zig: Plumbs drafts: bool = true through Options, wiring .copy_drafts = self.options.drafts to the drafts switch and setting plain .copy_match = if (self.options.drafts) 4 else 8 in Metal.facts().

  • zig/src/families/inkling2/metal/dspark.zig: Sets .copy_match = 12 alongside the existing .copy_first = true in dspark.Drafter.factsFn() so DSpark skips draft-model hold() calls when a >= 12-token prompt suffix match is available and falls back cleanly to DSpark otherwise while still absorbing target rows.

  • zig/src/native/inkling2.zig: Passes .drafts = o.drafts to backend.Metal.init(...) and adds a startup unit test checking draft_sources.copies under --drafts / --no-drafts across 1 and 2 lanes and DSpark copy_first / copy_match = 12 reporting.

  • zig/src/native/inkling2_copy_fixture.zig, zig/src/native/inkling2_copy_test.zig, zig/src/native/inkling2_serving_test.zig, zig/src/inkling2_metal_host_test.zig: Add deterministic multi-stream (N=2), threshold (3/4/6/12), warm prompt-cache reuse, cancellation rollback, SuffixLookup.proposeAfter graft parity, DSpark copy_first hold-skip/fallback, and real-checkpoint copy-draft parity host tests.

  • Issue: Refs Inkling 2 Mini Metal V4: exact-serial scaling, GPU sampling, DFlash 2 acceleration, and kernel fusions #4961

  • Stacked on: main (9961ca4920, git merge-tree --write-tree clean: 7f89cff548935fafe4bbd9882ec1b54573f0e899)

Checks on this head

  • Head commit: d6e6286493
  • Lean checker, python3 tools/pr_check.py --base origin/main --pr 5023 (python3 tools/zig/lean_check.py): lean 164 vs 164, 0 new, 0 error(s) (7 changed files, max touched file 597 lines); zig fmt --check build.zig zig and git diff --check origin/main..HEAD exit 0.
  • Tests and build checks run:
    • zig build compile-inkling2-native-host: exit 0.
    • zig build compile-inkling2-native-host -Dtarget=x86_64-windows: exit 0.
    • zig build test-inkling2-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 45 pass, 4 skip (49 total) (0 failed; skipped: 4 optional env-gated checkpoint/tokenizer tests).
    • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast test binary (--seed=0xcc8ea13f): 117 passed; 7 skipped; 0 failed (skipped: env-gated checkpoint/alloc tests and 2 JPEG tests pending matching libjpeg-turbo).
    • zig build tf-kernel-contract -Doptimize=ReleaseFast on Apple M5 Max (applegpu_g17, no .metal kernel modified); all 8 non-DSpark metal inkling2 suites exit 0:
      • metal inkling2 attention: SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 2668 ms, limits 76 ms
      • metal inkling2 prompt: SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 1363 ms, limits 62 ms
      • metal inkling2 glue: SUMMARY 10 kernels: 10 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 392 ms, limits 601 ms
      • metal inkling2 experts: SUMMARY 12 kernels: 12 pass gates 1-4, 0 fail, 0 known findings; 17 controls: 17 red as planted, 0 missed; 0 limit problems; gates 4402 ms, limits 547 ms
      • metal inkling2 mma: SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 0 ms, limits 63 ms
      • metal inkling2 loop: SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed; 0 limit problems; gates 158 ms, limits 62 ms
      • metal inkling2 runtime: SUMMARY 6 kernels: 6 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; gates 38 ms, limits 26 ms
      • metal inkling2 vision: SUMMARY 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 10 controls: 10 red as planted, 0 missed; 0 limit problems; gates 972 ms, limits 541 ms
      • Note on metal inkling2 dspark: on base 9961ca4920, --only "metal inkling2" hit the pre-existing dspark_kernels.zig:122-125 error: DsparkThreads bug (untouched here); that thread-guard fix (inkling2 metal dspark: match maxThreads guard to kappa (64) and attention (768) #5041) is now merged on main.
  • CI: Rebased on origin/main (9961ca4920); CI checks pending after force-push.
  • Not run, and why: Apple M1/M2/M4/M5 Pro served A/Bs were not run on this slice; 2-Mac served A-B-B-A receipts cover Apple M5 Ultra 512 GiB and Apple M3 Ultra 512 GiB across edit/copy-heavy workloads (plain and DSpark) and non-copy regression guards.

Receipt

  • Main against branch: A-B-B-A served runs (0 cross-leg token_sha256 mismatches across every cell on both machines; .copy_match = 12 for DSpark copy_first and .copy_match = 4 for plain copy drafting):
    • Apple M5 Ultra 512 GiB (main vs branch):
      • Edit/copy-heavy, plain (no DSpark):
        • distinct c=1: 68.75 -> 75.05 tok/s (+9.16%, TTFT +0.15%)
        • distinct c=4: 9.10 -> 8.60 agg tok/s (-5.49% due to first-prompt TTFT variance 39.7->45.3s over a 42.5s window), while steady decode throughput (decode_tps) improved 4.517 -> 4.820 tok/s (+6.7%).
      • Edit/copy-heavy, DSpark (--drafter {TARGET}, copy_match = 12):
        • distinct c=1: 66.20 -> 68.15 tok/s (+2.95%)
        • distinct c=4: 8.55 -> 9.50 tok/s (+11.11%, decode_tps 3.562 -> 4.782 tok/s, +34.2%)
      • Non-copy guard, DSpark (dspark_guard):
        • code c=1: 26.30 -> 26.10 (-0.76%, TTFT 0.315 -> 0.227s, -27.98%)
        • code c=4: 28.00 -> 28.05 (+0.18%, TTFT -2.79%)
        • chat c=1: 21.20 -> 21.70 (+2.36%, TTFT -2.12%)
        • chat c=4: 19.45 -> 19.45 (0.00%, TTFT -1.62%)
    • Apple M3 Ultra 512 GiB (main vs branch):
      • Edit/copy-heavy, plain (no DSpark):
        • distinct c=1: 35.35 -> 37.45 tok/s (+5.94%, TTFT -11.20%)
        • distinct c=4: 3.70 -> 3.80 tok/s (+2.70%, decode_tps 1.717 -> 1.741)
      • Edit/copy-heavy, DSpark (--drafter {TARGET}, copy_match = 12):
        • distinct c=1: 35.30 -> 36.00 tok/s (+1.98%, TTFT +2.90%)
        • distinct c=4: 3.75 -> 3.95 tok/s (+5.33%, decode_tps 1.502 -> 1.869 tok/s, +24.4%)
      • Non-copy guard, DSpark (dspark_guard, copy_match = 12):
        • code c=1: 17.15 -> 17.15 (0.00%)
        • code c=4: 14.90 -> 14.90 (0.00%, TTFT -3.95%)
        • chat c=1: 15.25 -> 15.70 (+2.95%)
        • chat c=4: 10.30 -> 10.30 (0.00%)
  • Boxes it reaches: backend.zig, dspark.zig, native/inkling2.zig, native/inkling2_copy_fixture.zig, native/inkling2_copy_test.zig, native/inkling2_serving_test.zig, inkling2_metal_host_test.zig.
  • New switch? No (--drafts / --no-drafts already exists; this wires drafts to backend.Metal alongside Model's existing .copy_drafts = true default and DSpark's existing .copy_first = true, updating plain .copy_match (4/8) and DSpark .copy_match = 12).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit d6e6286493 on main (9961ca4920), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M3 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches, which I ran and which I could not: Inkling 2 Mini Metal serving behind lanes (SuffixLookup prompt copies and DSpark hybrid drafting); verified locally and served across Apple M5 Ultra and Apple M3 Ultra; Apple M1/M2/M4/M5 Pro served A/Bs not directly run on this slice.
  • Exactness: 0 cross-leg token_sha256 mismatches across all plain, DSpark edit, and DSpark non-copy guard cells on both Apple M5 Ultra and Apple M3 Ultra; host tests prove token/logprob parity vs --no-drafts.
  • Decode speed before and after: See A-B-B-A tables above (edit workloads up to +9.16% plain / +11.11% DSpark on M5 Ultra, +5.94% plain / +5.33% DSpark on M3 Ultra; non-copy DSpark guard -0.76%..+2.36% M5 Ultra and 0.00%..+2.95% M3 Ultra).
  • Prompt speed before and after: Host-side prompt matching adds zero GPU prompt work and preserves or improves TTFT on non-copy guards (-1.62%..-27.98% M5 Ultra, 0.00%..-3.95% M3 Ultra).
  • Licence of any new weights or drafter: n/a.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled.

@AIFlowML

Copy link
Copy Markdown
Contributor

I independently trial-merged 2926de502e cleanly into 47a605aa12 on M5 Max. The four targets below passed 159 tests with 11 skips, covering the two-lane suffix-copy fixture, warm reuse, cancellation rollback and the DSpark hold/fallback cases.

zig build test-inkling2-host test-inkling2-metal-host test-inkling2-launch-map-host \
  compile-inkling2-native-host -Dfixed-cpu -j2 --summary all

The #5020 Q4 change also fits with this head once its new shader keeps the 256-thread reservation. I posted that independent current-main finding on #5020. I ran the host fixtures here. The optional checkpoint-backed cells and served throughput were outside this run.

Igor Lessio - AIFLOW LABS

@gilby
gilby force-pushed the v4-4-suffix-lookup-rebased branch from 2926de5 to d6e6286 Compare November 25, 2026 12:39
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The target forward, verification, sampling and cache operations stay unchanged in source, and copied tokens still pass through target verification. This looks ready for a controlled comparison, including positive copy activity, plain/drafted equality and warm-cache checks.

Please correct the baseline description: Model already defaults copy_drafts to true, and DSpark already sets copy_first. This head explicitly wires the drafts switch and changes the plain and DSpark match thresholds. The separate DSpark thread-guard fix is needed for its current load path.

  • Synthena

@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR description to reflect the main baseline accurately: lanes.Model already defaults .copy_drafts = true and DSpark already sets .copy_first = true; this change wires drafts through backend.Metal / Options and adjusts the plain (.copy_match = 4 / 8) and DSpark (.copy_match = 12) thresholds. Also noted that #5041 (DSpark thread-guard fix) is now merged on main.

  • Kevin

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@gilby, our A/Bs of d6e6286493 against main on Inkling-2-Mini, with tokens equal in every row on both Macs:

  • M5 Ultra: 4-stream code decode +3.0%, the rest level.
  • M3 Ultra: decode after a 2,048-token prompt is slower in every phase and repetition: main 18.18 to 18.20 tok/s, this head 17.28 to 17.34 (about -4.9%). The short-prompt sweeps are within ±1%, except 4-stream code at temperature 1.0 at -3.0%.

Our bar is no slower cell, so this can't land yet. Our guess is that the prompt-copy drafts cost more on M3's slower verify than they win back after a long prompt. Could you look at whether the copy thresholds should depend on the measured verify cost, or skip copy drafting where it doesn't pay, so M3 stays at main's speed? We'll rerun both Macs on the next head.

  • Synthena

@gilby
gilby force-pushed the v4-4-suffix-lookup-rebased branch from d6e6286 to f95bc47 Compare November 26, 2026 10:14
@gilby

gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated #5023 (f95bc4760c, rebased cleanly onto origin/main 0102f73721, clean merge against current origin/main 6b03d41768) to fix both Apple M3 Ultra regressions (-4.9% after a 2,048-token prompt and -3.0% on 4-stream code at T=1.0):

Root Cause & Fix

  • On d6e6286493, lowering copy_match from 8 to 4 while leaving copy_guard = false, own_copy_rates = false, and h.metal.measure() skipped when drafter == null allowed coincidental 4-token matches to trigger unpriced verify rounds on M3 Ultra.
  • In backend.zig, facts() now sets .copy_drafts = d, .copy_guard = d, .own_copy_rates = d, .copy_match = 8 (keeping window_costs / shared_costs populated) and dspark.zig keeps .copy_first = true, .copy_match = 12.
  • In native/inkling2.zig, h.metal.measure() now also runs for plain multi-stream serving (drafter != null or (o.drafts and streams > 1)) so 4-stream shared copy windows are priced against measured batch verify costs.
  • Added deterministic host tests in inkling2_copy_test.zig verifying copy_guard skips unprofitable copies on a high-verify-cost curve and own_copy_rates + shared_costs trim breaking copy streams at T=1.0, while keeping bit-exact token parity.

Interleaved A-B-B-A Verification (origin/main 0102f73721 vs f95bc4760c, ReleaseFast -Dcpu=apple_m1, --parallel 4 --context 8192 --prompt-cache-gib 0 --snapshot-dir none --max-snapshots 0)

Tokens equal (0 cross mismatches) and pmset -g therm nominal (No thermal warning level / No performance warning level) on every leg on both Macs.

Apple M3 Ultra 512 GiB (plain serving, copy drafts active)

Cell origin/main (A1/A2) #5023 (B1/B2) Delta Cross Mismatches
2,070-tok prompt -> 128-tok decode (c=1, T=0) 20.03 tok/s (20.04, 20.00, 19.85, 20.22) 20.14 tok/s (20.08, 20.02, 20.19, 20.26) +0.55% 0
code T=0 c=1 27.35 tok/s 27.35 tok/s 0.00% 0
code T=0 c=4 41.65 tok/s 41.75 tok/s +0.24% 0
chat T=0 c=1 27.55 tok/s 27.65 tok/s +0.36% 0
chat T=0 c=4 43.50 tok/s 43.35 tok/s -0.34% 0
code T=1 c=1 28.20 tok/s 28.40 tok/s +0.71% 0
code T=1 c=4 42.15 tok/s 42.05 tok/s -0.24% 0
chat T=1 c=1 27.70 tok/s 27.65 tok/s -0.18% 0
chat T=1 c=4 43.65 tok/s 43.60 tok/s -0.11% 0
edit (c=1, T=0) 26.90 tok/s 26.95 tok/s +0.19% 0

Apple M5 Ultra 512 GiB

Cell origin/main (A1/A2) #5023 (B1/B2) Delta Cross Mismatches
2,070-tok prompt -> 128-tok decode (c=1, T=0) 26.75 tok/s 26.81 tok/s +0.22% 0
code T=0 c=1 35.80 tok/s 36.20 tok/s +1.12% 0
code T=0 c=4 55.45 tok/s 55.70 tok/s +0.45% 0
chat T=0 c=1 36.05 tok/s 36.35 tok/s +0.83% 0
chat T=0 c=4 57.55 tok/s 57.55 tok/s 0.00% 0
code T=1 c=1 36.55 tok/s 36.55 tok/s 0.00% 0
code T=1 c=4 55.45 tok/s 55.60 tok/s +0.27% 0
chat T=1 c=1 36.10 tok/s 36.45 tok/s +0.97% 0
chat T=1 c=4 57.55 tok/s 57.45 tok/s -0.17% 0
edit (c=1, T=0, plain) 36.05 tok/s 35.80 tok/s -0.69% 0
edit (c=1, T=0, --drafter DSpark) 43.85 tok/s 44.25 tok/s +0.91% 0

Host & Lint Checks

  • zig fmt --check build.zig zig & git diff --check: clean
  • python3 tools/pr_check.py --base origin/main --pr 5023: 0 errors (7 files, 0 new lean, 0 new import cycle edges)
  • zig build test-inkling2-host test-shared-lane-regressions-host -Dcpu=apple_m1: 568/573 passed (47 pass, 4 skip inkling2 host; 521 pass, 1 skip shared lane regressions).

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. We re-checked the latest head and found no correctness problem in the production path, so it's clear for our Mac device runs.

One gap in the test: the checkpoint parity test can pass without any copy drafting happening. Please make it assert a positive drafted/accepted copy count, so it can't go green with SuffixLookup idle. We'll include copy-heavy cells and a warm prompt-cache case in the device run.

  • Synthena

@gilby

gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated #5023 (3d74d9f789) to close the checkpoint parity test gap:

  • zig/src/native/inkling2_serving_test.zig:
    • Added copyPrompt() (6 × 16 + 8 = 104 tokens: a distinct 16-token span repeated 6 times plus its 8-token prefix so greedy continuation triggers SuffixLookup at copy_match = 8).
    • Added expectCopyParity(plain, copied) asserting bit-identical tokens and per-token logprob rows, plain.drafted == 0, plain.accepted == 0, copied.drafted > 0, and copied.accepted > 0, so the checkpoint parity test cannot pass with SuffixLookup idle.
    • Added a host unit test verifying copyPrompt() triggers copy_match = 8 (15 copied tokens) and that expectCopyParity fails whenever copied.drafted == 0 or copied.accepted == 0.
  • zig/src/native/inkling2_copy_fixture.zig / inkling2_copy_test.zig: updated Proposers type reference (@FieldType(api.LaneHost, "proposers")) for current main.
  • Verification (Apple M5 Max 48 GiB):
    • python3 tools/pr_check.py --base origin/main: 0 errors
    • zig build test-inkling2-metal-host compile-inkling2-native-host: PASS (136/144 passed, 8 checkpoint-gated skipped cleanly without TF_INKLING2_METAL_DIR).

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you for adding the positive copy-activity assertions and the separate zero-count controls. The latest head merges cleanly with current main.

The earlier source asks are now covered: the baseline description is corrected, the measured verify-cost guards and shared-cost measurements are present, and checkpoint parity now requires both positive drafted and accepted counts. The new controls can fail when either count stays zero. We found no new production correctness problem in the static re-check.

The remaining work is the device comparison on the latest head. We still need M3 and M5 results for the long-prompt and sampled four-stream regressions, copy-heavy cells, and a warm prompt-cache second turn. The real-checkpoint parity test also needs to execute rather than skip. Each copy-heavy run should show positive activity, exact plain/drafted output, and no slower cell.

Thank you for closing the test gap cleanly.

  • Synthena

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. We ran this on an M3 Ultra with Inkling 2 Mini, main against this head, two runs each in the order base, new, new, base.

  • Tokens: identical in all 20 prompt rows, so the copy drafting is exact.
  • Prompt at 2,048 tokens, first phase from position 0: 447.5 to 435.9 tokens/s for the prompt pass and 33.1 to 32.3 for decode, -2.6% each. Both runs of each side agree (447.5 and 447.5 against 436.2 and 435.6), so this is a steady cost rather than noise.
  • Every other cell is flat: the repeated 2,048 phase, starts 5,000 and 10,000, 8,192 tokens, and the 1, 2, 4 and 8 stream sweeps of chat and code prompts at temperature 0 and 1 are all within 0.6%.

So on these prompts the change costs about 2.6% on a first prompt and gains nothing elsewhere. Default-on behaviour has to be at least as fast everywhere. Could you either keep the lookup's setup off the first prompt's critical path, or make the copy drafting opt-in, and show the prompts where it wins (for example long code with repeated spans) with tokens and timings against main? Either way the exactness result above holds.

  • Synthena

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.

3 participants