Skip to content

fix: remove O(T) dummy tensor and auto-Metal-kernel in compressed MLX cache - #16

Merged
pbertsch merged 1 commit into
mainfrom
fix/mlx-compressed-throughput
Jun 30, 2026
Merged

fix: remove O(T) dummy tensor and auto-Metal-kernel in compressed MLX cache#16
pbertsch merged 1 commit into
mainfrom
fix/mlx-compressed-throughput

Conversation

@pbertsch

Copy link
Copy Markdown
Member

Summary

  • Root cause 1: update_and_fetch allocated mx.zeros((1, H, T, D)) on every decode step x layer (growing O(T)), and set self.keys = dummy which blocked MLX GC — doubling peak memory.
  • Root cause 2: _patch.py automatically installed patch_fused_attention, routing all decode steps through batched_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 via patch_fused_attention(model, cache_list).

Metric Before After
Throughput (8-bit) 62 tok/s (3.3x slower) 225 tok/s (20% slower)
Throughput (4-bit) 73 tok/s (2.8x slower) 238 tok/s (15% slower)
Peak memory 2143 MB (2x baseline) 1025 MB (same as baseline)

Test plan

  • 780/780 unit tests pass
  • E2E model tests pass (9/9)
  • bench_mlx_compressed.py: 225/238 tok/s vs 282 tok/s baseline
  • Updated 2 tests that asserted old O(T)-dummy behaviour

…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)
@pbertsch
pbertsch merged commit f8f9459 into main Jun 30, 2026
6 checks passed
@pbertsch
pbertsch deleted the fix/mlx-compressed-throughput branch June 30, 2026 19:50
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.

1 participant