Skip to content

Fix bmm outer product Triton launch on non-current CUDA device - #187983

Merged
atalman merged 1 commit into
release/2.13from
cherry-pick-187733-by-pytorch_bot_bot_
Jun 23, 2026
Merged

atalman merged 1 commit into
release/2.13from
cherry-pick-187733-by-pytorch_bot_bot_

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

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: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.

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)
@pytorch-bot

pytorch-bot Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

🔗 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 SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⏳ No Failures, 52 Pending

As of commit 7fe3b33 with merge base 636bc91 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot

pytorch-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

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.

@atalman
atalman merged commit af333cf into release/2.13 Jun 23, 2026
85 of 129 checks passed
pytorchmergebot pushed a commit that referenced this pull request Jun 26, 2026
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
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
@github-actions
github-actions Bot deleted the cherry-pick-187733-by-pytorch_bot_bot_ branch July 24, 2026 02:26
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants