Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Save custom module kwargs if specified #3112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,11 @@ def save(
# For other cases, we want to add the class name:
elif not class_ref.startswith("sentence_transformers."):
class_ref = f"{class_ref}.{type(module).__name__}"
modules_config.append({"idx": idx, "name": name, "path": os.path.basename(model_path), "type": class_ref})

module_config = {"idx": idx, "name": name, "path": os.path.basename(model_path), "type": class_ref}
if self.module_kwargs and name in self.module_kwargs and (module_kwargs := self.module_kwargs[name]):
module_config["kwargs"] = module_kwargs
modules_config.append(module_config)

with open(os.path.join(path, "modules.json"), "w") as fOut:
json.dump(modules_config, fOut, indent=2)
Expand Down Expand Up @@ -1550,7 +1554,7 @@ def _load_module_class_from_ref(
if class_ref.startswith("sentence_transformers."):
return import_from_string(class_ref)

if trust_remote_code:
if trust_remote_code or os.path.exists(model_name_or_path):
code_revision = model_kwargs.pop("code_revision", None) if model_kwargs else None
try:
return get_class_from_dynamic_module(
Expand Down
Loading