| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
There was a problem hiding this comment.
Fixed initialization issue in SentenceTransformerPatched where super().init() caused incorrect configuration due to class name checks in parent class.
1 file reviewed, 1 comment
Edit PR Review Bot Settings | Greptile
Sorry, something went wrong.
| temp_model = SentenceTransformer(**dict( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| trust_remote_code=engine_args.trust_remote_code, | ||
| device=ls.device_placement, | ||
| model_kwargs=model_kwargs, | ||
| ) | ||
| )) | ||
| self.__dict__.update(temp_model.__dict__) | ||
| self.to(ls.device_placement) |
There was a problem hiding this comment.
style: This pattern of creating a temp instance and copying state bypasses normal inheritance. Consider adding comment explaining why dictionary update is safer than inheritance here
| temp_model = SentenceTransformer(**dict( | |
| model_name_or_path=engine_args.model_name_or_path, | |
| revision=engine_args.revision, | |
| trust_remote_code=engine_args.trust_remote_code, | |
| device=ls.device_placement, | |
| model_kwargs=model_kwargs, | |
| ) | |
| )) | |
| self.__dict__.update(temp_model.__dict__) | |
| self.to(ls.device_placement) | |
| # Create temporary model instance and copy its state to bypass SentenceTransformer's | |
| # __init__ which doesn't support our extended configuration. This allows us to | |
| # customize initialization while preserving all the model's internal state. | |
| temp_model = SentenceTransformer(**dict( | |
| model_name_or_path=engine_args.model_name_or_path, | |
| revision=engine_args.revision, | |
| trust_remote_code=engine_args.trust_remote_code, | |
| device=ls.device_placement, | |
| model_kwargs=model_kwargs, | |
| )) | |
| self.__dict__.update(temp_model.__dict__) | |
| self.to(ls.device_placement) |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Related Issue
The problem stems from the __init__ method of the parent class, SentenceTransformer. This method uses self.__class__.__name__ to set a configuration value.
When subclass SentenceTransformerPatched calls super().__init__(), self.__class__.__name__ evaluates to "SentenceTransformerPatched". The parent class's internal logic is not designed for this and expects the name to be "SentenceTransformer", which leads to incorrect config and problems like this:
The fix avoids calling super().__init__() directly. Instead, it creates a temporary instance of the base SentenceTransformer class, which initializes correctly. It then copies the state from this temporary object to the current instance (self), effectively bypassing the problematic check while still properly initializing the object.
Checklist
Additional Notes
I tried to run the poetry tests but got stuck here: