Skip to content

Enforce and expose effective maxKVSize cache policy - #514

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:avoid_maxKVSize_ignored
Aug 12, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:avoid_maxKVSize_ignored

Conversation

@aleroot

@aleroot aleroot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

GenerateParameters.maxKVSize can currently be silently ignored by models that provide custom newCache(parameters:) implementations. This is particularly problematic for hybrid architectures, where attention and state-space layers use different cache types.

A caller may request a bounded KV cache while the model continues allocating an unbounded attention cache, with no public API for detecting that the requested policy was not applied. This makes memory usage unpredictable during long conversations and forces downstream applications to inspect private cache implementation details.

This PR makes the requested and effective KV-cache policies explicit and reliable.

What

  • Enforces maxKVSize across the audited model-specific cache implementations.
  • Applies the smaller of the architecture's sliding-window size and the requested maxKVSize.
  • Handles full-attention, sliding-window, hybrid attention/state-space, and pure state-space models.
  • Adds a typed EffectiveCacheConfiguration API that describes:
    • the requested maximum KV-cache size;
    • the effective policy for every model layer;
    • bounded and unbounded attention caches;
    • rotating/sliding-window caches;
    • state-space caches;
    • composite caches used by hybrid models;
    • whether the request was fully applied, partially applied, unsupported, or ignored.
  • Exposes the effective policy through LanguageModel, ModelContainer, and ChatSession.
  • Makes ChatSession report the realized cache configuration rather than allocating and inspecting probe caches.
  • Rebuilds a structured conversation cache when maxKVSize changes, ensuring the new policy is actually applied without losing the conversation transcript.
  • Preserves externally restored raw caches when their original prefix cannot be reconstructed, while still reporting their actual effective policy.
  • Safely handles very small limits such as maxKVSize = 1.
  • Adds documentation describing cache-policy implementation and inspection.

Why

maxKVSize is a memory-management contract. Silently ignoring it can cause unexpectedly high memory pressure, especially during long-running conversations and on memory-constrained Apple Silicon devices.

The new API also lets applications distinguish between:

  • a limit that was fully enforced;
  • a limit applied only to the attention layers of a hybrid model;
  • an architecture where token-window limits do not apply;
  • and an implementation that failed to apply the requested policy.

This distinction is important because state-space caches do not grow per token in the same way as attention KV caches and should not be reported as bounded attention caches.

With this change, an App using the library can:

  • rely on maxKVSize being enforced for supported attention layers;
  • display accurate user-facing diagnostics about the active cache policy;
  • distinguish fully bounded models from partially bounded hybrid models;
  • avoid allocating probe caches or casting to internal cache implementations;
  • safely change the cache limit between conversation turns;
  • make better model-selection and memory-management decisions before long generations;
  • avoid presenting a configured memory limit as active when the model cannot apply it.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@aleroot
aleroot force-pushed the avoid_maxKVSize_ignored branch from de7edc4 to 20ec2df Compare August 7, 2026 03:55
Comment thread Libraries/MLXLMCommon/ChatSession.swift Resolved

@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! See what you think about the error case -- given the recent work in turning some fatals into throws this might be worth fixing.

@aleroot
aleroot force-pushed the avoid_maxKVSize_ignored branch 2 times, most recently from cc3cf77 to 9f83553 Compare August 10, 2026 19:20
@aleroot
aleroot requested a review from davidkoski August 10, 2026 19:21

@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.

I think this is ready to go -- just needs a rebase to fix a conflict. Thank you!

Honor maxKVSize across full, sliding-window, and hybrid model caches. Report the realized per-layer policy through model containers and chat sessions, rebuild structured session caches when the limit changes, and add broad regression coverage and documentation.
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