Skip to content

Fix SSD streaming buffer overflow with MTP, and scope streaming to its model - #71

Merged
solderzzc merged 3 commits into
mainfrom
fix/ssd-streaming-followups
Sep 26, 2026
Merged

solderzzc merged 3 commits into
mainfrom
fix/ssd-streaming-followups

Conversation

@solderzzc

@solderzzc solderzzc commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Proposed changes

Two existing SSD expert-streaming bugs found in review of #69. Both were already on main; #69 made the first one reachable for Qwen3.5/3.6.

  1. Persistent buffer overflow (--stream-experts --mtp). SwitchGLU.projectExperts sizes its per-layer persistent expert buffers on the first call that takes the idx.size <= 32 path, usually single-token decode (top_k = 8 slots). But maxBuffers was re-read from idx.size on every call. A later call routing 9–32 slots with more than 8 distinct experts, such as an MTP verify step (2 tokens × 8 = 16), picked freeSlot >= 8 and trapped on _persistentGate![freeSlot]. The slot-exhaustion fallback (for i in 0..<ranges.count) could overflow the same way. Fix: a small persistentBufferPlan(allocated:needed:) helper decides the capacity, and the buffers (plus the prefetch state) are regrown when a call needs more than they hold.
  2. Draft/assistant model takes over the streaming setup. loadWeights created a new ExpertStreamerManager (and did lazy load, relaxed key checks and tensor naming) for every model loaded while streaming was on. A --draft-model or --mtp-assistant-model loaded after the main model therefore replaced the main model's shard index. Fix: ExpertStreamingConfig.isStreaming(modelDirectory:), and loadWeights applies the streaming setup only to the directory streaming was activated for. Other models load normally, and a log line notes when streaming is active but a load targets a different directory, so a main model whose path resolved differently doesn't silently load every expert.

The SwiftLM side of the third finding (streaming never activates on the first run, before the model is downloaded) goes in a separate SwiftLM PR.

Verification

  • New unit test SSDPersistentBufferTests covers the 8 → 16 → 8 → 32 capacity sequence. Ran it with swift test --filter SSDPersistentBufferTests and it passes. xcodebuild test hangs on this machine (Xcode 27; the build service idles at PruneExplicitPrecompiledModules), so I couldn't use it here. The test is pure logic, so it doesn't need Metal.
  • End to end on a Mac mini M6 32 GB, unsloth/Qwen3.6-35B-A3B-UD-MLX-4bit, 548 tokens, median of 3, needle checked: SSD 250.9 prefill / 12.66 decode tok/s, 5.37 GB (Fix Qwen3.5/3.6 SSD expert streaming after the upstream sync (crashes + full weight load) #69 alone: 252.3 / 12.71 / 5.37 GB). GPU 715.6 / 47.23. No regression, and streaming still activates for the main model: memory stays at 5.4 GB rather than jumping to ~20 GB.
  • End-to-end repro (SSDPersistentBufferReproTests, written by the SwiftLM review session): a synthetic 4-bit MoE checkpoint is loaded through the real loadWeights with streaming activated. The resident experts are zeroed so only bytes read from disk can match, with a negative control confirming that. Every call is compared against a non-streamed reference. It covers decode → verify (8 → 16 slots) and verify → decode (stale slots). Per the review it traps on e7df2ae and passes with this fix, including with MLX_MOE_STACKED=1. On M6 it passes: swift test --filter SSDPersistentBuffer, 3/3 (with mlx.metallib copied next to the test bundle).

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: Written by Claude Code (Claude Opus 5.5) in the M6 benchmarking session, with the repo owner's approval to open this PR. The bugs were found by the SwiftLM review session (also Claude) while reviewing Fix Qwen3.5/3.6 SSD expert streaming after the upstream sync (crashes + full weight load) #69. Claude wrote the code, the test and this description, and ran the benchmarks on the owner's Mac mini M6. pre-commit/swift-format is not installed on this machine, so formatting wasn't run.

🤖 Generated with Claude Code

- SwitchGLU sized its persistent SSD buffers on the first call (top_k slots
  for decode) but re-read idx.size on every call. A later call routing 9-32
  slots, such as an MTP verify step, picked free slots past the end and
  trapped. Regrow the buffers when a call needs more.
- loadWeights set up streaming for every model it loaded, so a draft or
  assistant model loaded after the main one replaced its shard index. Only
  the model directory streaming was activated for gets the streaming setup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
simba and others added 2 commits September 25, 2026 14:50
…treamed loads

Adds the SwiftLM review session's repro: a synthetic 4-bit MoE checkpoint loaded
through loadWeights with streaming on, resident experts zeroed so only pread
bytes can match, each call compared against a non-streamed reference. It traps
on e7df2ae and passes with the regrow fix.

Also log when streaming is active but a load targets another directory, so a
main model that silently loads without streaming is visible.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@solderzzc
solderzzc merged commit 5596071 into main Sep 26, 2026
7 checks passed
solderzzc added a commit to SharpAI/SwiftLM that referenced this pull request Sep 26, 2026
Picks up the SSD persistent-buffer regrow (--stream-experts with --mtp or a
draft model), streaming scoped to the main model's directory, and the
end-to-end SSD tests (#70, #71).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@solderzzc
solderzzc deleted the fix/ssd-streaming-followups branch November 4, 2026 05:33
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.

1 participant