fix: use caller's class name as default model_type for subclasses#3692
Open
AtharvaJaiswal005 wants to merge 1 commit intohuggingface:mainfrom
Open
fix: use caller's class name as default model_type for subclasses#3692AtharvaJaiswal005 wants to merge 1 commit intohuggingface:mainfrom
AtharvaJaiswal005 wants to merge 1 commit intohuggingface:mainfrom
Conversation
When subclassing SentenceTransformer, _get_model_type() hardcoded the default to "SentenceTransformer" which mismatched with _model_config model_type (set to self.__class__.__name__). This caused subclassed models loading older models without model_type in their config to incorrectly use _load_auto_model instead of _load_sbert_model, resulting in wrong pooling defaults (mean pooling instead of CLS token). Fixes huggingface#3536
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.
Summary
self.__class__.__name__instead of hardcoded"SentenceTransformer"as the defaultmodel_typein_get_model_type()SentenceTransformerloading older models (withoutmodel_typein config) with wrong pooling defaultsRoot Cause
_get_model_type()hardcoded the fallback to"SentenceTransformer"in two places:config_sentence_transformers.jsonexistsmodel_typekeyMeanwhile,
_model_config["model_type"]is set toself.__class__.__name__(e.g."MySentenceTransformer"). This mismatch caused the comparison at line 325 to fail, falling through to_load_auto_modelinstead of_load_sbert_model, resulting in wrong pooling defaults (mean pooling instead of CLS token).Reproduction
Changes
sentence_transformers/SentenceTransformer.py: Changed default from"SentenceTransformer"toself.__class__.__name__in both fallback paths of_get_model_type()Fixes #3536