Skip to content

replace with torch.cuda.stream() - #33225

Open
xinyu-intel wants to merge 5 commits into
vllm-project:mainfrom
xinyu-intel:dev/torch-stream
Open

xinyu-intel wants to merge 5 commits into
vllm-project:mainfrom
xinyu-intel:dev/torch-stream

Conversation

@xinyu-intel

@xinyu-intel xinyu-intel commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

Purpose

Part of #30679

In PyTorch, torch.Stream objects implement enter / exit and it should work well with context manager. So, replace with torch.cuda.stream(astream): with with astream:. For cases which astream can be None, use torch. accelerator.current_stream() instead.

Test Plan

Test Result


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

@mergify mergify Bot added the kv-connector label Jan 28, 2026
@xinyu-intel xinyu-intel changed the title replace torch.cuda.stream() replace with torch.cuda.stream() Jan 28, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the codebase to replace the deprecated torch.cuda.stream() with the modern context manager usage on torch.Stream objects. The changes are generally correct and improve code clarity. However, I've identified a potential performance issue in one of the files where a new torch.Stream() is created in a loop, which can be inefficient. I've provided a suggestion to address this.

@@ -1894,12 +1894,12 @@ def forward_impl(
# sync end point immediately after it is done. This is
# important to avoid excessive stream allocations by the cuda
# graph replay later.
with torch.cuda.stream(self.shared_experts_stream):
shared_experts_stream = self.shared_experts_stream or torch.Stream()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Creating a new torch.Stream() on every forward pass when self.shared_experts_stream is None is inefficient and can lead to resource exhaustion. The original code with torch.cuda.stream(None) was a no-op that ran on the current stream, which is more efficient.

To preserve the original behavior while using the new context manager syntax, you can use torch.cuda.current_stream() as a fallback. This avoids creating a new stream in a loop.

Suggested change
shared_experts_stream = self.shared_experts_stream or torch.Stream()
shared_experts_stream = self.shared_experts_stream or torch.cuda.current_stream()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@mergify

mergify Bot commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

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

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

@mergify

mergify Bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Documentation preview: https://vllm--33225.org.readthedocs.build/en/33225/

@mergify mergify Bot added documentation Improvements or additions to documentation performance Performance-related issues and removed needs-rebase labels Feb 25, 2026
@mergify

mergify Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

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

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

@mergify

mergify Bot commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Hi @xinyu-intel, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

@xinyu-intel
xinyu-intel force-pushed the dev/torch-stream branch 5 times, most recently from eac6944 to 3353e7b Compare March 8, 2026 06:23
@hmellor

hmellor commented Mar 9, 2026

Copy link
Copy Markdown
Member

Please fix DCO

@hmellor hmellor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should also update the pattern in tools/pre_commit/check_torch_cuda.py to include stream

@mergify mergify Bot added intel-gpu Related to Intel GPU speculative-decoding labels Apr 8, 2026
@jikunshang jikunshang removed the ready-run-all-tests Trigger CI with all tests for wide-ranging PRs label Apr 8, 2026
@mergify

mergify Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Hi @xinyu-intel, the pre-commit checks have failed. Please run:

uv pip install pre-commit>=4.5.1
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy failing?
mypy is run differently in CI. If the failure is related to this check, please use the following command to run it locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10

@mergify

mergify Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

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

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

Signed-off-by: Xinyu Chen <xinyu1.chen@intel.com>
Signed-off-by: Xinyu Chen <xinyu1.chen@intel.com>
Signed-off-by: Xinyu Chen <xinyu1.chen@intel.com>
Signed-off-by: Xinyu Chen <xinyu1.chen@intel.com>
Signed-off-by: Xinyu Chen <xinyu1.chen@intel.com>
@jikunshang jikunshang removed the ready ONLY add when PR is ready to merge/full CI is needed label Jun 29, 2026
@mergify

mergify Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

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

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

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

Labels

documentation Improvements or additions to documentation intel-gpu Related to Intel GPU kv-connector needs-rebase nvidia performance Performance-related issues speculative-decoding v1

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

4 participants