Skip to content

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

Closed
jansel wants to merge 1 commit into
gh/jansel/1505/basefrom
gh/jansel/1505/head
Closed

jansel wants to merge 1 commit into
gh/jansel/1505/basefrom
gh/jansel/1505/head

Conversation

@jansel

@jansel jansel commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

As of commit 942a21d with merge base 9d625a9 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@jansel

jansel commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

@claude review these changes

@claude

claude Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Claude finished @jansel's task in 50s —— View job


PR Review: Fix bmm outer product Triton launch on non-current CUDA device

  • Read PR diff against base
  • Analyze triton_impl.py changes
  • Analyze test changes
  • Post review

I reviewed both changed files against origin/gh/jansel/1505/base. The fix is correct, well-scoped, and matches the stated root cause. Details below.


Correctness ✅

The diagnosis is sound: the Python dispatch override launches Triton directly, and Triton uses the current device/stream rather than the input tensor's device. The two changes together address it:

  1. Device guard in _bmm_outer_product_impl (triton_impl.py:26-31) — only wraps the launch in torch.cuda.device(device) when the input device differs from the current device, so kernel launch and output allocation happen on the right device. a.get_device() returns the ordinal and torch.cuda.device(...) accepts an int ordinal, so the comparison and guard are type-correct.

  2. a.device == b.device in _bmm_outer_product_cond (triton_impl.py:45) — mismatched CUDA inputs now fall back to native bmm and surface the normal same-device error rather than reaching Triton. Since the impl guards only on a's device, this cond check is what makes guarding on a alone safe.

The conditional guard (vs. unconditional) is justified by the benchmark in the description, and the same-device fast path stays direct.

Test ✅

test_non_current_device_outer_product covers both halves of the fix: it asserts the result lands on cuda:1 while the process stays on cuda:0, and that the mismatched-device case raises. The try/finally restores the current device, which is good hygiene given the test mutates global device state. The "same device|different" regex should match the native error ("Expected all tensors to be on the same device, ...").

Minor observations (non-blocking)

  • XPU path uses torch.cuda. register_to_dispatch also registers for XPU (triton_impl.py:66-67), but _bmm_outer_product_impl is hardcoded to torch.cuda.current_device() / torch.cuda.device(...). In practice the XPU registration never fires because _bmm_outer_product_cond requires a.is_cuda and b.is_cuda, which is false for XPU tensors — so this is dead for XPU today and not a regression from this PR. Still, if XPU support is ever wired up, the same non-current-device bug would exist there and this guard wouldn't help. Worth a # TODO or a device-type-generic guard if XPU is on the roadmap.

  • get_device() vs .device.index. a.get_device() is fine here since cond guarantees CUDA, but a.device.index would be slightly more explicit. Purely stylistic — no change needed.

Summary

LGTM. The fix is correct, minimal, and the new test exercises the exact failure mode from #187729. The only thing I'd flag is the latent CUDA-only assumption on the (currently dead) XPU path.
· branch gh/jansel/1505/head

@jansel jansel added release notes: cuda release notes category agentic Created by an AI agent labels Jun 19, 2026
@jansel
jansel marked this pull request as ready for review June 21, 2026 15:55
@jansel
jansel requested review from atalman and drisspg June 21, 2026 15:55
@jansel
jansel requested a review from slayton58 as a code owner June 21, 2026 15:55
@jansel

jansel commented Jun 21, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 21, 2026
@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

@atalman

atalman commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

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

@pytorchbot

Copy link
Copy Markdown
Collaborator

Cherry picking #187733

The cherry pick PR is at #187983 and it is recommended to link a critical cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

atalman pushed a commit that referenced this pull request Jun 23, 2026
Fix bmm outer product Triton launch on non-current CUDA device (#187733)

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)

Co-authored-by: Jason Ansel <[email protected]>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ch#187733)

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 pytorch#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: pytorch#187733
Approved by: https://github.com/slayton58
@github-actions
github-actions Bot deleted the gh/jansel/1505/head branch July 24, 2026 02:24
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ch#187733)

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 pytorch#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: pytorch#187733
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

agentic Created by an AI agent ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: cuda release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants