avatarJean-Charles Risch

Summary

The website content provides a workaround for the "Unscale FP16 gradients" error encountered when fine-tuning Stable Diffusion with LoRA, including the correct accelerate command and script modifications.

Abstract

The article, dated March 2024, addresses a common issue faced by users attempting to fine-tune Stable Diffusion models using LoRA (Low-Rank Adaptation). It references previous work by Hugging Face and a tutorial by Julien Simon on cost-effective fine-tuning. The core of the article is a solution to the "Attempting to unscale FP16 gradients" error, which has been widely reported. The author provides the correct accelerate command, which includes the --mixed_precision="fp16" argument, and points out the necessity of adding this argument to the train_text_to_image_lora.py script. The provided commands and script modifications aim to enable proper fine-tuning without the mentioned error, with a specific example using the "CompVis/stable-diffusion-v1-4" model and the "lambdalabs/pokemon-blip-captions" dataset.

Opinions

  • The author implies that the official documentation may not be fully up-to-date or clear on handling the "Unscale FP16 gradients" error.
  • There is an acknowledgment that fine-tuning Stable Diffusion models can be complex and error-prone, necessitating community-sourced solutions.
  • The article suggests that the solution provided was not immediately obvious, as it required specific script changes and command arguments that were not initially included.
  • The author likely believes that the cost of fine-tuning, as demonstrated by Julien Simon's video, can be significantly reduced by using AWS EC2 spot instances, making the process more accessible.
  • By referencing a GitHub issue comment, the author indicates that community discussions and comments are valuable resources for troubleshooting and resolving technical issues.

Fine-Tuning Stable Diffusion with LoRA (Workaround for ‘Unscale FP16 Gradients’ Error)

I am writing this article at the end of March 2024, more than a year since this article was published on Hugging Face and several months since Julien Simon released this video explaining how to fine-tune Stable Diffusion for less than $1 using AWS EC2 spot instances.

First of all, here is the official page that you should refer to.

Unfortunately, it is very likely that you will encounter the “Attempting to unscale FP16 gradients.” error. This error has been reported by multiple users here. If it’s your case, here’s how to fix your problem.

Here is the accelerate command to start the fine-tuning:

export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export DATASET_NAME="lambdalabs/pokemon-blip-captions"

accelerate launch --mixed_precision="fp16" --multi_gpu  train_text_to_image.py \
  --pretrained_model_name_or_path=$MODEL_NAME \
  --dataset_name=$DATASET_NAME \
  --use_ema \
  --resolution=512 --center_crop --random_flip \
  --train_batch_size=1 \
  --gradient_accumulation_steps=4 \
  --gradient_checkpointing \
  --max_train_steps=15000 \
  --learning_rate=1e-05 \
  --max_grad_norm=1 \
  --lr_scheduler="constant" --lr_warmup_steps=0 \
  --output_dir="sd-pokemon-model"

The argument --mixed_precision="fp16" is missing in the train_text_to_image_lora.py script for it to work properly. This will also solve the error with the sdxl script.

Here is the working accelerate command:

export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export DATASET_NAME="lambdalabs/pokemon-blip-captions"

accelerate launch --mixed_precision="fp16" --multi_gpu  train_text_to_image.py \
  --mixed_precision="fp16" \
  --pretrained_model_name_or_path=$MODEL_NAME \
  --dataset_name=$DATASET_NAME \
  --use_ema \
  --resolution=512 --center_crop --random_flip \
  --train_batch_size=1 \
  --gradient_accumulation_steps=4 \
  --gradient_checkpointing \
  --max_train_steps=15000 \
  --learning_rate=1e-05 \
  --max_grad_norm=1 \
  --lr_scheduler="constant" --lr_warmup_steps=0 \
  --output_dir="sd-pokemon-model"

This solution was mentioned initially in this comment.

Diffusers
Stable Diffusion
Hugging Face
Artificial Intelligence
Recommended from ReadMedium