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..99d0683257 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. 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 +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..505e6bd87c 160000 --- a/libs/mlx-swift-lm +++ b/libs/mlx-swift-lm @@ -1 +1 @@ -Subproject commit 67110e7b5588baa280f6bae33e82808d4d925137 +Subproject commit 505e6bd87c01a89a78d3a981bd6fcb99b91fd2a4