Size the KV cache to the request rather than model.config.max_seq_len - #1327
Open
risjain wants to merge 1 commit into
Open
Size the KV cache to the request rather than model.config.max_seq_len#1327risjain wants to merge 1 commit into
risjain wants to merge 1 commit into
Conversation
Every decode step attends over the whole allocated cache, so defaulting to the model maximum (32768) makes a few-hundred-token utterance pay for positions it never uses. Deriving the default from T + max_new_tokens measured 0.84 -> 4.23 decode tok/s on a gfx1151 APU, and RTF 4.10 -> 1.96 end-to-end through the API server. A new MAX_SEQ_LEN environment variable overrides it. launch_thread_safe_queue() builds its cache before generate() runs and cannot know request lengths, so it keeps the model maximum unless MAX_SEQ_LEN is set. Without that second site the override has no effect on the queue path, since generate() skips sizing once _cache_setup_done is set. Integrated GPUs see this worst, sharing DRAM bandwidth with the CPU.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every decode step attends over the whole allocated cache, so defaulting
max_seq_lento the model maximum (32768) makes a few-hundred-token utterance payfor positions it never uses.
Isolated decode on a gfx1151 APU (s2-pro bf16):
End-to-end through the API server with a cloned reference voice: RTF 4.10 ->
1.96, i.e. a 10-hour audiobook render in 20 h instead of 42 h.
This derives the default from
T + max_new_tokens + 64and adds aMAX_SEQ_LENenvironment variable as an override.
Two things worth reviewing:
launch_thread_safe_queue()builds its cache beforegenerate()runs andcannot know request lengths, so it keeps the model maximum unless
MAX_SEQ_LENis set. Without touching that second site the override does nothing on the queue
path, because
generate()skips its own sizing once_cache_setup_doneis set.and then overflows on a long chunk (1167 prompt tokens + up to 1023 generated),
hence the
+ 64margin on top ofT + max_new_tokens.Not AMD-specific, though integrated GPUs feel it worst: they share DRAM bandwidth
with the CPU, and bandwidth is exactly what this wastes. Measured on a Radeon
8060S (gfx1151, 128 GB shared LPDDR5 ~256 GB/s), ROCm 7.13.
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.