Skip to content

[V1] Add num_cached_tokens stats for request output#17519

Closed
simon-mo wants to merge 5 commits intovllm-project:mainfrom
simon-mo:num_cached_tokens
Closed

[V1] Add num_cached_tokens stats for request output#17519
simon-mo wants to merge 5 commits intovllm-project:mainfrom
simon-mo:num_cached_tokens

Conversation

@simon-mo
Copy link
Copy Markdown
Collaborator

@simon-mo simon-mo commented May 1, 2025

V1 never supported this item in request output. so output.num_cached_tokens from LLM.generate is always None. This PR adds support for it.

Signed-off-by: simon-mo <xmo@berkeley.edu>
…okens

Signed-off-by: simon-mo <xmo@berkeley.edu>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify Bot added the v1 label May 1, 2025
Signed-off-by: simon-mo <xmo@berkeley.edu>
@simon-mo simon-mo enabled auto-merge (squash) May 1, 2025 06:09
@github-actions github-actions Bot added the ready ONLY add when PR is ready to merge/full CI is needed label May 1, 2025
@WoosukKwon
Copy link
Copy Markdown
Collaborator

One caveat is that num_cached_tokens will be refreshed if the request is preempted and resumed. I think this needs to be documented somewhere.

simon-mo added 2 commits May 8, 2025 10:07
@simon-mo simon-mo enabled auto-merge (squash) May 8, 2025 17:08
Copy link
Copy Markdown
Collaborator

@WoosukKwon WoosukKwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw why do we want to have this feature?

Comment thread vllm/v1/request.py
self._all_token_ids: list[int] = self.prompt_token_ids.copy()
self.spec_token_ids: list[int] = []
self.num_computed_tokens = 0
self.num_cached_tokens = 0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is confusing.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simon-mo Please add a comment. I think people will be confused between num_computed_tokens and num_cached_tokens otw.

@WoosukKwon WoosukKwon disabled auto-merge May 8, 2025 17:16
@WoosukKwon
Copy link
Copy Markdown
Collaborator

I think this exposes implementation details to the API, which is not recommended unless we have a clear use case.

Copy link
Copy Markdown
Collaborator Author

simon-mo commented May 8, 2025

This needs to be piped to the API as part of the protocol here

https://openai.com/index/api-prompt-caching/

Copy link
Copy Markdown
Collaborator

@WoosukKwon WoosukKwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simon-mo Oh I see, I didn't know that the prompt caching api had this.

Comment thread vllm/v1/request.py
self._all_token_ids: list[int] = self.prompt_token_ids.copy()
self.spec_token_ids: list[int] = []
self.num_computed_tokens = 0
self.num_cached_tokens = 0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simon-mo Please add a comment. I think people will be confused between num_computed_tokens and num_cached_tokens otw.

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 12, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @simon-mo.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label May 12, 2025
@WoosukKwon
Copy link
Copy Markdown
Collaborator

@simon-mo Also, I think we can initialize num_cached_tokens to -1 and update it only once when the request is first scheduled.

@calvin0327
Copy link
Copy Markdown
Contributor

I didn't notice this fix. I also submitted a PR to address this issue. #18192 😅

@DarkLight1337
Copy link
Copy Markdown
Member

Closing as superseded by #18149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-rebase ready ONLY add when PR is ready to merge/full CI is needed v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants