Skip to content

Guard CuTeDSL topk override against non-current CUDA device - #188022

Closed
he-yufeng wants to merge 1 commit into
pytorch:mainfrom
he-yufeng:fix/native-topk-device-guard
Closed

he-yufeng wants to merge 1 commit into
pytorch:mainfrom
he-yufeng:fix/native-topk-device-guard

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

The Python-native topk override launches a CuTeDSL kernel for eligible CUDA
inputs. Unlike the generated C++ ATen CUDA path, this Python dispatch path does
not get an automatic CUDA device guard before launching the kernel, so the
CuTeDSL kernel runs on the current device's stream. When the input lives on a
device other than the current one (e.g. model hooks running buffers on cuda:1
while the process current device is still cuda:0), the kernel launches on the
wrong device and rejects the off-device pointers.

This is the same root cause that #187983 just fixed for the bmm outer-product
override. The output of _run is allocated on the input's device (inside the
kernels), but the launch goes to the current device's stream, so a non-current
input mismatches.

The fix guards the kernel launch with the input's device when it isn't already
current, and leaves the common already-current path direct to avoid the
context-manager overhead (mirroring the conditional guard in #187983). Both the
functional and .out variants route through _run, so a single guard covers
them.

The same missing-guard pattern also affects the other Python-native overrides
that launch on the current/env stream — scatter_add and _fused_rms_norm
(CuTeDSL), and the nvmath path of _foreach_mm (which uses
torch.cuda.current_stream() directly). I kept this PR scoped to topk because
it has a dedicated override test I can extend with a clean repro; happy to send
the same guard for the others as a follow-up, or fold them in here if you'd
prefer one sweep.

Test Plan

Adds test_non_current_device to TestCuTeDSLTopK. It reuses the existing
_test_n(k) shape helper (so the override actually fires rather than falling
through to aten), allocates the input on cuda:1 with the current device set to
cuda:0, and checks the result lands on cuda:1 and matches the aten reference.
It is gated on torch.cuda.device_count() >= 2.

Note: I don't have a 2-GPU machine, so I could not execute this test locally;
the change is verified by code inspection against the #187983 precedent and is
arch/device-gated for CI. python -m py_compile and ruff check / ruff format --check pass on both files.

The Python-native topk override launches a CuTeDSL kernel for eligible
CUDA inputs. Unlike the generated C++ ATen CUDA path, this Python dispatch
path does not get an automatic device guard, so the kernel launches on the
current device's stream. When the input lives on a device other than the
current one (e.g. model hooks running buffers on cuda:1 while the process
current device is cuda:0), the kernel launches on the wrong device and
rejects the off-device pointers.

Guard the kernel launch when the input device is not already current, and
leave the common already-current path direct to avoid the context-manager
overhead. This mirrors the bmm outer-product fix in pytorch#187983; the same
missing-guard pattern also affects the other Python-native overrides
(scatter_add, _fused_rms_norm, and the _foreach_mm nvmath path).

Adds a 2-GPU regression test that runs the override on cuda:1 while the
current device is cuda:0 and checks the result lands on the input's device.
@pytorch-bot

pytorch-bot Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/188022

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 949da1b with merge base d92fee3 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@liangel-02 liangel-02 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 24, 2026

@slayton58 slayton58 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.

Looks good to me - stamping.

@slayton58

Copy link
Copy Markdown
Contributor

@he-yufeng Thanks for the fix! It'd be great to have follow-up(s) for the other ops!

@slayton58

Copy link
Copy Markdown
Contributor

@pytorchbot cherry-pick --onto release/2.13 -c critical

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the cherry-pick! I'll take a pass over the other CuTeDSL overrides for the same non-current-device guard and send follow-ups for any that need it.

@slayton58

Copy link
Copy Markdown
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 26, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@slayton58

Copy link
Copy Markdown
Contributor

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…188022)

The Python-native `topk` override launches a CuTeDSL kernel for eligible CUDA
inputs. Unlike the generated C++ ATen CUDA path, this Python dispatch path does
not get an automatic CUDA device guard before launching the kernel, so the
CuTeDSL kernel runs on the *current* device's stream. When the input lives on a
device other than the current one (e.g. model hooks running buffers on `cuda:1`
while the process current device is still `cuda:0`), the kernel launches on the
wrong device and rejects the off-device pointers.

This is the same root cause that pytorch#187983 just fixed for the `bmm` outer-product
override. The output of `_run` is allocated on the input's device (inside the
kernels), but the launch goes to the current device's stream, so a non-current
input mismatches.

