Skip to content

make sure model.save_pretrained has the correct is_main_process - #39866

Closed
winglian wants to merge 1 commit into
huggingface:mainfrom
winglian:save-is-main-process
Closed

winglian wants to merge 1 commit into
huggingface:mainfrom
winglian:save-is-main-process

Conversation

@winglian

@winglian winglian commented Aug 2, 2025

Copy link
Copy Markdown
Collaborator

What does this PR do?

Now that multiple processes can be involved in saving due to parallelism (see #39693), there are intermittent race conditions since the default to save_pretrained is is_main_process=True. Stacktrace below that intermittently happens on later checkpoints

[rank1]:   File "/root/miniconda3/envs/py3.11/lib/python3.11/site-packages/transformers/trainer.py", line 3237, in _save_checkpoint
[rank1]:     self.save_model(output_dir, _internal_call=True)                                                                                                                    
[rank1]:   File "/root/miniconda3/envs/py3.11/lib/python3.11/site-packages/transformers/trainer.py", line 3959, in save_model
[rank1]:     self._save(output_dir)   
[rank1]:   File "/workspace/axolotl/src/axolotl/core/trainers/mixins/distributed_parallel.py", line 20, in _save
[rank1]:     super()._save(output_dir, state_dict=state_dict)
[rank1]:   File "/root/miniconda3/envs/py3.11/lib/python3.11/site-packages/transformers/trainer.py", line 4091, in _save
[rank1]:     self.model.save_pretrained(                                                                                                                                         
[rank1]:   File "/root/miniconda3/envs/py3.11/lib/python3.11/site-packages/transformers/modeling_utils.py", line 4062, in save_pretrained
[rank1]:     os.remove(full_filename)                                                   
[rank1]: FileNotFoundError: [Errno 2] No such file or directory: './model-out/checkpoint-34/model-00001-of-00004.safetensors'

@SunMarc @ArthurZucker @S1ro1

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM let's add a test! 🤗

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Thanks for the PR, and sorry it didn't make it in! This branch is based on v4, and v5 is very different, so rather than rebasing we'd love to invite you to open a new PR against main using our latest tools and good practices:

Closing this one in the meantime, thanks again!

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