Stop recreating the hashmap all the time. #363
Merged
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?
This is a different approach from #362 but should fix the same issue in the same way.
This doesn't change any external python API, and makes every existing code faster (by just using the structurs smarter).
This is better IMO as it fixes every potential use for the
safe_open
method, whereas #362 introduces a new layer that has to be used in order to benefit (so only users ofsafetensors.torch.load_file
would actually see the difference).The crux of the problem was
Metadata::tensors
which is a method that creates a fullHashMap<String, String>
to access the tensors. The structure used to be exactly that and just give directly the structure to callers, therefore relatively free. But since, the structure was refactored into aHashMap<String, usize>
andVec<TensorInfo>
.The main reason for it, is that the
Vec
is intrinsically ordered, and we maintain thedata_offsets
ordered in order to get efficient comparison when checking for malformed files (with overlapping data_offsets).It also helps in during the serialization where we already have those information sorted so we write them sorted into the JSON header.
Fixes #361
Potentially superseeds #362
Fixes # (issue) or description of the problem this PR solves.