Repository navigation
Accept torch.device as map_location for safetensors in load_state_dict - #15489
Open
mertkayacs wants to merge 1 commit into
Open
mertkayacs wants to merge 1 commit into
mertkayacs wants to merge 1 commit into
Conversation
This branch has not been deployed
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 does this PR do?
load_state_dictraises anOSErrorfor a valid.safetensorsfile whenmap_locationis atorch.device. The signature acceptsstr | torch.device, and the same argument works for.binfiles.safetensors.safe_openonly takes the device as a string, sotorch.device("cpu")is rejected.The fix converts a
torch.deviceto its string form at thesafe_opencall. Strings pass through unchanged.The new test loads a small checkpoint with
"cpu"andtorch.device("cpu"), with mmap enabled and disabled, and checks the tensor values and metadata.Fixes #15459
Tests
Self-review notes
I ran the
self-reviewrubric over the diff. No blocking issues and no dead code. The change adds no comments or docstrings, and themap_locationtype was already documented in the signature, so no docs needed updating. Nothing was left unfixed.Before submitting
self-reviewskill on the diff?Who can review?
@sayakpaul @DN6