pytorch - 💡(How to fix) Fix C++ Can't capture Transformer CUDA Graph [1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177738Fetched 2026-04-08 00:58:02
View on GitHub
Comments
1
Participants
1
Timeline
47
Reactions
0
Participants
Timeline (top)
mentioned ×20subscribed ×20labeled ×6commented ×1
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

If there is C++ Transformer layer call it fails due to torch::equal in Multi Headed Attention which uses .item() in CUDA Graph Capture mode.

Versions

2.8.0 But latest too I believe

cc @jbschlosser @albanD @mruberry @walterddr @mikaylagawarecki @mcarilli @ezyang @eellison @penguinwu @BoyuanFeng

extent analysis

Fix Plan

The fix involves modifying the torch::equal call in the Multi Headed Attention layer to avoid using .item() in CUDA Graph Capture mode.

Step-by-Step Solution

  • Identify the torch::equal call in the Multi Headed Attention layer.
  • Replace .item() with a tensor comparison that works in CUDA Graph Capture mode.
  • Example code snippet:
// Before
if (torch::equal(input, other).item<bool>()) {
    // ...
}

// After
auto equality = torch::equal(input, other);
if (equality.all().item<bool>()) {
    // ...
}

Alternatively, you can use torch::allclose for floating point comparisons:

if (torch::allclose(input, other).all().item<bool>()) {
    // ...
}

Verification

Verify the fix by running the Transformer layer with CUDA Graph Capture mode enabled and checking that it no longer fails due to the torch::equal call.

Extra Tips

  • Make sure to test the modified code thoroughly to ensure it works correctly in all scenarios.
  • Consider adding a check to ensure that the input tensors are on the same device before comparing them.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING