Skip to content

fix(huggingface): recompute context after KV-cache overflow ( - #420

Open
KartikP wants to merge 4 commits into
mainfrom
fix/kv-cache-overflow-parity
Open

KartikP wants to merge 4 commits into
mainfrom
fix/kv-cache-overflow-parity

Conversation

@KartikP

@KartikP KartikP commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #415.

Problem

#400 added a KV cache for behavioral-only tasks. Once the accumulated context exceeded the model's window, it sliced the oldest cache entries and kept feeding only new tokens. That is not equivalent to re-encoding the truncated window: kept keys/values still encode the dropped tokens, and absolute-position models (GPT-2, GPT-Neo, OPT) see mismatched positions. Futrell2018 scores changed for every model whose window is shorter than the corpus (~13-14k tokens).

Change

Drop the cache and recompute whenever the tokenizer truncates the context (past + new != truncated context length). Before overflow the cache is still used; after overflow behavior matches pre-#400.

Adds test_kv_cache_matches_recompute_past_context_window: distilgpt2 past its 1024-token window, cached path vs. the no-cache path (neural recording enabled).

Verification (g5.xlarge, A10G, transformers 4.57.1, torch 2.9.0)

Follow-up

Scores computed with the sliding cache after #400 merged will be rescored separately: falcon-7b, gemma-2b and opt-6.7b on Futrell2018. Models with windows of 32k or more never overflow on Futrell, and the post-#400 Wikitext rows have no stored score.

Note: Futrell scoring for short-window models returns to its pre-#400 cost (about 78 min for gpt-neo-1.3B on an A10G).

Slicing the KV cache once the context window overflows is not
equivalent to re-encoding the truncated window, which changed
Futrell2018 scores for models with short context windows. Drop the
cache when the tokenizer truncates so results match pre-#400 behavior.

Fixes #415
@KartikP KartikP closed this Oct 5, 2026
@KartikP KartikP reopened this Oct 5, 2026
@KartikP KartikP closed this Oct 5, 2026
@KartikP KartikP reopened this Oct 5, 2026
@KartikP KartikP closed this Oct 6, 2026
@KartikP KartikP reopened this Oct 6, 2026
@KartikP KartikP closed this Oct 6, 2026
@KartikP KartikP reopened this Oct 6, 2026
@KartikP KartikP changed the title fix(huggingface): recompute context after KV-cache overflow fix(huggingface): recompute context after KV-cache overflow ( Oct 9, 2026
KartikP added a commit that referenced this pull request Oct 15, 2026
Three plugins break against newly released third-party packages. All
three failures showed up in the CI for #420. That PR edits
`model_helpers`, which makes CI test every language plugin, and none of
the failures involve its diff.

## Failures and fixes
- **wikitext data**: the plugin requires `datasets==2.*`, which does not
cap `pyarrow`.
- pyarrow 26.0.0 (released 2026-10-09) raises `ImportError: pyarrow
requires NumPy 2.0 or newer` against our `numpy<2` pin.
- This fails
`tests/test_model_helpers/test_huggingface.py::TestNextWord::test_0_dimensional_tensor`
(Non-Plugins) on any PR that runs it.
  - Fix: pin `pyarrow<26`.
- **earley_parser**: NLTK 3.10 refuses to read corpus directories that
are not listed in `nltk.data.path`.
- Loading the bundled treebank fails with `PermissionError: Security
Violation [CorpusReader.__init__]: Unauthorized path
.../earley_parser/treebank`.
- Fix: add the treebank directory to `nltk.data.path` before reading it.
- **lm1b**: an unpinned `tensorflow` install upgrades NumPy to 2.x.
- The plugin's xarray import then fails at test collection with
`AttributeError: np.unicode_ was removed in the NumPy 2.0 release`.
  - Fix: add `numpy<2`.

## Verification
- `pyarrow==26.0.0` with `numpy==1.26.4` reproduces the ImportError.
`pyarrow==25.0.1` with the same NumPy imports cleanly.
- `nltk==3.10.3` reproduces the PermissionError on the bundled treebank.
With the path added, all 11 trees in `sample_treebank` parse.
- `pip install --dry-run` for linux x86_64 / Python 3.11:
- `tensorflow` alone resolves to numpy 2.4.6, which reproduces the lm1b
failure.
- `tensorflow` with `numpy<2` resolves to tensorflow 2.20.0 and numpy
1.26.4.
- `datasets==2.*` with `pyarrow<26` and `numpy<2` resolves to datasets
2.21.0, pyarrow 25.0.1 and numpy 1.26.4.
- The model tests themselves have not been run locally. This PR touches
all three plugins, so CI runs their tests.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

Futrell2018 GPT-Neo score changes at context overflow with HF sliding KV cache

1 participant