Skip to content

fix(optim): prevent learning rate reset at warmdown iteration boundary - #2180

Open
reginaldalfret wants to merge 2 commits into
openai:mainfrom
reginaldalfret:fix/warmdown-iteration-boundary
Open

reginaldalfret wants to merge 2 commits into
openai:mainfrom
reginaldalfret:fix/warmdown-iteration-boundary

Conversation

@reginaldalfret

@reginaldalfret reginaldalfret commented Oct 21, 2026 •

Copy link
Copy Markdown

Problem

In both train_gpt.py and train_gpt_mlx.py, the iteration-based learning rate multiplier evaluates:

warmdown_start = max(args.iterations - args.warmdown_iters, 0)
return max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0

When training with fixed iteration budgets (when wallclock timeout is disabled or not reached), the condition checked step < args.iterations. For any evaluation where step >= args.iterations:

  1. step < args.iterations evaluates to False.
  2. Execution falls through to the ternary else branch, returning 1.0.
  3. Consequently, evaluating lr_mul(step, ...) at or past the iteration boundary returns full learning rate (1.0) instead of 0.0. The inner clamp max((iterations - step) / warmdown_iters, 0.0) was rendered unreachable at non-positive values due to the < args.iterations upper guard.

Stock loop context:
In standard training runs (train_gpt.py L974/L1005 and train_gpt_mlx.py L1004/L1026), last_step = step == args.iterations triggers validation and breaks before lr_mul(step, elapsed_ms) is invoked. Thus, standard runs exit without training a step at step == args.iterations, and existing benchmark records were not affected.

However, for continuation runs past args.iterations, post-budget logging/evaluations calling lr_mul, custom training loops, or forks that do not break immediately, returning 1.0 is unexpected.

Change

  1. Simplify the decay condition to step >= warmdown_start in both train_gpt.py and train_gpt_mlx.py:

    return max((self.iterations - step) / max(self.warmdown_iters, 1), 0.0) if step >= warmdown_start else 1.0
    • For step < warmdown_start, multiplier remains 1.0.
    • For warmdown_start <= step < iterations, multiplier decays linearly from 1.0 to 0.0.
    • For step >= iterations, max(..., 0.0) cleanly clamps to 0.0.
  2. In train_gpt.py, define lr_mul(self, step, elapsed_ms) on Hyperparameters and alias lr_mul = args.lr_mul in main(), mirroring the structure of train_gpt_mlx.py. This avoids duplicated closure logic in the test suite and enables direct unit testing of production code.

Line counts remain well within the 1,500-line limit:

  • train_gpt.py: 1,127 lines
  • train_gpt_mlx.py: 1,104 lines

Validation

  • Added tests/test_warmdown_schedule.py with 8 parameterized tests directly exercising train_gpt.Hyperparameters and train_gpt_mlx.Hyperparameters.
  • Covers standard warmdown, exact iteration boundary (step == iterations), post-budget steps (step > iterations), disabled warmdown (warmdown_iters = 0), warmdown exceeding iteration budget, and wallclock expiration.
  • All 8 unit tests pass cleanly.
  • ruff check --select E,F,W --ignore E501 and git diff --check pass with 0 errors.

When training with fixed iteration warmdown (without wallclock timeout), lr_mul checked warmdown_start <= step < iterations. When step reached iterations, the condition evaluated to False, causing the multiplier to fall back to the else branch and jump from near-zero back up to 1.0. This change simplifies the condition to step >= warmdown_start so that max((iterations - step) / warmdown_iters, 0.0) correctly yields 0.0 at and beyond the iteration boundary across PyTorch and MLX baselines.

Signed-off-by: reginaldalfret <[email protected]>
@Evreu1pro

Copy link
Copy Markdown

The fix itself looks right to me, and dropping the < args.iterations upper bound makes the max(..., 0.0) clamp actually mean something. One thing I'd adjust in the description though: if I'm reading the loop right, the step == iterations case never reaches lr_mul on current main.

In train_gpt.py, last_step is set at L974 when step == args.iterations, and the loop breaks at L1005 before scale = lr_mul(step, elapsed_ms) at L1008. train_gpt_mlx.py has the same order: L1004 / L1026 / L1028. So a run using the stock loop never actually trains a step at full LR after warmdown (record folders with their own copy of the loop could differ, I only checked the root scripts). I'd frame this as a defensive cleanup (it would only bite someone who continues training past iterations or calls lr_mul from elsewhere), so nobody goes back wondering whether old records were affected.

Small nit on the tests: test_train_gpt_step_schedule_logic re-implements the closure instead of testing the one in train_gpt.py, so it would keep passing if the real code drifted. Not sure it's worth restructuring main() just for that, though, given the line limit.

…irectly

Hoist lr_mul to Hyperparameters in train_gpt.py to match train_gpt_mlx.py, aliasing lr_mul = args.lr_mul in main(). Update test_warmdown_schedule.py to test both production Hyperparameters implementations directly across all schedule edge cases without duplicate closure logic.

Signed-off-by: reginaldalfret <[email protected]>
@reginaldalfret

Copy link
Copy Markdown
Author

Thanks for the careful review and the spot-on catch regarding the loop break order!

You are completely right: on stock main, last_step = step == args.iterations validates and breaks before lr_mul() is called for that step, so standard runs never actually executed an optimizer step at 1.0. I have updated the PR description to make it clear that this is a defensive cleanup for continued training past iterations, custom loops, and external post-budget callers, and explicitly clarified that standard runs and historical records were not affected.

Regarding the test suite: to avoid replicating the closure in test_warmdown_schedule.py, we hoisted lr_mul to Hyperparameters.lr_mul on train_gpt.py (with lr_mul = args.lr_mul kept in main()), identical to how train_gpt_mlx.py is structured. This required only a net +1 line in train_gpt.py (staying well under the line limit at 1,127 lines) and allows the test suite to import and test both train_gpt and train_gpt_mlx production implementations directly.

@Evreu1pro

Copy link
Copy Markdown

Nice, hoisting it onto Hyperparameters is cleaner than what I had in mind. Checked out c9726d6 and ran it: tests/test_warmdown_schedule.py gives 8 passed (torch 2.14.1, CPU, no mlx installed, so the MagicMock path for train_gpt_mlx gets exercised too). The max_wallclock_seconds <= 0 check is equivalent to the old max_wallclock_ms is None, so no behavior change there.

Tiny thing, not blocking: the mocks stay in sys.modules for the whole pytest session, so if more test files land in tests/ later, anything that does import mlx to detect Apple Silicon would see a MagicMock instead of an ImportError. Fine as is since this is the only test file (and there's no CI running tests/ yet, if I'm reading the repo right), just something to keep in mind.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants