Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reserve KV cache capacity after the first model run #408

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

robertknight
Copy link
Owner

@robertknight robertknight commented Nov 15, 2024

Hugging Face models with separate branches for the first and subsequent iterations do not use the input KV cache buffer on the first run. Thus they did not benefit from the pre-allocated capacity and ended up re-allocating a new KV cache buffer on each run.

To resolve this, change the KV cache growth strategy to grow the buffer after the model runs, if the capacity limit has been reached. Also replace the hard-coded capacity with a growth strategy that doubles the capacity each time. This amortizes the costs of copying the old KV cache into the new buffer.

This impacts the Whisper and TrOCR examples. For the Whisper example, this reduced time for transcribing a 2-minute audio clip with the base model from ~6.1s to ~5.7s (~6%). With the larger models the benefit will be greater since the caches are larger.

Hugging Face models with separate branches for the first and subsequent
iterations do not use the input KV cache buffer on the first run. Thus they did
not benefit from the pre-allocated capacity and ended up re-allocating a new KV
cache buffer on each run.

To resolve this, change the KV cache growth strategy to grow the buffer after
the model runs, if the capacity limit has been reached. Also replace the
hard-coded capacity with a growth strategy that doubles the capacity each time.
This amortizes the costs of copying the old KV cache into the new buffer.
@robertknight robertknight marked this pull request as ready for review November 15, 2024 08:59
@robertknight robertknight merged commit 408ebe0 into main Nov 15, 2024
2 checks passed
@robertknight robertknight deleted the kv-cache-grow-after-run branch November 15, 2024 08:59
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.

1 participant