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

[enh] Add Support for multiple adapters on Transformers-based models #2993

Closed
wants to merge 2 commits into from

Conversation

carlesonielfa
Copy link
Contributor

@carlesonielfa carlesonielfa commented Oct 17, 2024

Hi!

Pull Request overview

Adds support for loading, activating and deactivating adapters for a SentenceTransformer model when its underlying model is Transformers-based.

Details

Adds a wrapper mixin so that the methods to manipulate adapters are exposed on the SentenceTransformer model. It includes checks to ensure that the underlying model supports it.

I wanted to implement it in a manner that won't break or require maintenance if the transformers library changes the signature for these methods, this however compromises typing and hints, so if that is more of a concern than maintenance, the implementation could be changed to a more hard-coded one.

As it is only a wrapper, the functionality follows that documented on the hf docs for transformers .

Usage example

Pre-trained adapter

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("my_org/mymodel")
model.load_adapter("my_org/my_pretrained_adapter", "my_adapter")

Adapter from scratch for training purposes

from sentence_transformers import SentenceTransformer
from peft import LoraConfig, TaskType

model = SentenceTransformer("my_org/mymodel")

peft_config = LoraConfig(
    target_modules=["query", "key", "value"],
    task_type=TaskType.FEATURE_EXTRACTION,
    inference_mode=False,
    r=8,
    lora_alpha=32,
    lora_dropout=0.1
)
model.add_adapter(peft_config)

Additional considerations

  • Consider incorporating adapter switching to the SentenceTransformer.encode function
  • Test with a pretrained model is commented out on this PR waiting for feedback from a maintainer on if an adapter model should be uploaded to the sentence-transformers hf hub
  • Might be worth including documentation about the feature

@carlesonielfa carlesonielfa marked this pull request as ready for review October 17, 2024 09:52
@carlesonielfa carlesonielfa changed the title Add Support for multiple adapters on Transformers-based models [enh] Add Support for multiple adapters on Transformers-based models Oct 17, 2024
@tomaarsen
Copy link
Collaborator

Superseded by #3046.
Thanks a bunch for tackling this, and apologies for the delay. I usually tackle PRs that look "almost ready" directly before I bring out a release (which will be soon now!)

  • Tom Aarsen

@tomaarsen tomaarsen closed this Nov 8, 2024
@tomaarsen
Copy link
Collaborator

I've pushed back the release from today to Monday, should be a bit safer.

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.

2 participants