Skip to content

fix(examples): correct lr_scheduler step count for distributed training - #15389

Open
nandanau wants to merge 2 commits into
huggingface:mainfrom
nandanau:fix/lr-scheduler-step-count-distributed
Open

nandanau wants to merge 2 commits into
huggingface:mainfrom
nandanau:fix/lr-scheduler-step-count-distributed

Conversation

@nandanau

Copy link
Copy Markdown

What this PR does / why we need it

Fixes #15087

Several training scripts create the lr_scheduler before accelerator.prepare() using a step count derived from the non-sharded dataloader length. After accelerator.prepare(), the dataloader is sharded across GPUs so its length drops by num_processes — but the scheduler was already built with an N× inflated step count. This causes:

  • Learning rate warmup and decay to take N× longer than intended
  • In practice, the LR never decays at all in many training runs
  • Silent corruption that is hard to detect when using constant or cosine schedules

Root cause

# BEFORE (buggy): max_train_steps computed from non-sharded len(train_dataloader)
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
if args.max_train_steps is None:
    args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch  # too large by num_processes

lr_scheduler = get_scheduler(
    ...,
    num_training_steps=args.max_train_steps * accelerator.num_processes,  # doubly wrong
)

# THEN accelerator.prepare() shards the dataloader — but scheduler already built
transformer, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(...)

Fix

Applies the correct pattern established in PR #8312 consistently across all affected scripts:

# AFTER: pre-compute sharded length before creating the scheduler
num_warmup_steps_for_scheduler = args.lr_warmup_steps * accelerator.num_processes
if args.max_train_steps is None:
    len_train_dataloader_after_sharding = math.ceil(len(train_dataloader) / accelerator.num_processes)
    num_update_steps_per_epoch = math.ceil(len_train_dataloader_after_sharding / args.gradient_accumulation_steps)
    num_training_steps_for_scheduler = args.num_train_epochs * accelerator.num_processes * num_update_steps_per_epoch
    overrode_max_train_steps = True
else:
    num_training_steps_for_scheduler = args.max_train_steps * accelerator.num_processes

lr_scheduler = get_scheduler(
    ...,
    num_warmup_steps=num_warmup_steps_for_scheduler,
    num_training_steps=num_training_steps_for_scheduler,
)

Affected scripts (15 files)

  • examples/dreambooth/train_dreambooth_lora_qwen_image.py
  • examples/dreambooth/train_dreambooth_lora_qwenimage21.py
  • examples/dreambooth/train_dreambooth_lora_qwenimage21_img2img.py
  • examples/dreambooth/train_dreambooth_lora_hidream.py
  • examples/dreambooth/train_dreambooth_lora_krea2.py
  • examples/dreambooth/train_dreambooth_lora_lumina2.py
  • examples/dreambooth/train_dreambooth_lora_sana.py
  • examples/dreambooth/train_dreambooth_sd3.py
  • examples/controlnet/train_controlnet_sd3.py
  • examples/controlnet/train_controlnet_flux.py (had num_training_steps_for_scheduler computed correctly but not passed to get_scheduler)
  • examples/cogvideo/train_cogvideox_lora.py
  • examples/cogvideo/train_cogvideox_image_to_video_lora.py
  • examples/cogview4-control/train_control_cogview4.py (same partial-fix as controlnet_flux)
  • examples/textual_inversion/textual_inversion.py
  • examples/rfd4_proteina/train_rfd4_proteina_lora.py

Who can help?

@sayakpaul @DN6

The scheduler was created using max_train_steps * accelerator.num_processes
where max_train_steps itself was derived from the pre-sharded dataloader
length. After accelerator.prepare() the dataloader is sharded across GPUs,
so the real update steps per epoch drop by num_processes, but the scheduler
had already been initialized with a value N times too large.

Applies the pattern established in PR huggingface#8312:
- Pre-compute len_train_dataloader_after_sharding = ceil(len(dataloader) / num_processes)
- Derive num_training_steps_for_scheduler using the sharded length
- Pass num_training_steps_for_scheduler to get_scheduler() and DummyScheduler

Fixes huggingface#15087
@github-actions github-actions Bot added examples fixes-issue size/M PR with diff < 200 LOC labels Nov 24, 2026

This branch has not been deployed

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

Labels

examples fixes-issue size/M PR with diff < 200 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lr_scheduler step bug in examples code

1 participant