From 8ecb2819ca0035df30b55e09af4690c0eef0750a Mon Sep 17 00:00:00 2001 From: jonathan308 <5055076+jonathan308@users.noreply.github.com> Date: Tue, 10 Nov 2026 09:40:57 -0800 Subject: [PATCH 1/4] fix(provider): pin the SDK's batched, multi-core checkpoint export coding After a request's last token, the provider publishes the prompt's complete checkpoint before the terminal. At the default `balanced` checkpoint precision that export read its attention rows one row at a time and coded each on one core; under GLM-5.3-Flash's native profile each read also revalidated the native binding. On an M5 Ultra (0.10.12 candidate #1958 + #1964) the stream stayed open 63.0 s after a 200k-token prompt's last token and 17.3 s after a 57k-token one, at ~28 us per latent row, with the slot charged throughout. Production on 0.10.11 (12 h, request_profiles: terminal_built_us - last_delta_us): Gemma 4 26B 4-8k / 8-32k prompts p50 1.24 / 1.62 s, GPT-OSS 20B 8-32k p50 1.65 s, GLM-5.3-Flash >= 128k p50 51 s; for OpenRouter requests with prompts >= 4k the tail is 28 % of Gemma's generation time (p50 101.5 -> 68.3 tok/s) and 17 % of GLM's. SDK pin 67110e7b -> bca3aa05 (mlx-swift-lm branch fix/checkpoint-export-batched-row-reads, on SDK main 093563be, so the pin also carries 093563be's merges, as #1671, #1944 and #1614 do): - afe380af test (red): a native-bound export reads coded rows in batches; the row-by-row reference oracle at segment sizes 1 to whole. - 2eb747f3 fix: read a head's consecutive rows in batches of up to 256 KiB. - bca3aa05 perf: code a batch on up to four cores (a quarter of the active ones), rotate eight lanes at a time; identical bytes and first refusal. The checkpoint bytes are unchanged, so restores and answers are exactly as before. Docs: inference.md (donor publication), ssd-kv-cache.md (conversion row); changelog fragment checkpoint-export-batched-rows. Co-Authored-By: Claude Opus 5.5 --- changelog.d/checkpoint-export-batched-rows.md | 4 ++++ docs/architecture/inference.md | 17 +++++++++++++---- docs/reference/ssd-kv-cache.md | 4 ++-- libs/mlx-swift-lm | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 changelog.d/checkpoint-export-batched-rows.md diff --git a/changelog.d/checkpoint-export-batched-rows.md b/changelog.d/checkpoint-export-batched-rows.md new file mode 100644 index 0000000000..84efba7420 --- /dev/null +++ b/changelog.d/checkpoint-export-batched-rows.md @@ -0,0 +1,4 @@ +### A stream that stores its prompt's checkpoint no longer waits for row-by-row coding + +- When a request's prompt checkpoint is written to the SSD prefix tier at a quantized checkpoint precision (`balanced`, the default), the response's terminal waits until the checkpoint is written. Writing it read the checkpoint's attention rows one row at a time and coded each on one CPU core. On GLM-5.3-Flash every read also rechecked the native binding, so on an M5 Ultra the stream stayed open 63 s after a 200k-token prompt's last token and 17 s after a 57k-token one; in production on 0.10.11, Gemma 4 26B requests with 4–32k-token prompts waited a median 1.2–1.6 s, GPT-OSS 20B 8–32k 1.7 s and GLM-5.3-Flash ≥ 128k 51 s, and the Mac kept the request's memory reserved meanwhile. +- The export now reads a head's rows in batches of up to 256 KiB, codes each batch on up to four cores (a quarter of the Mac's active cores) and rotates eight lanes at a time. The checkpoint's bytes are unchanged, so restores and their answers are exactly as before. CPU coding cost per 512-wide row falls from about 3 µs to about 0.6 µs on an M5 Ultra. diff --git a/docs/architecture/inference.md b/docs/architecture/inference.md index 001cf9cb3e..f41d89571c 100644 --- a/docs/architecture/inference.md +++ b/docs/architecture/inference.md @@ -1,6 +1,6 @@ # Provider inference engine -> Last updated: 2026-11-09 +> Last updated: 2026-11-10 How a chat-completion request is served inside the `darkbloom` provider process: one in-process engine (`mlx-swift-lm` @@ -2064,10 +2064,19 @@ reaches a destination A donor's stream retires only after its complete checkpoint is published (`EngineLoopV2.finishAfterCompletePrefixPublication`), so export time is added to the donating response's end. At a quantized checkpoint precision, export -encodes each older attention row on the CPU with one prepared row encoder per -segment read, which writes the scalar codec's exact bytes -([conversion](../reference/ssd-kv-cache.md#complete-checkpoint-payload), +encodes each older attention row on the CPU and writes the scalar codec's exact +bytes ([conversion](../reference/ssd-kv-cache.md#complete-checkpoint-payload), `libs/mlx-swift-lm/Libraries/MLXLMCommon/ContinuousBatchingV2/Prefix/NativeQuantizedCheckpointTensorSource.swift`). +It reads a head's consecutive native rows from the source in batches of up to +256 KiB (`CBv2NativeCheckpointRowCodec.exportBatchBytes`), so a read's fixed +cost is paid per batch: under GLM-5.3-Flash's native profile each read +revalidates the publication's native binding, about 25 µs, which one read per +row made the larger part of a 63 s end-of-stream wait after a 200k-token +prompt on an M5 Ultra. Each batch is coded on up to four cores +(`exportCodingWorkers`, a quarter of the active ones), one prepared encoder +per core, and the encoder rotates eight lanes at a time with the scalar +butterflies' additions in the same order, so the bytes and the first refused +row are those of coding row by row (`CBv2NativeCheckpointBatchCoding`). Segmented paged admission no longer waits for queued GPU work. A released row's free 64 MiB segments are parked per pool, up to `min(2 GiB, slot grant / 8)`, diff --git a/docs/reference/ssd-kv-cache.md b/docs/reference/ssd-kv-cache.md index 8e4040c6f4..928e38baaa 100644 --- a/docs/reference/ssd-kv-cache.md +++ b/docs/reference/ssd-kv-cache.md @@ -1,6 +1,6 @@ # SSD KV cache reference -> Last updated: 2026-11-06 +> Last updated: 2026-11-10 Exact on-disk format, paths, identity binding, environment knobs, size and eviction rules, and per-family reuse capability of the provider's encrypted SSD @@ -124,7 +124,7 @@ and MTP codec. No public header field exposes those token boundaries | Import admission | Authenticate manifest → allocation-free import plan → native per-buffer destination, scratch and metadata admission → bounded whole-file read. Shared native ownership is separate from provider host IO | `SSDHybridCheckpointStore+ReadAttempt.swift` (`readCheckpoint`), `CompleteCheckpointImportPlan.swift` (`allocate`) | | Same-file coordination | A stage read holds its leaf file's per-file lease in shared mode, so concurrent restores of one checkpoint read it together. Writers, duplicate validation, scans and retirement hold the lease exclusively. Requests are granted in arrival order and a queued exclusive request holds back later readers; scans and retirement never wait and skip a file that any request holds or awaits. A reader changes a file only while nothing else holds or awaits it, without waiting: its leaf through an upgrade of its shared lease, an ancestor through a free exclusive lease. It then rewrites an mtime that is at least `ttlSeconds / 30` old (60 s at the default TTL) or in the future, and removes a file that failed authentication. A busy file keeps its mtime; a busy corrupt file stays indexed and fails each later read until a reader finds it alone, a same-tag writer's validation removes it, or it expires | `provider-swift/Sources/ProviderCore/KVCacheSSD/SSDCheckpointFileCoordinator.swift`, `SSDHybridCheckpointStore+ReadAttempt.swift` (`persistRecency`, `whileSoleHolder`) | | Provider-staged destinations | The provider-staged (non-native-bound) import creates contiguous destinations, paged segment storage and recent rows already available from zeroed host bytes, then skips `eval` and stream fences for them. Staging therefore never waits for MLX-Swift's process-wide `evalLock`, which the engine holds for a whole prefill chunk. The zero source is read by the copy and can briefly commit up to one destination outside the MLX allocator, so the import plan charges the largest as staging scratch. Native-bound imports and engine pool growth keep device allocation | `libs/mlx-swift-lm/Libraries/MLXLMCommon/ContinuousBatchingV2/Prefix/CheckpointHostZeros.swift` (`MLXArray.cbv2HostZeros`), `libs/mlx-swift-lm/Libraries/MLXLMCommon/ContinuousBatchingV2/Prefix/CompleteCheckpointTransfer.swift` (`CBv2CompleteCheckpointImport`), `libs/mlx-swift-lm/Libraries/MLXLMCommon/ContinuousBatchingV2/Prefix/PagedCheckpointStorage.swift` (`CBv2PagedCheckpointStorage`, `hostMaterialized`), `libs/mlx-swift-lm/Libraries/MLXLMCommon/ContinuousBatchingV2/Prefix/CompleteCheckpointImportPlan.swift` (`CBv2CompleteCheckpointImportPlan.scratchBytes`, `evaluateUnavailable`) | -| Lossy-to-native conversion | CPU row encoding/decoding; `64 << 10` bytes of conversion scratch, reserved per export read and in the import plan. Each export read prepares one row encoder (`CBv2PreparedNativeCheckpointRowEncoder`): it reuses its row buffers and one representability decoder and writes the scalar codec's exact bytes. Import reuses row buffers, retains at most one fragmented input row, and batches validated native writes up to `16 << 10` bytes within that allowance. Native destination sizes, not encoded bytes, price restored state | `NativeCheckpointRowCodec.swift`, `NativeQuantizedCheckpointTensorSource.swift`, `CompleteCheckpointImportPlan.swift`, `NativeQuantizedCheckpointRestore.swift` | +| Lossy-to-native conversion | CPU row encoding/decoding; `64 << 10` bytes of conversion scratch, reserved per export read and in the import plan. An export read also reserves one batch: up to `256 << 10` bytes of a head's consecutive native rows, read from the source at once, and their coded rows (`CBv2NativeCheckpointRowCodec.exportBatchBytes`). It codes the batch on up to `exportCodingWorkers` cores (a quarter of the active ones, one to four), each with its own prepared row encoder (`CBv2PreparedNativeCheckpointRowEncoder`, `CBv2NativeCheckpointBatchCoding`) that reuses its row buffers and one representability decoder; the bytes and the first refusal are the scalar codec's, row by row. Import reuses row buffers, retains at most one fragmented input row, and batches validated native writes up to `16 << 10` bytes within that allowance. Native destination sizes, not encoded bytes, price restored state | `NativeCheckpointRowCodec.swift`, `NativeQuantizedCheckpointTensorSource.swift`, `CompleteCheckpointImportPlan.swift`, `NativeQuantizedCheckpointRestore.swift` | | Idle state | Metadata index only; no resident tensor bank or persistent slot carve | `SSDHybridCheckpointStore.swift`, `provider-swift/Sources/ProviderCore/Inference/PrefixCache/PrefixCachePolicy.swift` (`isMemoryEnabled`) | | Imported lifetime | Single-use staged state retains native owners through aliases. Paged adoption replaces temporary staging with the full request promise and actual backing; recurrent/MTP auxiliary state has its own charge | `CompleteCheckpointTransfer.swift` | | Host IO lifetime | Read/decrypt aliases retire before the read charge returns; writers claim host buffers before encoding and release them after the complete write stack drains | `SSDHybridCheckpointStore+ReadAttempt.swift` (`readCheckpoint`), `SSDHybridCheckpointStore+Write.swift` (`write`) | diff --git a/libs/mlx-swift-lm b/libs/mlx-swift-lm index 67110e7b55..bca3aa058b 160000 --- a/libs/mlx-swift-lm +++ b/libs/mlx-swift-lm @@ -1 +1 @@ -Subproject commit 67110e7b5588baa280f6bae33e82808d4d925137 +Subproject commit bca3aa058b8c8a67b96a6d247ee490f744310935 From f05ee31f476eea07ed5177df18b830051784a1b7 Mon Sep 17 00:00:00 2001 From: jonathan308 <5055076+jonathan308@users.noreply.github.com> Date: Tue, 10 Nov 2026 09:49:28 -0800 Subject: [PATCH 2/4] docs(inference): state the GLM export's per-row cost as measured, not the read's share The 25 us per-row read cost is inferred (a 28 us per-row tail against 3 us of measured coding); the paragraph now states the measured figures only. Co-Authored-By: Claude Opus 5.5 --- docs/architecture/inference.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/architecture/inference.md b/docs/architecture/inference.md index f41d89571c..99d0683257 100644 --- a/docs/architecture/inference.md +++ b/docs/architecture/inference.md @@ -2070,9 +2070,9 @@ bytes ([conversion](../reference/ssd-kv-cache.md#complete-checkpoint-payload), It reads a head's consecutive native rows from the source in batches of up to 256 KiB (`CBv2NativeCheckpointRowCodec.exportBatchBytes`), so a read's fixed cost is paid per batch: under GLM-5.3-Flash's native profile each read -revalidates the publication's native binding, about 25 µs, which one read per -row made the larger part of a 63 s end-of-stream wait after a 200k-token -prompt on an M5 Ultra. Each batch is coded on up to four cores +revalidates the publication's native binding. With one read per row, a +200k-token prompt's stream on an M5 Ultra stayed open 63 s after its last +token, about 28 µs per coded row, of which coding took about 3 µs. Each batch is coded on up to four cores (`exportCodingWorkers`, a quarter of the active ones), one prepared encoder per core, and the encoder rotates eight lanes at a time with the scalar butterflies' additions in the same order, so the bytes and the first refused From 324f527eeadf5235d93b55b4bab884d902e8f2bd Mon Sep 17 00:00:00 2001 From: jonathan308 <5055076+jonathan308@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:17:45 -0800 Subject: [PATCH 3/4] Repin the SDK to 67cf895d: the export fix merged with SDK main ef028fbe (#398) SDK main moved to ef028fbe (#398: non-speculating rows save prefix checkpoints). A pin after it must contain it, so the fix branch merged main (signed merge 67cf895d, no file in common) and this PR pins the merge. This PR therefore also carries #398's SDK change: merge it with or after #1965, which documents that change, and repin to SDK main once the fix branch is merged there. #398's new donors: Qwen 3.5/3.6 checkpoints keep native storage (the MTP history forces it), so they never take the coded path this fix changes and their end-of-stream wait is the native file write; Nemotron 3.5 Super's coded attention rows do take it. Co-Authored-By: Claude Opus 5.5 --- libs/mlx-swift-lm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/mlx-swift-lm b/libs/mlx-swift-lm index bca3aa058b..67cf895ded 160000 --- a/libs/mlx-swift-lm +++ b/libs/mlx-swift-lm @@ -1 +1 @@ -Subproject commit bca3aa058b8c8a67b96a6d247ee490f744310935 +Subproject commit 67cf895ded1dcf6c8fcf511aa8a1624d9a629155 From 8140eed8d850cc272ebefe2cefa9a2311857a2f9 Mon Sep 17 00:00:00 2001 From: jonathan308 <5055076+jonathan308@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:54:36 -0800 Subject: [PATCH 4/4] Repin the SDK to mlx-swift-lm main 505e6bd8 (SDK_MAIN4: #398, #378, #399) SDK main 505e6bd8 is the merge commit of mlx-swift-lm#399 (this fix) onto a6599fd2 (#378, Nemotron 3.5 Lightning), which follows ef028fbe (#398). Its tree equals the fix branch head 781c3820's (61d17c0d), green on Fork Tests 42882853010 (all 10 shards). #1965, #1969 and candidate (c) move to the same commit; resolve each gitlink to it. Co-Authored-By: Claude Opus 5.5 --- libs/mlx-swift-lm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/mlx-swift-lm b/libs/mlx-swift-lm index 67cf895ded..505e6bd87c 160000 --- a/libs/mlx-swift-lm +++ b/libs/mlx-swift-lm @@ -1 +1 @@ -Subproject commit 67cf895ded1dcf6c8fcf511aa8a1624d9a629155 +Subproject commit 505e6bd87c01a89a78d3a981bd6fcb99b91fd2a4