Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio MLX estimate: ask the quantized-attention route about a cache longer than the chunk (#15970)

Debojyoti Banerjee245b9d0ab7a4
diff --git a/studio/backend/core/inference/mlx_memory.py b/studio/backend/core/inference/mlx_memory.py
index 94b1c17a67e..45cdbfd8bc1 100644
--- a/studio/backend/core/inference/mlx_memory.py
+++ b/studio/backend/core/inference/mlx_memory.py
@@ -954,10 +954,12 @@ def _dequantized_row_bytes(widths, dtype_size: int, kv_bits, kv_group_size, pref
return None
try:
dtype = mx.float32 if dtype_size == 4 else mx.bfloat16
+ # A causal chunk never fuses over fewer keys than queries; later chunks see more (#15935).
+ cached = prefill_chunk + 1
def rows(width):
return mx.quantize(
- mx.zeros((1, kv_heads, 1, width), dtype = dtype),
+ mx.zeros((1, kv_heads, cached, width), dtype = dtype),
group_size = kv_group_size,
bits = kv_bits,
)
diff --git a/studio/backend/tests/test_memory_estimate.py b/studio/backend/tests/test_memory_estimate.py
index afe487ec2de..4518979433d 100644
--- a/studio/backend/tests/test_memory_estimate.py
+++ b/studio/backend/tests/test_memory_estimate.py
@@ -4520,6 +4520,50 @@ class TestTheQuantizedAttentionRouteIsTheOneZooTakes:
assert mm._dequantized_row_bytes((3584, 18944, 32, 0, 0, 0), 2, 4, 64, 2048) is None
[email protected]("chunk", [256, 2048])
+def test_the_route_is_asked_about_a_cache_longer_than_the_chunk(monkeypatch, chunk):
+ """zoo never fuses a causal chunk over fewer keys than queries (#15935)."""
+ asked = []
+
+ def quantize(w, group_size, bits):
+ *lead, width = w.shape
+ packed = SimpleNamespace(shape = (*lead, width * bits // 32), dtype = SimpleNamespace(size = 4))
+ scales = SimpleNamespace(shape = (*lead, width // group_size), dtype = w.dtype)
+ return packed, scales, scales
+
+ mx = SimpleNamespace(
+ float32 = SimpleNamespace(size = 4),
+ bfloat16 = SimpleNamespace(size = 2),
+ zeros = lambda shape, dtype: SimpleNamespace(shape = shape, dtype = dtype),
+ quantize = quantize,
+ )
+
+ def fused_kernel_exists(
+ queries,
+ q_keys,
+ q_values,
+ group_size,
+ mask = None,
+ ):
+ asked.append((queries.shape[-2], q_keys[0].shape[-2], q_values[0].shape[-2], mask))
+ return q_keys[0].shape[-2] >= queries.shape[-2]
+
+ zoo = SimpleNamespace(
+ _row_bytes = lambda cache, group_size, dtype: 7,
+ dequantizing_is_smaller = lambda *args: True,
+ fused_kernel_exists = fused_kernel_exists,
+ )
+ monkeypatch.setitem(sys.modules, "mlx", SimpleNamespace(core = mx))
+ monkeypatch.setitem(sys.modules, "mlx.core", mx)
+ monkeypatch.setitem(sys.modules, "unsloth_zoo.mlx.attention", zoo)
+
+ widths = (3584, 18944, 32, 8, 128, 0)
+ assert mm._dequantized_row_bytes(widths, 2, 4, 64, chunk) == 8 * 2 * 7
+ ((queries, keys, values, mask),) = asked
+ assert queries == chunk and keys == values > chunk
+ assert mask == "causal"
+
+
@pytest.mark.skipif(not _HAVE_MLX, reason = "imports architecture modules")
class TestProbeFollowsTheLoadersRoute:
"""The estimator must not price a load path that cannot be taken."""