adapters icon indicating copy to clipboard operation
adapters copied to clipboard

Is it possible to deactivate or swap between adapters within a single model?

Open ellenzchang opened this issue 4 years ago • 1 comments

Hello! I wish to use a single pre-trained model (GPT-2) with adapters, but swap between different adapters for different tasks.

For instance, an adapter has been trained on generating dialogues for a child, while a different adapter has been trained on generating dialogues for an adult. Instead of having two model instances, each with their own adapter, swapping between adapters within a single model is what I wish to do.

Is this possible in the current version of adapter-transformers?

(I am using GPT2ModelWithHeads as the base model)

ellenzchang avatar Mar 11 '22 13:03 ellenzchang

Hi @ellenzchang! Yes, this is totally possible!

Let's assume you have trained two adapters, one for each task, using GPT2ModelWithHeads. E.g., they might be trained using this setup:

# add adapter
model.add_adapter("dialogue_child")
# add matching prediction head (might need customization depending on needs)
model.add_causal_lm_head("dialogue_child")
# activate and enable both for training
model.train_adapter("dialogue_child")
# regular model training loop ...

You can load both adapters from their training checkpoints into the same model:

model.load_adapter("path/to/first", load_as="dialogue_child")
model.load_adapter("path/to/second", load_as="dialogue_adult")

Now, you can easily switch between both:

# activate first adapter & head
model.active_adapters = "dialogue_child"
# forward pass ...
# activate second adapter & head
model.active_adapters = "dialogue_adult"
# forward pass ...

Hope this helps!

calpt avatar Mar 15 '22 14:03 calpt