Repository navigation
Fix SSD streaming buffer overflow with MTP, and scope streaming to its model - #71
Merged
Merged
Conversation
- 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]>
…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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
--stream-experts --mtp).SwitchGLU.projectExpertssizes its per-layer persistent expert buffers on the first call that takes theidx.size <= 32path, usually single-token decode (top_k = 8 slots). ButmaxBufferswas re-read fromidx.sizeon 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), pickedfreeSlot >= 8and trapped on_persistentGate![freeSlot]. The slot-exhaustion fallback (for i in 0..<ranges.count) could overflow the same way. Fix: a smallpersistentBufferPlan(allocated:needed:)helper decides the capacity, and the buffers (plus the prefetch state) are regrown when a call needs more than they hold.loadWeightscreated a newExpertStreamerManager(and did lazy load, relaxed key checks and tensor naming) for every model loaded while streaming was on. A--draft-modelor--mtp-assistant-modelloaded after the main model therefore replaced the main model's shard index. Fix:ExpertStreamingConfig.isStreaming(modelDirectory:), andloadWeightsapplies 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
SSDPersistentBufferTestscovers the 8 → 16 → 8 → 32 capacity sequence. Ran it withswift test --filter SSDPersistentBufferTestsand it passes.xcodebuild testhangs on this machine (Xcode 27; the build service idles atPruneExplicitPrecompiledModules), so I couldn't use it here. The test is pure logic, so it doesn't need Metal.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.SSDPersistentBufferReproTests, written by the SwiftLM review session): a synthetic 4-bit MoE checkpoint is loaded through the realloadWeightswith 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 withMLX_MOE_STACKED=1. On M6 it passes:swift test --filter SSDPersistentBuffer, 3/3 (withmlx.metallibcopied next to the test bundle).Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.
🤖 Generated with Claude Code