Repository navigation
Support for scripted models (tests) - #701
Conversation
- add TranscriptKVCache (tests) - add ability to extend KVCache hierarchy
| /// | ||
| /// Staged speculative rounds (MTP) write through an auxiliary entry and trim it back to the | ||
| /// accepted positions, so it must stay trimmable after the round's width. | ||
| public protocol AuxiliaryKVCache: KVCache {} |
There was a problem hiding this comment.
Marker for special cache types. The plan is to compose a model like this:
- ScripedModel(Gemma3())
the outer model will inject a KVCache for its own state and strip it off to pass to the interior model (which will be a random weight model).
In order for the KVCache to participate in normal KVCache activities we need to let the KVCache infrastructure (plans, save/restore) know what it is.
In general this will give third parties the ability to write KVCache types outside of mlx-swift-lm, or at least a first pass at it.
There was a problem hiding this comment.
This looks like a nice addition! I like that a wrapper can keep its own state alongside the inner model’s caches and have it participate in trimming, copying, and save/restore.
Opening that up to cache types defined outside mlx-swift-lm also gives downstream projects more room to experiment. Thanks.
| open class var serializedClassName: String? { nil } | ||
|
|
||
| open class func restore(state: [MLXArray], metaState: [String]) throws -> any KVCache { |
There was a problem hiding this comment.
Now all KVCache types declare their key and a way to restore state. Previously was two switch statements for the encode and decode.
Note: types can register aliases if there are two keys they can decode from.
| public override class var serializedClassName: String? { "KVCache" } | ||
|
|
||
| public override class func restore( | ||
| state: [MLXArray], metaState: [String] | ||
| ) throws -> any KVCache { |
There was a problem hiding this comment.
Same pattern for all the cache types. serializedClassName comes from the switch in cacheClassName() (now gone), and restore is from the switch in restoreCacheFromMetaState() (remains but the body is now tiny).
| // MARK: - Utility Functions | ||
|
|
||
| /// Map a cache instance to its Python-compatible class name for serialization. | ||
| private func cacheClassName(_ cache: KVCache) -> String { |
There was a problem hiding this comment.
Replaced by static properties.
| switch className { | ||
| case "KVCache", "KVCacheSimple": |
There was a problem hiding this comment.
All these moved to the restore() function on each type.
| // Auxiliary entries are not leaves, but they append and trim like one. | ||
| if entry is any AuxiliaryKVCache { |
There was a problem hiding this comment.
My thought is that this would be the least intrusive for the fake KVCaches that would appear but still be able to participate like a normal KVCache.
| /// | ||
| /// It is an `AuxiliaryKVCache`, so capacity and compression settings ignore it, and it never | ||
| /// stores keys or values. | ||
| public final class TranscriptKVCache: BaseKVCache, AuxiliaryKVCache { |
There was a problem hiding this comment.
This is the new KVCache to be used by ScriptedModels in the next PR.
|
@aleroot FYI, some proposed KVCache infrastructure changes |
Thanks for the heads-up, @davidkoski. This come with good timing! The distinction provided by One question about external cache implementations: |
|
Good point on |
Brings in ml-explore#622 (exact prefix rewinds in rotating KV caches), ml-explore#619 (streaming detokenizers), ml-explore#698, ml-explore#699, ml-explore#700, ml-explore#701, ml-explore#705, ml-explore#697 and ml-explore#695. Conflicts: AttentionUtils.swift keeps TurboKV telemetry next to upstream's SharedAttentionKVCache; KVCache.swift takes upstream's serialization and rotating-cache view/trim while keeping the fork's TurboQuant state, public cache fields and temporallyOrdered. Follow-ups for upstream behavior the fork had changed: the Qwen2VL audio placeholder no longer indexes content.count - 1 when a tool-call message has no text part, and Gemma4MessageGenerator uses content parts for every non-system role. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Proposed changes
part 2 of scripted tokenizers & models. KVCache support
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.