Skip to content

VAE handle HIP OOM exceptions #7764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions comfy/model_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def mac_version():
except:
OOM_EXCEPTION = Exception


def is_oom_exception(ex):
if isinstance(ex, OOM_EXCEPTION):
return True
# handle also other kinds of oom, e.g. "HIP error: out of memory"
msg = str(ex)
return "out of memory" in msg

XFORMERS_VERSION = ""
XFORMERS_ENABLED_VAE = True
if args.disable_xformers:
Expand Down
9 changes: 6 additions & 3 deletions comfy/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ def decode(self, samples_in, vae_options={}):
if pixel_samples is None:
pixel_samples = torch.empty((samples_in.shape[0],) + tuple(out.shape[1:]), device=self.output_device)
pixel_samples[x:x+batch_number] = out
except model_management.OOM_EXCEPTION:
except Exception as ex:
if not model_management.is_oom_exception(ex):
raise
logging.warning("Warning: Ran out of memory when regular VAE decoding, retrying with tiled VAE decoding.")
dims = samples_in.ndim - 2
if dims == 1:
Expand Down Expand Up @@ -592,8 +594,9 @@ def encode(self, pixel_samples):
if samples is None:
samples = torch.empty((pixel_samples.shape[0],) + tuple(out.shape[1:]), device=self.output_device)
samples[x:x + batch_number] = out

except model_management.OOM_EXCEPTION:
except Exception as ex:
if not model_management.is_oom_exception(ex):
raise
logging.warning("Warning: Ran out of memory when regular VAE encoding, retrying with tiled VAE encoding.")
if self.latent_dim == 3:
tile = 256
Expand Down