Repository navigation
Conversation
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.
🔗 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 ( 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. |
slayton58
left a comment
There was a problem hiding this comment.
Looks good to me - stamping.
|
@he-yufeng Thanks for the fix! It'd be great to have follow-up(s) for the other ops! |
|
@pytorchbot cherry-pick --onto release/2.13 -c critical |
|
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. |
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour 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 |
…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
…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
…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
The Python-native
topkoverride launches a CuTeDSL kernel for eligible CUDAinputs. 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:1while the process current device is still
cuda:0), the kernel launches on thewrong device and rejects the off-device pointers.
This is the same root cause that #187983 just fixed for the
bmmouter-productoverride. The output of
_runis allocated on the input's device (inside thekernels), 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
.outvariants route through_run, so a single guard coversthem.
The same missing-guard pattern also affects the other Python-native overrides
that launch on the current/env stream —
scatter_addand_fused_rms_norm(CuTeDSL), and the nvmath path of
_foreach_mm(which usestorch.cuda.current_stream()directly). I kept this PR scoped totopkbecauseit 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_devicetoTestCuTeDSLTopK. It reuses the existing_test_n(k)shape helper (so the override actually fires rather than fallingthrough to aten), allocates the input on
cuda:1with the current device set tocuda:0, and checks the result lands oncuda:1and 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_compileandruff check/ruff format --checkpass on both files.