Repository navigation
Fix QuantizedCache residual flushing for multi-token updates - #49418
Conversation
CI recapDashboard: View test results in Grafana |
| 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: |
There was a problem hiding this comment.
aren;t we supposed to self.keys.shape[-2] + key_states.shape[-2] to get the full length?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
ah didn't notice the addition of + key_states.shape[-2]
|
The only failing test in |
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
python -m pytest tests/utils/test_cache_utils.py::CacheTest -q -o addopts= --disable-warnings: 18 passed / 1 skipped on CPU.test_generate_with_quant_cacheandtest_generate_continue_from_past_key_values: 2 passed.make fix-repo,make check-repo,make styleandgit 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.