Adding Adapter Methods¶
This document describes how different efficient fine-tuning methods can be integrated into the codebase of
It can be used as a guide to add new efficient fine-tuning/ adapter methods.
Before we start to go into implementation details, first some important design philosophies of
Adapters should integrate seamlessly with existing model classes: This means (a) if a model architecture supports adapters, it should be possible to use them with all model classes of this architecture and (b) adapters should be entirely opt-in, i.e. the model classes still must work without adapters.
Changes to the original should be minimal:
adapter-transformerstries to avoid changes to the original HF code as far as possible. We extensively use Python mixins to achieve this.
Now we highlight the most important components of integrating adapter methods into Transformer models. Each integration is highly dependent on the specific details of the adapter methods. Therefore, the described steps might not be applicable to each implementation.
❓ As adapter methods typically inject blocks of new parameters into an existing Transformer model, they mostly can be implemented using multiple blocks of classes deriving from
These module classes then have to be inserted into the correct locations within the Transformer model implementation.
Thus, each adapter method implementation at least should provide two classes:
a configuration class deriving from
AdapterConfigBasethat provides attributes for all configuration options of the method
a module class deriving from the abstract
AdapterLayerBasethat provides the method parameters and a set of standard adapter management functions
All configuration classes reside in
src/transformers/adapters/configuration.py. To add a new configuration class for a new method, create a new subclass of
AdapterConfigBase. Make sure to set the
architectureattribute in your class.
Finally, also make sure the config class is added to the
AdapterLayerBaseclass from which any new adapter modules should derive resides in
This abstract base class defines a set of methods that should be implemented by each deriving class, including methods for adding, enabling and deleting adapter weights.
Most importantly, the module classes deriving from this base class should implement the forward pass through an adaptation component.
The concrete implementation of these classes heavily depends on the specifics of the adapter method. For a reference implementation, have a look at
AdapterLayerfor bottleneck adapters.
To actually make use of the newly implemented classes, it’s finally necessary to integrate the forward calls to the modules in the actual model implementations.
This, again, is highly dependent on how the adapter method interacts with the base model classes Typically, module classes can be integrated either via mixins (see
src/transformers/adapters/mixins) or directly as submodules of the respective model components.
The model class integration has to be repeated for each supported Transformer model, as they typically don’t share a codebase. Please try to integrate any new adapter method into every model class when it’s reasonable. You can find all currently supported model classes at https://docs.adapterhub.ml/model_overview.html.
Additional things to consider
New adapter methods typically also require some changes in the
src/transformers/adapters/loading.py(also see here).
Depending on the method to be integrated, further changes in other classes might be necessary.
adapter-transformers provides a framework for testing adapter methods on implementing models in
Tests for each adapter method are provided via a mixin class.
All test mixins derive from the common
AdapterMethodBaseTestMixin class and reside in
Add a new
This module should contain a
<method>TestMixinclass deriving from
AdapterMethodBaseTestMixinthat implements typical methods of adding, loading and training modules of the new adapter method.
Have a look at existing test mixins for reference.
Next, add the newly implemented test mixin to the tests of all model types that support the new adapter method.
Each model type has its own test class
tests_adapters/test_<model_type>.pythat contains a
<model_type>AdapterTestclass. Add the new test mixin to the mixins of this class. E.g., if the new method is supported by BERT, add the its test mixin to
❓ The documentation for
adapter-transformers lives in the
Add the class documentation for the configuration class of the new method in
adapter_docs/overview.md, add a new section for the new adapter method that describes the most important concepts. Please try to follow the general format of the existing methods.
Add a new column in the table in
adapter_docs/model_overview.mdand check the models that support the new adapter method.
Finally, please add a row for the new method in the table of supported methods under Implemented Methods in the main
README.md of this repository.
Training Example Adapters¶
❓ To make sure the new adapter implementation works properly, it is useful to train some example adapters and compare the training results to full model fine-tuning and/or reference implementations. Ideally, this would include training adapters on one (or more) tasks that are good for demonstrating the new method and uploading them to AdapterHub.
HuggingFace already provides example training scripts for many tasks, some of them have already been modified to support adapter training (see https://github.com/Adapter-Hub/adapter-transformers/tree/master/examples).