[train_text_to_image_decoder.py] Fix the LR scheduler when num_train_epochs is passed in a distributed training env - #14542
Conversation
…epochs is passed in a distributed training env
|
Hi @shoutoutuoadi325, 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. |
|
Keeping |
|
Hi @shoutoutuoadi325, a follow-up on the reminder above: this PR still does not link an issue it fixes. Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice. Adding a closing keyword (e.g. |
What does this PR do?
Part of #8384. Propagates the LR scheduler fix from #8312 to
examples/kandinsky2_2/text_to_image/train_text_to_image_decoder.py.When
--num_train_epochsis used with more than one process, the scheduler'snum_warmup_steps/num_training_stepswere computed from the unsharded dataloader length and scaled bygradient_accumulation_steps, so the LR schedule was wrong. This follows the same pattern already applied to the other scripts in #8384 (e.g. #11239, #11557), including the mismatch warning when the dataloader length changes afteraccelerator.prepare. Per #8312, the scheduler is steppednum_processestimes per optimizer step, so thegradient_accumulation_stepsmultiplier is removed. In single-process runs withgradient_accumulation_steps > 1,--lr_warmup_stepsand--max_train_stepsare now counted in optimizer steps, as documented.Coordination: claimed here and acknowledged by @sayakpaul here.
Test commands and results
Minimal 2-process run exercising the
num_train_epochspath this fix changes:Result: 8 samples over 2 processes = 4 optimizer steps per process, ran to completion, models saved:
The lr goes 5e-6 -> 1e-5 -> 5e-6 -> 0, which matches
num_warmup_steps_for_scheduler = 2 * 2 processes = 4andnum_training_steps_for_scheduler = 1 epoch * 2 processes * 4 steps = 8. No post-preparemismatch warning was emitted.Self-review notes
Final self-review round against
.ai/review-rules.md(diff is a singleexamples/training script; nosrc/,tests/, or.ai/changes):overrode_max_train_stepsflag and the branch it guarded are both gone, and nothing else references it. All three new variables are used.examples/kandinsky2_2/has no test module; the issue asks for a minimal training command instead of unit tests).ruff check,ruff format --check, andpython -m py_compileall pass on the touched file.preparewarning condition usesnum_training_steps_for_scheduler != args.max_train_steps * accelerator.num_processes(same asexamples/text_to_image/train_text_to_image.py:892), not the older bare!= args.max_train_stepscheck some dreambooth scripts still use.Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.
(no updates needed: no documented behavior changed)
Who can review?
cc @sayakpaul @geniuspatrick