Skip to content

Proposal: export a CUDA prompt state and resume it on MLX (Spark prefill → Mac decode), with measurements #77

Closed as not planned

Description

@Boscoeuk

Hi Ash, thanks again for the fast #60 fix (confirmed on our 128-case replay: 115/128 valid on 0.3.6.2, up from 113, and both empty turns are gone).

We run a DGX Spark next to an M3 Ultra. On the Qwen3.8-27B, TensorFold's CUDA prefill reads prompts about 4× faster than the Mac, and the Mac decodes about 2–3× faster than the Spark. We'd like to prefill on the Spark and decode on the Mac, handing the prompt state across the wire. Before asking for anything, we checked it was feasible on unmodified 0.3.6.2. Here's what we found and a small ask.

Setup

  • Vontra/Qwen3.8-27B-MLX-4bit @ 70ae7fac, identical bytes on both machines. TensorFold 0.3.6.2, no source changes.
  • Spark (GB10, NGC PyTorch 26.07, TORCH_CUDA_ARCH_LIST=12.1a): families.qwen3_5.cuda.decode.prefill(w, ids, None), then the State written to safetensors.
  • Mac: mlx-lm's Qwen3.5 core (the forward Qwen35Family.prefill calls), state loaded into make_cache(). Greedy decode here is plain mlx-lm, not the lane decoder.
  • Prompts: TensorFold's own README + CHANGELOG as a user message, 8,925 and 28,227 tokens.

The state maps 1:1

CUDA State MLX cache Change needed
conv[i] (k−1, conv_dim) bf16 ArraysCache[0] (1, k−1, conv_dim) add batch dim
rec[i] (Hv, Dv, Dk) fp32 ArraysCache[1] (1, Hv, Dv, Dk) add batch dim
kv[i] (T, kv_heads, head_dim) bf16 KVCache (1, kv_heads, T, head_dim) add batch dim, swap T and heads

About 64 KiB per prompt token plus about 150 MiB fixed: 1.9 GiB at 28K. Import is an assignment. Transfer should take about 2 s over 10 GbE, and less over RDMA; that's an estimate, not yet measured (our test copy went over SSH).

Results (28,227-token prompt, greedy, 64 tokens)

Spark prefill Spark tok/s Tokens equal to the Mac's own prefill Min cosine, attention K/V Min cosine, DeltaNet state
default (fast_prefill, FP8 matmul inputs) 1,593 40 0.850 0.949
fast_prefill forced off (bf16 inputs) 469 55 0.964 0.997
Mac vs itself (2,048- vs 4,096-token chunks) 400 (Mac) 33 0.968 0.997

At 8,925 tokens the FP8 import matched the Mac's own prefill on all 64 tokens (as did the Mac's chunk control). The first token was equal in every case.

Where the gap comes from: per-position cosine of the imported K/V against the Mac's own, in 2,048-token buckets:

  • FP8: layer 3 flat at 0.998. Layer 63 falls from 0.98 to 0.77 across the prompt. K and V drift together, so it isn't RoPE; it compounds with depth and position.
  • bf16: layer 3 is 1.000 everywhere, and layer 63 only falls from 0.998 to 0.94.

So the handover itself is sound. With bf16 prefill inputs the imported state is as close to the Mac's own as the Mac is to itself under a different chunk plan, and on greedy agreement it's closer. Your README already accepts that tolerance across machines ("chunk size follows the memory budget"). The drift comes from the FP8 prefill inputs. But bf16 prefill is only about 1.2× the Mac, so an exact handover currently gives back the Spark's speed.

The ask (small, and we'll do the testing)

  1. Export/import in the existing snapshot format. Something like tensorfold export-prompt on CUDA (or a POST that returns the prefix_snapshots safetensors for a token prefix), and a Mac resume path that accepts it. The only real design point: the snapshot identity (model_id includes kernels, prefill plan and MLX version) needs a "cross-engine handover" identity keyed on checkpoint + revision + tokens, with the consumer opting in.
  2. A handover-safe fast prefill. An option to keep FP8 inputs only where they stay inside the Mac's own chunk-plan tolerance. Our guess, not measured, is that early layers are fine and late layers need bf16, but you'll know where FP8 does the most work. A per-layer cutoff flag would let us sweep it and report the speed/fidelity curve back.

Our acceptance bar: the imported state diverges from the Mac's own no earlier than the Mac's chunk-plan control does, needles pass, and our 128-case agent replay is unchanged. We'll run the full sweep on every build you tag and post results here. We're happy to open a PR for (1) if you'd rather review than write it.

Scripts and raw numbers are available on request. The export is about 30 lines; the Mac import is about 20.

Activity

  1. ashhart commented on Sep 30, 2026

    @ashhart
    Owner

    Thanks for doing the feasibility work before asking, and for the exact numbers: 55 of 64 tokens matching with bf16 inputs against 40 with FP8 made the trade-off easy to see. We're going to handle Spark prefill → Mac decode through mcdma (mcdma.dev), not inside TensorFold itself, so we'll close this here and carry it on there. Your measurements are a useful baseline for that work.

  2. Boscoeuk commented on Sep 30, 2026

    @Boscoeuk
    ContributorAuthor

    Thanks Ash, that makes sense. Doing it in MCDMA keeps TensorFold clean. We'll follow it there. Our Studio + Spark rig is set up for it (10GbE baseline at 9.9 Gb/s, and the CX5/Helios path on #9), so we're glad to test builds whenever they're useful.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions