Skip to content

Model offload #3889

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

Merged
merged 4 commits into from
Jun 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,18 @@ def enable_model_cpu_offload(self, gpu_id=0):
self.to("cpu", silence_dtype_warnings=True)
torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist)

model_sequence = (
[self.text_encoder, self.text_encoder_2] if self.text_encoder is not None else [self.text_encoder_2]
)
model_sequence.extend([self.unet, self.vae])

hook = None
for cpu_offloaded_model in [self.text_encoder, self.unet, self.vae]:
for cpu_offloaded_model in model_sequence:
_, hook = cpu_offload_with_hook(cpu_offloaded_model, device, prev_module_hook=hook)

if self.safety_checker is not None:
_, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)
# TODO: safety_checker
# if self.safety_checker is not None:
# _, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)

# We'll offload the last model manually.
self.final_offload_hook = hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,18 @@ def enable_model_cpu_offload(self, gpu_id=0):
self.to("cpu", silence_dtype_warnings=True)
torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist)

model_sequence = (
[self.text_encoder, self.text_encoder_2] if self.text_encoder is not None else [self.text_encoder_2]
)
model_sequence.extend([self.unet, self.vae])

hook = None
for cpu_offloaded_model in [self.text_encoder, self.unet, self.vae]:
for cpu_offloaded_model in model_sequence:
_, hook = cpu_offload_with_hook(cpu_offloaded_model, device, prev_module_hook=hook)

if self.safety_checker is not None:
_, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)
# TODO: safety_checker
# if self.safety_checker is not None:
# _, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)

# We'll offload the last model manually.
self.final_offload_hook = hook
Expand Down Expand Up @@ -518,6 +524,11 @@ def prepare_latents(self, image, timestep, batch_size, num_images_per_prompt, dt
f"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(image)}"
)

# Offload text encoder if `enable_model_cpu_offload` was enabled
if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
self.text_encoder_2.to("cpu")
torch.cuda.empty_cache()

image = image.to(device=device, dtype=dtype)

batch_size = batch_size * num_images_per_prompt
Expand Down