Skip to content

Stage SSD checkpoint restores without waiting on another prefill (#867) - #1519

Merged
Gajesh2007 merged 10 commits into
Layr-Labs:masterfrom
javiech1:fix/867-ssd-restore-ttft
Oct 21, 2026
Merged

Gajesh2007 merged 10 commits into
Layr-Labs:masterfrom
javiech1:fix/867-ssd-restore-ttft

Conversation

@javiech1

@javiech1 javiech1 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the remaining cause of #867 on current master and adds a real-model regression so it can't come back.

  • Bumps libs/mlx-swift-lm to Stage provider checkpoint imports without the MLX eval lock mlx-swift-lm#316: provider-staged complete-checkpoint imports create their destinations already available from host bytes, so the SSD stage no longer waits for MLX-Swift's process-wide evalLock while the engine evaluates another request's prefill chunk.
  • Adds ModelCheckpointRestoreContentionLiveTests (opt-in): serves the same fork prompt restored and cold, idle, during another tenant's long prefill, and right after another tenant's donor; fails if a restore misses demandedTokens or produces its first token later than cold under the same condition.
  • Docs: developer/test.md (how to run it), reference/ssd-kv-cache.md (destination contract), architecture/inference.md (stage vs evalLock); changelog fragment.

The SDK PR is merged: the gitlink points at Layr-Labs/mlx-swift-lm@c17693f (the squash of 86c7899, identical tree, parent = the previous pin 14661d9).

What #867 looks like today

M5 Max 128 GB, macOS 27.0, EigenLabs/Qwen3.8-27B-4bit-mtp, paged, native live/checkpoint, master ab423c1c0, isolated ephemeral cache. Fork prompt 16,667 tokens, 12,288 restored tokens (0.96 GB).

Condition Stage before → after Restore TTFT before → after Cold TTFT
Idle 0.30 s → 0.31 s 6.6 s → 6.6 s 20.5–21.5 s
During another tenant's prefill 2.57–3.76 s → 0.35–0.37 s 27.0–27.5 s → 26.1–27.1 s 40.4–42.3 s
After another tenant's donor (1.4 GB write in flight) 0.30 s → 0.31 s 6.6 s → 6.6 s 20.5–21.4 s
  • The 18 s stall reported on 0.9.0 does not reproduce: read + AES-GCM of 0.96 GB takes 240–285 ms in every condition, and a concurrent donation write has no effect.
  • The contended cost was plan.allocate waiting for evalLock (≈ one prefill chunk), found with temporary per-phase timers. Moving the allocation to the CPU stream did not help; avoiding eval/synchronize did.
  • Restore beat cold prefill in every case, so the remedy proposed in the issue (skip a restore whose cost exceeds cold prefill) would have been slower. TTFT gains are within run-to-run variance; the staging block is what's removed.
  • With MTP on (production default): 8,192 restored tokens (the 1 GiB DARKBLOOM_PREFIX_CACHE_SSD_MAX_STAGE_MB cap is reached earlier because the checkpoint carries MTP state; since Default deeper complete-checkpoint SSD restores on large-memory Macs #1520, Macs with ≥ 48 GiB default to 7 GiB). With this change the MTP run passes: contended stage 0.32–0.35 s, restore TTFT 37.4–37.7 s vs 47.0–49.3 s cold; idle 14.0–14.5 s vs 23.8–24.7 s cold.

Correctness: ModelCheckpointStorageLiveTests native/native with the bump: coldMatchesRestored and repeatedRestoreMatches true for 4/4 (shared 15k and 4k cells).

Before / After

flowchart LR
  classDef bad fill:#fde2e2,stroke:#c62828,color:#3b0d0d
  classDef good fill:#e3f4e8,stroke:#2e7d32,color:#0d2b14
  classDef neutral fill:#e3ecfa,stroke:#1565c0,color:#0b2140
  subgraph Before
    B0["request with SSD hit<br/>while another prefill runs"]:::neutral --> B1["stage: allocate destinations<br/>eval + synchronize"]:::bad
    B1 --> B2["blocked on evalLock<br/>≈2.6 s"]:::bad --> B3["read + decrypt 0.3 s"]:::neutral --> B4["engine.submit"]:::neutral
    B5["no contention regression test"]:::bad
  end
  subgraph After
    A0["request with SSD hit<br/>while another prefill runs"]:::neutral --> A1["stage: host-materialized<br/>destinations, no lock"]:::good
    A1 --> A3["read + decrypt 0.3 s"]:::neutral --> A4["engine.submit"]:::neutral
    A5["ModelCheckpointRestoreContentionLiveTests<br/>restore ≤ cold; stage ≤ idle + 1 s"]:::good
  end
Loading
sequenceDiagram
  autonumber
  participant T as Test task
  participant F as Foreign request (async let)
  participant S as EngineV2BenchmarkSession
  participant D as SSD checkpoint store
  T->>S: donor (target = demandedTokens) → checkpoint written
  F->>S: long cold prefill (other tenant scope)
  Note over T,F: +2 s, foreign prefill running
  T->>S: fork, cache on
  S->>D: stage (no evalLock wait)
  D-->>S: staged 12,288 tokens
  S-->>T: first token → sample
  T->>T: same condition, cache off → compare TTFT
Loading

Validation

cd provider-swift
swift build --build-tests && ../scripts/stage-test-metallib.sh "$(swift build --show-bin-path)"
env -i HOME="$HOME" PATH="$PATH" TMPDIR="$TMPDIR" \
  DARKBLOOM_RESTORE_CONTENTION_BENCHMARK=1 \
  DARKBLOOM_PREFIX_MODEL_SPEC=/absolute/qwen38-spec.json \
  DARKBLOOM_RESTORE_CONTENTION_OUTPUT=/absolute/new-report.json \
  swift test --skip-build --disable-automatic-resolution --filter ModelCheckpointRestoreContentionLiveTests
swift test --skip-build --filter 'SSDCheckpointReadBudgetTests|SSDShorterCheckpointRestoreTests|SSDShorterRestoreClockTests|SSDHybridCheckpointStoreTests|ModelCheckpointStorageSpecificationTests'
cd .. && make docs-impact-check BASE=origin/master && make docs-check && python3 scripts/changelog.py check

Spec: one case donorTokens 16640, sharedTokens 15360, forkTokens 16640, demandedTokens 12288, pairs 2, outputTokens 32, kvBackend paged (MTP run: mtpEnabled true, demandedTokens 8192).

Results: live contention test passed on the final code (2 pairs; plus earlier runs before/after the fix as tabled); 37 focused provider SSD/spec tests passed; SDK checkpoint suites passed (see SDK PR); docs-impact, docs-check and changelog check OK. Full make provider-test on this branch: 4,609 Swift Testing tests across 18 runs plus the XCTest suites (only expected failures, 0 unexpected); one unrelated failure, ModelScannerMemoryFilterTests "strict MiMo scanner propagates a complete native LOAD quote…" (UnwrappingOptional), which this PR cannot affect (it adds only a test file and docs on the provider side and the scanner does not use the changed SDK path).

Limitations

  • One machine and model; the GPU was shared with a live production provider (Gemma 4 / Qwen 3.8) during all runs; fixed pair order.
  • The live test is opt-in and needs a local model, so CI only compiles it. Ephemeral keys; no cross-process persistence.
  • The 2 s offset assumes the foreign prefill is still running at submission (documented: donorTokens must be long enough).
  • Transient host memory up to the largest single destination outside the MLX allocator while building it: a paged segment (64 MiB by default) or a whole auxiliary tensor such as Qwen 3.8's MTP hidden history (~10 KB per restored token, ~1 GB at ~94k tokens). The SDK charges it as staging scratch (c17693f) — see SDK PR.
  • Not addressed (pre-existing, separate issue): lossy balanced checkpoints with MTP off restore through CPU row decoding in 166–225 s vs 24.7 s cold.

Refactor pass

Separate refactor subagents reviewed both the test (structured async let for the foreign request so it is cancelled/joined before fixture close; single exit path; Condition.role(cache:)) and the SDK change (throwing cbv2HostZeros, corrected memory comment). The parent additionally required .length and full output per sample via ModelPrefixBenchmarkTerminal.completeSuccessfulRequest. Everything was rebuilt and re-run afterwards.

Refs #867, #865.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

Bumps libs/mlx-swift-lm to stage provider checkpoint imports with
host-materialized destinations, so a restore no longer waits for MLX-Swift's
process-wide evalLock while the engine evaluates another request's prefill
chunk (Layr-Labs#867).

Adds ModelCheckpointRestoreContentionLiveTests, an opt-in real-model
regression that serves the same fork restored and cold, idle, during another
tenant's prefill and after another tenant's donor, and requires the restore to
reach its demanded boundary and never to produce its first token later than
cold prefill.

Measured on M5 Max 128 GB, Qwen 3.8 27B, 16,667-token fork, 12,288 restored
tokens (0.96 GB): contended staging 2.6-3.8 s -> 0.31-0.37 s; contended
restore TTFT 27.0-27.5 s -> 26.1-27.1 s, within run-to-run variance (cold
40.4-42.3 s); idle unchanged (6.6 s vs 20.6 s cold). Restored outputs match cold in ModelCheckpointStorageLiveTests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 19, 2026

Copy link
Copy Markdown

@javiech1 is attempting to deploy a commit to the EigenLabs Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Threat model review — advisory

Reviewed head 6df3e1d16c3d against base b32e90082c3a.

Review not completed for this head. Previous findings are superseded, not confirmed resolved.
Service returned HTTP 409; no automatic paid retry

Scan incomplete: 3 file(s) could not be fully read as text. Binary, Git LFS and submodule changes require manual review.
Affected files: libs/mlx, libs/mlx-swift, libs/mlx-swift-lm

Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging.

Failure diagnostics: HTTP_ERROR

Coverage: 34/47 source units; cross-file integration pending. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned.

Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 7 reused batch(es), 0 provider cache-hit tokens.

Saved findings and earlier report · Historical findings are not confirmed resolved.

@Gajesh2007

Copy link
Copy Markdown
Member

@codex review this

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T23:28:41.053406Z 6df3e1d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93c67d5f78

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/reference/ssd-kv-cache.md Resolved
The contention regression now fails when a contended restore stages more
than 1 s slower than its pair's idle restore; the TTFT bound alone still
passed on the build that waited on evalLock (2.6-3.8 s stage vs 0.3 s idle).
The report is rewritten atomically after each condition and records the
failure, including a teardown error that was previously discarded.

Bumps libs/mlx-swift-lm to charge the largest host-zero source as import
staging scratch, and documents the staged-import exception in the
prefix-cache architecture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CDrGQJwa1LC3j4TE6Dvgmo
@javiech1

Copy link
Copy Markdown
Contributor Author

Live rerun on b1802199f (SDK 86c7899e), same M5 Max / Qwen 3.8 27B spec (16,640-token fork, 12,288 restored, 2 pairs): passed, including the new stage bound.

Condition Stage Restore TTFT Cold TTFT
Idle 305–329 ms 6.5–6.7 s 20.4–20.6 s
During another tenant's prefill 337–367 ms 26.0–26.1 s 40.2–40.4 s
After another tenant's donor 307–323 ms 6.6 s 20.5–23.1 s

Contended stage stays within ~60 ms of idle (bound: idle + 1 s; the pre-fix build staged at 2.6–3.8 s). The report was written incrementally (22 samples, no failure).

# Conflicts:
#	docs/developer/test.md
#	docs/reference/ssd-kv-cache.md
Layr-Labs/mlx-swift-lm#316 was squash-merged as c17693f5, whose tree is identical
to the reviewed PR head 86c7899e and whose only parent is the previous pin
14661d9b.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27db87d888

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/reference/ssd-kv-cache.md Resolved
# Conflicts:
#	docs/architecture/inference.md

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5bd8584f23

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/architecture/inference.md Resolved
Gajesh2007
Gajesh2007 previously approved these changes Oct 21, 2026

@Gajesh2007 Gajesh2007 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: CI green on 5bd8584; SDK pinned to Layr-Labs/mlx-swift-lm@c17693f (merged #316, current main).

…ratch

Addresses the second round of review on the restore contention test:
- require native live and checkpoint precision, since a lossy checkpoint can
  legitimately restore slower than recompute;
- record the session's runtime identity (makeReport) in the report;
- record contract violations and teardown-only failures in the saved report,
  not just as test issues;
- describe the provider-staged host-zero buffer and its scratch charge in
  hardware-support.md and advance the inference.md freshness stamp.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a627644615

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/reference/ssd-kv-cache.md Resolved
- Cite the provider-staged host-zero contract by full libs/mlx-swift-lm paths
  and symbols in ssd-kv-cache.md, prefix-cache.md, inference.md and
  hardware-support.md.
- Create the contention report's parent directory before the first write, as
  ModelPrefixBenchmarkReport.save does.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2783936b3a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

- Give each condition its own donor and cache scope, so a deeper checkpoint a
  measured fork donates can never serve a later condition; record a violation
  if a restore's depth differs from its pair's idle restore.
- Record whether the foreign prefill was still running when a contended
  stage finished, and fail the run when it was not.
- Require DARKBLOOM_PREFIX_EXCLUSIVE_GPU=1 like the sibling timing benchmarks,
  and document it in the invocation.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ba5a9e684

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

MiMo loads through the native engine, whose native-bound imports keep device
allocation and the eval lock, so the staging gate does not apply to it. The
test guide now states that the report is rewritten after each measured run.
@javiech1

Copy link
Copy Markdown
Contributor Author

Live rerun on your head 39a07d6ca (SDK c17693f), M5 Max 128 GB, Qwen 3.8 27B, same spec (16,640-token fork, 12,288 restored, 2 pairs): passed with the per-condition donors and the foreign-prefill overlap check.

Condition Stage Restore TTFT Cold TTFT
Idle 317–319 ms 6.6 s 20.7–29.7 s
During another tenant's prefill 356 ms 26.1–26.5 s 40.4–41.0 s
After another tenant's donor 310 ms 6.5–6.6 s 20.5–20.6 s

Every restore reached 12,288 tokens; report has 26 samples, runtime identity and no failure. Caveat: DARKBLOOM_PREFIX_EXCLUSIVE_GPU=1 was set, but my production provider stayed online with light traffic, which likely explains the 29.7 s cold outlier in pair 0; the restore gates were unaffected.

- libs/mlx-swift-lm: c17693f5 -> 19634dd8 (Nemotron 3.5 Super native VL/MTP/video, GLM-5.3-Flash native)
- libs/mlx-swift: 6923a80f -> 67dc7528 (Metal kernel math modes, thread-scoped command-buffer limits, GLM-5.3-Flash native stack)
- libs/mlx: cb77239b -> ad4745a2 (matches mlx-swift's nested Cmlx/mlx)
@Gajesh2007
Gajesh2007 merged commit 49a66cd into Layr-Labs:master Oct 21, 2026
10 of 17 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6df3e1d16c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +3 to +4
- Restoring a complete prefix checkpoint from SSD no longer blocks on MLX's
process-wide eval lock while the engine runs another request's prefill

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Qualify the release note to provider-staged restores

For native-bound complete-checkpoint imports such as MiMo, this wording promises a fix that the change explicitly does not provide: the new test rejects mimo_v2 at ModelCheckpointRestoreContentionLiveTests.swift:82-85, and docs/reference/ssd-kv-cache.md:119 says native-bound imports retain device allocation. Qualify the changelog entry as provider-staged so native-bound users are not told that their restore path no longer waits on the eval lock.

Useful? React with 👍 / 👎.

Comment on lines +171 to +173
let foreign = try await timed(fixture, id: requests.next(), pair: pair, role: "foreign_donor",
tokens: prompts.donor, scope: foreignScope, target: prompts.demandedTokens, cache: true)
samples = [try await fork(id: requests.next()), foreign]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Verify the donor write remains in flight

With a valid small demanded checkpoint or a fast SSD, the asynchronous donation can finish between this donor's terminal and the following fork: timed waits for request completion, but this sequence never observes pending write activity, and the later drain only proves that writes are finished afterward. The sample is still accepted as after_foreign_donor, so the third condition can pass without exercising the promised concurrent-write contention; check a write-in-flight signal (for example the host-write reservation) before starting the fork and fail or record a miss when no overlap occurs.

Useful? React with 👍 / 👎.

jonathan308 added a commit that referenced this pull request Oct 21, 2026
Brings #1519 (SSD checkpoint restore staging docs/tests; the same merged SDK pins as this branch, plus libs/mlx ad4745a2) and #1527 (Qwen3.8 forced tools). Conflicts were the Last updated stamps of docs/architecture/hardware-support.md and prefix-cache.md (master's date kept). No GLM code changes.
Gajesh2007 added a commit that referenced this pull request Oct 21, 2026
Refresh before review. master (#1519) now pins mlx-swift 67dc7528 and MLX
ad4745a2; keep those, and pin libs/mlx-swift-lm to the SDK branch's merge of
main (f4be1ba1), which carries the GDN kernels on top of the SDK commit
master pins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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