pytorch - 💡(How to fix) Fix `torch.compile(mode='reduce-overhead')` fails when user model uses wait_stream to synchronize side work

Official PRs (…)
ON THIS PAGE

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…

Error Message

The code below results in a cudaErrorStreamCaptureUnjoined (i.e. "torch.AcceleratorError: CUDA error: capturing stream has unjoined work") error when run.

Fix Action

Fix / Workaround

def fn_with_workaround(x): """The pattern PR #183711's test asserts inductor preserves.""" a = x * 2 side.wait_stream(torch.cuda.current_stream()) with torch.cuda.stream(side): b = a + 1 torch.cuda.current_stream().wait_stream(side) return b

x = torch.ones(1024, device="cuda") print(f"torch={torch.version}, cuda={torch.version.cuda}") print() run("no sync", fn_no_sync, "default", x) # fine run("with #183711 workaround", fn_with_workaround, "default", x) # fine run("with #183711 workaround", fn_with_workaround, "reduce-overhead", x) # fails


CPU:
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           52 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  24
On-line CPU(s) list:                     0-23
Vendor ID:                               GenuineIntel
Model name:                              Intel(R) Xeon(R) w5-2455X
CPU family:                              6
Model:                                   143
Thread(s) per core:                      2
Core(s) per socket:                      12
Socket(s):                               1
Stepping:                                8
CPU(s) scaling MHz:                      26%
CPU max MHz:                             4600.0000
CPU min MHz:                             800.0000
BogoMIPS:                                6384.00
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect user_shstk avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr ibt amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities ibpb_exit_to_user
Virtualization:                          VT-x
L1d cache:                               576 KiB (12 instances)
L1i cache:                               384 KiB (12 instances)
L2 cache:                                24 MiB (12 instances)
L3 cache:                                30 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-23
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace

Code Example

import torch

side = torch.cuda.Stream()


def fn_no_sync(x):
    a = x * 2
    with torch.cuda.stream(side):
        b = a + 1
    return b


def fn_with_workaround(x):
    """The pattern PR #183711's test asserts inductor preserves."""
    a = x * 2
    side.wait_stream(torch.cuda.current_stream())
    with torch.cuda.stream(side):
        b = a + 1
    torch.cuda.current_stream().wait_stream(side)
    return b


def fn_eager(x):
    a = x * 2
    b = a + 1
    return b


def run(name, fn, mode, x):
    torch._dynamo.reset()
    opt = torch.compile(fn, mode=mode)
    out = None
    for _ in range(3):
        out = opt(x)
        torch.cuda.synchronize()
    expected = fn_eager(x)
    ok = torch.allclose(out, expected)
    print(f"  {name:35s} mode={mode!r:18s}  ok={ok}  "
          f"got={out[:3].tolist()}  expected={expected[:3].tolist()}")


x = torch.ones(1024, device="cuda")
print(f"torch={torch.__version__}, cuda={torch.version.cuda}")
print()
run("no sync",         fn_no_sync,         "default",         x)         # fine
run("with #183711 workaround", fn_with_workaround, "default",         x) # fine
run("with #183711 workaround", fn_with_workaround, "reduce-overhead", x) # fails

---

Collecting environment information...
PyTorch version: 2.13.0a0+gitb5e90ff
Is debug build: False
CUDA used to build PyTorch: 13.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.4 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: Could not collect
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-111-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA RTX 6000 Ada Generation
Nvidia driver version: 590.48.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_tensor_ir.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_ext.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.10.99.0
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           52 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  24
On-line CPU(s) list:                     0-23
Vendor ID:                               GenuineIntel
Model name:                              Intel(R) Xeon(R) w5-2455X
CPU family:                              6
Model:                                   143
Thread(s) per core:                      2
Core(s) per socket:                      12
Socket(s):                               1
Stepping:                                8
CPU(s) scaling MHz:                      26%
CPU max MHz:                             4600.0000
CPU min MHz:                             800.0000
BogoMIPS:                                6384.00
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect user_shstk avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr ibt amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities ibpb_exit_to_user
Virtualization:                          VT-x
L1d cache:                               576 KiB (12 instances)
L1i cache:                               384 KiB (12 instances)
L2 cache:                                24 MiB (12 instances)
L3 cache:                                30 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-23
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace

Versions of relevant libraries:
[pip3] mypy==2.0.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.4.4
[pip3] nvtx==0.2.15
[pip3] optree==0.19.1
[pip3] torch==2.13.0a0+gitb5e90ff
[pip3] triton==3.6.0
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Under mode='reduce-overhead', work issued inside a user-written side stream does not synchronize back to the main stream even if the user does so explicitly. This causes stream capture errors.

The code below results in a cudaErrorStreamCaptureUnjoined (i.e. "torch.AcceleratorError: CUDA error: capturing stream has unjoined work") error when run.

import torch

side = torch.cuda.Stream()


def fn_no_sync(x):
    a = x * 2
    with torch.cuda.stream(side):
        b = a + 1
    return b


def fn_with_workaround(x):
    """The pattern PR #183711's test asserts inductor preserves."""
    a = x * 2
    side.wait_stream(torch.cuda.current_stream())
    with torch.cuda.stream(side):
        b = a + 1
    torch.cuda.current_stream().wait_stream(side)
    return b


def fn_eager(x):
    a = x * 2
    b = a + 1
    return b


def run(name, fn, mode, x):
    torch._dynamo.reset()
    opt = torch.compile(fn, mode=mode)
    out = None
    for _ in range(3):
        out = opt(x)
        torch.cuda.synchronize()
    expected = fn_eager(x)
    ok = torch.allclose(out, expected)
    print(f"  {name:35s} mode={mode!r:18s}  ok={ok}  "
          f"got={out[:3].tolist()}  expected={expected[:3].tolist()}")


x = torch.ones(1024, device="cuda")
print(f"torch={torch.__version__}, cuda={torch.version.cuda}")
print()
run("no sync",         fn_no_sync,         "default",         x)         # fine
run("with #183711 workaround", fn_with_workaround, "default",         x) # fine
run("with #183711 workaround", fn_with_workaround, "reduce-overhead", x) # fails

Versions

Collecting environment information...
PyTorch version: 2.13.0a0+gitb5e90ff
Is debug build: False
CUDA used to build PyTorch: 13.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.4 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: Could not collect
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-111-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA RTX 6000 Ada Generation
Nvidia driver version: 590.48.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_tensor_ir.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_ext.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.10.99.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.10.99.0
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           52 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  24
On-line CPU(s) list:                     0-23
Vendor ID:                               GenuineIntel
Model name:                              Intel(R) Xeon(R) w5-2455X
CPU family:                              6
Model:                                   143
Thread(s) per core:                      2
Core(s) per socket:                      12
Socket(s):                               1
Stepping:                                8
CPU(s) scaling MHz:                      26%
CPU max MHz:                             4600.0000
CPU min MHz:                             800.0000
BogoMIPS:                                6384.00
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect user_shstk avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr ibt amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities ibpb_exit_to_user
Virtualization:                          VT-x
L1d cache:                               576 KiB (12 instances)
L1i cache:                               384 KiB (12 instances)
L2 cache:                                24 MiB (12 instances)
L3 cache:                                30 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-23
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace

Versions of relevant libraries:
[pip3] mypy==2.0.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.4.4
[pip3] nvtx==0.2.15
[pip3] optree==0.19.1
[pip3] torch==2.13.0a0+gitb5e90ff
[pip3] triton==3.6.0
[conda] Could not collect

cc @mcarilli @ezyang @eellison @penguinwu @BoyuanFeng @chauhang @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

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