Skip to content

Keep GPT-OSS attention sinks with a quantized KV cache - #794

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
Augustas11:gpt-oss-quantized-kv-sinks
Dec 4, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
Augustas11:gpt-oss-quantized-kv-sinks

Conversation

@Augustas11

Copy link
Copy Markdown
Contributor

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:

  • GPT-OSS alternates sliding-window and full-attention layers. Unless a cache capacity is set (maxKVSize or a kvCache capacity), the full-attention layers get a KVCacheSimple.
  • With an affine quantized cache (kvBits, kvScheme "affine4" / "affine8", or kvCache with strategy .affine), generation converts each KVCacheSimple to a QuantizedKVCache once its offset passes the compression start (quantizedKVStart). The sliding-window layers keep their RotatingKVCache.
  • For a quantized cache, GPTOSS.swift calls quantizedScaledDotProductAttention, which has no sinks parameter. Only the unquantized branch passes sinks.

So the sliding-window layers still use their sinks and the full-attention layers silently drop them.

This PR gives quantizedScaledDotProductAttention an optional sinks argument 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 as MLXFast.scaledDotProductAttention(sinks:). The argument defaults to nil, so other callers are unchanged.

mlx-lm (0.32.2) refuses in this situation where Swift drops. With kv_bits, GPT-OSS stops with NotImplementedError ("RotatingKVCache Quantization NYI") when generation tries to quantize its sliding-window RotatingKVCache, and scaled_dot_product_attention in models/base.py raises "Quantized SDPA does not support attention sinks." for any quantized cache given sinks. MiMoV2Flash here has that check as a precondition. 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:

  • GPT-OSS calls quantizedScaledDotProductAttention directly, on main and here, and does not go through quantizedCacheAttention (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 prefer sinks on quantizedCacheAttention and GPT-OSS routed through it, I can do that here.
  • The sink column costs one concatenation and one slice per attention call. That is negligible for decode; for a prefill chunk it is one or two extra buffers the size of the scores.

Tests

Tests/MLXLMTests/GPTOSSQuantizedKVSinksTests.swift builds 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 own maybeQuantizeKVCache, 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:

Comparison main this PR
Unquantized cache: real sinks against neutral sinks 0.441 0.441
Quantized cache: real sinks against neutral sinks 0.0 0.442
Unquantized against quantized, one decode token 0.442 0.0015
Unquantized against quantized, three-token chunk 0.782 0.0021

On main the 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 on main with three assertions and passes with the change.

Not covered

  • I traced and tested the affine path only. I did not check the TurboQuant or variance-normalized cache types.
  • The test model has 4 query heads and 2 KV heads, so it covers the grouped layout, which is the one GPT-OSS uses. The ungrouped layout, an explicit array mask with sinks, and the GPU path are not tested.
  • I measured a tiny random model, not a published GPT-OSS checkpoint, so I cannot say how much the missing sinks change real output.
  • MiMoV2Flash keeps its precondition. It could pass its sinks now, but I have no test for that model, so I left it for a separate change.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes (I ran swift-format with the repository configuration on the three changed files)
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: Developed with AI coding assistance, including the test. I reviewed the code and the results.

Augustas from Malibu

@davidkoski

Copy link
Copy Markdown
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
Augustas11 force-pushed the gpt-oss-quantized-kv-sinks branch from 5c1e950 to 4f36478 Compare December 3, 2026 01:14
@Augustas11

Copy link
Copy Markdown
Contributor Author

Rebased onto main (0e4e170e, picking up #848). GPTOSSQuantizedKVSinksTests passes locally.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good to me. Thank you!

@davidkoski
davidkoski merged commit ebc444c into ml-explore:main Dec 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants