pytorch - 💡(How to fix) Fix XPU: Fatal Level Zero `RuntimeErrors` instead of recoverable `OutOfMemoryError` [9 comments, 4 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#177714Fetched 2026-04-08 00:52:30
View on GitHub
Comments
9
Participants
4
Timeline
40
Reactions
0
Author
Assignees
Timeline (top)
mentioned ×11subscribed ×11commented ×9labeled ×6

Error Message

Attempting to force OOM... Allocated tensor 1: 0.00 GB total Allocated tensor 2: 1.00 GB total Allocated tensor 3: 2.00 GB total Allocated tensor 4: 3.00 GB total Allocated tensor 5: 4.00 GB total Allocated tensor 6: 5.00 GB total Allocated tensor 7: 6.00 GB total Allocated tensor 8: 7.00 GB total Allocated tensor 9: 8.00 GB total Allocated tensor 10: 9.00 GB total Allocated tensor 11: 10.00 GB total Allocated tensor 12: 11.00 GB total Allocated tensor 13: 12.00 GB total Traceback (most recent call last): File "/home/weilinxu/coder/xpu/xpu_reset/device_loss_demo.py", line 13, in <module> tensor = torch.randn(size, dtype=torch.float32, device=device) RuntimeError: level_zero backend failed with error: 20 (UR_RESULT_ERROR_DEVICE_LOST)

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 20 On-line CPU(s) list: 0-19 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) i9-9820X CPU @ 3.30GHz CPU family: 6 Model: 85 Thread(s) per core: 2 Core(s) per socket: 10 Socket(s): 1 Stepping: 4 CPU(s) scaling MHz: 30% CPU max MHz: 4200.0000 CPU min MHz: 1200.0000 BogoMIPS: 6599.98 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 pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cdp_l3 pti ssbd mba ibrs ibpb stibp tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi md_clear flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 320 KiB (10 instances) L1i cache: 320 KiB (10 instances) L2 cache: 10 MiB (10 instances) L3 cache: 16.5 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-19 Vulnerability Gather data sampling: Vulnerable Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; IBRS 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; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Code Example

import torch
device = torch.device("xpu")

print(f"Attempting to force OOM...")
tensors = []
size = (16384, 16384)

# Keep allocating tensors until we run out of memory
for i in range(100):
    memory_allocated = torch.xpu.memory_allocated(device) / (1024**3)
    print(f"  Allocated tensor {i+1}: {memory_allocated:.2f} GB total")
    try:
        tensor = torch.randn(size, dtype=torch.float32, device=device)
    except torch.OutOfMemoryError as e:
        print(f"✓ OOM triggered at iteration {i+1}: {e}")
        break
    else:
        tensors.append(tensor)

---

Attempting to force OOM...
  Allocated tensor 1: 0.00 GB total
  Allocated tensor 2: 1.00 GB total
  Allocated tensor 3: 2.00 GB total
  Allocated tensor 4: 3.00 GB total
  Allocated tensor 5: 4.00 GB total
  Allocated tensor 6: 5.00 GB total
  Allocated tensor 7: 6.00 GB total
  Allocated tensor 8: 7.00 GB total
  Allocated tensor 9: 8.00 GB total
  Allocated tensor 10: 9.00 GB total
  Allocated tensor 11: 10.00 GB total
  Allocated tensor 12: 11.00 GB total
  Allocated tensor 13: 12.00 GB total
Traceback (most recent call last):
  File "/home/weilinxu/coder/xpu/xpu_reset/device_loss_demo.py", line 13, in <module>
    tensor = torch.randn(size, dtype=torch.float32, device=device)
RuntimeError: level_zero backend failed with error: 20 (UR_RESULT_ERROR_DEVICE_LOST)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Unlike the CUDA backend, PyTorch on XPU fails to raise recoverable exceptions such as torch.OutOfMemoryError when physical memory is exceeded. Instead, it triggers fatal RuntimeErrors linked to Level Zero backend failures--including UR_RESULT_ERROR_DEVICE_LOST, UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY and UR_RESULT_ERROR_OUT_OF_RESOURCES--which cause the entire process to terminate.

Proposed Fix

While implementing pre-flight checks in oneAPI Level Zero may be resource-intensive, we can mitigate this within PyTorch. I’ve observed that calling torch.xpu.set_per_process_memory_fraction(1.0) at the start of a script prevents these fatal crashes. Should we consider integrating this call into the default XPU runtime initialization to ensure more stable memory management?

import torch
device = torch.device("xpu")

print(f"Attempting to force OOM...")
tensors = []
size = (16384, 16384)

# Keep allocating tensors until we run out of memory
for i in range(100):
    memory_allocated = torch.xpu.memory_allocated(device) / (1024**3)
    print(f"  Allocated tensor {i+1}: {memory_allocated:.2f} GB total")
    try:
        tensor = torch.randn(size, dtype=torch.float32, device=device)
    except torch.OutOfMemoryError as e:
        print(f"✓ OOM triggered at iteration {i+1}: {e}")
        break
    else:
        tensors.append(tensor)
