Conversation
|
Thanks for putting this together. I tested PR head Environment/config:
Validation results:
One measured-run smoke result, not a perf claim: uncompiled prefill/decode Only downstream compatibility change needed outside the compiled-path replacement was handling the new throwing |
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.
Proposed changes
Fixes #406.
This PR fixes incorrect token generation when autoregressive decoding is wrapped in
MLX.compile. The failure could cause generated text to enter repeating-token loops even though the same model decoded correctly without compilation.Root cause
During generation, a model keeps a KV cache containing information from tokens it has already processed. Each new token must be written into the next position in that cache.
The existing
KVCacheSimplestores this write position as a SwiftInt. When MLX compiles the decode function, it traces the function once and reuses the resulting computation graph. The Swift integer and the array slices derived from it are captured as constants during that trace.As a result, later decode calls can continue using the position observed during compilation instead of the current position. In simpler terms, the compiled decoder keeps writing to and reading from the wrong place in its memory. This is the root cause of the repeating generation reported in #406.
How this PR fixes it
This PR introduces
FixedCapacityKVCache, a cache designed for reusable compiled graphs:MLXArrayinstead of a Swift integer.A
CompiledDecodeSessionowns the cache and provides the safe high-level interface. It:Why this is a separate cache
KVCacheSimpleremains the better default for normal, non-compiled generation. It grows as needed and only performs attention over tokens that have actually been written.A compiled graph benefits from the opposite behavior: fixed shapes and tensor-managed state.
FixedCapacityKVCachetherefore allocates the requested capacity up front and performs attention over that capacity, masking unused positions. This uses more attention work when the cache is only partially filled, but it makes compiled decoding correct and allows the graph to be reused.Keeping these implementations separate avoids making ordinary eager generation slower to solve a compilation-specific problem.
Supported models
Support is capability-based through
FixedCapacityKVCacheProviding. A model should conform only after its position handling and cache topology have been audited for compiled execution.This PR initially enables:
Qwen2Model.LlamaModel.Models with sliding-window, recurrent, hybrid, vision-language, or custom cache layouts are not assumed to be compatible.
Testing
The new tests cover:
The Qwen2.5-7B regression was also run locally for 16 decode steps. Compiled and eager decoding produced matching logits and selected tokens at every step.
Longer-term direction
This issue exposes a broader distinction between ordinary Swift state and state that must change inside a reusable MLX computation graph.
Long term, compiled generation should probably move toward:
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes