Skip to content

Add copy() to KVCache protocol and all implementations - #158

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
alankessler:kvcache-copy-clean
Mar 27, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
alankessler:kvcache-copy-clean

Conversation

@alankessler

Copy link
Copy Markdown
Contributor

Follows up on the discussion in #151 — with consuming on the cache init, there's no way to reuse a prefix cache across multiple sessions without reloading from disk. This adds copy() so you can do cache.map { $0.copy() } instead.

Implementation uses constructor params + state/metaState roundtrip, so there's no dependency on a new mlx-swift tag. Empty caches are handled (the state setters fatalError on empty arrays, so copy() guards against that).

One visibility change: ArraysCache.leftPadding goes from private to internal so MambaCache.copy() can preserve it.

Tests:

  • Parameterized copy independence test across all 6 cache types
  • Empty cache copy (no crash)
  • CacheList with mixed sub-cache types

Add an independent deep-copy method to enable reusing a prefix cache
across multiple ChatSession instances without reloading from disk.

- Add copy() requirement to KVCache protocol
- Implement on KVCacheSimple, RotatingKVCache, QuantizedKVCache,
  ChunkedKVCache, ArraysCache, MambaCache, and CacheList
- Guard against empty state in all copy() methods to avoid fatalError
  from state setters that reject empty arrays
- Preserve leftPadding in ArraysCache/MambaCache copies
- Add CacheList array-based initializer to support copy()
- Change ArraysCache.leftPadding from private to internal for
  subclass access in MambaCache.copy()

Tests:
- testCacheCopyIsIndependent: parameterized across 6 cache types,
  verifies copy has same state and mutation of copy leaves original
  unchanged
- testCacheCopyOnEmptyCache: verifies copy of unpopulated cache
  does not crash
- testCacheListCopyIsIndependent: verifies CacheList with
  heterogeneous sub-caches copies independently
Picks up the fix for array[.ellipsis] returning self instead of a
copy (ml-explore/mlx-swift#367), plus mlx 0.31.1 C++ updates.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but we need to wait for ml-explore/mlx-swift#376 -- the fmt C++ library is failing to build on the new Xcode.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thank you!

@davidkoski

Copy link
Copy Markdown
Member

Oops, per my previous s comment, waiting on a new tag for mlx-swift.

@davidkoski

Copy link
Copy Markdown
Member

And that is tagged, let's go!

@davidkoski
davidkoski merged commit 807885c into ml-explore:main Mar 27, 2026
2 checks passed
TheTom referenced this pull request in TheTom/mlx-swift-lm Apr 5, 2026
* Add copy() to KVCache protocol and all implementations

Add an independent deep-copy method to enable reusing a prefix cache
across multiple ChatSession instances without reloading from disk.

- Add copy() requirement to KVCache protocol
- Implement on KVCacheSimple, RotatingKVCache, QuantizedKVCache,
  ChunkedKVCache, ArraysCache, MambaCache, and CacheList
- Guard against empty state in all copy() methods to avoid fatalError
  from state setters that reject empty arrays
- Preserve leftPadding in ArraysCache/MambaCache copies
- Add CacheList array-based initializer to support copy()
- Change ArraysCache.leftPadding from private to internal for
  subclass access in MambaCache.copy()

Tests:
- testCacheCopyIsIndependent: parameterized across 6 cache types,
  verifies copy has same state and mutation of copy leaves original
  unchanged
- testCacheCopyOnEmptyCache: verifies copy of unpopulated cache
  does not crash
- testCacheListCopyIsIndependent: verifies CacheList with
  heterogeneous sub-caches copies independently

* Bump mlx-swift dependency to 0.31.1

Picks up the fix for array[.ellipsis] returning self instead of a
copy (ml-explore/mlx-swift#367), plus mlx 0.31.1 C++ updates.
mikedoise added a commit to mikedoise/mlx-swift-lm that referenced this pull request Apr 6, 2026
Enables cache cloning for speculative decoding and session management.
Upstream commit 807885c.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
davidtai added a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
The fork does not merge or rebase onto upstream. base.hash stays fixed.
Upstream fixes are ported one commit at a time, each described in its
own fork.yaml section. This replaces the rebase instructions, which no
longer apply.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
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.

2 participants