Attempting to force OOM...
  Allocated tensor 1: 0.00 GB total
  Allocated tensor 2: 1.00 GB total
  Allocated tensor 3: 2.00 GB total
  Allocated tensor 4: 3.00 GB total
  Allocated tensor 5: 4.00 GB total
  Allocated tensor 6: 5.00 GB total
  Allocated tensor 7: 6.00 GB total
  Allocated tensor 8: 7.00 GB total
  Allocated tensor 9: 8.00 GB total
  Allocated tensor 10: 9.00 GB total
  Allocated tensor 11: 10.00 GB total
  Allocated tensor 12: 11.00 GB total
  Allocated tensor 13: 12.00 GB total
Traceback (most recent call last):
  File "/home/weilinxu/coder/xpu/xpu_reset/device_loss_demo.py", line 13, in <module>
    tensor = torch.randn(size, dtype=torch.float32, device=device)
RuntimeError: level_zero backend failed with error: 20 (UR_RESULT_ERROR_DEVICE_LOST)

Versions

Collecting environment information... PyTorch version: 2.10.0+xpu Is debug build: False CUDA used to build PyTorch: Could not collect ROCM used to build PyTorch: N/A

OS: Ubuntu 25.10 (x86_64) GCC version: (Ubuntu 15.2.0-4ubuntu4) 15.2.0 Clang version: Could not collect CMake version: version 4.3.0 Libc version: glibc-2.42

Python version: 3.13.8 (main, Oct 13 2025, 15:28:49) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-6.17.0-14-generic-x86_64-with-glibc2.42 Is CUDA available: False CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: N/A GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 Ti Nvidia driver version: 580.126.09 cuDNN version: Could not collect Is XPU available: True XPU used to build PyTorch: 20250301 Intel GPU driver version:

  • intel-opencl-icd: 25.31.34666.3-1ubuntu1
  • libze1: 1.24.1-2 Intel GPU models onboard: N/A Intel GPU models detected:
  • [0] _XpuDeviceProperties(name='Intel(R) Arc(TM) B580 Graphics', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero V2', type='gpu', device_id=0xE20B, uuid=86800be2-0000-0000-6a00-000000000000, driver_version='1.6.34666', total_memory=11605MB, max_compute_units=160, gpu_eu_count=160, gpu_subslice_count=20, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1) 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: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 20 On-line CPU(s) list: 0-19 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) i9-9820X CPU @ 3.30GHz CPU family: 6 Model: 85 Thread(s) per core: 2 Core(s) per socket: 10 Socket(s): 1 Stepping: 4 CPU(s) scaling MHz: 30% CPU max MHz: 4200.0000 CPU min MHz: 1200.0000 BogoMIPS: 6599.98 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 pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cdp_l3 pti ssbd mba ibrs ibpb stibp tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi md_clear flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 320 KiB (10 instances) L1i cache: 320 KiB (10 instances) L2 cache: 10 MiB (10 instances) L3 cache: 16.5 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-19 Vulnerability Gather data sampling: Vulnerable Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; IBRS 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; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Versions of relevant libraries: [pip3] Could not collect [conda] Could not collect

cc @gujinghui @EikanWang @fengyuan14 @guangyey

extent analysis

Fix Plan

To prevent fatal crashes due to memory overflow on XPU, we can set the per-process memory fraction to 1.0 at the start of the script.

  • Add the following line at the beginning of your script:

torch.xpu.set_per_process_memory_fraction(1.0)

*   This will ensure that PyTorch raises a `torch.OutOfMemoryError` instead of a fatal `RuntimeError` when the physical memory is exceeded.

### Verification
To verify that the fix worked, you can run the following code snippet:
```python
import torch
device = torch.device("xpu")
torch.xpu.set_per_process_memory_fraction(1.0)

tensors = []
size = (16384, 16384)

for i in range(100):
    memory_allocated = torch.xpu.memory_allocated(device) / (1024**3)
    print(f"  Allocated tensor {i+1}: {memory_allocated:.2f} GB total")
    try:
        tensor = torch.randn(size, dtype=torch.float32, device=device)
    except torch.OutOfMemoryError as e:
        print(f"✓ OOM triggered at iteration {i+1}: {e}")
        break
    else:
        tensors.append(tensor)

If the fix is successful, you should see a torch.OutOfMemoryError exception raised instead of a fatal RuntimeError.

Extra Tips

  • Note that setting the per-process memory fraction to 1.0 may have performance implications, as it can lead to more frequent memory allocations and deallocations.
  • You can adjust the memory fraction value to balance between stability and performance based on your specific use case.
  • It's also recommended to implement proper memory management practices, such as releasing unused tensors and using torch.cuda.empty_cache() (or the equivalent XPU function) to free up memory when possible.

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

pytorch - 💡(How to fix) Fix XPU: Fatal Level Zero `RuntimeErrors` instead of recoverable `OutOfMemoryError` [9 comments, 4 participants]