Skip to content

Commit 4f4f865

Browse files
authored
fix: guard torch.AcceleratorError for compatibility with torch < 2.8.0 (Comfy-Org#12874)
* fix: guard torch.AcceleratorError for compatibility with torch < 2.8.0 torch.AcceleratorError was introduced in PyTorch 2.8.0. Accessing it directly raises AttributeError on older versions. Use a try/except fallback at module load time, consistent with the existing pattern used for OOM_EXCEPTION. * fix: address review feedback for AcceleratorError compat - Fall back to RuntimeError instead of type(None) for ACCELERATOR_ERROR, consistent with OOM_EXCEPTION fallback pattern and valid for except clauses - Add "out of memory" message introspection for RuntimeError fallback case - Use RuntimeError directly in discard_cuda_async_error except clause ---------
1 parent 3365008 commit 4f4f865

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

comfy/model_management.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,15 @@ def mac_version():
270270
except:
271271
OOM_EXCEPTION = Exception
272272

273+
try:
274+
ACCELERATOR_ERROR = torch.AcceleratorError
275+
except AttributeError:
276+
ACCELERATOR_ERROR = RuntimeError
277+
273278
def is_oom(e):
274279
if isinstance(e, OOM_EXCEPTION):
275280
return True
276-
if isinstance(e, torch.AcceleratorError) and getattr(e, 'error_code', None) == 2:
281+
if isinstance(e, ACCELERATOR_ERROR) and (getattr(e, 'error_code', None) == 2 or "out of memory" in str(e).lower()):
277282
discard_cuda_async_error()
278283
return True
279284
return False
@@ -1275,7 +1280,7 @@ def discard_cuda_async_error():
12751280
b = torch.tensor([1], dtype=torch.uint8, device=get_torch_device())
12761281
_ = a + b
12771282
synchronize()
1278-
except torch.AcceleratorError:
1283+
except RuntimeError:
12791284
#Dump it! We already know about it from the synchronous return
12801285
pass
12811286

0 commit comments

Comments
 (0)