Add packed paged KV formats with bounded workspace admission - #142
Draft
Gajesh2007 wants to merge 3 commits into
Draft
Add packed paged KV formats with bounded workspace admission#142Gajesh2007 wants to merge 3 commits into
Gajesh2007 wants to merge 3 commits into
Conversation
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
Add optional packed full-attention KV storage with K4/V4, K8/V4 and K8/V8 formats, token-local affine groups, FP32 scale/offset metadata and a fixed signed Hadamard basis for Q/K. Native KV remains the default; sliding windows and recurrent state retain their existing representation. K4/V4 uses five effective bits per value including metadata, so its BF16 payload reduction is 3.2× before other memory costs.
Direct attention consumes packed pages without expanding the history. Physical admission separately prices growing storage, page/fixed costs and guaranteed step workspace. Step-owned arenas have real completion fences, including overlapping decode/MTP work; credits are refunded after GPU retirement. Resident reuse and complete checkpoints preserve packed bytes and reject incompatible storage identities before allocation.
An explicit optional prefill mode can reserve additional FP32 reconstruction and output memory for forced fused SDPA on eligible D64/D256 shapes. It falls back to the funded direct path when geometry or budget is unsuitable. Deadline-aware arrivals retire the actual optional step before forecasting remaining work against their original absolute deadline, including real drain time. Native/default direct forecasts are unchanged.
Before
After
flowchart LR R[Request and explicit storage config] --> A[AdmissionV2: storage plus guaranteed workspace] A --> P[PagedLayerCache: quantize fresh K/V once] P --> D[Direct packed attention] P --> G{Eligible prefill and extra permit?} G -->|yes| S[Step arena plus forced fused SDPA] G -->|no| D D --> F[GPU fenced finalize then refund] S --> F N[Deadline arrival with optional step in flight] --> F F --> V[Forecast remaining work using original deadline] P --> C[Exact packed checkpoint with format identity]Validation
On Apple M4 Max, the scoped baseline covered 126 unique native tests across numerical, checkpoint and admission runs. Subsequent overlapping suites include 40 optional-fused integration tests, 23 stride tests and 28 XCTest plus 11 Swift Testing deadline/ownership tests. The last group includes six new arrival cases covering near-full grants, partial prefills, expiration during actual retirement, cancellation and reused-ID ownership, and the unchanged direct path. These overlapping counts are not a new aggregate total.
Raw logs, exact model observations and performance limitations are retained by d-inference #860. Small authored quality probes do not establish broad non-inferiority. The current direct path is slower than native for measured 4K/16K prefill; no native-speed or serving-default claim is made.
Depends on mlx-swift #22, which pins mlx-c #9 and mlx #17 for the explicit fused API and FP32 wide-head tile correction. No merge or rollout is included.