Skip to content

Fix QuantizedCache residual flushing for multi-token updates - #49418

Merged
zucchini-nlp merged 1 commit into
huggingface:mainfrom
LE0-Lin:fix/quantized-cache-chunk-flush
Oct 8, 2026
Merged

zucchini-nlp merged 1 commit into
huggingface:mainfrom
LE0-Lin:fix/quantized-cache-chunk-flush

Conversation

@LE0-Lin

@LE0-Lin LE0-Lin commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

What does this PR do?

Fixes #49393.

I check the incoming key length together with the existing residual length, treating the empty residual as length zero. This flushes the full-precision cache when a cached-prefix continuation reaches or exceeds residual_length.

The regression uses the real Quanto backend and covers empty/nonempty residuals, exact/oversized updates, repeated flushes, below-threshold updates and single-token decoding. It also checks that the complete keys, values and sequence length are preserved.

Coordinated with a maintainer here. The issue timeline and independent QuantizedCache/residual searches did not find an overlapping fix. AI assistance was used. I reviewed the full diff and ran the CacheTest suite and cached-prefix generation reproducer locally.

Validation

  • New scenarios: 4 failed / 2 passed before; all 6 passed after.
  • python -m pytest tests/utils/test_cache_utils.py::CacheTest -q -o addopts= --disable-warnings: 18 passed / 1 skipped on CPU.
  • Llama test_generate_with_quant_cache and test_generate_continue_from_past_key_values: 2 passed.
  • The original cached-prefix generate reproduction retains 0 full-precision and 272 quantized tokens, with cache length 272 and output length 273.
  • make fix-repo, make check-repo, make style and git diff --check: passed.

GPU/HQQ and the full selector-generated suite were not run. A broader offline cache test attempt encountered 3 failures and 22 setup errors while loading uncached pretrained checkpoints; those integration/export cases remain unvalidated locally.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 37759137442
Result: failure | Grafana metrics are not available yet.

Comment on lines -775 to +776
if self.keys.dim() == 4 and self.keys.shape[-2] + 1 >= self.residual_length:
residual_length = self.keys.shape[-2] if self.keys.dim() == 4 else 0
if residual_length + key_states.shape[-2] >= self.residual_length:

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.

aren;t we supposed to self.keys.shape[-2] + key_states.shape[-2] to get the full length?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes — residual_length on the preceding line is self.keys.shape[-2] when the residual is nonempty, so the condition uses that sum. The fallback to 0 handles the 1-D empty tensor left after initialization or a flush, where shape[-2] would raise. The regression covers both empty and nonempty residuals at and above the threshold.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I could have explained that more clearly. The condition adds both lengths: residual_length is self.keys.shape[-2] when the residual is 4-D.

After initialization or a flush, self.keys can be a 1-D empty tensor. We use 0 in that case because accessing shape[-2] would raise an IndexError.

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.

ah didn't notice the addition of + key_states.shape[-2]

@LE0-Lin

LE0-Lin commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The only failing test in tests_torch shard 7/8 is ZayaModelTest.test_causal_lm_loss_honors_shift_labels (1 failed, 6645 passed). It passes in isolation locally on CPU both on the unmodified fa40619 baseline and on this PR head. That test sets use_cache=False; I have not reproduced the CI assertion locally. Could you rerun shard 7/8 and refresh the CI status? I have not changed the quantized-cache patch.

@zucchini-nlp
zucchini-nlp added this pull request to the merge queue Oct 8, 2026
Merged via the queue into huggingface:main with commit 1734d8d Oct 8, 2026
218 of 220 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QuantizedCache exceeds residual_length on multi-token cache updates

2 participants