6161 import wandb
6262
6363# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
64- check_min_version ("0.41 .0.dev0" )
64+ check_min_version ("0.40 .0.dev0" )
6565
6666logger = get_logger (__name__ )
6767
@@ -1080,17 +1080,22 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
10801080 )
10811081
10821082 # Scheduler and math around the number of training steps.
1083- overrode_max_train_steps = False
1084- num_update_steps_per_epoch = math . ceil ( len ( train_dataloader ) / args . gradient_accumulation_steps )
1083+ # Check the PR https://github.com/huggingface/diffusers/pull/8312 for detailed explanation.
1084+ num_warmup_steps_for_scheduler = args . lr_warmup_steps * accelerator . num_processes
10851085 if args .max_train_steps is None :
1086- args .max_train_steps = args .num_train_epochs * num_update_steps_per_epoch
1087- overrode_max_train_steps = True
1086+ len_train_dataloader_after_sharding = math .ceil (len (train_dataloader ) / accelerator .num_processes )
1087+ num_update_steps_per_epoch = math .ceil (len_train_dataloader_after_sharding / args .gradient_accumulation_steps )
1088+ num_training_steps_for_scheduler = (
1089+ args .num_train_epochs * num_update_steps_per_epoch * accelerator .num_processes
1090+ )
1091+ else :
1092+ num_training_steps_for_scheduler = args .max_train_steps * accelerator .num_processes
10881093
10891094 lr_scheduler = get_scheduler (
10901095 args .lr_scheduler ,
10911096 optimizer = optimizer ,
1092- num_warmup_steps = args . lr_warmup_steps ,
1093- num_training_steps = args . max_train_steps ,
1097+ num_warmup_steps = num_warmup_steps_for_scheduler ,
1098+ num_training_steps = num_training_steps_for_scheduler ,
10941099 num_cycles = args .lr_num_cycles ,
10951100 power = args .lr_power ,
10961101 )
@@ -1102,8 +1107,14 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
11021107
11031108 # We need to recalculate our total training steps as the size of the training dataloader may have changed.
11041109 num_update_steps_per_epoch = math .ceil (len (train_dataloader ) / args .gradient_accumulation_steps )
1105- if overrode_max_train_steps :
1110+ if args . max_train_steps is None :
11061111 args .max_train_steps = args .num_train_epochs * num_update_steps_per_epoch
1112+ if num_training_steps_for_scheduler != args .max_train_steps * accelerator .num_processes :
1113+ logger .warning (
1114+ f"The length of the 'train_dataloader' after 'accelerator.prepare' ({ len (train_dataloader )} ) does not match "
1115+ f"the expected length ({ len_train_dataloader_after_sharding } ) when the learning rate scheduler was created. "
1116+ f"This inconsistency may result in the learning rate scheduler not functioning properly."
1117+ )
11071118 # Afterwards we recalculate our number of training epochs
11081119 args .num_train_epochs = math .ceil (args .max_train_steps / num_update_steps_per_epoch )
11091120
0 commit comments