fix: remove O(T) dummy tensor and auto-Metal-kernel in compressed MLX cache - #16
Merged
Conversation
…ategy Default compressed strategy now falls through to _reconstruct_compressed + native mx.fast.scaled_dot_product_attention instead of dispatching to the custom batched_fused_polar_decode_v2 Metal kernel. Before: 62 tok/s, 2143 MB peak (3.3x slower + 2x more memory than baseline) After: 225 tok/s, 1025 MB peak (20% slower, same memory as baseline) The Metal kernel path remains available as an explicit opt-in via patch_fused_attention(model, cache_list) for users needing fused decode at very long context lengths where re-materialising float16 K/V is costly. Root causes fixed: - mx.zeros((1, H, T, D)) allocated on every decode step x layer, growing O(T) - self.keys = dummy prevented MLX GC of the growing tensor - batched_fused_polar_decode_v2 has higher dispatch overhead than Apple SDPA for short sequences (<100 tokens)
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.
Summary
update_and_fetchallocatedmx.zeros((1, H, T, D))on every decode step x layer (growing O(T)), and setself.keys = dummywhich blocked MLX GC — doubling peak memory._patch.pyautomatically installedpatch_fused_attention, routing all decode steps throughbatched_fused_polar_decode_v2(custom Metal kernel with higher dispatch overhead than Apple SDPA for short sequences).Fix: Default compressed strategy now falls through to
_reconstruct_compressed+mx.fast.scaled_dot_product_attention. Metal kernel path remains as explicit opt-in viapatch_fused_attention(model, cache_list).Test plan
bench_mlx_compressed.py: 225/238 tok/s vs 282 tok/s baseline