[Serialization] Fix: remove context manager when loading shards + handle mlx
weights
#2709
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.
This PR fixes some failing tests in transformers when using the hfh loading helpers introduced in #2687.
See: https://app.circleci.com/pipelines/github/huggingface/transformers/113474/workflows/1a1ecbcd-26dc-4d3e-872d-79e5aedf04f2/jobs/1516443/tests#failed-test-1
in
test_model_from_pretrained_from_mlx
: mlx weights should be loaded aspt
according to this comment : https://github.com/huggingface/transformers/blob/3d213b57fe74302e5902d68ed9478c3ad1aaa713/src/transformers/modeling_utils.py#L3966in
test_resume_training_with_safe_checkpoint
: The UnboundLocalError occurred because in the context manager _load_shard_into_memory, the finally block tried to delete state_dict before it was created due to probably an exception in the try block. Instead of fixing this directly in the context manager, I'd rather remove it since a direct load-delete pattern is simpler and achieves the same memory management goals.