-
Notifications
You must be signed in to change notification settings - Fork 346
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
Add AdapterSetup
context manager
#257
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hSterz
reviewed
Dec 7, 2021
This reverts commit 4759a8f.
calpt
force-pushed
the
dev/adapter_context
branch
from
January 6, 2022 21:35
de37653
to
2f9e3f9
Compare
hSterz
approved these changes
Jan 14, 2022
calpt
added a commit
that referenced
this pull request
Feb 2, 2022
calpt
added a commit
that referenced
this pull request
Feb 9, 2022
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
All adapter-supporting model classes currently provide an
adapter_names
parameter in theirforward()
call that allows dynamic & state-less configuration of the activated adapters. We tried to remove this parameter with the release of v2.0.0, however it turned out to be essential for a multi-threaded inference use-case (see #165 for context). The removal was therefore reverted shortly after (#176; v.2.0.1).Problem
The
adapter_names
parameter has to be passed down all module classes to the actual adapter layers. This means that the method headers of all top-level and intermediate module classes need to be modified which makes maintanance difficult & prevents further disentangling AH & HF.Suggested solution
As a replacement to the
adapter_names
parameter, this PR introduces a newAdapterSetup
class. This class is intended to be used as a context manager, i.e. a typical use case would look like this:Note that in the above example no adapters are activated via
active_adapters
. Within thewith
block, the adapter implementation will read the currently active setup from the context manager.This solution allows dynamic adapter activation, e.g. also with nesting:
Most importantly, the context manager is thread-local, i.e. we can use different setups in different threads with the same model instance. There's a test for this:
https://github.com/calpt/adapter-transformers/blob/93065b74ae933b34ec5c3f4047223792a795239f/tests/test_adapter_setup_context.py#L75-L112
Breaking changes
adapter_names
parameter is removed for all model classes