





import unittest

from transformers import PegasusTokenizer, PegasusTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


a_			=      get_tests_dir("""fixtures/test_sentencepiece_no_bos.model""")






@require_sentencepiece
@require_tokenizers
class 		__snake_case (		__lowercase							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					PegasusTokenizer
 _lowerCamelCase				=					PegasusTokenizerFast
 _lowerCamelCase				=					True
 _lowerCamelCase				=					True

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().setUp()

     # We have a SentencePiece fixture for testing
     __A   :						Optional[int]									=					PegasusTokenizer(_a  )
     tokenizer.save_pretrained(self.tmpdirname  )

 @cached_property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return PegasusTokenizer.from_pretrained('''google/pegasus-large'''  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return PegasusTokenizer.from_pretrained(self.tmpdirname ,						**_a  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return ("This is a test", "This is a test")

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					'''</s>'''
     __A   :						Tuple									=					1

     self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a  ) ,						_a  )
     self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a  ) ,						_a  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					list(self.get_tokenizer().get_vocab().keys()  )

     self.assertEqual(vocab_keys[0] ,						'''<pad>'''  )
     self.assertEqual(vocab_keys[1] ,						'''</s>'''  )
     self.assertEqual(vocab_keys[-1] ,						'''v'''  )
     self.assertEqual(len(_a  ) ,						1103  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.assertEqual(self.get_tokenizer().vocab_size ,						1103  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					self.rust_tokenizer_class.from_pretrained(self.tmpdirname  )
     __A   :						str									=					self.tokenizer_class.from_pretrained(self.tmpdirname  )
     __A   :						List[str]									=					(
         '''Let\'s see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important'''
         ''' </s> <pad> <pad> <pad>'''
     )
     __A   :						Optional[int]									=					rust_tokenizer([raw_input_str] ,						return_tensors=_a ,						add_special_tokens=_a  ).input_ids[0]
     __A   :						str									=					py_tokenizer([raw_input_str] ,						return_tensors=_a ,						add_special_tokens=_a  ).input_ids[0]
     self.assertListEqual(_a ,						_a  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self._large_tokenizer
     # <mask_1> masks whole sentence while <mask_2> masks single word
     __A   :						int									=					'''<mask_1> To ensure a <mask_2> flow of bank resolutions.'''
     __A   :						List[str]									=					[2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1]
     __A   :						Dict									=					tokenizer([raw_input_str] ,						return_tensors=_a  ).input_ids[0]
     self.assertListEqual(_a ,						_a  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Any									=					self._large_tokenizer
     # The tracebacks for the following asserts are **better** without messages or self.assertEqual
     assert tokenizer.vocab_size == 9_6103
     assert tokenizer.pad_token_id == 0
     assert tokenizer.eos_token_id == 1
     assert tokenizer.offset == 103
     assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105
     assert tokenizer.unk_token == "<unk>"
     assert tokenizer.model_max_length == 1024
     __A   :						Union[str, Any]									=					'''To ensure a smooth flow of bank resolutions.'''
     __A   :						Any									=					[413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1]
     __A   :						int									=					tokenizer([raw_input_str] ,						return_tensors=_a  ).input_ids[0]
     self.assertListEqual(_a ,						_a  )
     assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3]  ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"]

 @require_torch
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					['''This is going to be way too long.''' * 150, '''short example''']
     __A   :						Union[str, Any]									=					['''not super long but more than 5 tokens''', '''tiny''']
     __A   :						int									=					self._large_tokenizer(_a ,						padding=_a ,						truncation=_a ,						return_tensors='''pt'''  )
     __A   :						Any									=					self._large_tokenizer(
         text_target=_a ,						max_length=5 ,						padding=_a ,						truncation=_a ,						return_tensors='''pt'''  )

     assert batch.input_ids.shape == (2, 1024)
     assert batch.attention_mask.shape == (2, 1024)
     assert targets["input_ids"].shape == (2, 5)
     assert len(_a  ) == 2  # input_ids, attention_mask.







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					{'''input_ids''': [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1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# fmt: on

     self.tokenizer_integration_test_util(
         expected_encoding=_a ,						model_name='''google/bigbird-pegasus-large-arxiv''' ,						revision='''ba85d0851d708441f91440d509690f1ab6353415''' ,						)






@require_sentencepiece
@require_tokenizers
class 		__snake_case (		__lowercase							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					PegasusTokenizer
 _lowerCamelCase				=					PegasusTokenizerFast
 _lowerCamelCase				=					True
 _lowerCamelCase				=					True

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().setUp()

     # We have a SentencePiece fixture for testing
     __A   :						Optional[Any]									=					PegasusTokenizer(_a ,						offset=0 ,						mask_token_sent=_a ,						mask_token='''[MASK]'''  )
     tokenizer.save_pretrained(self.tmpdirname  )

 @cached_property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return PegasusTokenizer.from_pretrained('''google/bigbird-pegasus-large-arxiv'''  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return PegasusTokenizer.from_pretrained(self.tmpdirname ,						**_a  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return ("This is a test", "This is a test")

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					self.rust_tokenizer_class.from_pretrained(self.tmpdirname  )
     __A   :						Union[str, Any]									=					self.tokenizer_class.from_pretrained(self.tmpdirname  )
     __A   :						Tuple									=					(
         '''Let\'s see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>'''
         ''' <pad> <pad> <pad>'''
     )
     __A   :						Union[str, Any]									=					rust_tokenizer([raw_input_str] ,						return_tensors=_a ,						add_special_tokens=_a  ).input_ids[0]
     __A   :						Any									=					py_tokenizer([raw_input_str] ,						return_tensors=_a ,						add_special_tokens=_a  ).input_ids[0]
     self.assertListEqual(_a ,						_a  )

 @require_torch
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Dict									=					['''This is going to be way too long.''' * 1000, '''short example''']
     __A   :						Union[str, Any]									=					['''not super long but more than 5 tokens''', '''tiny''']
     __A   :						Tuple									=					self._large_tokenizer(_a ,						padding=_a ,						truncation=_a ,						return_tensors='''pt'''  )
     __A   :						Optional[int]									=					self._large_tokenizer(
         text_target=_a ,						max_length=5 ,						padding=_a ,						truncation=_a ,						return_tensors='''pt'''  )

     assert batch.input_ids.shape == (2, 4096)
     assert batch.attention_mask.shape == (2, 4096)
     assert targets["input_ids"].shape == (2, 5)
     assert len(_a  ) == 2  # input_ids, attention_mask.







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					(
         '''This is an example string that is used to test the original TF implementation against the HF'''
         ''' implementation'''
     )

     __A   :						Optional[int]									=					self._large_tokenizer(_a  ).input_ids

     self.assertListEqual(
         _a ,						[182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] ,						)











"""simple docstring"""



import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_			=      logging.get_logger(__name__)

a_			=      {
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					"""sew-d"""

 def __init__(   self ,						__lowerCamelCase=32 ,						__lowerCamelCase=768 ,						__lowerCamelCase=12 ,						__lowerCamelCase=12 ,						__lowerCamelCase=3072 ,						__lowerCamelCase=2 ,						__lowerCamelCase=512 ,						__lowerCamelCase=256 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=("p2c", "c2p") ,						__lowerCamelCase="layer_norm" ,						__lowerCamelCase="gelu_python" ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1e-7 ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase="group" ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,						__lowerCamelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,						__lowerCamelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,						__lowerCamelCase=False ,						__lowerCamelCase=128 ,						__lowerCamelCase=16 ,						__lowerCamelCase=True ,						__lowerCamelCase=0.0_5 ,						__lowerCamelCase=10 ,						__lowerCamelCase=2 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=10 ,						__lowerCamelCase=0 ,						__lowerCamelCase="mean" ,						__lowerCamelCase=False ,						__lowerCamelCase=False ,						__lowerCamelCase=256 ,						__lowerCamelCase=0 ,						__lowerCamelCase=1 ,						__lowerCamelCase=2 ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase ,						pad_token_id=__lowerCamelCase ,						bos_token_id=__lowerCamelCase ,						eos_token_id=__lowerCamelCase  )
     __A   :						str									=					hidden_size
     __A   :						List[Any]									=					feat_extract_norm
     __A   :						Tuple									=					feat_extract_activation
     __A   :						Dict									=					list(__lowerCamelCase  )
     __A   :						int									=					list(__lowerCamelCase  )
     __A   :						List[Any]									=					list(__lowerCamelCase  )
     __A   :						Any									=					conv_bias
     __A   :						List[Any]									=					num_conv_pos_embeddings
     __A   :						Any									=					num_conv_pos_embedding_groups
     __A   :						Optional[Any]									=					len(self.conv_dim  )
     __A   :						int									=					num_hidden_layers
     __A   :						Union[str, Any]									=					intermediate_size
     __A   :						Union[str, Any]									=					squeeze_factor
     __A   :						int									=					max_position_embeddings
     __A   :						Tuple									=					position_buckets
     __A   :						Tuple									=					share_att_key
     __A   :						List[str]									=					relative_attention
     __A   :						Optional[Any]									=					norm_rel_ebd
     __A   :						Dict									=					list(__lowerCamelCase  )
     __A   :						str									=					hidden_act
     __A   :						List[str]									=					num_attention_heads
     __A   :						Union[str, Any]									=					hidden_dropout
     __A   :						Optional[int]									=					attention_dropout
     __A   :						Optional[Any]									=					activation_dropout
     __A   :						List[str]									=					feat_proj_dropout
     __A   :						str									=					final_dropout
     __A   :						Tuple									=					layer_norm_eps
     __A   :						int									=					feature_layer_norm_eps
     __A   :						Optional[int]									=					initializer_range
     __A   :						Dict									=					vocab_size

     if (
         (len(self.conv_stride  ) != self.num_feat_extract_layers)
         or (len(self.conv_kernel  ) != self.num_feat_extract_layers)
         or (len(self.conv_dim  ) != self.num_feat_extract_layers)
     ):
         raise ValueError(
             '''Configuration for convolutional layers is incorrect.'''
             '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'''
             F"""but is `len(config.conv_dim) = {len(self.conv_dim  )}`, `len(config.conv_stride)"""
             F"""= {len(self.conv_stride  )}`, `len(config.conv_kernel) = {len(self.conv_kernel  )}`."""  )

     # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
     __A   :						int									=					apply_spec_augment
     __A   :						Any									=					mask_time_prob
     __A   :						Optional[int]									=					mask_time_length
     __A   :						Any									=					mask_time_min_masks
     __A   :						int									=					mask_feature_prob
     __A   :						Tuple									=					mask_feature_length
     __A   :						Dict									=					mask_feature_min_masks

     # ctc loss
     __A   :						Tuple									=					ctc_loss_reduction
     __A   :						Union[str, Any]									=					ctc_zero_infinity

     # sequence classification
     __A   :						Tuple									=					use_weighted_layer_sum
     __A   :						List[str]									=					classifier_proj_size







 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return functools.reduce(operator.mul ,						self.conv_stride ,						1  )




