Skip to content

[None][feat] Add per-iteration cached KV length to iter log#14734

Merged
yizhang-nv merged 1 commit into
NVIDIA:feat/deepseek_v4from
yizhang-nv:dev-yizhan-per-iter-cached-kv-len
Jun 4, 2026
Merged

[None][feat] Add per-iteration cached KV length to iter log#14734
yizhang-nv merged 1 commit into
NVIDIA:feat/deepseek_v4from
yizhang-nv:dev-yizhan-per-iter-cached-kv-len

Conversation

@yizhang-nv

@yizhang-nv yizhang-nv commented May 29, 2026

Copy link
Copy Markdown
Member

Description

Adds a cached_kv_len field to the per-iteration log emitted when print_iter_log is enabled, reporting the cached KV length (in tokens) currently held for the sequences scheduled in that iteration.

The value is computed in PyTorchModelEngine as 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_states as cached_kv_tokens so the /metrics serializer can pick it up.

Changes

  • tensorrt_llm/_torch/pyexecutor/model_engine.py: populate iter_states['cached_kv_tokens'] with sum(num_cached_tokens_per_seq).
  • tensorrt_llm/_torch/pyexecutor/py_executor.py: add cached_kv_len to the per-iteration log line.

Validation

Built and served Llama-3.2-1B-Instruct on A100 with print_iter_log: true and sent a completion request (12 prompt tokens, 30 generated). The field behaves as expected:

iter = 1 ... cached_kv_len = 0   states = {..., 'num_ctx_tokens': 12, 'num_generation_tokens': 0, 'cached_kv_tokens': 0}
iter = 2 ... cached_kv_len = 12  states = {..., 'num_generation_tokens': 1, 'cached_kv_tokens': 12}
iter = 3 ... cached_kv_len = 13
...
iter = 31 ... cached_kv_len = 40
  • Context iteration reports 0 — the prompt is being written this step and is not yet cached.
  • Each generation iteration reports the accumulated already-cached length (prompt + previously generated tokens), growing by 1 per step.

Test Coverage

Behavioral validation via trtllm-serve (above). No new unit test added (log-only change).

🤖 Generated with Claude Code

@yizhang-nv
yizhang-nv force-pushed the dev-yizhan-per-iter-cached-kv-len branch from 4979859 to e8f6630 Compare May 29, 2026 07:07
@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@yizhang-nv
yizhang-nv marked this pull request as ready for review May 29, 2026 07:30
@yizhang-nv
yizhang-nv requested a review from a team as a code owner May 29, 2026 07:30
@yizhang-nv
yizhang-nv requested review from leslie-fang25 and removed request for a team May 29, 2026 07:30
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51002 [ run ] triggered by Bot. Commit: e8f6630 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51002 [ run ] completed with state SUCCESS. Commit: e8f6630
/LLM/main/L0_MergeRequest_PR pipeline #40451 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@yizhang-nv
yizhang-nv force-pushed the dev-yizhan-per-iter-cached-kv-len branch from e8f6630 to 25df6d3 Compare June 1, 2026 05:30
@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51315 [ run ] triggered by Bot. Commit: 25df6d3 Link to invocation

@yizhang-nv
yizhang-nv force-pushed the dev-yizhan-per-iter-cached-kv-len branch from 25df6d3 to 8cdc8db Compare June 1, 2026 06:51
@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51325 [ run ] triggered by Bot. Commit: 8cdc8db Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51315 [ run ] completed with state ABORTED. Commit: 25df6d3

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51325 [ run ] completed with state SUCCESS. Commit: 8cdc8db
/LLM/main/L0_MergeRequest_PR pipeline #40739 completed with status: 'SUCCESS'

CI Report

Link to invocation

@lfr-0531
lfr-0531 requested review from HuiGao-NV, QiJune and kaiyux June 2, 2026 04:12
Comment thread tensorrt_llm/_torch/pyexecutor/model_engine.py Outdated
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
yizhang-nv force-pushed the dev-yizhan-per-iter-cached-kv-len branch from 8cdc8db to b7286ed Compare June 4, 2026 05:52
@yizhang-nv
yizhang-nv merged commit c5399fc into NVIDIA:feat/deepseek_v4 Jun 4, 2026
5 checks passed
jiaganc added a commit to jiaganc/TensorRT-LLM that referenced this pull request Jun 26, 2026
…4734)

Source-Commit: c5399fc
Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
jiaganc added a commit to jiaganc/TensorRT-LLM that referenced this pull request Jun 29, 2026
…4734)

Source-Commit: c5399fc
Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants