Repository navigation
Keep GPT-OSS attention sinks with a quantized KV cache - #794
Merged
Merged
Conversation
Augustas11
force-pushed
the
gpt-oss-quantized-kv-sinks
branch
from
November 30, 2026 23:33
01f9481 to
a531836
Compare
davidkoski
force-pushed
the
gpt-oss-quantized-kv-sinks
branch
from
December 1, 2026 14:13
a531836 to
5c1e950
Compare
Member
|
Rebase to pick up #848 |
With `kvBits` set, generation turns each full-attention `KVCacheSimple` into a `QuantizedKVCache`. GPT-OSS then takes its quantized branch, which calls `quantizedScaledDotProductAttention` without the sinks, so those layers attend as if the model had no sinks. Nothing fails or warns. `quantizedScaledDotProductAttention` takes an optional `sinks` array and adds each head's sink as an extra softmax logit with no value row, as `MLXFast.scaledDotProductAttention(sinks:)` does. GPT-OSS passes its sinks. Callers that pass no sinks get the same computation as before.
Augustas11
force-pushed
the
gpt-oss-quantized-kv-sinks
branch
from
December 3, 2026 01:14
5c1e950 to
4f36478
Compare
Contributor
Author
|
Rebased onto |
davidkoski
approved these changes
Dec 4, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good to me. Thank you!
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.
Proposed changes
With an affine quantized KV cache, GPT-OSS runs its full-attention layers without their attention sinks, and nothing fails or warns.
How it happens on
main:maxKVSizeor akvCachecapacity), the full-attention layers get aKVCacheSimple.kvBits,kvScheme"affine4"/"affine8", orkvCachewith strategy.affine), generation converts eachKVCacheSimpleto aQuantizedKVCacheonce its offset passes the compression start (quantizedKVStart). The sliding-window layers keep theirRotatingKVCache.GPTOSS.swiftcallsquantizedScaledDotProductAttention, which has no sinks parameter. Only the unquantized branch passessinks.So the sliding-window layers still use their sinks and the full-attention layers silently drop them.
This PR gives
quantizedScaledDotProductAttentionan optionalsinksargument and passes the GPT-OSS sinks to it. Each head's sink is added as one extra logit before the softmax and its column is dropped afterwards, the same formula asMLXFast.scaledDotProductAttention(sinks:). The argument defaults tonil, so other callers are unchanged.mlx-lm (0.32.2) refuses in this situation where Swift drops. With
kv_bits, GPT-OSS stops withNotImplementedError("RotatingKVCache Quantization NYI") when generation tries to quantize its sliding-windowRotatingKVCache, andscaled_dot_product_attentioninmodels/base.pyraises "Quantized SDPA does not support attention sinks." for any quantized cache given sinks.MiMoV2Flashhere has that check as aprecondition. I chose to support the sinks because the change is small, but if you would prefer GPT-OSS to refuse a quantized cache as mlx-lm does, I can change the PR to that.Two design notes:
quantizedScaledDotProductAttentiondirectly, onmainand here, and does not go throughquantizedCacheAttention(Prefill over a quantized KV cache through the fused SDPA kernel #703). I kept the change at the call GPT-OSS already makes. If you would prefersinksonquantizedCacheAttentionand GPT-OSS routed through it, I can do that here.Tests
Tests/MLXLMTests/GPTOSSQuantizedKVSinksTests.swiftbuilds a tiny GPT-OSS model with one sliding-window and one full-attention layer, prefills 10 tokens, quantizes the cache to 8 bits with the library's ownmaybeQuantizeKVCache, and compares decode logits. As a control it runs everything again with the full-attention layer's sinks set to −1e9, which is the same as having no sinks.Largest logit difference, CPU, float32:
mainOn
mainthe quantized output does not change at all when the sinks change, and its distance from the unquantized output is the size of the sinks' effect. With this change the quantized cache follows the unquantized one to within quantization noise. The test fails onmainwith three assertions and passes with the change.Not covered
MiMoV2Flashkeeps itsprecondition. It could pass its sinks now, but I have no test for that model, so I left it for a separate change.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (I ranswift-formatwith the repository configuration on the three changed files)AI usage
accurately describes the code changes.
Augustas from Malibu