-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix: SD3 ControlNet validation so that it runs on a A100. #11238
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import contextlib | ||
| import copy | ||
| import functools | ||
| import gc | ||
| import logging | ||
| import math | ||
| import os | ||
|
|
@@ -52,6 +53,7 @@ | |
| from diffusers.training_utils import compute_density_for_timestep_sampling, compute_loss_weighting_for_sd3, free_memory | ||
| from diffusers.utils import check_min_version, is_wandb_available, make_image_grid | ||
| from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card | ||
| from diffusers.utils.testing_utils import backend_empty_cache | ||
| from diffusers.utils.torch_utils import is_compiled_module | ||
|
|
||
|
|
||
|
|
@@ -74,8 +76,9 @@ def log_validation(controlnet, args, accelerator, weight_dtype, step, is_final_v | |
|
|
||
| pipeline = StableDiffusion3ControlNetPipeline.from_pretrained( | ||
| args.pretrained_model_name_or_path, | ||
| controlnet=controlnet, | ||
| controlnet=None, | ||
| safety_checker=None, | ||
| transformer=None, | ||
| revision=args.revision, | ||
| variant=args.variant, | ||
| torch_dtype=weight_dtype, | ||
|
|
@@ -102,18 +105,55 @@ def log_validation(controlnet, args, accelerator, weight_dtype, step, is_final_v | |
| "number of `args.validation_image` and `args.validation_prompt` should be checked in `parse_args`" | ||
| ) | ||
|
|
||
| with torch.no_grad(): | ||
| ( | ||
| prompt_embeds, | ||
| negative_prompt_embeds, | ||
| pooled_prompt_embeds, | ||
| negative_pooled_prompt_embeds, | ||
| ) = pipeline.encode_prompt( | ||
| validation_prompts, | ||
| prompt_2=None, | ||
| prompt_3=None, | ||
| ) | ||
|
|
||
| del pipeline | ||
| gc.collect() | ||
| backend_empty_cache(accelerator.device.type) | ||
|
|
||
| pipeline = StableDiffusion3ControlNetPipeline.from_pretrained( | ||
| args.pretrained_model_name_or_path, | ||
| controlnet=controlnet, | ||
| safety_checker=None, | ||
| text_encoder=None, | ||
| text_encoder_2=None, | ||
| text_encoder_3=None, | ||
| revision=args.revision, | ||
| variant=args.variant, | ||
| torch_dtype=weight_dtype, | ||
| ) | ||
| pipeline.enable_model_cpu_offload(device=accelerator.device.type) | ||
| pipeline.set_progress_bar_config(disable=True) | ||
|
|
||
| image_logs = [] | ||
| inference_ctx = contextlib.nullcontext() if is_final_validation else torch.autocast(accelerator.device.type) | ||
|
|
||
| for validation_prompt, validation_image in zip(validation_prompts, validation_images): | ||
| for i, validation_image in enumerate(validation_images): | ||
| validation_image = Image.open(validation_image).convert("RGB") | ||
| validation_prompt = validation_prompts[i] | ||
|
|
||
| images = [] | ||
|
|
||
| for _ in range(args.num_validation_images): | ||
| with inference_ctx: | ||
| image = pipeline( | ||
| validation_prompt, control_image=validation_image, num_inference_steps=20, generator=generator | ||
| prompt_embeds=prompt_embeds[i].unsqueeze(0), | ||
| negative_prompt_embeds=negative_prompt_embeds[i].unsqueeze(0), | ||
| pooled_prompt_embeds=pooled_prompt_embeds[i].unsqueeze(0), | ||
| negative_pooled_prompt_embeds=negative_pooled_prompt_embeds[i].unsqueeze(0), | ||
| control_image=validation_image, | ||
| num_inference_steps=20, | ||
| generator=generator, | ||
| ).images[0] | ||
|
|
||
| images.append(image) | ||
|
|
@@ -655,6 +695,7 @@ def make_train_dataset(args, tokenizer_one, tokenizer_two, tokenizer_three, acce | |
| dataset = load_dataset( | ||
| args.train_data_dir, | ||
| cache_dir=args.cache_dir, | ||
| trust_remote_code=True, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, could be an argument but more convenient like this especially as the example dataset requires it. Can be replicated across training scripts. |
||
| ) | ||
| # See more about loading custom images at | ||
| # https://huggingface.co/docs/datasets/v2.0.0/en/dataset_script | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker for this PR but looks like
prompt_2andprompt_3should be madeOptionalin the pipeline.