-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
[BartTokenizer] add prepare s2s batch #6212
Merged
sshleifer
merged 11 commits into
huggingface:master
from
patil-suraj:bart-tok-s2s-batch
Aug 17, 2020
+179
−1
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4448ce6
add prepare s2s batch
patil-suraj baf4018
doc nit
patil-suraj 0301dfd
better docs
patil-suraj 24736dd
fix doc indent
patil-suraj ad1b918
Fix indentation
sgugger f0c3039
better doc
patil-suraj 7084cf0
Merge branch 'bart-tok-s2s-batch' of https://github.com/patil-suraj/t…
patil-suraj fdf93d9
typo
patil-suraj e1a7942
Merge branch 'bart-tok-s2s-batch' of https://github.com/patil-suraj/t…
patil-suraj ebd603e
fix docs
patil-suraj b05ee8f
fix docs
patil-suraj 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 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 |
---|---|---|
|
@@ -45,6 +45,103 @@ class BartTokenizer(RobertaTokenizer): | |
"merges_file": {m: merges_url for m in _all_bart_models}, | ||
} | ||
|
||
def prepare_seq2seq_batch( | ||
self, | ||
src_texts: List[str], | ||
tgt_texts: Optional[List[str]] = None, | ||
max_length: Optional[int] = None, | ||
max_target_length: Optional[int] = None, | ||
padding: str = "longest", | ||
return_tensors: str = "None", | ||
truncation=True, | ||
**kwargs, | ||
) -> BatchEncoding: | ||
r""" | ||
Prepare a batch that can be passed directly to an instance of :class:`~transformers.BartModel`. | ||
|
||
Args: | ||
src_texts: (:obj:`list`): | ||
list of documents to summarize or source language texts | ||
tgt_texts: (:obj:`list`, `optional`): | ||
list of tgt language texts or summaries. | ||
max_length (:obj:`int`, `optional`): | ||
Controls the maximum length for encoder inputs (documents to summarize or source language texts) | ||
If left unset or set to :obj:`None`, this will use the predefined model maximum length if a maximum | ||
length is required by one of the truncation/padding parameters. If the model has no specific maximum | ||
input length (like XLNet) truncation/padding to a maximum length will be deactivated. | ||
max_target_length (:obj:`int`, `optional`): | ||
Controls the maximum length of decoder inputs (target language texts or summaries) | ||
If left unset or set to :obj:`None`, this will use the max_length value. | ||
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`False`): | ||
Activates and controls padding. Accepts the following values: | ||
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a | ||
single sequence if provided). | ||
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the | ||
maximum acceptable input length for the model if that argument is not provided. | ||
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of | ||
different lengths). | ||
return_tensors (:obj:`str` or :class:`~transformers.tokenization_utils_base.TensorType`, `optional`, defaults to "pt"): | ||
If set, will return tensors instead of list of python integers. Acceptable values are: | ||
* :obj:`'tf'`: Return TensorFlow :obj:`tf.constant` objects. | ||
* :obj:`'pt'`: Return PyTorch :obj:`torch.Tensor` objects. | ||
* :obj:`'np'`: Return Numpy :obj:`np.ndarray` objects. | ||
truncation (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.TruncationStrategy`, `optional`, defaults to :obj:`True`): | ||
Activates and controls truncation. Accepts the following values: | ||
* :obj:`True` or :obj:`'longest_first'`: Truncate to a maximum length specified with the argument | ||
:obj:`max_length` or to the maximum acceptable input length for the model if that argument is not | ||
provided. This will truncate token by token, removing a token from the longest sequence in the pair | ||
if a pair of sequences (or a batch of pairs) is provided. | ||
* :obj:`'only_first'`: Truncate to a maximum length specified with the argument :obj:`max_length` or to | ||
the maximum acceptable input length for the model if that argument is not provided. This will only | ||
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. | ||
* :obj:`'only_second'`: Truncate to a maximum length specified with the argument :obj:`max_length` or | ||
to the maximum acceptable input length for the model if that argument is not provided. This will only | ||
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. | ||
* :obj:`False` or :obj:`'do_not_truncate'` (default): No truncation (i.e., can output batch with | ||
sequence lengths greater than the model maximum admissible input size). | ||
**kwargs: | ||
Additional keyword arguments passed along to :obj:`self.__call__`. | ||
Returns: | ||
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. There is a new docstring on master/ tokenization_utils_base.py that you may want to (a) reuse or (b) modify. |
||
:class:`~transformers.BatchEncoding`: A :class:`~transformers.BatchEncoding` with the following fields: | ||
- **input_ids** -- List of token ids to be fed to the encoder. | ||
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model. | ||
- **decoder_input_ids** -- List of token ids to be fed to the decoder. | ||
- **decoder_attention_mask** -- List of indices specifying which tokens should be attended to by the decoder. | ||
This does not include causal mask, which is built by the model. | ||
|
||
The full set of keys ``[input_ids, attention_mask, decoder_input_ids, decoder_attention_mask]``, | ||
will only be returned if tgt_texts is passed. Otherwise, input_ids, attention_mask will be the only keys. | ||
""" | ||
if max_length is None: | ||
max_length = self.model_max_length | ||
model_inputs: BatchEncoding = self( | ||
src_texts, | ||
add_special_tokens=True, | ||
return_tensors=return_tensors, | ||
max_length=max_length, | ||
padding=padding, | ||
truncation=truncation, | ||
**kwargs, | ||
) | ||
if tgt_texts is None: | ||
return model_inputs | ||
# Process tgt_texts | ||
if max_target_length is None: | ||
max_target_length = max_length | ||
decoder_inputs: BatchEncoding = self( | ||
tgt_texts, | ||
add_special_tokens=True, | ||
return_tensors=return_tensors, | ||
padding=padding, | ||
max_length=max_target_length, | ||
truncation=truncation, | ||
**kwargs, | ||
) | ||
for k, v in decoder_inputs.items(): | ||
model_inputs[f"decoder_{k}"] = v | ||
|
||
return model_inputs | ||
|
||
|
||
class BartTokenizerFast(RobertaTokenizerFast): | ||
# merges and vocab same as Roberta | ||
|
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
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.
The type annotations here were better before. The docstrings should not have abbreviations (and start with a capital and end with a full stop nit).
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.
Aah, I blindly copy pasted, will make the changes. Also can you tell me where the doc error is coming from ?
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.
You're missing new lines before your lists I'd say.