Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
[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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b6de72532bd473724ddf93aed05c8ac462b18e6bf785ff777File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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: | ||
MemberAuthor 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) | ||
MemberAuthor 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() | ||
Uh oh!
There was an error while loading. Please reload this page.