The fix guards the kernel launch with the input's device when it isn't already
current, and leaves the common already-current path direct to avoid the
context-manager overhead (mirroring the conditional guard in pytorch#187983). Both the
functional and `.out` variants route through `_run`, so a single guard covers
them.

The same missing-guard pattern also affects the other Python-native overrides
that launch on the current/env stream — `scatter_add` and `_fused_rms_norm`
(CuTeDSL), and the nvmath path of `_foreach_mm` (which uses
`torch.cuda.current_stream()` directly). I kept this PR scoped to `topk` because
it has a dedicated override test I can extend with a clean repro; happy to send
the same guard for the others as a follow-up, or fold them in here if you'd
prefer one sweep.

## Test Plan

Adds `test_non_current_device` to `TestCuTeDSLTopK`. It reuses the existing
`_test_n(k)` shape helper (so the override actually fires rather than falling
through to aten), allocates the input on `cuda:1` with the current device set to
`cuda:0`, and checks the result lands on `cuda:1` and matches the aten reference.
It is gated on `torch.cuda.device_count() >= 2`.

Note: I don't have a 2-GPU machine, so I could not execute this test locally;
the change is verified by code inspection against the pytorch#187983 precedent and is
arch/device-gated for CI. `python -m py_compile` and `ruff check` / `ruff
format --check` pass on both files.

Pull Request resolved: pytorch#188022
Approved by: https://github.com/slayton58
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
…188022)

The Python-native `topk` override launches a CuTeDSL kernel for eligible CUDA
inputs. Unlike the generated C++ ATen CUDA path, this Python dispatch path does
not get an automatic CUDA device guard before launching the kernel, so the
CuTeDSL kernel runs on the *current* device's stream. When the input lives on a
device other than the current one (e.g. model hooks running buffers on `cuda:1`
while the process current device is still `cuda:0`), the kernel launches on the
wrong device and rejects the off-device pointers.

This is the same root cause that pytorch#187983 just fixed for the `bmm` outer-product
override. The output of `_run` is allocated on the input's device (inside the
kernels), but the launch goes to the current device's stream, so a non-current
input mismatches.

The fix guards the kernel launch with the input's device when it isn't already
current, and leaves the common already-current path direct to avoid the
context-manager overhead (mirroring the conditional guard in pytorch#187983). Both the
functional and `.out` variants route through `_run`, so a single guard covers
them.

The same missing-guard pattern also affects the other Python-native overrides
that launch on the current/env stream — `scatter_add` and `_fused_rms_norm`
(CuTeDSL), and the nvmath path of `_foreach_mm` (which uses
`torch.cuda.current_stream()` directly). I kept this PR scoped to `topk` because
it has a dedicated override test I can extend with a clean repro; happy to send
the same guard for the others as a follow-up, or fold them in here if you'd
prefer one sweep.

## Test Plan

Adds `test_non_current_device` to `TestCuTeDSLTopK`. It reuses the existing
`_test_n(k)` shape helper (so the override actually fires rather than falling
through to aten), allocates the input on `cuda:1` with the current device set to
`cuda:0`, and checks the result lands on `cuda:1` and matches the aten reference.
It is gated on `torch.cuda.device_count() >= 2`.

Note: I don't have a 2-GPU machine, so I could not execute this test locally;
the change is verified by code inspection against the pytorch#187983 precedent and is
arch/device-gated for CI. `python -m py_compile` and `ruff check` / `ruff
format --check` pass on both files.

Pull Request resolved: pytorch#188022
Approved by: https://github.com/slayton58
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…188022)

The Python-native `topk` override launches a CuTeDSL kernel for eligible CUDA
inputs. Unlike the generated C++ ATen CUDA path, this Python dispatch path does
not get an automatic CUDA device guard before launching the kernel, so the
CuTeDSL kernel runs on the *current* device's stream. When the input lives on a
device other than the current one (e.g. model hooks running buffers on `cuda:1`
while the process current device is still `cuda:0`), the kernel launches on the
wrong device and rejects the off-device pointers.

This is the same root cause that pytorch#187983 just fixed for the `bmm` outer-product
override. The output of `_run` is allocated on the input's device (inside the
kernels), but the launch goes to the current device's stream, so a non-current
input mismatches.

The fix guards the kernel launch with the input's device when it isn't already
current, and leaves the common already-current path direct to avoid the
context-manager overhead (mirroring the conditional guard in pytorch#187983). Both the
functional and `.out` variants route through `_run`, so a single guard covers
them.

The same missing-guard pattern also affects the other Python-native overrides
that launch on the current/env stream — `scatter_add` and `_fused_rms_norm`
(CuTeDSL), and the nvmath path of `_foreach_mm` (which uses
`torch.cuda.current_stream()` directly). I kept this PR scoped to `topk` because
it has a dedicated override test I can extend with a clean repro; happy to send
the same guard for the others as a follow-up, or fold them in here if you'd
prefer one sweep.

## Test Plan

Adds `test_non_current_device` to `TestCuTeDSLTopK`. It reuses the existing
`_test_n(k)` shape helper (so the override actually fires rather than falling
through to aten), allocates the input on `cuda:1` with the current device set to
`cuda:0`, and checks the result lands on `cuda:1` and matches the aten reference.
It is gated on `torch.cuda.device_count() >= 2`.

Note: I don't have a 2-GPU machine, so I could not execute this test locally;
the change is verified by code inspection against the pytorch#187983 precedent and is
arch/device-gated for CI. `python -m py_compile` and `ruff check` / `ruff
format --check` pass on both files.

Pull Request resolved: pytorch#188022
Approved by: https://github.com/slayton58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants