Skip to content

inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) - #4917

Open
cwschroeder wants to merge 1 commit into
ashhart:mainfrom
cwschroeder:pr/inkling2-metal-dflash2-main
Open

cwschroeder wants to merge 1 commit into
ashhart:mainfrom
cwschroeder:pr/inkling2-metal-dflash2-main

Conversation

@cwschroeder

Copy link
Copy Markdown
Contributor

Summary

Adds native Metal speculative decoding support for DFlash2DraftModel (such as incoai/Inkling-2-Mini-DFlash2) alongside the existing DSpark drafter on --drafter DIR.

When --drafter DIR is passed, zig/src/native/inkling2.zig inspects config.json and auto-selects DFlash2 (architectures containing DFlash2DraftModel) or DSpark, reporting kind = "DFlash2" in Engine.Info.

What Changed

  • Weights & config validation (zig/src/families/inkling2/metal/dflash2_weights.zig): Strict config.json and 81-tensor model.safetensors loader validating target_layer_ids = [5, 23, 41, 48], block_size = 8, 5 Qwen3 layers, sliding_window = 2048, valid_vocab = 200058, selector_top_k = 16, selector_softcap_mode = "after_correction", and final_logit_softcapping = 30.0.
  • Metal kernels (zig/kernels/metal/inkling2/dflash2.metal, dflash2_sources.zig, inkling2_sources.zig): 11 pure-BF16 kernels (tf_ink2_df2_*) covering target-hidden concat/projection/RMSNorm (fc), masked block embedding with embed_norm, Qwen3 blocks with 2-tap dynamic depthwise convolution (conv_proj), 2048-token sliding-window GQA 4:1 attention on a fixed 2176-slot KV ring, SwiGLU MLP, 1-pass radix top-16 candidate selection over valid_vocab using the shared target lm_head, and exact after_correction softcapping plus greedy path selection.
  • Runtime & adaptive depth timing (zig/src/families/inkling2/metal/dflash2.zig, dflash2_kernels.zig, backend.zig, zig/src/native/inkling2.zig): Fixed 44.5 MiB/stream KV ring per Lane, async absorb pipelined with target verification, and warm() timing of window_costs (1..16 rows) so lanes adaptive depth selection unlocks full 7-token draft blocks instead of falling back to depth 1-3.
  • Contracts & host tests (zig/src/core/contract/inkling2_dflash2_metal_records.zig, registry_joins.zig, contract.zig, zig/tests/contract/metal_limits.zig, zig/src/families/inkling2/metal/host_test.zig): Registers all 11 tf_ink2_df2_* Metal launch records in Gate-5 contracts, shader limits verification, and host unit tests for weight config parsing, ABI sizes, and ring addressing.

Local Validation

Run against origin/main (00e0e9878) with Zig 0.17.0:

  • zig fmt --check build.zig zig — pass
  • python3 tools/pr_check.py --base origin/main — 0 errors, lean 165 vs 165, 0 new
  • zig build test-kernel-contract — pass (1272 registered launches, 2 controls, 0 missing)
  • zig build test-metal-launch-map-host — pass
  • zig build test-inkling2-controls-host — pass
  • zig build test-inkling2-launch-map-host — pass
  • zig build test-inkling2-host — pass
  • zig build test-inkling2-metal-host — pass
  • zig build compile-inkling2-native-host — pass
  • zig build native -Doptimize=ReleaseFast — pass
  • Live greedy bit-exact check on Inkling-2-Mini + Inkling-2-Mini-DFlash2 (temp=0.0, 128 tokens, "Write a Python AVL tree."):
    • --no-drafts: 127 rounds, SHA d0993c9ef225
    • --drafter <DFlash2>: 39 rounds (3.28 tok/round, 89/237 accepted drafts, 9.53 ms draft block), identical SHA d0993c9ef225 (2.67x speedup on unoptimized main)

@AIFlowML

Copy link
Copy Markdown
Contributor

I trial-merged 75669dd87b into 4c79cfe346. The three current conflicts are in the Metal source list, the contract registry and the native entry point. In scratch I kept main's vision wiring and budget contribution alongside the new drafter selection and reservation. The M5 Max host and native compile gates passed 151 tests with ten skips.

zig build test-inkling2-host test-inkling2-metal-host compile-inkling2-native-host test-inkling2-launch-map-host -Dfixed-cpu -j2 --summary all
zig build tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2 dflash2'

The device run validates all 11 DFlash2 launch limits, then prints SUMMARY 0 kernels and 0 controls for the numerical gates. The launch records are present, but the runner has no DFlash2 numerical case wired. The first missing reproducible check is one numerical Metal gate with its planted control. The posted live transcript A/B covers a different check.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @cwschroeder. The live greedy comparison includes accepted drafts and is useful evidence.

Please guard the previous token before the walk's codebook read. A non-finite score row produces invalid candidate IDs; the next slot uses that invalid address before the host checks it.

The launch-limit records are present, but the filtered contract run reports zero numerical kernels and controls. Please wire numerical cases and planted controls, including this exceptional-value path. Keep every proposal checked by the unchanged target.

  • Synthena

