-
Notifications
You must be signed in to change notification settings - Fork 6.6k
[T2I LoRA training] fix: unscale fp16 gradient problem #6119
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
7 commits
Select commit
Hold shift + click to select a range
b6de725
fix: unscale fp16 gradient problem
sayakpaul 32bd473
fix for dreambooth lora sdxl
sayakpaul 724ddf9
Merge branch 'main' into fix/lora-training
sayakpaul 3aed05c
Merge branch 'main' into fix/lora-training
sayakpaul 8ac462b
make the type-casting conditional.
sayakpaul 18e6bf7
Apply suggestions from code review
sayakpaul 85ff777
Merge branch 'main' into fix/lora-training
sayakpaul 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
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 |
|---|---|---|
|
|
@@ -460,7 +460,13 @@ def main(): | |
| vae.to(accelerator.device, dtype=weight_dtype) | ||
| text_encoder.to(accelerator.device, dtype=weight_dtype) | ||
|
|
||
| # Add adapter and make sure the trainable params are in float32. | ||
| unet.add_adapter(unet_lora_config) | ||
| if args.mixed_precision == "fp16": | ||
| for param in unet.parameters(): | ||
| # only upcast trainable parameters (LoRA) into fp32 | ||
| if param.requires_grad: | ||
sayakpaul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| param.data = param.to(torch.float32) | ||
|
|
||
| if args.enable_xformers_memory_efficient_attention: | ||
| if is_xformers_available(): | ||
|
|
@@ -888,39 +894,42 @@ def collate_fn(examples): | |
| ignore_patterns=["step_*", "epoch_*"], | ||
| ) | ||
|
|
||
| # Final inference | ||
| # Load previous pipeline | ||
| pipeline = DiffusionPipeline.from_pretrained( | ||
| args.pretrained_model_name_or_path, revision=args.revision, variant=args.variant, torch_dtype=weight_dtype | ||
| ) | ||
| pipeline = pipeline.to(accelerator.device) | ||
| # Final inference | ||
| # Load previous pipeline | ||
| if args.validation_prompt is not None: | ||
|
Member
Author
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. If not |
||
| pipeline = DiffusionPipeline.from_pretrained( | ||
| args.pretrained_model_name_or_path, | ||
| revision=args.revision, | ||
| variant=args.variant, | ||
| torch_dtype=weight_dtype, | ||
| ) | ||
| pipeline = pipeline.to(accelerator.device) | ||
|
|
||
| # load attention processors | ||
| pipeline.unet.load_attn_procs(args.output_dir) | ||
| # load attention processors | ||
| pipeline.load_lora_weights(args.output_dir) | ||
|
Member
Author
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. Make sure to use |
||
|
|
||
| # run inference | ||
| generator = torch.Generator(device=accelerator.device) | ||
| if args.seed is not None: | ||
| generator = generator.manual_seed(args.seed) | ||
| images = [] | ||
| for _ in range(args.num_validation_images): | ||
| images.append(pipeline(args.validation_prompt, num_inference_steps=30, generator=generator).images[0]) | ||
| # run inference | ||
| generator = torch.Generator(device=accelerator.device) | ||
| if args.seed is not None: | ||
| generator = generator.manual_seed(args.seed) | ||
| images = [] | ||
| for _ in range(args.num_validation_images): | ||
| images.append(pipeline(args.validation_prompt, num_inference_steps=30, generator=generator).images[0]) | ||
|
|
||
| if accelerator.is_main_process: | ||
| for tracker in accelerator.trackers: | ||
| if len(images) != 0: | ||
| if tracker.name == "tensorboard": | ||
| np_images = np.stack([np.asarray(img) for img in images]) | ||
| tracker.writer.add_images("test", np_images, epoch, dataformats="NHWC") | ||
| if tracker.name == "wandb": | ||
| tracker.log( | ||
| { | ||
| "test": [ | ||
| wandb.Image(image, caption=f"{i}: {args.validation_prompt}") | ||
| for i, image in enumerate(images) | ||
| ] | ||
| } | ||
| ) | ||
| for tracker in accelerator.trackers: | ||
| if len(images) != 0: | ||
| if tracker.name == "tensorboard": | ||
| np_images = np.stack([np.asarray(img) for img in images]) | ||
| tracker.writer.add_images("test", np_images, epoch, dataformats="NHWC") | ||
| if tracker.name == "wandb": | ||
| tracker.log( | ||
| { | ||
| "test": [ | ||
| wandb.Image(image, caption=f"{i}: {args.validation_prompt}") | ||
| for i, image in enumerate(images) | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| accelerator.end_training() | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.