-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Doctest longformer #16441
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
Doctest longformer #16441
Changes from 4 commits
1f14e25
996d032
b240fba
c4b9a25
9c811f7
3926af7
34809da
2da3790
ca8f6dc
c53fcc2
2858d81
f5f5040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1407,6 +1407,28 @@ def _set_gradient_checkpointing(self, module, value=False): | |
| module.gradient_checkpointing = value | ||
|
|
||
|
|
||
| LONGFORMER_GENERATION_DOCSTRING = r""" | ||
| Mask filling example: | ||
|
|
||
| ```python | ||
| >>> from transformers import LongformerTokenizer, LongformerForMaskedLM | ||
|
|
||
| >>> tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096") | ||
| >>> model = LongformerForMaskedLM.from_pretrained("allenai/longformer-base-4096") | ||
|
|
||
| >>> TXT = "My friends are <mask> but they eat too many carbs." + " That's why I decide not to eat with them." * 300 | ||
| >>> input_ids = tokenizer([TXT], return_tensors="pt")["input_ids"] | ||
| >>> logits = model(input_ids).logits | ||
|
|
||
| >>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item() | ||
| >>> probs = logits[0, masked_index].softmax(dim=0) | ||
| >>> values, predictions = probs.topk(5) | ||
|
|
||
| >>> tokenizer.decode(predictions).split() | ||
| ['healthy', 'skinny', 'thin', 'good', 'vegetarian'] | ||
| ``` | ||
| """ | ||
|
|
||
| LONGFORMER_START_DOCSTRING = r""" | ||
|
|
||
| This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the | ||
|
|
@@ -1852,9 +1874,11 @@ def __init__(self, config): | |
| @add_start_docstrings_to_model_forward(LONGFORMER_INPUTS_DOCSTRING.format("batch_size, sequence_length")) | ||
| @add_code_sample_docstrings( | ||
| processor_class=_TOKENIZER_FOR_DOC, | ||
| checkpoint=_CHECKPOINT_FOR_DOC, | ||
| checkpoint="jpelhaw/longformer-base-plagiarism-detection", | ||
| output_type=LongformerSequenceClassifierOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| expected_output=[1, 2], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KMFODA Is this the expected output? I think it should be something like |
||
| expected_loss=0.08, | ||
| ) | ||
| def forward( | ||
| self, | ||
|
|
@@ -2118,9 +2142,11 @@ def __init__(self, config): | |
| @add_start_docstrings_to_model_forward(LONGFORMER_INPUTS_DOCSTRING.format("batch_size, sequence_length")) | ||
| @add_code_sample_docstrings( | ||
| processor_class=_TOKENIZER_FOR_DOC, | ||
| checkpoint=_CHECKPOINT_FOR_DOC, | ||
| checkpoint="brad1141/Longformer-finetuned-norm", | ||
| output_type=LongformerTokenClassifierOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| expected_output="['Lead', 'Evidence', 'Lead', 'Evidence', 'Lead']", | ||
| expected_loss=0.01, | ||
| ) | ||
| def forward( | ||
| self, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2081,10 +2081,12 @@ def get_prefix_bias_name(self): | |
| @add_start_docstrings_to_model_forward(LONGFORMER_INPUTS_DOCSTRING.format("batch_size, sequence_length")) | ||
| @add_code_sample_docstrings( | ||
| processor_class=_TOKENIZER_FOR_DOC, | ||
| checkpoint=_CHECKPOINT_FOR_DOC, | ||
| checkpoint="saibo/legal-longformer-base-4096", | ||
|
KMFODA marked this conversation as resolved.
Outdated
|
||
| output_type=TFLongformerMaskedLMOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| mask="<mask>", | ||
| expected_output="' no'", | ||
| expected_loss=4.5, | ||
| ) | ||
| def call( | ||
| self, | ||
|
|
@@ -2178,6 +2180,8 @@ def __init__(self, config, *inputs, **kwargs): | |
| checkpoint="allenai/longformer-large-4096-finetuned-triviaqa", | ||
| output_type=TFLongformerQuestionAnsweringModelOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| expected_output="' puppet'", | ||
| expected_loss=0.96, | ||
| ) | ||
| def call( | ||
| self, | ||
|
|
@@ -2325,6 +2329,8 @@ def __init__(self, config, *inputs, **kwargs): | |
| checkpoint=_CHECKPOINT_FOR_DOC, | ||
| output_type=TFLongformerSequenceClassifierOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| expected_output="LABEL_0", | ||
| expected_loss=0.58, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the checkpoint
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct! |
||
| ) | ||
| def call( | ||
| self, | ||
|
|
@@ -2565,6 +2571,8 @@ def __init__(self, config, *inputs, **kwargs): | |
| checkpoint=_CHECKPOINT_FOR_DOC, | ||
| output_type=TFLongformerTokenClassifierOutput, | ||
| config_class=_CONFIG_FOR_DOC, | ||
| expected_output="['LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_1', 'LABEL_1', 'LABEL_0', 'LABEL_0', 'LABEL_0', 'LABEL_0']", | ||
| expected_loss=0.62, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as I give for sequence classification model. |
||
| ) | ||
| def call( | ||
| self, | ||
|
|
||
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.
This example looks super cool - could you replace it with what's currently written for
LongformerForMaskedLM?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.
Agree with @patrickvonplaten
A few more comments:
No more need for
LONGFORMER_GENERATION_DOCSTRING, but moving the docstring example toLongformerForMaskedLMdirectlyI think it would be great to add a comment before the line containing
* 300, sayLet's try a very long input.