@cwschroeder

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart and @AIFlowML for the reviews and trial merge. Rebased onto main (4beda4249) and updated in f6cbbbba8:

  1. Guarded tf_ink2_df2_walk codebook reads: Before reading A[prev * rank], the walk now checks prev >= a.vocab, skips the codebook read, and writes 0xffffffffu for the current and remaining slots so non-finite score rows or invalid candidate IDs never dereference out-of-bounds addresses before host validation. Candidate reads from B are likewise guarded with cand < a.vocab, and non-finite scores (!isfinite(s)) are rejected. The target verification path remains unchanged.
  2. Numerical Metal contract gates and planted controls: Added zig/tests/contract/metal_inkling2_dflash2.zig, zig/tests/contract/metal_inkling2_dflash2_ref.zig, and zig/tests/contract/metal_inkling2_dflash2_controls.zig (wired into metal_inkling2_main.zig and metal_inkling2_controls_host.zig) covering all 11 DFlash 2 kernels plus 13 planted controls, including dedicated .nonfinite gates and controls for both topk16 (NaN, +inf, -inf rows -> (-inf, 0xffffffffu)) and walk (invalid prev after a non-finite slot 0 -> guarded codebook skip and 0xffffffffu propagation).
  3. Resolved main conflicts and Windows CI: Kept main's vision wiring and budget accounting alongside DFlash 2 selection/reservation, and switched now() in backend.zig and dflash2.zig to sys.c.clock_gettime(sys.c.CLOCK.MONOTONIC, ...) so x86_64-windows builds cleanly.

Verification on Apple M5 Max (f6cbbbba8):

  • zig build test-inkling2-host test-inkling2-metal-host compile-inkling2-native-host test-inkling2-launch-map-host test-inkling2-controls-host -Dfixed-cpu -j2: pass
  • zig build compile-inkling2-native-host -Dtarget=x86_64-windows -Dfixed-cpu -j2: pass
  • ./zig-out/bin/tf-kernel-contract --only 'metal inkling2 dflash2': SUMMARY 11 kernels: 11 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems
  • python3 tools/pr_check.py --base origin/main: 0 errors, 0 new lean findings

@cwschroeder
cwschroeder force-pushed the pr/inkling2-metal-dflash2-main branch from 75669dd to f6cbbbb Compare November 25, 2026 08:56
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the revised head f6cbbbba8e against b6ab6dbfd7 after the DSpark snapshot landed. The only conflict is in zig/src/native/inkling2.zig. In a local resolution, I kept the new DSpark snapshot field assignment only for the .dspark case, used d.iface() for both drafters, and retained main's cache budget after subtracting the drafter reservation.

zig build test-inkling2-host test-inkling2-metal-host compile-inkling2-native-host test-inkling2-launch-map-host -Dfixed-cpu -j2 --summary all passed all 13 steps, with 153 tests passed and 10 skipped on M5 Max. This checks the integration with the landed snapshot path at host and compile scope. The revised numerical controls and live checkpoint parity are separate checks.

Igor Lessio - AIFLOW LABS

@cwschroeder

Copy link
Copy Markdown
Contributor Author

Rebased onto main (b6ab6dbfd) as 867fcf6e4, preserving #4542's DSpark prompt-cache snapshot hook; DFlash 2 prompt-cache KV-ring snapshotting is left as a follow-up (tracked as V4-5 in #4961, since absorbFn safely advances lane.from on cached restores).

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @cwschroeder. The previous-token guard and bounded candidate reads are present. The numerical suite and exceptional-value controls are now wired, so those earlier asks are closed in source.

One new setup issue remains: both collector helpers publish a probe before the fallible kernel-list append. If that append fails, local cleanup frees it while parent teardown still owns its pointer. Please make publication transactional and cover that failure for ordinary and control probes. Keep every proposal target-verified.

  • Synthena

@cwschroeder
cwschroeder force-pushed the pr/inkling2-metal-dflash2-main branch from 867fcf6 to 9a4a161 Compare November 25, 2026 14:20
@cwschroeder

Copy link
Copy Markdown
Contributor Author

Fixed in 9a4a1619c (rebased onto main e002bc620, preserving #4699, #4919, #4756, and #4981):

  • Transactional probe publication: Both addKernel and addControl in zig/tests/contract/metal_inkling2_dflash2.zig now publish through controls.publishProbe, which reserves capacity in both s.probes and s.kernels before committing ownership with appendAssumeCapacity, so a kernel-list OOM frees the probe once locally without leaving a dangling pointer for parent teardown.
  • Failure-path coverage: Added FailingAllocator OOM tests for both ordinary and control probes in zig/tests/contract/metal_inkling2_dflash2_controls.zig (test-inkling2-controls-host: 27/27 pass, 0 leaks) and zig/tests/contract/metal_inkling2_dflash2.zig.
  • Verification on Apple M5 Max (9a4a1619c):
    • ./zig-out/bin/tf-kernel-contract --only 'metal inkling2 dflash2': SUMMARY 11 kernels: 11 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems
    • zig build test-inkling2-host test-inkling2-metal-host compile-inkling2-native-host test-inkling2-launch-map-host test-inkling2-controls-host -Dfixed-cpu -j2 + compile-inkling2-native-host -Dtarget=x86_64-windows + test-kernel-contract (290/290) + zig fmt --check build.zig zig + pr_check.py --base origin/main (0 errors): all pass.
    • Target-verified greedy parity on Inkling-2-Mini (64 tokens, T=0): plain sha=edd9190ece69 (33.5 tok/s) matches --drafter Inkling-2-Mini-DFlash2 sha=edd9190ece69 (accepted=48/99, 62.8 tok/s).

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@cwschroeder, about the probe-registration cleanup our review asked for: the same two-append pattern was on main in 85 places, and #5058 (now merged) fixes it there with one helper, contract.register. It reserves room in both lists before publishing, so a failed append can no longer free a probe the suite still holds:

errdefer p.deinit(); // with errdefer s.a.destroy(p) after create, as the suite's teardown frees it
try ct.register(s.a, &s.probes, &s.kernels, p, p.kernel());

Using it for this PR's new suites closes that item. zig/src/core/contract/register_test.zig shows the failure cases it covers.

  • 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