-
Notifications
You must be signed in to change notification settings - Fork 6
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 support for replacing listeners #7
Merged
danieldk
merged 11 commits into
explosion:main
from
shadeMe:feature/replace-listeners-support
Jul 11, 2023
Merged
Add support for replacing listeners #7
danieldk
merged 11 commits into
explosion:main
from
shadeMe:feature/replace-listeners-support
Jul 11, 2023
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
Multiple changes were required to facilitate this: * All transformer model entrypoints now have a `wrapped_listener` optional parameter. This parameter is only meant to be used by the machinery that performs the listener replacement. * Listeners are no longer subclasses of the `TransformerListener` class. Previously, the `TransformerListener` class subclassed `Model` and stored some state as instance attributes. To perform the replacement, the original listener instance in the downstream component needs to be (deep)copied. However, the implementation of `Model.copy` doesn't support classes that subclass `Model` - it merely performs deepcopies of the different `Model` instance attributes and initializes a new `Model` instance with them. What this results in is the loss of any state that was directly stored on the listener instance such as `upstream_name`, `name`, etc. To workaround this limitation, all listener state is now directly stored in `Model.attrs`. This ensures that no persistent state is lost between copies. * A new `WrappedTransformerAndListener` class has been introduced to be used as the replacement model for the downstream component's original listener. This wraps the upstream transformer pipe's model and the original listener. During training and prediction, it calls the wrapped transformer and direcly passes the outputs to the wrapped listener. Gradients are additionally allocated during training, and during prediction, the wrapped listener is instructed to ignore any transformer annotations present on the `Doc`s and use the ones directly stored in the listener.
3 tasks
danieldk
reviewed
Jul 10, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A first bunch of comments, I probably need to go over this another time.
danieldk
reviewed
Jul 10, 2023
Inline listener construction code and remove classes
danieldk
approved these changes
Jul 11, 2023
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.
Description
Multiple changes were required to facilitate this:
All transformer model entrypoints now have a
wrapped_listener
optional parameter. This parameter is only meant to be used by the machinery that performs the listener replacement.Listeners are no longer subclasses of the
TransformerListener
class. Previously, theTransformerListener
class subclassedModel
and stored some state as instance attributes. To perform the replacement, the original listener instance in the downstream component needs to be (deep)copied. However, the implementation ofModel.copy
doesn't support classes that subclassModel
- it merely performs deepcopies of the differentModel
instance attributes and initializes a newModel
instance with them. What this results in is the loss of any state that was directly stored on the listener instance such asupstream_name
,name
, etc.To workaround this limitation, all listener state is now directly stored in
Model.attrs
. This ensures that no persistent state is lost between copies.A new
WrappedTransformerAndListener
class has been introduced to be used as the replacement model for the downstream component's original listener. This wraps the upstream transformer pipe's model and the original listener. During training and prediction, it calls the wrapped transformer and directly passes the outputs to the wrapped listener. Gradients are additionally allocated during training, and during prediction, the wrapped listener is instructed to ignore any transformer annotations present on theDoc
s and use the ones directly stored in the listener.This PR depends on the following:
Language.replace_listeners
: Pass the replaced listener and thetok2vec
pipe to the callback spaCy#12785copy
/deepcopy
+SentencePieceProcessor
deserialization bugfix curated-tokenizers#44Types of change
(Cursed) enhancement
Checklist