-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[refactor] model loading - no more unnecessary file downloads
#2345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tomaarsen
merged 9 commits into
huggingface:master
from
tomaarsen:feat/efficient_loading
Dec 12, 2023
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4bf9e99
Refactor model loading: no full repo download
tomaarsen 31646a9
Add simple test regarding efficient loading
tomaarsen a1a1cd7
Replace use_auth_token with token in docstring
tomaarsen e1ca408
Prevent crash if internet is down
tomaarsen 7618a4f
Merge branch 'master' into feat/efficient_loading
tomaarsen f26ba94
Use load_file_path in "is_sbert_model"
tomaarsen a00482f
Merge branch 'master' of https://github.com/UKPLab/sentence-transform…
tomaarsen 033bf6d
Merge branch 'master' into feat/efficient_loading
tomaarsen 255e828
Merge branch 'master' into feat/efficient_loading
tomaarsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """ | ||
| Tests general behaviour of the SentenceTransformer class | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| import tempfile | ||
|
|
||
| import torch | ||
| from sentence_transformers import SentenceTransformer | ||
| from sentence_transformers.models import Transformer, Pooling | ||
| import unittest | ||
|
|
||
|
|
||
| class TestSentenceTransformer(unittest.TestCase): | ||
| def test_load_with_safetensors(self): | ||
| with tempfile.TemporaryDirectory() as cache_folder: | ||
| safetensors_model = SentenceTransformer( | ||
| "sentence-transformers-testing/stsb-bert-tiny-safetensors", | ||
| cache_folder=cache_folder, | ||
| ) | ||
|
|
||
| # Only the safetensors file must be loaded | ||
| pytorch_files = list(Path(cache_folder).glob("**/pytorch_model.bin")) | ||
| self.assertEqual(0, len(pytorch_files), msg="PyTorch model file must not be downloaded.") | ||
| safetensors_files = list(Path(cache_folder).glob("**/model.safetensors")) | ||
| self.assertEqual(1, len(safetensors_files), msg="Safetensors model file must be downloaded.") | ||
|
|
||
| with tempfile.TemporaryDirectory() as cache_folder: | ||
| transformer = Transformer( | ||
| "sentence-transformers-testing/stsb-bert-tiny-safetensors", | ||
| cache_dir=cache_folder, | ||
| model_args={"use_safetensors": False}, | ||
| ) | ||
| pooling = Pooling(transformer.get_word_embedding_dimension()) | ||
| pytorch_model = SentenceTransformer(modules=[transformer, pooling]) | ||
|
|
||
| # Only the pytorch file must be loaded | ||
| pytorch_files = list(Path(cache_folder).glob("**/pytorch_model.bin")) | ||
| self.assertEqual(1, len(pytorch_files), msg="PyTorch model file must be downloaded.") | ||
| safetensors_files = list(Path(cache_folder).glob("**/model.safetensors")) | ||
| self.assertEqual(0, len(safetensors_files), msg="Safetensors model file must not be downloaded.") | ||
|
|
||
| sentences = ["This is a test sentence", "This is another test sentence"] | ||
| self.assertTrue( | ||
| torch.equal(safetensors_model.encode(sentences, convert_to_tensor=True), pytorch_model.encode(sentences, convert_to_tensor=True)), | ||
| msg="Ensure that Safetensors and PyTorch loaded models result in identical embeddings", | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.