fix: wrap validation inference with torch.no_grad() in dreambooth examples#13273
Open
gambletan wants to merge 1 commit intohuggingface:mainfrom
Open
fix: wrap validation inference with torch.no_grad() in dreambooth examples#13273gambletan wants to merge 1 commit intohuggingface:mainfrom
gambletan wants to merge 1 commit intohuggingface:mainfrom
Conversation
…mples
When `log_validation()` runs pipeline inference during training, it uses the
same UNet/transformer that is being trained. Without `torch.no_grad()`, PyTorch
computes and stores gradients during validation. With `--mixed_precision="fp16"`,
this causes the gradient scaler to encounter FP16 gradients from the validation
pass when training resumes, resulting in:
ValueError: Attempting to unscale FP16 gradients.
This adds `torch.no_grad()` around all pipeline inference calls in
`log_validation()` across all dreambooth training scripts to prevent gradient
computation during validation.
Fixes huggingface#13124
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #13124
log_validation()in dreambooth training scripts runs pipeline inference using the same UNet/transformer that is being trained, withouttorch.no_grad(). With--mixed_precision="fp16", gradients are computed and stored during validation inference. When the training loop resumes, the gradient scaler tries to unscale these FP16 gradients, causing:This PR wraps all pipeline inference calls in
log_validation()withtorch.no_grad()across all 17 dreambooth training scripts, not just the one reported in the issue.Changes
train_dreambooth.pytrain_dreambooth_flux.pytrain_dreambooth_sd3.pytrain_dreambooth_lora.pytrain_dreambooth_lora_flux.pytrain_dreambooth_lora_flux2.pytrain_dreambooth_lora_flux2_img2img.pytrain_dreambooth_lora_flux2_klein.pytrain_dreambooth_lora_flux2_klein_img2img.pytrain_dreambooth_lora_flux_kontext.pytrain_dreambooth_lora_hidream.pytrain_dreambooth_lora_lumina2.pytrain_dreambooth_lora_qwen_image.pytrain_dreambooth_lora_sana.pytrain_dreambooth_lora_sd3.pytrain_dreambooth_lora_sdxl.pytrain_dreambooth_lora_z_image.pyRoot Cause
Validation runs the pipeline which internally calls the UNet/transformer forward pass. Since
torch.no_grad()is not set, autograd tracks operations and creates FP16 gradient tensors. TheGradScalerin the training loop then fails because it expects FP32 gradients to unscale.Test Plan
train_dreambooth_lora.pywith--mixed_precision="fp16"and--validation_promptno longer crashes withValueError: Attempting to unscale FP16 gradientsno_grad)