-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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 BlenderbotTokenizerFast
#13720
Merged
LysandreJik
merged 19 commits into
huggingface:master
from
stancld:blenderbot_fast_tokenizer
Oct 29, 2021
Merged
Add BlenderbotTokenizerFast
#13720
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a02b805
Add the support for the fast (rust) implementation of BlenbderbotToke…
stancld df77310
Merge branch 'master' into blenderbot_fast_tokenizer
stancld 4d8702c
Fix a converter and a typo in a doc
stancld 53f36ba
Merge branch 'master' into blenderbot_fast_tokenizer
stancld f3cc692
Merge branch 'master' into blenderbot_fast_tokenizer
stancld b27b247
Merge branch 'master' into blenderbot_fast_tokenizer
stancld 3a9cb48
Apply the patil-suraj's suggestion
stancld 12d6ae0
(Nitpick) Fast tokenization -> Fast Tokenization in doc
stancld 4d78b39
Merge branch 'master' into blenderbot_fast_tokenizer
stancld f24e104
Apply the SaulLu's suggestion
stancld 918bf93
Merge branch 'master' into blenderbot_fast_tokenizer
stancld 025432b
Apply Narsil's suggestion to fix test pipelines
stancld d3d4b1a
Add encoder_no_repeat_ngram_size according to the Narsil's suggestion
stancld aa0cb70
Revert the last (unnecessary) commit
stancld 423fe52
Merge branch 'master' into blenderbot_fast_tokenizer
stancld be988fb
Override pipeline config for Blenderbot to allow for larger pos. emb.
stancld ee3c72e
Merge branch 'master' into blenderbot_fast_tokenizer
stancld a393420
make fix-copies
stancld a40b52d
Merge branch 'master' into blenderbot_fast_tokenizer
stancld 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
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
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
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
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
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
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
96 changes: 96 additions & 0 deletions
96
src/transformers/models/blenderbot/tokenization_blenderbot_fast.py
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# coding=utf-8 | ||
# Copyright 2021 The Facebook Inc. and The HuggingFace Inc. team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Fast Tokenization class for Blenderbot.""" | ||
|
||
from typing import TYPE_CHECKING, List, Optional | ||
|
||
from ...utils import logging | ||
from ..roberta.tokenization_roberta_fast import RobertaTokenizerFast | ||
from .tokenization_blenderbot import BlenderbotTokenizer | ||
|
||
|
||
if TYPE_CHECKING: | ||
from transformers.pipelines.conversational import Conversation | ||
|
||
logger = logging.get_logger(__name__) | ||
|
||
|
||
VOCAB_FILES_NAMES = { | ||
"vocab_file": "vocab.json", | ||
"merges_file": "merges.txt", | ||
"tokenizer_config_file": "tokenizer_config.json", | ||
} | ||
|
||
PRETRAINED_VOCAB_FILES_MAP = { | ||
"vocab_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json"}, | ||
"merges_file": {"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt"}, | ||
"tokenizer_config_file": { | ||
"facebook/blenderbot-3B": "https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json" | ||
}, | ||
} | ||
|
||
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {"facebook/blenderbot-3B": 128} | ||
|
||
|
||
class BlenderbotTokenizerFast(RobertaTokenizerFast): | ||
r""" | ||
Construct a "fast" Blenderbot tokenizer (backed by HuggingFace's `tokenizers` library). | ||
|
||
:class:`~transformers.BlenderbotFast` is nearly identical to :class:`~transformers.RobertaTokenizerFast` and runs | ||
end-to-end tokenization: punctuation splitting and wordpiece. The only difference is that it doesn't add BOS token | ||
to the beginning of sequences. | ||
|
||
Refer to superclass :class:`~transformers.RobertaTokenizerFast` for usage examples and documentation concerning | ||
parameters. | ||
""" | ||
vocab_files_names = VOCAB_FILES_NAMES | ||
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP | ||
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES | ||
slow_tokenizer_class = BlenderbotTokenizer | ||
|
||
def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None): | ||
""" | ||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and | ||
adding special tokens. A Blenderbot sequence has the following format: | ||
|
||
- single sequence: `` X </s>`` | ||
|
||
Args: | ||
token_ids_0 (:obj:`List[int]`): | ||
List of IDs to which the special tokens will be added | ||
token_ids_1 (:obj:`List[int]`, `optional`): | ||
Will be ignored | ||
|
||
Returns: | ||
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens. | ||
""" | ||
return token_ids_0 + [self.eos_token_id] | ||
|
||
def _build_conversation_input_ids(self, conversation: "Conversation") -> List[int]: | ||
inputs = [] | ||
for is_user, text in conversation.iter_texts(): | ||
if is_user: | ||
# We need to space prefix as it's being done within blenderbot | ||
inputs.append(" " + text) | ||
else: | ||
# Generated responses should contain them already. | ||
inputs.append(text) | ||
|
||
full_string = " ".join(inputs) | ||
input_ids = self.encode(full_string) | ||
if len(input_ids) > self.model_max_length: | ||
input_ids = input_ids[-self.model_max_length :] | ||
logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens.") | ||
return input_ids |
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
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
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
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.
(nit) maybe call this
fast_tokenizer
for consistancyThere 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.
In test files, it seems to me the
rust_tokenizer
naming is used instead offast_tokenizer
. However, I can change it here :)