Fix sft_trainer crash when adapter_file is None (#908)#987
Open
H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
Open
Fix sft_trainer crash when adapter_file is None (#908)#987H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
H-A-Khan wants to merge 1 commit intoBlaizzy:mainfrom
Conversation
`sft_trainer.train` (and the analogous `orpo_trainer.train_orpo`)
crash on the first checkpoint or final save if `args.adapter_file`
is `None`:
File "mlx_vlm/trainer/sft_trainer.py", line 471, in train
save_adapter(model, args.adapter_file)
File "mlx_vlm/trainer/utils.py", line 235, in save_adapter
path = Path(adapter_file)
TypeError: argument should be a str or an os.PathLike object
where __fspath__ returns a str, not 'NoneType'
This happens when callers construct `TrainingArgs` (or pass a
`SimpleNamespace`) without explicitly setting `adapter_file`. The
dataclass default is `"adapters.safetensors"`, but if a caller
overrides it to None — or if the field is shadowed during
serialisation — the train loop runs to the first save, then
crashes and loses the work.
Fix: defend at the top of `train` / `train_orpo`. If
`args.adapter_file` is `None`, set it to the dataclass default
(`adapters.safetensors`) and emit a warning on rank 0 so the
substitution is visible. Covers all three call sites
(steps_per_save checkpoint, indexed checkpoint filename, final
save) with one guard.
Includes a regression test in `test_trainer.py` that constructs
`TrainingArgs(...)` then sets `adapter_file = None`, runs a
single-iteration training loop, and asserts both that the default
is applied and that the save still runs.
Closes Blaizzy#908
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.
What
sft_trainer.train(and the analogousorpo_trainer.train_orpo) crash on the first checkpoint or final save ifargs.adapter_fileisNone:This happens when callers construct
TrainingArgs(or pass aSimpleNamespace) without explicitly settingadapter_file. The dataclass default is"adapters.safetensors", but if a caller overrides that toNone— or if the field is shadowed during serialisation — the train loop runs to the first save, then crashes and loses the work.Closes #908.
Fix
Defend at the top of
train/train_orpo. Ifargs.adapter_fileisNone, set it to the dataclass default (adapters.safetensors) and emit a warning on rank 0 so the substitution is visible. Covers all three call sites (steps_per_save checkpoint, indexed checkpoint filename, final save) with one guard per trainer.Changes
mlx_vlm/trainer/sft_trainer.py: defensive guard at top oftrain().mlx_vlm/trainer/orpo_trainer.py: same guard at top oftrain_orpo()(the same bug exists in the ORPO trainer).mlx_vlm/tests/test_trainer.py: regression testtest_train_handles_none_adapter_filethat constructsTrainingArgs(...), setsadapter_file = None, runs a single-iteration training loop, and asserts both that the default is applied and thatsave_safetensorsis still invoked.Tests