Skip to content

fix: Fix train_dreambooth_lora.py fp16 unscale error after validation - #14507

Open
Ricardo-M-L wants to merge 1 commit into
huggingface:mainfrom
Ricardo-M-L:fix/dreambooth-lora-fp16-validation-unscale-clean
Open

fix: Fix train_dreambooth_lora.py fp16 unscale error after validation#14507
Ricardo-M-L wants to merge 1 commit into
huggingface:mainfrom
Ricardo-M-L:fix/dreambooth-lora-fp16-validation-unscale-clean

Conversation

@Ricardo-M-L

Copy link
Copy Markdown
Contributor

Clean rebase of fix/dreambooth-lora-fp16-validation-unscale

When `--mixed_precision=fp16` and `--validation_prompt` are both set,
training aborts on the first step after the first validation with:

  ValueError: Attempting to unscale FP16 gradients.

Root cause:

* The LoRA trainable params are upcast to fp32 once, before training,
  via `cast_training_params(models, dtype=torch.float32)`.
* Validation builds `DiffusionPipeline.from_pretrained(unet=unwrap_model(unet),
  torch_dtype=weight_dtype, ...)` and hands the pipeline to `log_validation`.
* `log_validation` calls `pipeline.to(accelerator.device, dtype=torch_dtype)`,
  which casts the *shared* `unet` module — including the LoRA adapter weights
  registered as trainable — back down to fp16.
* The next backward then produces fp16 grads, and the grad scaler refuses to
  unscale them.

Re-run `cast_training_params(..., dtype=torch.float32)` immediately after
`log_validation` returns (only when `mixed_precision == "fp16"`), mirroring
the pre-training upcast. bf16 mixed-precision is unaffected since no grad
scaler is in play there.

Fixes huggingface#13124
@github-actions github-actions Bot added examples size/S PR with diff < 50 LOC labels Aug 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi @Ricardo-M-L, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked. See the contribution guide for more details. If this PR intentionally does not fix a tracked issue, a maintainer can add the no-issue-needed label to silence this reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant