From 6df774f0425ea3f60fb8df6f192b4a544925c167 Mon Sep 17 00:00:00 2001 From: David Tai Date: Wed, 30 Sep 2026 16:24:36 -0500 Subject: [PATCH] fix(paged): write a full destination for broadcast direct KV prepareRetainedWork compacts direct prompt K and V with where(true, bits, bits). When the bits hold one element, for example a broadcast scalar, MLX keeps the one-element buffer and the broadcast strides. finishEvaluation then refuses the step: "retained paged producer root is not a bounded compact destination". contiguous now writes the full row-major destination in that case. For a full result it shares the buffer and copies nothing. Co-Authored-By: Claude Opus 5.5 --- .../ContinuousBatchingV2/Paged/PagedLayerCache.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Libraries/MLXLMCommon/ContinuousBatchingV2/Paged/PagedLayerCache.swift b/Libraries/MLXLMCommon/ContinuousBatchingV2/Paged/PagedLayerCache.swift index 2604ac2e26..8034750742 100644 --- a/Libraries/MLXLMCommon/ContinuousBatchingV2/Paged/PagedLayerCache.swift +++ b/Libraries/MLXLMCommon/ContinuousBatchingV2/Paged/PagedLayerCache.swift @@ -1123,10 +1123,15 @@ public final class PagedLayerCache: CBv2AttendingLayerCache { // Detach its raw bits so a cache loan never retains that unknown parent // beyond this producer step. History concat/gather already owns a fresh // exact role-shaped destination. No dtype or attention math changes. + // + // When every input of `where` holds one element (a broadcast + // scalar), MLX keeps the one-element buffer and the broadcast + // strides. `contiguous` then writes the full row-major destination. + // For the usual full result it shares the buffer and copies nothing. func compact(_ input: MLXArray) -> MLXArray { let dtype: DType = input.dtype.size == 2 ? .uint16 : .uint32 let bits = input.view(dtype: dtype) - return MLX.where(MLXArray(true), bits, bits).view(dtype: input.dtype) + return contiguous(MLX.where(MLXArray(true), bits, bits)).view(dtype: input.dtype) } let direct = value.historyCount == 0 && row.frozenHighWater <= value.queryStart let keys = direct ? compact(value.keys) : value.keys