Enable option for subword regularization in more tokenizers. (#11417)
* improve slow class tok usage at xlm rob * add subword regularization for barthez * improve barthez tok. test * fix tokenizer tests * add subword regularization for camembert * add subword regularization for deberta v2 tokenizer * add more doc to deberta v2 tokenizer * add subword regularization for speech to text tok. * fix sp_model_kwargs type in speech 2 text tok. * add subword regularization for M2M100 tok. * add more concrete type hints * fix tests for m2m100 and s2t tok. * add missing Any import * fix syntax error in m2m100 tok. * fix unpickle of m2m100 and s2t tok. * fix test of m2m100 and s2t tok. * improve unpickle of deberta v2 tok. * add test for pickle of barthez & camembert * fix pickle of barthez & camembert * add test for deberta v2 tok. pickle * fix m2m100 tok. pickle * fix s2t tok. pickle * add subword regularization to albert tok. * refactor subword reg. test into TokenizerTesterMixin improve albert tok. test remove sample argument form albert tok. check subword reg. using TokenizerTesterMixin improve tok. tests improve xlm roberta tok. tests improve xlm roberta tok. tests * add subword regularization for big bird t. * improve xlm roberta tok. test * add subword regularization for mbart50 tok. * add subword regularization for pegasus tok. * add subword regularization for reformer tok. * add subword regularization for T5 tok. * fix t5 tok. test formatting * add subword regularization for xlm_proph. tok. * add subword regularization for xlnet tok. * add subword regularization for gert_gen tok. * add typing to tokenizers * add typing to xlm rob. tok * add subword regularization for marian tok. * add reverse tok. test * fix marian tok test * fix marian tok test * fix casing in tok. tests * fix style of tok. common test * fix deberta v2 tok test * add type annotations to tok. tests * add type annotations to tok. __init__ * add typing to kokenizer * add type annotations to tok. __init__ * don't specify the default when it's None * fix barthez tok. doc * move sentencepiece tok. tests to TokenizerTesterMixin * fix unused imports * fix albert tok. test * add comment to sentencepiece test options * fix Any import at big bird tok. * fix Any import at xlm prophetnet tok. * empty commit to trigger CI
This commit is contained in:
@@ -13,10 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import itertools
|
||||
import os
|
||||
import pickle
|
||||
import unittest
|
||||
|
||||
from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast
|
||||
@@ -36,6 +33,7 @@ class XLMRobertaTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
||||
tokenizer_class = XLMRobertaTokenizer
|
||||
rust_tokenizer_class = XLMRobertaTokenizerFast
|
||||
test_rust_tokenizer = True
|
||||
test_sentencepiece = True
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
@@ -120,41 +118,6 @@ class XLMRobertaTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
def test_subword_regularization_tokenizer(self):
|
||||
# Subword regularization is only available for the slow tokenizer.
|
||||
tokenizer = XLMRobertaTokenizer(
|
||||
SAMPLE_VOCAB, keep_accents=True, sp_model_kwargs={"enable_sampling": True, "alpha": 0.1, "nbest_size": -1}
|
||||
)
|
||||
|
||||
# Subword regularization augments training data with subword sampling.
|
||||
# This has a random component. We test if the tokenizer generates different
|
||||
# results when subword regularization is enabled.
|
||||
tokens_list = []
|
||||
for _ in range(5):
|
||||
tokens_list.append(tokenizer.tokenize("This is a test for subword regularization."))
|
||||
|
||||
# the list of different pairs of tokens_list
|
||||
combinations = itertools.combinations(tokens_list, 2)
|
||||
|
||||
all_equal = True
|
||||
for combination in combinations:
|
||||
if combination[0] != combination[1]:
|
||||
all_equal = False
|
||||
|
||||
self.assertFalse(all_equal)
|
||||
|
||||
def test_pickle_subword_regularization_tokenizer(self):
|
||||
"""Google pickle __getstate__ __setstate__ if you are struggling with this."""
|
||||
# Subword regularization is only available for the slow tokenizer.
|
||||
sp_model_kwargs = {"enable_sampling": True, "alpha": 0.1, "nbest_size": -1}
|
||||
tokenizer = XLMRobertaTokenizer(SAMPLE_VOCAB, keep_accents=True, sp_model_kwargs=sp_model_kwargs)
|
||||
tokenizer_bin = pickle.dumps(tokenizer)
|
||||
tokenizer_new = pickle.loads(tokenizer_bin)
|
||||
|
||||
self.assertIsNotNone(tokenizer_new.sp_model_kwargs)
|
||||
self.assertTrue(isinstance(tokenizer_new.sp_model_kwargs, dict))
|
||||
self.assertEqual(tokenizer_new.sp_model_kwargs, sp_model_kwargs)
|
||||
|
||||
@cached_property
|
||||
def big_tokenizer(self):
|
||||
return XLMRobertaTokenizer.from_pretrained("xlm-roberta-base")
|
||||
|
||||
Reference in New Issue
Block a user