This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Transformer toolkit updates #5270
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4626957
Fix duplicate line
dirkgr 668022a
Easy access to the output dimension of an activation layer
dirkgr 3194b2d
Take an ignore an attention mask in TransformerEmbeddings
dirkgr ad41685
Make it so a pooler can be derived from a huggingface module
dirkgr 7c45761
Pooler that can load from a transformer module
dirkgr 67041cb
Changelog
dirkgr b684bf0
Update transformer_embeddings.py
AkshitaB 0df423e
Productivity through formatting
dirkgr 77d189b
Don't break positional arguments
dirkgr 2aa1b72
Merge branch 'AkshitaB-patch-1' into TransformerToolkitUpdates
dirkgr 453d645
Some mode module names
dirkgr 32fda86
Remove _get_input_arguments()
dirkgr d847601
Merge branch 'main' into TransformerToolkitUpdates
dirkgr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
from typing import Dict, Optional, Any, Union, TYPE_CHECKING | ||
|
||
import torch | ||
|
||
from allennlp.common import FromParams | ||
from allennlp.modules.transformer.activation_layer import ActivationLayer | ||
|
||
if TYPE_CHECKING: | ||
from transformers.configuration_utils import PretrainedConfig | ||
|
||
|
||
class TransformerPooler(ActivationLayer, FromParams): | ||
|
||
_pretrained_relevant_module = ["pooler", "bert.pooler"] | ||
|
||
def __init__( | ||
self, | ||
hidden_size: int, | ||
intermediate_size: int, | ||
activation: Union[str, torch.nn.Module] = "relu", | ||
): | ||
super().__init__(hidden_size, intermediate_size, "relu", pool=True) | ||
super().__init__(hidden_size, intermediate_size, activation, pool=True) | ||
|
||
@classmethod | ||
def _get_input_arguments( | ||
cls, | ||
pretrained_module: torch.nn.Module, | ||
source: str = "huggingface", | ||
mapping: Optional[Dict[str, str]] = None, | ||
**kwargs, | ||
) -> Dict[str, Any]: | ||
final_kwargs = {} | ||
|
||
final_kwargs["hidden_size"] = pretrained_module.dense.in_features | ||
final_kwargs["intermediate_size"] = pretrained_module.dense.out_features | ||
final_kwargs["activation"] = pretrained_module.activation | ||
|
||
final_kwargs.update(kwargs) | ||
|
||
return final_kwargs | ||
|
||
@classmethod | ||
def _from_config(cls, config: "PretrainedConfig", **kwargs): | ||
return cls(config.hidden_size, config.hidden_size, "tanh") # BERT has this hardcoded |
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.
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.
We don't require this method any longer.
from_config
takes care of what we need.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.
Removed!