-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix: unscale fp16 gradient problem & potential error (#6086) #6231
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,6 +640,17 @@ def main(args): | |
text_encoder_one.add_adapter(text_lora_config) | ||
text_encoder_two.add_adapter(text_lora_config) | ||
|
||
# Make sure the trainable params are in float32. | ||
if args.mixed_precision == "fp16": | ||
models = [unet] | ||
if args.train_text_encoder: | ||
models.extend([text_encoder_one, text_encoder_two]) | ||
for model in models: | ||
for param in model.parameters(): | ||
# only upcast trainable parameters (LoRA) into fp32 | ||
if param.requires_grad: | ||
param.data = param.to(torch.float32) | ||
|
||
# create custom saving & loading hooks so that `accelerator.save_state(...)` serializes in a nice format | ||
def save_model_hook(models, weights, output_dir): | ||
if accelerator.is_main_process: | ||
|
@@ -1187,6 +1198,9 @@ def compute_time_ids(original_size, crops_coords_top_left): | |
torch.cuda.empty_cache() | ||
|
||
# Final inference | ||
# Make sure vae.dtype is consistent with the unet.dtype | ||
if args.mixed_precision == "fp16": | ||
vae.to(weight_dtype) | ||
Comment on lines
+1201
to
+1203
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. This is not needed in my opinion. We already set the 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 no other
and the pipeline here does not reload vae. So 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. Ah okay got it! |
||
# Load previous pipeline | ||
pipeline = StableDiffusionXLPipeline.from_pretrained( | ||
args.pretrained_model_name_or_path, | ||
|
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.
Works for me!