Big Bird Fast Tokenizer implementation (#11075)
* Added Big Bird Fast Tokenizer initial file * style fixes * flake fixes * Added big bird fast tokenizer to init files * Added big bird fast to Auto tokenization * fix styles * minor quality fixes * Added initial test code * Fix SpmConverter when precompiled_charsmap doesn't exist * fixed post processor * minor style fix * minor fix input names * Actually fix identity normalization * style * Added token type ids to fast tokenizer * style * flake fix * fix copies Co-authored-by: Anthony MOI <m.anthony.moi@gmail.com>
This commit is contained in:
@@ -276,7 +276,7 @@ Flax), PyTorch, and/or TensorFlow.
|
|||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| Bert Generation | ✅ | ❌ | ✅ | ❌ | ❌ |
|
| Bert Generation | ✅ | ❌ | ✅ | ❌ | ❌ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| BigBird | ✅ | ❌ | ✅ | ❌ | ❌ |
|
| BigBird | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| BigBirdPegasus | ❌ | ❌ | ✅ | ❌ | ❌ |
|
| BigBirdPegasus | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
|
|||||||
@@ -67,6 +67,11 @@ BigBirdTokenizer
|
|||||||
:members: build_inputs_with_special_tokens, get_special_tokens_mask,
|
:members: build_inputs_with_special_tokens, get_special_tokens_mask,
|
||||||
create_token_type_ids_from_sequences, save_vocabulary
|
create_token_type_ids_from_sequences, save_vocabulary
|
||||||
|
|
||||||
|
BigBirdTokenizerFast
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.BigBirdTokenizerFast
|
||||||
|
:members:
|
||||||
|
|
||||||
BigBird specific outputs
|
BigBird specific outputs
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|||||||
@@ -320,6 +320,7 @@ if is_tokenizers_available():
|
|||||||
_import_structure["models.bart"].append("BartTokenizerFast")
|
_import_structure["models.bart"].append("BartTokenizerFast")
|
||||||
_import_structure["models.barthez"].append("BarthezTokenizerFast")
|
_import_structure["models.barthez"].append("BarthezTokenizerFast")
|
||||||
_import_structure["models.bert"].append("BertTokenizerFast")
|
_import_structure["models.bert"].append("BertTokenizerFast")
|
||||||
|
_import_structure["models.big_bird"].append("BigBirdTokenizerFast")
|
||||||
_import_structure["models.camembert"].append("CamembertTokenizerFast")
|
_import_structure["models.camembert"].append("CamembertTokenizerFast")
|
||||||
_import_structure["models.deberta"].append("DebertaTokenizerFast")
|
_import_structure["models.deberta"].append("DebertaTokenizerFast")
|
||||||
_import_structure["models.distilbert"].append("DistilBertTokenizerFast")
|
_import_structure["models.distilbert"].append("DistilBertTokenizerFast")
|
||||||
@@ -1712,6 +1713,7 @@ if TYPE_CHECKING:
|
|||||||
from .models.bart import BartTokenizerFast
|
from .models.bart import BartTokenizerFast
|
||||||
from .models.barthez import BarthezTokenizerFast
|
from .models.barthez import BarthezTokenizerFast
|
||||||
from .models.bert import BertTokenizerFast
|
from .models.bert import BertTokenizerFast
|
||||||
|
from .models.big_bird import BigBirdTokenizerFast
|
||||||
from .models.camembert import CamembertTokenizerFast
|
from .models.camembert import CamembertTokenizerFast
|
||||||
from .models.convbert import ConvBertTokenizerFast
|
from .models.convbert import ConvBertTokenizerFast
|
||||||
from .models.deberta import DebertaTokenizerFast
|
from .models.deberta import DebertaTokenizerFast
|
||||||
|
|||||||
@@ -373,9 +373,12 @@ class SpmConverter(Converter):
|
|||||||
|
|
||||||
def normalizer(self, proto):
|
def normalizer(self, proto):
|
||||||
precompiled_charsmap = proto.normalizer_spec.precompiled_charsmap
|
precompiled_charsmap = proto.normalizer_spec.precompiled_charsmap
|
||||||
return normalizers.Sequence(
|
if not precompiled_charsmap:
|
||||||
[normalizers.Precompiled(precompiled_charsmap), normalizers.Replace(Regex(" {2,}"), " ")]
|
return normalizers.Sequence([normalizers.Replace(Regex(" {2,}"), " ")])
|
||||||
)
|
else:
|
||||||
|
return normalizers.Sequence(
|
||||||
|
[normalizers.Precompiled(precompiled_charsmap), normalizers.Replace(Regex(" {2,}"), " ")]
|
||||||
|
)
|
||||||
|
|
||||||
def pre_tokenizer(self, replacement, add_prefix_space):
|
def pre_tokenizer(self, replacement, add_prefix_space):
|
||||||
return pre_tokenizers.Metaspace(replacement=replacement, add_prefix_space=add_prefix_space)
|
return pre_tokenizers.Metaspace(replacement=replacement, add_prefix_space=add_prefix_space)
|
||||||
@@ -686,11 +689,24 @@ class T5Converter(SpmConverter):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BigBirdConverter(SpmConverter):
|
||||||
|
def post_processor(self):
|
||||||
|
return processors.TemplateProcessing(
|
||||||
|
single="[CLS]:0 $A:0 [SEP]:0",
|
||||||
|
pair="[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1",
|
||||||
|
special_tokens=[
|
||||||
|
("[CLS]", self.original_tokenizer.convert_tokens_to_ids("[CLS]")),
|
||||||
|
("[SEP]", self.original_tokenizer.convert_tokens_to_ids("[SEP]")),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
SLOW_TO_FAST_CONVERTERS = {
|
SLOW_TO_FAST_CONVERTERS = {
|
||||||
"AlbertTokenizer": AlbertConverter,
|
"AlbertTokenizer": AlbertConverter,
|
||||||
"BartTokenizer": RobertaConverter,
|
"BartTokenizer": RobertaConverter,
|
||||||
"BarthezTokenizer": BarthezConverter,
|
"BarthezTokenizer": BarthezConverter,
|
||||||
"BertTokenizer": BertConverter,
|
"BertTokenizer": BertConverter,
|
||||||
|
"BigBirdTokenizer": BigBirdConverter,
|
||||||
"CamembertTokenizer": CamembertConverter,
|
"CamembertTokenizer": CamembertConverter,
|
||||||
"ConvBertTokenizer": BertConverter,
|
"ConvBertTokenizer": BertConverter,
|
||||||
"DebertaTokenizer": DebertaConverter,
|
"DebertaTokenizer": DebertaConverter,
|
||||||
|
|||||||
@@ -157,6 +157,7 @@ if is_tokenizers_available():
|
|||||||
from ..bart.tokenization_bart_fast import BartTokenizerFast
|
from ..bart.tokenization_bart_fast import BartTokenizerFast
|
||||||
from ..barthez.tokenization_barthez_fast import BarthezTokenizerFast
|
from ..barthez.tokenization_barthez_fast import BarthezTokenizerFast
|
||||||
from ..bert.tokenization_bert_fast import BertTokenizerFast
|
from ..bert.tokenization_bert_fast import BertTokenizerFast
|
||||||
|
from ..big_bird.tokenization_big_bird_fast import BigBirdTokenizerFast
|
||||||
from ..camembert.tokenization_camembert_fast import CamembertTokenizerFast
|
from ..camembert.tokenization_camembert_fast import CamembertTokenizerFast
|
||||||
from ..convbert.tokenization_convbert_fast import ConvBertTokenizerFast
|
from ..convbert.tokenization_convbert_fast import ConvBertTokenizerFast
|
||||||
from ..deberta.tokenization_deberta_fast import DebertaTokenizerFast
|
from ..deberta.tokenization_deberta_fast import DebertaTokenizerFast
|
||||||
@@ -190,6 +191,7 @@ else:
|
|||||||
BartTokenizerFast = None
|
BartTokenizerFast = None
|
||||||
BarthezTokenizerFast = None
|
BarthezTokenizerFast = None
|
||||||
BertTokenizerFast = None
|
BertTokenizerFast = None
|
||||||
|
BigBirdTokenizerFast = None
|
||||||
CamembertTokenizerFast = None
|
CamembertTokenizerFast = None
|
||||||
ConvBertTokenizerFast = None
|
ConvBertTokenizerFast = None
|
||||||
DebertaTokenizerFast = None
|
DebertaTokenizerFast = None
|
||||||
@@ -268,7 +270,7 @@ TOKENIZER_MAPPING = OrderedDict(
|
|||||||
(TapasConfig, (TapasTokenizer, None)),
|
(TapasConfig, (TapasTokenizer, None)),
|
||||||
(LEDConfig, (LEDTokenizer, LEDTokenizerFast)),
|
(LEDConfig, (LEDTokenizer, LEDTokenizerFast)),
|
||||||
(ConvBertConfig, (ConvBertTokenizer, ConvBertTokenizerFast)),
|
(ConvBertConfig, (ConvBertTokenizer, ConvBertTokenizerFast)),
|
||||||
(BigBirdConfig, (BigBirdTokenizer, None)),
|
(BigBirdConfig, (BigBirdTokenizer, BigBirdTokenizerFast)),
|
||||||
(IBertConfig, (RobertaTokenizer, RobertaTokenizerFast)),
|
(IBertConfig, (RobertaTokenizer, RobertaTokenizerFast)),
|
||||||
(Wav2Vec2Config, (Wav2Vec2CTCTokenizer, None)),
|
(Wav2Vec2Config, (Wav2Vec2CTCTokenizer, None)),
|
||||||
(GPTNeoConfig, (GPT2Tokenizer, GPT2TokenizerFast)),
|
(GPTNeoConfig, (GPT2Tokenizer, GPT2TokenizerFast)),
|
||||||
|
|||||||
@@ -17,14 +17,25 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from ...file_utils import _BaseLazyModule, is_torch_available
|
from ...file_utils import (
|
||||||
|
_BaseLazyModule,
|
||||||
|
is_sentencepiece_available,
|
||||||
|
is_tf_available,
|
||||||
|
is_tokenizers_available,
|
||||||
|
is_torch_available,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
_import_structure = {
|
_import_structure = {
|
||||||
"configuration_big_bird": ["BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP", "BigBirdConfig"],
|
"configuration_big_bird": ["BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP", "BigBirdConfig"],
|
||||||
"tokenization_big_bird": ["BigBirdTokenizer"],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if is_sentencepiece_available():
|
||||||
|
_import_structure["tokenization_big_bird"] = ["BigBirdTokenizer"]
|
||||||
|
|
||||||
|
if is_tokenizers_available():
|
||||||
|
_import_structure["tokenization_big_bird_fast"] = ["BigBirdTokenizerFast"]
|
||||||
|
|
||||||
if is_torch_available():
|
if is_torch_available():
|
||||||
_import_structure["modeling_big_bird"] = [
|
_import_structure["modeling_big_bird"] = [
|
||||||
"BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST",
|
"BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||||
@@ -44,7 +55,12 @@ if is_torch_available():
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .configuration_big_bird import BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdConfig
|
from .configuration_big_bird import BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdConfig
|
||||||
from .tokenization_big_bird import BigBirdTokenizer
|
|
||||||
|
if is_sentencepiece_available():
|
||||||
|
from .tokenization_big_bird import BigBirdTokenizer
|
||||||
|
|
||||||
|
if is_tokenizers_available():
|
||||||
|
from .tokenization_big_bird_fast import BigBirdTokenizerFast
|
||||||
|
|
||||||
if is_torch_available():
|
if is_torch_available():
|
||||||
from .modeling_big_bird import (
|
from .modeling_big_bird import (
|
||||||
|
|||||||
240
src/transformers/models/big_bird/tokenization_big_bird_fast.py
Normal file
240
src/transformers/models/big_bird/tokenization_big_bird_fast.py
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Copyright 2018 Google AI, Google Brain and the HuggingFace Inc. team.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
""" Tokenization classes for Big Bird model."""
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
from shutil import copyfile
|
||||||
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
|
from ...file_utils import is_sentencepiece_available
|
||||||
|
from ...tokenization_utils import AddedToken
|
||||||
|
from ...tokenization_utils_fast import PreTrainedTokenizerFast
|
||||||
|
from ...utils import logging
|
||||||
|
|
||||||
|
|
||||||
|
if is_sentencepiece_available():
|
||||||
|
from .tokenization_big_bird import BigBirdTokenizer
|
||||||
|
else:
|
||||||
|
BigBirdTokenizer = None
|
||||||
|
|
||||||
|
logger = logging.get_logger(__name__)
|
||||||
|
VOCAB_FILES_NAMES = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}
|
||||||
|
|
||||||
|
PRETRAINED_VOCAB_FILES_MAP = {
|
||||||
|
"vocab_file": {
|
||||||
|
"google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model",
|
||||||
|
"google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model",
|
||||||
|
"google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model",
|
||||||
|
},
|
||||||
|
"tokenizer_file": {
|
||||||
|
"google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/tokenizer.json",
|
||||||
|
"google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/tokenizer.json",
|
||||||
|
"google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/tokenizer.json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
||||||
|
"google/bigbird-roberta-base": 4096,
|
||||||
|
"google/bigbird-roberta-large": 4096,
|
||||||
|
"google/bigbird-base-trivia-itc": 4096,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SPIECE_UNDERLINE = "▁"
|
||||||
|
|
||||||
|
|
||||||
|
class BigBirdTokenizerFast(PreTrainedTokenizerFast):
|
||||||
|
"""
|
||||||
|
Construct a "fast" BigBird tokenizer (backed by HuggingFace's `tokenizers` library). Based on `Unigram
|
||||||
|
<https://huggingface.co/docs/tokenizers/python/latest/components.html?highlight=unigram#models>`__. This tokenizer
|
||||||
|
inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the main methods. Users should
|
||||||
|
refer to this superclass for more information regarding those methods
|
||||||
|
|
||||||
|
Args:
|
||||||
|
vocab_file (:obj:`str`):
|
||||||
|
`SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a `.spm` extension) that
|
||||||
|
contains the vocabulary necessary to instantiate a tokenizer.
|
||||||
|
bos_token (:obj:`str`, `optional`, defaults to :obj:`"[CLS]"`):
|
||||||
|
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
When building a sequence using special tokens, this is not the token that is used for the beginning of
|
||||||
|
sequence. The token used is the :obj:`cls_token`.
|
||||||
|
eos_token (:obj:`str`, `optional`, defaults to :obj:`"[SEP]"`):
|
||||||
|
The end of sequence token. .. note:: When building a sequence using special tokens, this is not the token
|
||||||
|
that is used for the end of sequence. The token used is the :obj:`sep_token`.
|
||||||
|
unk_token (:obj:`str`, `optional`, defaults to :obj:`"<unk>"`):
|
||||||
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||||
|
token instead.
|
||||||
|
sep_token (:obj:`str`, `optional`, defaults to :obj:`"[SEP]"`):
|
||||||
|
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
|
||||||
|
sequence classification or for a text and a question for question answering. It is also used as the last
|
||||||
|
token of a sequence built with special tokens.
|
||||||
|
pad_token (:obj:`str`, `optional`, defaults to :obj:`"<pad>"`):
|
||||||
|
The token used for padding, for example when batching sequences of different lengths.
|
||||||
|
cls_token (:obj:`str`, `optional`, defaults to :obj:`"[CLS]"`):
|
||||||
|
The classifier token which is used when doing sequence classification (classification of the whole sequence
|
||||||
|
instead of per-token classification). It is the first token of the sequence when built with special tokens.
|
||||||
|
mask_token (:obj:`str`, `optional`, defaults to :obj:`"[MASK]"`):
|
||||||
|
The token used for masking values. This is the token used when training this model with masked language
|
||||||
|
modeling. This is the token which the model will try to predict.
|
||||||
|
"""
|
||||||
|
|
||||||
|
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 = BigBirdTokenizer
|
||||||
|
model_input_names = ["input_ids", "attention_mask"]
|
||||||
|
prefix_tokens: List[int] = []
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
vocab_file,
|
||||||
|
tokenizer_file=None,
|
||||||
|
unk_token="<unk>",
|
||||||
|
bos_token="<s>",
|
||||||
|
eos_token="</s>",
|
||||||
|
pad_token="<pad>",
|
||||||
|
sep_token="[SEP]",
|
||||||
|
mask_token="[MASK]",
|
||||||
|
cls_token="[CLS]",
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
|
bos_token = AddedToken(bos_token, lstrip=False, rstrip=False) if isinstance(bos_token, str) else bos_token
|
||||||
|
eos_token = AddedToken(eos_token, lstrip=False, rstrip=False) if isinstance(eos_token, str) else eos_token
|
||||||
|
unk_token = AddedToken(unk_token, lstrip=False, rstrip=False) if isinstance(unk_token, str) else unk_token
|
||||||
|
pad_token = AddedToken(pad_token, lstrip=False, rstrip=False) if isinstance(pad_token, str) else pad_token
|
||||||
|
cls_token = AddedToken(cls_token, lstrip=False, rstrip=False) if isinstance(cls_token, str) else cls_token
|
||||||
|
sep_token = AddedToken(sep_token, lstrip=False, rstrip=False) if isinstance(sep_token, str) else sep_token
|
||||||
|
|
||||||
|
# Mask token behave like a normal word, i.e. include the space before it
|
||||||
|
mask_token = AddedToken(mask_token, lstrip=True, rstrip=False) if isinstance(mask_token, str) else mask_token
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
vocab_file,
|
||||||
|
tokenizer_file=tokenizer_file,
|
||||||
|
bos_token=bos_token,
|
||||||
|
eos_token=eos_token,
|
||||||
|
unk_token=unk_token,
|
||||||
|
sep_token=sep_token,
|
||||||
|
pad_token=pad_token,
|
||||||
|
cls_token=cls_token,
|
||||||
|
mask_token=mask_token,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.vocab_file = vocab_file
|
||||||
|
|
||||||
|
def build_inputs_with_special_tokens(
|
||||||
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
|
) -> List[int]:
|
||||||
|
"""
|
||||||
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
|
||||||
|
adding special tokens. An BigBird sequence has the following format:
|
||||||
|
|
||||||
|
- single sequence: ``[CLS] X [SEP]``
|
||||||
|
- pair of sequences: ``[CLS] A [SEP] B [SEP]``
|
||||||
|
|
||||||
|
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`, defaults to :obj:`None`):
|
||||||
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`List[int]`: list of `input IDs <../glossary.html#input-ids>`__ with the appropriate special tokens.
|
||||||
|
"""
|
||||||
|
sep = [self.sep_token_id]
|
||||||
|
cls = [self.cls_token_id]
|
||||||
|
if token_ids_1 is None:
|
||||||
|
return cls + token_ids_0 + sep
|
||||||
|
return cls + token_ids_0 + sep + token_ids_1 + sep
|
||||||
|
|
||||||
|
def get_special_tokens_mask(
|
||||||
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
||||||
|
) -> List[int]:
|
||||||
|
"""
|
||||||
|
Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
|
||||||
|
special tokens using the tokenizer ``prepare_for_model`` method.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
token_ids_0 (:obj:`List[int]`):
|
||||||
|
List of ids.
|
||||||
|
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
||||||
|
Optional second list of IDs for sequence pairs.
|
||||||
|
already_has_special_tokens (:obj:`bool`, `optional`, defaults to :obj:`False`):
|
||||||
|
Set to True if the token list is already formatted with special tokens for the model
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if already_has_special_tokens:
|
||||||
|
if token_ids_1 is not None:
|
||||||
|
raise ValueError(
|
||||||
|
"You should not supply a second sequence if the provided sequence of "
|
||||||
|
"ids is already formatted with special tokens for the model."
|
||||||
|
)
|
||||||
|
return list(map(lambda x: 1 if x in [self.sep_token_id, self.cls_token_id] else 0, token_ids_0))
|
||||||
|
|
||||||
|
if token_ids_1 is None:
|
||||||
|
return [1] + ([0] * len(token_ids_0)) + [1]
|
||||||
|
return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1]
|
||||||
|
|
||||||
|
def create_token_type_ids_from_sequences(
|
||||||
|
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||||
|
) -> List[int]:
|
||||||
|
"""
|
||||||
|
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT
|
||||||
|
sequence pair mask has the following format:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
||||||
|
| first sequence | second sequence |
|
||||||
|
|
||||||
|
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
token_ids_0 (:obj:`List[int]`):
|
||||||
|
List of ids.
|
||||||
|
token_ids_1 (:obj:`List[int]`, `optional`, defaults to :obj:`None`):
|
||||||
|
Optional second list of IDs for sequence pairs.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
:obj:`List[int]`: List of `token type IDs <../glossary.html#token-type-ids>`_ according to the given
|
||||||
|
sequence(s).
|
||||||
|
"""
|
||||||
|
sep = [self.sep_token_id]
|
||||||
|
cls = [self.cls_token_id]
|
||||||
|
|
||||||
|
if token_ids_1 is None:
|
||||||
|
return len(cls + token_ids_0 + sep) * [0]
|
||||||
|
return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1]
|
||||||
|
|
||||||
|
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
||||||
|
if not os.path.isdir(save_directory):
|
||||||
|
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
||||||
|
return
|
||||||
|
out_vocab_file = os.path.join(
|
||||||
|
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
||||||
|
)
|
||||||
|
|
||||||
|
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file):
|
||||||
|
copyfile(self.vocab_file, out_vocab_file)
|
||||||
|
|
||||||
|
return (out_vocab_file,)
|
||||||
@@ -38,6 +38,15 @@ class BertTokenizerFast:
|
|||||||
requires_backends(self, ["tokenizers"])
|
requires_backends(self, ["tokenizers"])
|
||||||
|
|
||||||
|
|
||||||
|
class BigBirdTokenizerFast:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["tokenizers"])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["tokenizers"])
|
||||||
|
|
||||||
|
|
||||||
class CamembertTokenizerFast:
|
class CamembertTokenizerFast:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
requires_backends(self, ["tokenizers"])
|
requires_backends(self, ["tokenizers"])
|
||||||
|
|||||||
@@ -17,9 +17,9 @@
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from transformers import BigBirdTokenizer
|
from transformers import BigBirdTokenizer, BigBirdTokenizerFast
|
||||||
from transformers.file_utils import cached_property
|
from transformers.file_utils import cached_property
|
||||||
from transformers.testing_utils import require_sentencepiece, require_torch, slow
|
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow
|
||||||
|
|
||||||
from .test_tokenization_common import TokenizerTesterMixin
|
from .test_tokenization_common import TokenizerTesterMixin
|
||||||
|
|
||||||
@@ -30,9 +30,12 @@ SAMPLE_VOCAB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixture
|
|||||||
|
|
||||||
|
|
||||||
@require_sentencepiece
|
@require_sentencepiece
|
||||||
|
@require_tokenizers
|
||||||
class BigBirdTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
class BigBirdTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
||||||
|
|
||||||
tokenizer_class = BigBirdTokenizer
|
tokenizer_class = BigBirdTokenizer
|
||||||
|
rust_tokenizer_class = BigBirdTokenizerFast
|
||||||
|
test_rust_tokenizer = True
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
@@ -40,6 +43,28 @@ class BigBirdTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
|||||||
tokenizer = BigBirdTokenizer(SAMPLE_VOCAB, keep_accents=True)
|
tokenizer = BigBirdTokenizer(SAMPLE_VOCAB, keep_accents=True)
|
||||||
tokenizer.save_pretrained(self.tmpdirname)
|
tokenizer.save_pretrained(self.tmpdirname)
|
||||||
|
|
||||||
|
def test_rust_and_python_full_tokenizers(self):
|
||||||
|
if not self.test_rust_tokenizer:
|
||||||
|
return
|
||||||
|
|
||||||
|
tokenizer = self.get_tokenizer()
|
||||||
|
rust_tokenizer = self.get_rust_tokenizer()
|
||||||
|
|
||||||
|
sequence = "I was born in 92000, and this is falsé."
|
||||||
|
|
||||||
|
tokens = tokenizer.tokenize(sequence)
|
||||||
|
rust_tokens = rust_tokenizer.tokenize(sequence)
|
||||||
|
self.assertListEqual(tokens, rust_tokens)
|
||||||
|
|
||||||
|
ids = tokenizer.encode(sequence, add_special_tokens=False)
|
||||||
|
rust_ids = rust_tokenizer.encode(sequence, add_special_tokens=False)
|
||||||
|
self.assertListEqual(ids, rust_ids)
|
||||||
|
|
||||||
|
rust_tokenizer = self.get_rust_tokenizer()
|
||||||
|
ids = tokenizer.encode(sequence)
|
||||||
|
rust_ids = rust_tokenizer.encode(sequence)
|
||||||
|
self.assertListEqual(ids, rust_ids)
|
||||||
|
|
||||||
def test_full_tokenizer(self):
|
def test_full_tokenizer(self):
|
||||||
tokenizer = BigBirdTokenizer(SAMPLE_VOCAB, keep_accents=True)
|
tokenizer = BigBirdTokenizer(SAMPLE_VOCAB, keep_accents=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user