fix(moe): prevent crash when persistent buffer slots are exhausted#37
Merged
Conversation
When all buffer slots are claimed by speculative-hit routing (ranges.count
== maxBuffers and all experts get different slot assignments), the
force-unwrap on '.first { !usedSlots.contains($0) }!' returns nil and
crashes with _assertionFailure.
Replace the force-unwrap with a guard that sets a slotExhausted flag and
breaks out. When detected, the hit/miss arrays are cleared and we fall
through to the existing full-pread fallback path — same correctness,
no crash.
Fixes SharpAI/SwiftLM#87
There was a problem hiding this comment.
Pull request overview
This PR fixes a crash in the SwitchGLU.callAsFunction warm-path SSD streaming logic when resolving expert-to-persistent-buffer slots, by removing a force-unwrap that could trigger an _assertionFailure and cleanly falling back to the existing full-pread path.
Changes:
- Replace force-unwrapped free-slot selection with a
guard letthat detects slot exhaustion and exits the warm-path loop safely. - Skip miss-only preads when slot exhaustion is detected, and instead trigger the existing full-pread fallback.
- Consolidate “no predictions” and “slot exhausted” handling into a single fallback condition based on
usedGate.count != ranges.count.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…cenario 6 unit tests exercising the pure-CPU slot resolution algorithm: - testOldAlgorithmCrashesOnSlotExhaustion: documents the crash path - testFixedAlgorithmHandlesSlotExhaustion: validates graceful detection - testNormalHitMissResolution: regression guard for normal operation - testAllHits: 100% speculation accuracy edge case - testAllMisses: 0% speculation accuracy edge case - testDuplicateExpertInRangesExhaustsSlots: sorted-idx duplicate expert
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.
Problem
SwitchGLU.callAsFunctioncrashes with_assertionFailure(force-unwrap of nil) in the warm-path hit/miss slot resolution when all persistent buffer slots are consumed by speculative hits:Root cause: Line 726 uses a force-unwrap:
When
ranges.count == maxBuffers(typically 8 for top_k=8) and every expert inrangesgets a unique slot from the speculative-prefetch hit map, there are zero free slots remaining for any miss —.firstreturns nil and the!crashes.Fix
Replace the force-unwrap with a
guard letthat sets aslotExhaustedflag and breaks out of the loop. When detected, the partial buffers are cleared and we fall through to the existing full-pread fallback path (same code as the no-predictions branch). Same correctness, no crash.Fixes SharpAI/SwiftLM#87