Skip to content

Conversation

@raul3820
Copy link

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:

WARNING: No sentence-transformers model found with name temp/models/model_1752105604572. Creating a new one with mean pooling.

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

  • I have read the CONTRIBUTING guidelines.
  • I have added tests to cover my changes.
  • I have updated the documentation (docs folder) accordingly.

Additional Notes

I tried to run the poetry tests but got stuck here:

Installing dependencies from lock file

Finding the necessary packages for the current system

Package operations: 167 installs, 1 update, 0 removals, 3 skipped

  - Installing certifi (2024.8.30): Pending...
Checking if keyring is available
  - Installing charset-normalizer (3.4.0): Pending...
  - Installing frozenlist (1.4.1): Pending...
  - Installing idna (3.10): Pending...
  - Installing multidict (6.1.0): Pending...
  - Installing nvidia-nvjitlink-cu12 (12.4.127): Pending...
  - Installing propcache (0.2.0): Pending...
  - Installing urllib3 (2.2.3): Pending...
[keyring:keyring.backend] Loading KWallet
[keyring:keyring.backend] Loading SecretService
[keyring:keyring.backend] Loading Windows
[keyring:keyring.backend] Loading chainer
[keyring:keyring.backend] Loading libsecret
[keyring:keyring.backend] Loading macOS
Using keyring backend 'SecretService Keyring'

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Fixed initialization issue in SentenceTransformerPatched where super().init() caused incorrect configuration due to class name checks in parent class.

  • Implements workaround in libs/infinity_emb/infinity_emb/transformer/embedder/sentence_transformer.py by creating temporary SentenceTransformer instance and copying state
  • Resolves warning 'No sentence-transformers model found' by avoiding direct super() call that triggered name-based config checks
  • Potentially introduces future maintenance challenges due to state copying approach
  • Tests are currently incomplete due to poetry dependency installation issues

1 file reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

Comment on lines +72 to 80
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant