Skip to content
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

In exception handlers, clear the torch CUDA cache (if we're using CUD… #2549

Merged
merged 1 commit into from
Feb 6, 2023
Merged
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
12 changes: 12 additions & 0 deletions invokeai/backend/invoke_ai_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,19 +1208,31 @@ def diffusers_step_callback_adapter(*cb_args, **kwargs):
)

except KeyboardInterrupt:
# Clear the CUDA cache on an exception
self.empty_cuda_cache()
self.socketio.emit("processingCanceled")
raise
except CanceledException:
# Clear the CUDA cache on an exception
self.empty_cuda_cache()
self.socketio.emit("processingCanceled")
pass
except Exception as e:
# Clear the CUDA cache on an exception
self.empty_cuda_cache()
print(e)
self.socketio.emit("error", {"message": (str(e))})
print("\n")

traceback.print_exc()
print("\n")

def empty_cuda_cache(self):
if self.generate.device.type == "cuda":
import torch.cuda

torch.cuda.empty_cache()

def parameters_to_generated_image_metadata(self, parameters):
try:
# top-level metadata minus `image` or `images`
Expand Down
10 changes: 9 additions & 1 deletion ldm/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def __init__(
print('>> xformers memory-efficient attention is available but disabled')
else:
print('>> xformers not installed')

# model caching system for fast switching
self.model_manager = ModelManager(mconfig,self.device,self.precision,max_loaded_models=max_loaded_models)
# don't accept invalid models
Expand Down Expand Up @@ -565,11 +565,19 @@ def process_image(image,seed):
image_callback = image_callback)

except KeyboardInterrupt:
# Clear the CUDA cache on an exception
if self._has_cuda():
torch.cuda.empty_cache()

if catch_interrupts:
print('**Interrupted** Partial results will be returned.')
else:
raise KeyboardInterrupt
except RuntimeError:
# Clear the CUDA cache on an exception
if self._has_cuda():
torch.cuda.empty_cache()

print(traceback.format_exc(), file=sys.stderr)
print('>> Could not generate image.')

Expand Down
4 changes: 4 additions & 0 deletions ldm/invoke/generator/txt2img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def make_image(x_T):
mode="bilinear"
)

# Free up memory from the last generation.
if self.model.device.type == 'cuda':
torch.cuda.empty_cache()

second_pass_noise = self.get_noise_like(resized_latents)

verbosity = get_verbosity()
Expand Down