Repository navigation
Save checkpoint to temporary directory to handle partial saves during failures - #35580
Conversation
SunMarc
left a comment
There was a problem hiding this comment.
Thanks for the PR ! Left a comment. Can you double check @muellerzr ?
| for checkpoint in sorted(checkpoints, key=lambda x: int(_re_checkpoint.search(x).groups()[0]), reverse=True): | ||
| if is_valid_checkpoint_dir(checkpoint): | ||
| break | ||
| return os.path.join(folder, checkpoint) |
There was a problem hiding this comment.
I think it can be good to display a warning to the user, specifying that we decided to skip a particular checkpoint because it is missing files.
|
Checking for existence of files is only a partial solution to this problem, you can have partially written files in a crash during save. The needed fixes include
|
|
@rwightman thanks for your comments.
You're right, but, I believe the underlying write to file calls usually handle this issue of partial file when crashing. For instance, safetensors.save uses numpy.save which this SO issue mentions doesn't create file until success. Even in my own runs, I hadn't faced that issue. It could happen due to edge cases, but I believe much less likely than this case.
I initially planned to fix it that way (try catch around the load and change dir), but unfortunately, the resume_from_checkpoint dir, once selected, is used in too many places to easily catch and then replace everything prior as well. Another problem is that the user could pass that variable as well (get_last_checkpoint is only called in case of auto-resume, in other cases the variable is set to what the user passes), in which case we don't want to change the directory and should probably raise an error (maybe a better error message than FileNotFound though).
This is a good suggestion and could also solve this problem. At a quick glance, all the saving happens in |
This solution from @rwightman seems to be the most robust one. Would you like to try to implement this @SilverSoldier ? |
|
Sure, let me change the PR to implement the save to tmp dir approach |
|
@SilverSoldier in these cases, the usual situation is to do the writes to a distinct temp name in the same location as the final destination. So if you normally save checkpoints to For most filesystem renaming a file/folder on the same drive is an atomic operation and very low risk of failure. I looked at the numpy save and it's a chunked write, pretty sure you can cause partial write failures in many situations. |
9d0410d to
8f8ead2
Compare
|
Have changed to the save to tmp approach using @rwightman's idea (thanks!).
@SunMarc could you please review the PR now? |
SunMarc
left a comment
There was a problem hiding this comment.
Thanks for iterating ! Left a few comments. Can you double check @muellerzr ?
8f8ead2 to
81175b6
Compare
81175b6 to
3c9677f
Compare
|
Make sure to fix the CI with |
Since partial/missing files due to failures throw error during load
3c9677f to
f7a972a
Compare
|
Fixed the code style issues. |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
@SunMarc do we need approval from anyone else? Thanks! |
|
I think @muellerzr wanted to check if there were no issues in multi-node scenario also. |
muellerzr
left a comment
There was a problem hiding this comment.
Thanks! Small scale test seems fine, but let's keep an eye out on issues to see if we need to redo this approach again 🤗
|
Thanks @muellerzr @SunMarc! |
… failures (#35580) Save checkpoint to temporary folder first Since partial/missing files due to failures throw error during load
|
I call for reverting this pull request, not everyone has enough space in the tmp path to store model weights, this approach is too inappropriate. |
|
@wizyoung the tmp path is in the same directory location to where the final model weights will be stored. We're looking into other issues related to it, however what you are describing is not how this PR works at all |
|
@muellerzr You are right. This PR has broken my model saving, causing me some frustration, and I haven't reviewed the code carefully, sorry about that. |
|
@wizyoung no worries, we'll have a solution by EOD :) (which may include reverting, we'll see) |
|
@wizyoung can you try with |
|
@muellerzr Using your branch, the model save failure is gone! |
|
@muellerzr New issue arrised. In this patch, under deepspeed zero2 8xH100 training, the model saves the model incompletely. Using the original save mode: |
|
call for reverting this pull request this pr do not consider any multi-process save problem, like rng states |
|
This approach will not work if it's being used where multiple processes write a checkpoint. To work in that scenario it'd neeed synchronization across the processes and that will increase the testing requirements significantly. Even the proposal to to check the existence of files becomes much more complicated. Since there wasn That probably leaves the catch / detect error on load and try the the previous checkpoint as the best option, though that could also fail to work for distributed cases. Some processes could have succeeded and some fail but they all need to moec to the same other checkpoint tro try, again synchronization and lots of testing needed |
same issue, with deepspeed zero2, it saved incomplete checkpoint (with a single file bf16_zero_pp_rank_0_mp_rank_00_optim_states.pt). |
my checkpoint savings are incomplete when training qwen2_5_vl model in zero2 with 8*a800 |
|
Thanks for everyone feedbacks ! We will most probably revert the PR today until @muellerzr manages to find a fix before |
… failures (huggingface#35580) Save checkpoint to temporary folder first Since partial/missing files due to failures throw error during load
… failures (huggingface#35580) Save checkpoint to temporary folder first Since partial/missing files due to failures throw error during load
* Revert "Fix OS err (huggingface#36094)" This reverts commit ba29a43. * Revert "Save checkpoint to temporary directory to handle partial saves during failures (huggingface#35580)" This reverts commit 20d1735.



What does this PR do?
When auto-resuming from checkpoint, currently the "max" folder is picked as the checkpoint folder to resume from. If there are some missing files (like model, config or trainer_state), this results in FileNotFoundError. This PR picks the latest checkpoint folder (should ideally be last or second last) with all required files allowing training to resume instead of just throwing an error requiring manual removing of files.
Fixes #35782
Currently, I test for atleast one of the model weights along with definitely requiring config and trainer_state. From what I understood the others (like optimizer) were optional, but please let me know if I missed something.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
Trainer: @muellerzr and @SunMarc