SimpleTuner icon indicating copy to clipboard operation
SimpleTuner copied to clipboard

Schedule-free optimizers: the final LoRA/LyCORIS export is captured before mark_optimizer_eval(), so the root file differs from checkpoint-<last>

Open DustyL opened this issue 3 weeks ago • 0 comments

What happens

With a schedule-free optimizer (prodigy, adamw_schedulefree and its variants), the LoRA written at the end of training, the root pytorch_lora_weights.safetensors, does not hold the same weights as checkpoint-<last>. The root file holds the train-mode iterate. The last checkpoint and the final validation images use the eval-mode (averaged) weights.

Where

simpletuner/helpers/training/trainer.py, end of Trainer.train(), on main 9e8ff82:

  1. The block at L7736–7758 builds final_lora_save_kwargs from _materialize_state_dict_for_save(get_peft_model_state_dict(trained_component)). _materialize_tensor_for_save (save_hooks.py L166–169) returns tensor.detach().cpu().contiguous().clone(), a copy. The LyCORIS branch (L7770, _materialize_lycoris_state_dict_for_save) captures the same way.
  2. self.mark_optimizer_eval() runs only later, at L7809, before the final validation.
  3. L7846 save_lora_weights(**final_lora_save_kwargs) writes the copy from step 1, which was taken while the optimizer was still in train mode.

The training loop always exits in train mode, because every in-loop switch is paired: eval at L6705 / train at L6763, L6867 / L6889, L7594 / L7608.

Both schedule-free classes swap the parameters in place when the mode changes:

  • ProdigyPlusScheduleFree.set_train_mode (prodigy-plus-schedule-free 2.0.1, the prodigy choice): p.lerp_(end=z, weight=w) with w = 1 - beta1 for train.
  • AdamWScheduleFreeKahan.eval() / .train() (the adamw_schedulefree, +aggressive and +no_kahan choices, also FSDP2_FALLBACK_OPTIMIZER): p.data.lerp_(...).

So for every optimizer registered with is_schedulefree: True, the final export is y = x + (1 - beta1)(z - x), not x.

Why I think it is a bug and not a choice

Intermediate checkpoints are saved in eval mode (L6705 before the save, back to train at L6763), so checkpoint-N/ holds x. The final validation also runs in eval mode (after L7809), so its images show x while the file written next to them is y. The same run's checkpoint-<last> and root export therefore differ, and the root file is not what the final validation depicted.

Regression

eef772d7c (2026-06-19, "FSDP2 context-parallel fixes for LTX2.3 and data broadcasting") moved the capture above the eval call. In its parent (2ef5ffde1) mark_optimizer_eval() came before get_peft_model_state_dict(trained_component). v4.3.x has the old order; v4.4.0 is the first tag with the new one.

Measured

A Qwen-Image 2.1 LoRA trained with optimizer: prodigy, 4000 steps. The root pytorch_lora_weights.safetensors differs from checkpoint-4000's by about 7 % in ‖BA‖. Numerically, root == x + (1 - beta1)(z - x) with x = the checkpoint-4000 weights, z = the schedule-free base iterate from checkpoint-4000/optimizer.bin, and beta1 = 0.9, to a relative residual of 1.4e-3 (bf16 rounding). That is exactly the train-mode lerp above.

Suggested fix

Call self.mark_optimizer_eval() before the capture block (before L7736) instead of after it. Both optimizers guard on group["train_mode"], so the existing call at L7809 becomes a no-op.

One open question: the capture appears to run on every rank (FSDP2 gathers the shards there), while the L7809 call sits inside if self.accelerator.is_main_process: (L7800). Each rank would probably need to swap its own shard before the gather, so the earlier call may belong on all ranks. I have not tested this under FSDP2.

Workaround until then

When training with a schedule-free optimizer, load checkpoint-<last>/pytorch_lora_weights.safetensors instead of the root file.


Prepared with the help of an AI coding assistant. The code references were re-checked against 9e8ff82 on 2026-11-11, and the measurement is from my own training run.

DustyL avatar Nov 11 '26 22:11 DustyL