Replace fatalError with throwing KVCacheError in recoverable KV cache paths#451
Open
JuanColilla wants to merge 2 commits into
Open
Replace fatalError with throwing KVCacheError in recoverable KV cache paths#451JuanColilla wants to merge 2 commits into
JuanColilla wants to merge 2 commits into
Conversation
KVCacheSimple.toQuantized, RotatingKVCache.toQuantized, and the prompt cache deserialization path (restoreCacheFromMetaState) crashed the process on corrupt cache files or incompatible group-size/head-dim combinations. These are now throwing KVCacheError instead, matching the existing throwing surface of loadPromptCache/savePromptCache. maybeQuantizeKVCache falls back to leaving the cache unquantized on failure rather than propagating, so its existing non-throwing public signature is unchanged. (cherry picked from commit 3440b9f)
…antized() maybeTurboQuantizeKVCache's boundary-layer protection path called KVCacheSimple.toQuantized() before it became throwing. The call site already guards that groupSize resolves against the layer's head dimensions, so the throw is not expected here in practice; use try? and skip the layer (matching this function's existing skip-on-ineligibility pattern) rather than force-unwrap, so a future edge case degrades to leaving that layer at fp16 instead of crashing.
Contributor
|
@JuanColilla please see my PR: #453 |
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.
Why
Several KV cache paths crashed the whole process with
fatalErroronconditions that are recoverable, not programmer bugs:
KVCacheSimple.toQuantized/RotatingKVCache.toQuantizedcrash when therequested group size doesn't evenly divide the model's key/value head
dimensions, or (for
RotatingKVCache) unconditionally, since rotating-cachequantization isn't implemented yet.
restoreCacheFromMetaState(used byloadPromptCache) had no validationof the array/metaState counts it reads out of a saved prompt-cache file,
so a truncated or corrupted file crashed the process instead of failing
the load.
A library shouldn't
fatalErroron attacker- or disk-corruption-controlledinput, or on a caller passing a group size that happens not to fit a given
model's head dimension — both are inputs the caller can reasonably want to
handle (skip quantization, reject a bad file, log and continue).
What
KVCacheSimple.toQuantizedandRotatingKVCache.toQuantizednowthrows KVCacheErrorinstead of callingfatalError, matching the existingthrowing surface of
loadPromptCache/savePromptCache.restoreCacheFromMetaStatenow validates array/metaState counts forKVCacheSimple,RotatingKVCache,QuantizedKVCache, andChunkedKVCachebefore constructing the cache, throwing
KVCacheErrorwith a messageidentifying which field was short.
maybeQuantizeKVCache's public, non-throwing signature is unchanged: itnow falls back to leaving a cache unquantized (
try?) rather thanpropagating, since silently keeping the unquantized cache was already its
behavior for other ineligibility reasons.
How tested
Tests/MLXLMTests/KVCacheTests.swift: new regression tests constructinghand-crafted corrupt prompt-cache files (wrong array count for
KVCacheSimple/RotatingKVCache, wrong metaState count forQuantizedKVCache/ChunkedKVCache) and assertingloadPromptCachethrowsKVCacheErrorinstead of crashing; a test assertingRotatingKVCache.toQuantized()throws; a test assertingKVCacheSimple.toQuantized(groupSize:bits:)throws on a head dimensionthat divides none of the supported group sizes (32/64/128).
swift buildcompiles clean across every target.main's current state, including theTurboQuantKV-cache-compression path merged after this change was firstwritten; its one call site to the now-throwing
toQuantizedis updated ina second, clearly separated commit so the diff shows exactly what had to
adapt and why.