Add patience (early stopping) based on validation loss#1408
Open
BitcrushedHeart wants to merge 3 commits intoNerogar:masterfrom
Open
Add patience (early stopping) based on validation loss#1408BitcrushedHeart wants to merge 3 commits intoNerogar:masterfrom
BitcrushedHeart wants to merge 3 commits intoNerogar:masterfrom
Conversation
When enabled, tracks validation loss across runs and saves the best checkpoint. If loss doesn't improve for N consecutive validation checks, stops training and restores the best weights as the final output model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds early stopping to standard training based on validation loss. When enabled, the trainer monitors validation loss after each validation run and saves a lightweight checkpoint whenever a new minimum is reached. If loss doesn't improve for a configurable number of consecutive validation checks, training stops and the best checkpoint is restored as the final output model.
This mirrors the same pattern already used for DPO early stopping (accuracy-based), but operates on the general validation loss path in the General tab.
How It Works
Each validation run is a "tick." The trainer keeps a running minimum of the total average validation loss across all concepts. When loss improves, the counter resets and the current parameter state is saved to
{workspace}/backup/patience-best.pt. When loss doesn't improve, the counter increments. Once the counter reaches the configured threshold (patience_epochs, default 5), training stops viacommands.stop().At the end of training, if a best checkpoint exists, the trainer restores those weights before saving the final model. The user gets the best-performing weights, not the last (potentially overfit) ones. The log output makes this explicit:
The best checkpoint is a separate file from regular interval saves and backups. Users who already have "save every N steps" configured will see both their regular checkpoints and the best checkpoint appearing independently. No existing backup/checkpoint behaviour is changed.
UI
Two new fields in the General tab, filling previously empty cells next to Dataloader Threads and Train Device:
Enabling the Patience toggle auto-enables Validation if it's off. There's also a training-time guard that does the same thing with a console warning, in case someone edits the config JSON directly.
Changes
modules/util/config/TrainConfig.py: Two new fields (patience,patience_epochs), config version bump (10→11), migrationmodules/ui/TrainUI.py: Patience toggle + Early Stop After input in General tab, auto-enable callbackmodules/trainer/GenericTrainer.py: Patience check after validation, lightweight checkpoint save/restore, training-time guardtests/test_patience.py: 8 tests covering counter increment, reset, stop trigger, checkpoint save/restore, auto-enable, and TensorBoard loggingTesting Notes
All 8 unit tests pass. Tests use dummy tensors and mock objects to verify the patience logic independently of the full training pipeline — counter behaviour, stop triggering, and parameter save/restore are all exercised.
Tested on Windows 11, Python 3.11.9.