[None][feat] Add per-iteration cached KV length to iter log#14734
Merged
yizhang-nv merged 1 commit intoJun 4, 2026
Merged
Conversation
yizhang-nv
force-pushed
the
dev-yizhan-per-iter-cached-kv-len
branch
from
May 29, 2026 07:07
4979859 to
e8f6630
Compare
Member
Author
|
/bot run --disable-fail-fast |
Collaborator
|
PR_Github #51002 [ run ] triggered by Bot. Commit: |
Collaborator
|
PR_Github #51002 [ run ] completed with state
|
yizhang-nv
force-pushed
the
dev-yizhan-per-iter-cached-kv-len
branch
from
June 1, 2026 05:30
e8f6630 to
25df6d3
Compare
Member
Author
|
/bot run --disable-fail-fast |
Collaborator
|
PR_Github #51315 [ run ] triggered by Bot. Commit: |
yizhang-nv
force-pushed
the
dev-yizhan-per-iter-cached-kv-len
branch
from
June 1, 2026 06:51
25df6d3 to
8cdc8db
Compare
Member
Author
|
/bot run --disable-fail-fast |
Collaborator
|
PR_Github #51325 [ run ] triggered by Bot. Commit: |
Collaborator
|
PR_Github #51315 [ run ] completed with state |
Collaborator
|
PR_Github #51325 [ run ] completed with state |
kaiyux
approved these changes
Jun 4, 2026
kaiyux
reviewed
Jun 4, 2026
Add cached KV length to the per-iteration log state dictionary emitted when print_iter_log is enabled. The value is stored in ModelEngine iter_states as 'cached_kv_tokens', so it appears under the existing states = {...} payload instead of as a separate top-level log field.
The value is computed as the sum of the per-sequence already-cached prefix (num_cached_tokens_per_seq) over the batch. Tokens computed in the current step (sequence_lengths) are being written this iteration and are not counted as cached. Using the logical cached length rather than the number of allocated blocks avoids under-counting under sliding window attention, where allocated blocks are capped by the window size.
Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
yizhang-nv
force-pushed
the
dev-yizhan-per-iter-cached-kv-len
branch
from
June 4, 2026 05:52
8cdc8db to
b7286ed
Compare
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.
Description
Adds a
cached_kv_lenfield to the per-iteration log emitted whenprint_iter_logis enabled, reporting the cached KV length (in tokens) currently held for the sequences scheduled in that iteration.The value is computed in
PyTorchModelEngineas the sum over the batch of the per-sequence already-cached prefix (num_cached_tokens_per_seq). Tokens computed in the current step (sequence_lengths) are being written this iteration and are not counted as cached. Using the logical cached length rather than the number of allocated blocks avoids under-counting under sliding window attention (SWA), where the number of allocated blocks is capped by the window size.It is also stored in
iter_statesascached_kv_tokensso the/metricsserializer can pick it up.Changes
tensorrt_llm/_torch/pyexecutor/model_engine.py: populateiter_states['cached_kv_tokens']withsum(num_cached_tokens_per_seq).tensorrt_llm/_torch/pyexecutor/py_executor.py: addcached_kv_lento the per-iteration log line.Validation
Built and served
Llama-3.2-1B-Instructon A100 withprint_iter_log: trueand sent a completion request (12 prompt tokens, 30 generated). The field behaves as expected:Test Coverage
Behavioral validation via
trtllm-serve(above). No new unit test added (log-only change).🤖 Generated with Claude Code