Skip to content

Commit

Permalink
Merge pull request #2709 from tomaarsen/fix/override_saved_versions
Browse files Browse the repository at this point in the history
[`fix`] Always override the originally saved __version__ in the ST config
  • Loading branch information
tomaarsen authored Jun 4, 2024
2 parents cd9e80c + fe96913 commit 1e72d91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,12 +1004,11 @@ def save(
modules_config = []

# Save some model info
if "__version__" not in self._model_config:
self._model_config["__version__"] = {
"sentence_transformers": __version__,
"transformers": transformers.__version__,
"pytorch": torch.__version__,
}
self._model_config["__version__"] = {
"sentence_transformers": __version__,
"transformers": transformers.__version__,
"pytorch": torch.__version__,
}

with open(os.path.join(path, "config_sentence_transformers.json"), "w") as fOut:
config = self._model_config.copy()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_sentence_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,14 @@ def test_similarity_score_save(stsb_bert_tiny_model: SentenceTransformer) -> Non
assert loaded_model.similarity_fn_name == "euclidean"
dot_scores = model.similarity(embeddings, embeddings)
assert np.not_equal(cosine_scores, dot_scores).all()


def test_override_config_versions(stsb_bert_tiny_model: SentenceTransformer) -> None:
model = stsb_bert_tiny_model

assert model._model_config["__version__"]["sentence_transformers"] == "2.2.2"
with tempfile.TemporaryDirectory() as tmp_folder:
model.save(tmp_folder)
loaded_model = SentenceTransformer(tmp_folder)
# Verify that the version has now been updated when saving the model again
assert loaded_model._model_config["__version__"]["sentence_transformers"] != "2.2.2"

0 comments on commit 1e72d91

Please sign in to comment.