Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ def __init__(self, *, engine_args=EngineArgs):
if ls.loading_dtype is not None:
model_kwargs["torch_dtype"] = ls.loading_dtype

super().__init__(
engine_args.model_name_or_path,
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)
Comment on lines +72 to 80
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)

# make a copy of the tokenizer,
# to be able to could the tokens in another thread
Expand Down