Skip to content

deepseek_v41 handoff: Metal packers, importer, protocol 2 adapter and landing sink - #3564

Open
BlivionIaG wants to merge 40 commits into
ashhart:mainfrom
BlivionIaG:pr/dsv41-handoff-metal-landing
Open

BlivionIaG wants to merge 40 commits into
ashhart:mainfrom
BlivionIaG:pr/dsv41-handoff-metal-landing

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before, nothing in the tree writes the neutral hand-off parts into a Metal lane's caches or reads them out. After, a host-only library does: the packers that write the neutral bf16/f32 parts into the release lane's and the served lane's cache layouts, the importer that accepts exactly the parts a shape implies, the export from a Metal snapshot, the protocol 2 adapter (p2: manifest check and a sink for fabric.decoder.pull), the landing sink and a test-only hand-off file. Export-to-import round trips cover both layouts and replicated and split pools, and protocol 2 runs end to end over a fake link. No server calls it yet. Slice 2 of the chain in #3553.

Checks on this head

  • Head commit: 3f0c7716d2
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 165 problems on upstream/main 4c79cfe346, 165 on this head, 0 new (tools/zig/lean_new.py). tools/pr_check.py --base 4c79cfe346 --pr 3564: 0 errors, 1 advisory warning, inherited from Prefill on a GB10, decode on a Mac: long prompts for Qwen3.8-27B handed over ConnectX (#1594, step 2b)  #2648 (cli_remote.zig:40 names 192.0.2.20, a documentation address).
  • Tests run, with the command and pass, fail and skip counts: the refreshed tip (DeepSeek-V4.1 hand-off: the CUDA producer exports while the prompt prefills and the decoder pulls beside it #3729, d855b9bb5d) runs test-dsv41-handoff 92, test-handoff-routes-host 35, test-dsv41-cuda-host 111, test-lane-api-host 812, test-wait-reasons-host 40 and test-qwen27-reply-cache-host 10, all pass, 0 fail, 0 skip, over the same tree plus the later slices; zig build -Dcpu=apple_m1 -j4 exits 0 there. This head's own steps were not run locally.
  • zig fmt --check build.zig zig and git diff --check: clean.
  • CI: all ten jobs passed and the two nightly jobs were skipped on the previous head 76816f3321 (run 45127082603). The refreshed head reruns them.
  • Not run, and why: zig build test test-golden (full suite) locally, dev-tools, any Metal or GPU device run. The failing-before check for the new tests was not done.

Receipt

n/a: a host-only library; only its tests call it, and it cannot reach inference.

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.

Update after review

ThinkOffApp and others added 19 commits November 14, 2026 10:39
…, sent as KV handoff protocol 2

hybrid_state gives Qwen3.8-27B's recurrent and attention state one byte order, header and token sha256 that CUDA and Metal both read. Protocol 2 sends a recurrent layer's state part by part with a CRC-32 a frame (ARMv8 CRC instructions where present); protocol 1 readers refuse it. The producer table holds exports for a hand-off, refuses a reused id, expires them, and caps the bytes held (HeldExportsFull). fabric_handoff and tf-mcdma-pair tests cover the fake link and the frames. (ashhart#1594 step 2b)
…a kept state, and is kept as remote

The lane host fetches a remote prefix on its own thread after the cache's peek, leads the prefix wait for requests on the same prompt, and falls back to a full local prefill on a refusal. With TF_PREFILL_KEEP (default 1) the states a pulled prefix builds are kept in the prompt cache marked remote, never written to disk or learned from, so a chat's follow-ups resume on the Mac. Tests pin provenance (remote_prefix, pulled_prefix), the keep rules and slot 0's let-go. (ashhart#1594 step 2b)
…ls them over an MKVH link

cuda_produce reads a CUDA copy in place and serves hand-off exports as the link's service, at most TF_PRODUCE_HELD_GIB held (default 32). On the Mac, qwen27_remote pulls a prefix into a blank state, checks model, checkpoint, token sha, TP, geometry and protocol, and refuses a checkpoint whose state is not Qwen3.8-27B's (Qwen3.5-9B) at start. remote_trigger asks the GB10 over bounded HTTP, with TF_PREFILL_API_KEY_FILE's key as a Bearer token and a named 401. A detected mismatch turns remote prefill off; a refusal or timeout does not. The TF_PREFILL_* settings join the environment registry. (ashhart#1594 step 2b)
… a GB10, and the hand-off's API fields

A completion's kv_transfer_params.mcdma_handoff becomes its hand-off; a malformed one now returns 400 on every native server. Replies on a remote prefix report exact_mode "remote_prefix" with remote_prefix, pulled_prefix and kept_remote_cache_tokens. docs/api.md and docs/environment.md describe both. Off unless both machines are started with the new options. (ashhart#1594 step 2b)
…ecks on real weights

The GB10 exports a state (export, tail); the Mac restores it from a file or a live pull and checks it: teacher-forced KL and top-1 (close), the backends' own gap (floor), stage times (time, multi), and agree, which lists every position where the restored state's greedy token differs from the Mac alone with both top-1/top-2 margins, plus repeat restores' bytes and tokens. State files are read in 1 GiB pieces, as macOS refuses one read over 2 GiB. (ashhart#1594 step 2b)
…rrow and no promise

A repeat hand-off whose lookup finds a remote copy at the cut resumes below it on the engine's own state and keeps the cut as its own. When a queued chat protects the remote copy and the budget has no room beside it, the guard refuses that keep (CachePromised). The test shows the export and the reply still go on, the borrow from below() is let go, no job is left holding a promise, and the remote copy still serves the next chat.
…umes on the Mac

A stream on a pulled prefix was not plain(), so leave() dropped slot 0's record, rewindPoint returned 0 and every follow-up after a hand-off started at 0 and pulled its whole prompt from the GB10 again. Such a stream now counts as plain for slot 0: its pass makes a rewind point at the history end past the cut, and leave() keeps the record, marked remote (Standing.remote). A pass that resumes on that record, here or back at its point, takes its rows' origin (inherit: reuse.remote set, reuse.pulled cleared), so its reply reports the prefix as kept remote and its keeps are marked remote, never the Mac's own. A refused copy that ran here from 0 leaves an own record. With TF_PREFILL_KEEP=0 slot 0 keeps no record and no rewind point of a hand-off's rows, and Job.kept and Job.keep keep nothing on a remote prefix. The unplanned cache checkpoint still skips remote streams. The decisions move into qwen27_resume.zig (plain, leftRemote, inherit, rewindAt, Standing.left, Standing.point, Standing.keeps), where host tests pin the hand-off chain, a refused import, each case that keeps nothing (TF_PREFILL_KEEP=0, a cancelled or failed reply, images, prompt logprobs, untaught rows) and a pull that reaches the history end, which makes no rewind point. (ashhart#1594 step 2b)
New host tests (qwen27_handoff_test.zig, "qwen27 hand-off:", run by test-qwen27-reply-cache-host) drive the real lane host with a fake backend over a device-less Metal whose slot 0 choices are qwen27_slots' own: resumeAt as the lane host's resume_peek, opening where slot 0 resumes, rewindPoint and leave. A pulled turn leaves a remote record with its rewind point at the history end; its follow-up pulls nothing, resumes in place where the turn left slot 0 and reports the prefix as kept remote. With TF_PREFILL_KEEP=0 the follow-up pulls again. A pass on slot 0's remote rows makes no unplanned checkpoint, on its own rows it does, and either way it clears a pull it overtook. To reach these host-side, takeStanding inherits the origin once after its switch (here or back), attach's TF_PREFILL_KEEP goes through remote.keepRule (lane host and slot 0 alike), and leave and rewindPoint are pub.
The cache plans a fill's marks without slot 0's in-place resume, so a follow-up that resumes slot 0 (its own rows, or a hand-off's since the last commit) can carry marks below where its pass begins. It never stands at them, yet a request arriving during its pass waited for one until the pass ended, then computed the prefix itself. The prefix wait now skips a leader's marks at or below its stream's cached rows. A request picked in the same admission as the leader, before its first chunk, is not covered: the leader's start is not known yet. Host tests: a request arriving during a fill waits for its cut and resumes it; one arriving during a fill that resumed past the cut does not wait.
…DA export needs

compile-qwen35-rounds-host builds zig/tests/qwen35_cuda_main.zig as its own module, with cuda, qwen35, core and lanes only. Since the main imports qwen35_cuda_export.zig, which reads @import("hybrid_state"), that step stopped compiling. It now gets the same hybrid_state module zig/build/cuda.zig gives tf-qwen35-cuda, built for the census target. (ashhart#1594 step 2b)
inherit now takes the plan and leaves a stream's origin alone for .none, so the decision that a pass resumed here or back at the rewind point stands on the record's rows (remote or not) lives in qwen27_resume.zig, not in takeStanding's arms. The .back arm needs rewindSlot, a GPU blit, so no host test reaches it in qwen27_slots.zig; the rule it calls is pinned by the resume tests, whose helper now calls inherit as takeStanding does instead of deciding by itself. (ashhart#1594 step 2b)
…ixture

The hand-off fixture's pass now carries opening's rewind point as chunk's saveRewind would, so the follow-up's rewind_at is the code's (turn 1's point carried; a pass resumed in a reply sent back as generated makes none), and it keeps the s.cached opening set over the fake's own fill instead of setting it again from the resumed position, so qwen27_slots.opening's line is what the reported remote prefix reads. Slot 0's hand-off rules (qwen27_resume_remote_test.zig) also run in zig build test on every OS; the Metal fixture's tests stay in test-qwen27-reply-cache-host, macOS only. (ashhart#1594 step 2b)
Adds the host-only record library for the DeepSeek-V4.1 prefill hand-off: the release geometry,
the codec between CUDA's fp8 cache records and the neutral bf16/f32 parts, the E2M1/E4M3 FP4
mirrors of the Metal packers, the part plan (what travels, cut into frames of at most 2 MiB)
and the CUDA-side export of a snapshot's copies into those parts. Each piece is tested against
independent tables or f64 references; `test-dsv41-handoff` runs them on the host.

Not included: the Metal packers and importer, protocol 2 adapter, landing sink and stream
file, the CUDA export, and any transport.
… landing sink

Adds the Metal side of the host-only hand-off library: the packers that write the neutral
bf16/f32 parts into the release lane's and the served lane's cache layouts, the importer that
accepts exactly the parts a shape implies, the export from a Metal snapshot, the protocol 2
adapter (`p2`: manifest check and a sink for `fabric.decoder.pull`), the landing sink and a
test-only hand-off file (`blob`). Export-to-import round trips cover both layouts, replicated and
split pools, and protocol 2 runs end to end over a fake link.

Not included: the CUDA export, the byte-stream file, any transport, and the tests and checks
that read the served lane's source.
@BlivionIaG
BlivionIaG marked this pull request as ready for review November 24, 2026 11:10
An E2M1 index block holding 6 and 1.25 came back as 6 and 1: the packers wrote codes
and never checked what those codes decode to. Each packer now dequantises its own bytes
and returns error.NotExact when any value moves, so a record whose values are off the
lane's grid is refused instead of rounded. The synthetic world is generated on that
grid, so the import, landing and protocol-2 cases still land, and a raw fp8 index row
is the refusal they check.
@AIFlowML

Copy link
Copy Markdown
Contributor

I pulled the hand-off sources and build fragment from 97268a6a30 onto 4c79cfe346 in scratch to see which checks could run independently of #2648. The complete host target first stops at handoff/blob.zig: current main's fabric.manifest has no parseUpTo. The protocol-2 adapter also needs the newer manifest.Part, Layer.parts and producer API.

With the protocol-2 and file-landing test imports omitted from the scratch harness, the packer and in-memory importer checks pass on M5 Max. That is 54/54 host tests, including the CUDA-to-Metal round trips, plus both ARM64 test executable builds.

zig build test-dsv41-handoff compile-dsv41-handoff-aarch64-linux compile-dsv41-handoff-aarch64-macos -Dfixed-cpu -j2 --summary all

The packer layer can be reviewed against main now. To rerun the full target independently, the first prerequisite is the protocol-2 fabric interface. The host run does not qualify live checkpoint transfers.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @BlivionIaG. The exact BF16 round-trip refusal is present, and the host checks are useful. One importer gap remains: Release accepts window_slots=1 and ds_window=1 for a 128-row state. The writes overwrite earlier rows, yet complete can succeed. Please reject undersized logical rings before landing and cover both boundaries with distinct-row tests. Live use also needs a defined consumer that preserves exact state and tokens. Current-main integration can stay with us.

  • Synthena

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Merged the refreshed parent (main 4c79cfe346 inside) to clear the conflicts. Head 3f0c7716d2; lean 165 vs 165, 0 new; pr_check 0 errors (the one warning is the documentation IP at cli_remote.zig:40 from #2648); fmt clean; host steps green on an M4 Pro (the #3729 head: handoff 94/94, handoff routes 36/36, CUDA host 478/478, lane API 823/823, reply cache 12/12, environment 36/36; the retire/enqueue race test binary 200/200).

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.

4 participants