Repository navigation
Fix bmm outer product Triton launch on non-current CUDA device - #187983
Conversation
The Python native bmm outer-product override launches a Triton kernel for CUDA inputs shaped like `(B, M, 1) x (B, 1, N)`. Unlike the generated C++ ATen CUDA path, this Python dispatch path does not get an automatic CUDA device guard before launching the kernel. In vLLM multimodal tests, the model hooks can run the relevant rotary embedding buffers and inputs on `cuda:1` while the process current device is still `cuda:0`; Triton then launches on the wrong device/stream and rejects the `cuda:1` pointers with `ValueError: Pointer argument cannot be accessed from Triton (cpu tensor?)`. Guard the Triton call when the input device is not already current, while leaving the common already-current path direct to avoid unnecessary Python context manager overhead. Also require both bmm inputs to be on the same CUDA device before selecting the override, so mismatched CUDA inputs fall back to native bmm and report the normal same-device error instead of reaching the Triton kernel. I considered guarding unconditionally, but a small same-device benchmark showed that the Python context manager adds measurable overhead for small outer-product bmm calls. The conditional guard keeps the fix scoped to the non-current-device case that needs it. Fixes #187729 Generated by my agent Benchmark Results: - Command: `CUDA_VISIBLE_DEVICES=0 python - <<'PY' ...` benchmarking 7 trials of 1000 `torch.bmm` calls on `(32, 128, 1) x (32, 1, 512)` CUDA tensors with current device matching the inputs. - Before: median 24.210 us/call; raw `[24.34, 24.365, 24.195, 24.206, 24.21, 24.272, 24.142]`. - After: median 24.946 us/call; raw `[25.045, 24.909, 25.008, 25.042, 24.906, 24.946, 24.926]`. Test Plan: - `CUDA_VISIBLE_DEVICES=0,1 python - <<'PY' ...` minimal repro: before the fix failed with the Triton pointer `ValueError`; after the fix produced a `cuda:1` result and preserved current device `0`. - `CUDA_VISIBLE_DEVICES=0,1 python -m pytest test/test_bmm_outer_product.py -q -rs` passed: `11 passed, 10 subtests passed`. - `lintrunner torch/_native/ops/bmm_outer_product/triton_impl.py test/test_bmm_outer_product.py` passed. - `git diff --cached --check` passed. - `lintrunner -a` was run and failed on unrelated pre-existing clang-tidy findings in untouched C++ files. Pull Request resolved: #187733 Approved by: https://github.com/slayton58 (cherry picked from commit 5a5e50f)
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/187983
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ⏳ No Failures, 52 PendingAs of commit 7fe3b33 with merge base 636bc91 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
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. Pull Request resolved: #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
…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
Stack from ghstack (oldest at bottom):
The Python native bmm outer-product override launches a Triton kernel for
CUDA inputs shaped like
(B, M, 1) x (B, 1, N). Unlike the generated C++ATen CUDA path, this Python dispatch path does not get an automatic CUDA
device guard before launching the kernel. In vLLM multimodal tests, the
model hooks can run the relevant rotary embedding buffers and inputs on
cuda:1while the process current device is stillcuda:0; Triton thenlaunches on the wrong device/stream and rejects the
cuda:1pointers withValueError: Pointer argument cannot be accessed from Triton (cpu tensor?).Guard the Triton call when the input device is not already current, while
leaving the common already-current path direct to avoid unnecessary Python
context manager overhead. Also require both bmm inputs to be on the same CUDA
device before selecting the override, so mismatched CUDA inputs fall back to
native bmm and report the normal same-device error instead of reaching the
Triton kernel.
I considered guarding unconditionally, but a small same-device benchmark showed
that the Python context manager adds measurable overhead for small outer-product
bmm calls. The conditional guard keeps the fix scoped to the non-current-device
case that needs it.
Fixes #187729
Generated by my agent
Benchmark Results:
CUDA_VISIBLE_DEVICES=0 python - <<'PY' ...benchmarking 7 trials of 1000torch.bmmcalls on(32, 128, 1) x (32, 1, 512)CUDA tensors with current device matching the inputs.[24.34, 24.365, 24.195, 24.206, 24.21, 24.272, 24.142].[25.045, 24.909, 25.008, 25.042, 24.906, 24.946, 24.926].Test Plan:
CUDA_VISIBLE_DEVICES=0,1 python - <<'PY' ...minimal repro: before the fix failed with the Triton pointerValueError; after the fix produced acuda:1result and preserved current device0.CUDA_VISIBLE_DEVICES=0,1 python -m pytest test/test_bmm_outer_product.py -q -rspassed:11 passed, 10 subtests passed.lintrunner torch/_native/ops/bmm_outer_product/triton_impl.py test/test_bmm_outer_product.pypassed.git diff --cached --checkpassed.lintrunner -awas run and failed on unrelated pre-existing clang-tidy findings in untouched C++ files.