import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
     from tokenizers import Tokenizer
     from tokenizers.models import WordPiece







class      SCREAMING_SNAKE_CASE__					(  unittest.TestCase    ):

       def 			snake_case__    (							self					:     List[str]			,			_lowerCAmelCase					:     List[Any]			):
           __snake_case       :				Tuple 			=    list(tokenizer_class.max_model_input_sizes.keys()			)
           for model_name in sa_models[:1]:
               __snake_case       :				Union[str, Any] 			=    tokenizer_class.from_pretrained(_lowerCAmelCase			)
               self.assertIsNotNone(_lowerCAmelCase			)
               self.assertIsInstance(_lowerCAmelCase			,			_lowerCAmelCase			)
               self.assertIsInstance(_lowerCAmelCase			,			_lowerCAmelCase			)

               for special_tok in tokenizer.all_special_tokens:
                   self.assertIsInstance(_lowerCAmelCase			,			_lowerCAmelCase			)
                   __snake_case       :				str 			=    tokenizer.convert_tokens_to_ids(_lowerCAmelCase			)
                   self.assertIsInstance(_lowerCAmelCase			,			_lowerCAmelCase			)

       def 			snake_case__    (							self					:     Optional[Any]			,			_lowerCAmelCase					:     BatchEncoding			,			_lowerCAmelCase					:     Optional[Callable] = None			):
           __snake_case       :				List[str] 			=    pickle.dumps(_lowerCAmelCase			)
           self.assertIsNotNone(_lowerCAmelCase			)

           __snake_case       :				Optional[int] 			=    pickle.loads(_lowerCAmelCase			)

           # Ensure is_fast is correctly restored
           self.assertEqual(be_restored.is_fast			,			be_original.is_fast			)

           # Ensure encodings are potentially correctly restored
           if be_original.is_fast:
               self.assertIsNotNone(be_restored.encodings			)
           else:
               self.assertIsNone(be_restored.encodings			)

           # Ensure the keys are the same
           for original_v, restored_v in zip(be_original.values()			,			be_restored.values()			):
               if equal_op:
                   self.assertTrue(equal_op(_lowerCAmelCase			,			_lowerCAmelCase			)			)
               else:
                   self.assertEqual(_lowerCAmelCase			,			_lowerCAmelCase			)

       @slow
       def 			snake_case__    (							self					:     Tuple			):
           self.check_tokenizer_from_pretrained(_lowerCAmelCase			)

       def 			snake_case__    (							self					:     Optional[Any]			):
           self.assertEqual(TensorType("""tf"""			)			,			TensorType.TENSORFLOW			)
           self.assertEqual(TensorType("""pt"""			)			,			TensorType.PYTORCH			)
           self.assertEqual(TensorType("""np"""			)			,			TensorType.NUMPY			)

       @require_tokenizers
       def 			snake_case__    (							self					:     Optional[Any]			):
           import numpy as np

           __snake_case       :				Union[str, Any] 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)
           __snake_case       :				List[str] 			=    BertTokenizerFast.from_pretrained("""bert-base-cased"""			)

           # Python no tensor
           with self.subTest("""BatchEncoding (Python, return_tensors=None)"""			):
               self.assert_dump_and_restore(tokenizer_p("""Small example to encode"""			)			)

           with self.subTest("""BatchEncoding (Python, return_tensors=NUMPY)"""			):
               self.assert_dump_and_restore(
                   tokenizer_p("""Small example to encode"""			,			return_tensors=TensorType.NUMPY			)			,			np.array_equal			)

           with self.subTest("""BatchEncoding (Rust, return_tensors=None)"""			):
               self.assert_dump_and_restore(tokenizer_r("""Small example to encode"""			)			)

           with self.subTest("""BatchEncoding (Rust, return_tensors=NUMPY)"""			):
               self.assert_dump_and_restore(
                   tokenizer_r("""Small example to encode"""			,			return_tensors=TensorType.NUMPY			)			,			np.array_equal			)

       @require_tf
       @require_tokenizers
       def 			snake_case__    (							self					:     Tuple			):
           import tensorflow as tf

           def tf_array_equals(_lowerCAmelCase					:     Tuple			,			_lowerCAmelCase					:     Union[str, Any]			):
               return tf.reduce_all(tf.equal(_lowerCAmelCase			,			_lowerCAmelCase			)			)

           __snake_case       :				List[Any] 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)
           __snake_case       :				Optional[int] 			=    BertTokenizerFast.from_pretrained("""bert-base-cased"""			)

           with self.subTest("""BatchEncoding (Python, return_tensors=TENSORFLOW)"""			):
               self.assert_dump_and_restore(
                   tokenizer_p("""Small example to encode"""			,			return_tensors=TensorType.TENSORFLOW			)			,			_lowerCAmelCase			)

           with self.subTest("""BatchEncoding (Rust, return_tensors=TENSORFLOW)"""			):
               self.assert_dump_and_restore(
                   tokenizer_r("""Small example to encode"""			,			return_tensors=TensorType.TENSORFLOW			)			,			_lowerCAmelCase			)

       @require_torch
       @require_tokenizers
       def 			snake_case__    (							self					:     List[str]			):
           import torch

           __snake_case       :				List[Any] 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)
           __snake_case       :				Optional[int] 			=    BertTokenizerFast.from_pretrained("""bert-base-cased"""			)

           with self.subTest("""BatchEncoding (Python, return_tensors=PYTORCH)"""			):
               self.assert_dump_and_restore(
                   tokenizer_p("""Small example to encode"""			,			return_tensors=TensorType.PYTORCH			)			,			torch.equal			)

           with self.subTest("""BatchEncoding (Rust, return_tensors=PYTORCH)"""			):
               self.assert_dump_and_restore(
                   tokenizer_r("""Small example to encode"""			,			return_tensors=TensorType.PYTORCH			)			,			torch.equal			)

       @require_tokenizers
       def 			snake_case__    (							self					:     Any			):
           __snake_case       :				List[Any] 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)
           __snake_case       :				Optional[int] 			=    BertTokenizerFast.from_pretrained("""bert-base-cased"""			)

           with self.subTest("""Python Tokenizer"""			):
               self.assertFalse(tokenizer_p("""Small example to_encode"""			).is_fast			)

           with self.subTest("""Rust Tokenizer"""			):
               self.assertTrue(tokenizer_r("""Small example to_encode"""			).is_fast			)

       @require_tokenizers
       def 			snake_case__    (							self					:     Optional[Any]			):
           __snake_case       :				Union[str, Any] 			=    BertTokenizerFast.from_pretrained("""bert-base-cased"""			)
           __snake_case       :				List[Any] 			=    tokenizer_r(["""Test""", """\xad""", """test"""]			,			is_split_into_words=_lowerCAmelCase			)

           self.assertEqual(encoded.word_to_tokens(0			)			,			TokenSpan(start=1			,			end=2			)			)
           self.assertEqual(encoded.word_to_tokens(1			)			,			_lowerCAmelCase			)
           self.assertEqual(encoded.word_to_tokens(2			)			,			TokenSpan(start=2			,			end=3			)			)

       def 			snake_case__    (							self					:     str			):
           __snake_case       :				Union[str, Any] 			=    BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]}			)
           __snake_case       :				Optional[Any] 			=    batch.convert_to_tensors(tensor_type="""np"""			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(2, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(2,)			)
           # test converting the converted
           with CaptureStderr() as cs:
               __snake_case       :				str 			=    batch.convert_to_tensors(tensor_type="""np"""			)
           self.assertFalse(len(cs.err			)			,			msg=f'''should have no warning, but got {cs.err}'''			)

           __snake_case       :				Tuple 			=    BatchEncoding({"""inputs""": [1, 2, 3], """labels""": 0}			)
           __snake_case       :				Dict 			=    batch.convert_to_tensors(tensor_type="""np"""			,			prepend_batch_axis=_lowerCAmelCase			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(1, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(1,)			)

       @require_torch
       def 			snake_case__    (							self					:     Optional[int]			):
           __snake_case       :				str 			=    BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]}			)
           __snake_case       :				Optional[Any] 			=    batch.convert_to_tensors(tensor_type="""pt"""			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(2, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(2,)			)
           # test converting the converted
           with CaptureStderr() as cs:
               __snake_case       :				Optional[int] 			=    batch.convert_to_tensors(tensor_type="""pt"""			)
           self.assertFalse(len(cs.err			)			,			msg=f'''should have no warning, but got {cs.err}'''			)

           __snake_case       :				Union[str, Any] 			=    BatchEncoding({"""inputs""": [1, 2, 3], """labels""": 0}			)
           __snake_case       :				Union[str, Any] 			=    batch.convert_to_tensors(tensor_type="""pt"""			,			prepend_batch_axis=_lowerCAmelCase			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(1, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(1,)			)

       @require_tf
       def 			snake_case__    (							self					:     Optional[Any]			):
           __snake_case       :				List[Any] 			=    BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]}			)
           __snake_case       :				List[str] 			=    batch.convert_to_tensors(tensor_type="""tf"""			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(2, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(2,)			)
           # test converting the converted
           with CaptureStderr() as cs:
               __snake_case       :				Optional[int] 			=    batch.convert_to_tensors(tensor_type="""tf"""			)
           self.assertFalse(len(cs.err			)			,			msg=f'''should have no warning, but got {cs.err}'''			)

           __snake_case       :				Union[str, Any] 			=    BatchEncoding({"""inputs""": [1, 2, 3], """labels""": 0}			)
           __snake_case       :				str 			=    batch.convert_to_tensors(tensor_type="""tf"""			,			prepend_batch_axis=_lowerCAmelCase			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(1, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(1,)			)

       @require_flax
       def 			snake_case__    (							self					:     str			):
           __snake_case       :				Optional[Any] 			=    BatchEncoding({"""inputs""": [[1, 2, 3], [4, 5, 6]], """labels""": [0, 1]}			)
           __snake_case       :				Any 			=    batch.convert_to_tensors(tensor_type="""jax"""			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(2, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(2,)			)
           # test converting the converted
           with CaptureStderr() as cs:
               __snake_case       :				int 			=    batch.convert_to_tensors(tensor_type="""jax"""			)
           self.assertFalse(len(cs.err			)			,			msg=f'''should have no warning, but got {cs.err}'''			)

           __snake_case       :				Union[str, Any] 			=    BatchEncoding({"""inputs""": [1, 2, 3], """labels""": 0}			)
           __snake_case       :				Optional[int] 			=    batch.convert_to_tensors(tensor_type="""jax"""			,			prepend_batch_axis=_lowerCAmelCase			)
           self.assertEqual(tensor_batch["""inputs"""].shape			,			(1, 3)			)
           self.assertEqual(tensor_batch["""labels"""].shape			,			(1,)			)

       def 			snake_case__    (							self					:     int			):
           __snake_case       :				Any 			=    [{"""input_ids""": np.array([0, 1, 2]			)}, {"""input_ids""": np.array([0, 1, 2, 3]			)}]
           __snake_case       :				Dict 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)

           __snake_case       :				int 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			np.ndarray			)			)
           self.assertEqual(batch["""input_ids"""].tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)
           __snake_case       :				Tuple 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			,			return_tensors="""np"""			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			np.ndarray			)			)
           self.assertEqual(batch["""input_ids"""].tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)

       @require_torch
       def 			snake_case__    (							self					:     int			):
           import torch

           __snake_case       :				Any 			=    [{"""input_ids""": torch.tensor([0, 1, 2]			)}, {"""input_ids""": torch.tensor([0, 1, 2, 3]			)}]
           __snake_case       :				Any 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)

           __snake_case       :				List[Any] 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			torch.Tensor			)			)
           self.assertEqual(batch["""input_ids"""].tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)
           __snake_case       :				int 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			,			return_tensors="""pt"""			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			torch.Tensor			)			)
           self.assertEqual(batch["""input_ids"""].tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)

       @require_tf
       def 			snake_case__    (							self					:     Optional[int]			):
           import tensorflow as tf

           __snake_case       :				Union[str, Any] 			=    [{"""input_ids""": tf.constant([0, 1, 2]			)}, {"""input_ids""": tf.constant([0, 1, 2, 3]			)}]
           __snake_case       :				Any 			=    BertTokenizer.from_pretrained("""bert-base-cased"""			)

           __snake_case       :				Union[str, Any] 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			tf.Tensor			)			)
           self.assertEqual(batch["""input_ids"""].numpy().tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)
           __snake_case       :				Optional[int] 			=    tokenizer.pad(_lowerCAmelCase			,			padding=_lowerCAmelCase			,			return_tensors="""tf"""			)
           self.assertTrue(isinstance(batch["""input_ids"""]			,			tf.Tensor			)			)
           self.assertEqual(batch["""input_ids"""].numpy().tolist()			,			[[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]			)

       @require_tokenizers
       def 			snake_case__    (							self					:     Optional[int]			):
           __snake_case       :				Union[str, Any] 			=    Tokenizer(WordPiece(unk_token="""[UNK]"""			)			)
           PreTrainedTokenizerFast(tokenizer_object=_lowerCAmelCase			)





       @require_tokenizers
       def 			snake_case__    (							self					:     Union[str, Any]			):
           __snake_case       :				str 			=    Tokenizer(WordPiece(unk_token="""[UNK]"""			)			)
           with tempfile.TemporaryDirectory() as tmpdirname:
               bert_tokenizer.save(os.path.join(_lowerCAmelCase			,			"""tokenizer.json"""			)			)
               PreTrainedTokenizerFast(tokenizer_file=os.path.join(_lowerCAmelCase			,			"""tokenizer.json"""			)			)




import functools
import operator

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


lowercase_  							=			logging.get_logger(__name__)

lowercase_  							=			{
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):
       A							:	List[str]          =	"hubert"

       def __init__(							self					:     List[str]			,			_lowerCAmelCase					:     Union[str, Any]=32			,			_lowerCAmelCase					:     Any=7_68			,			_lowerCAmelCase					:     Optional[Any]=12			,			_lowerCAmelCase					:     Optional[int]=12			,			_lowerCAmelCase					:     int=30_72			,			_lowerCAmelCase					:     Optional[Any]="gelu"			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Any=0.1			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Dict=True			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Optional[int]=0.1			,			_lowerCAmelCase					:     Dict=0.02			,			_lowerCAmelCase					:     Any=1e-5			,			_lowerCAmelCase					:     str="group"			,			_lowerCAmelCase					:     Any="gelu"			,			_lowerCAmelCase					:     List[str]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,			_lowerCAmelCase					:     Any=(5, 2, 2, 2, 2, 2, 2)			,			_lowerCAmelCase					:     Dict=(10, 3, 3, 3, 3, 2, 2)			,			_lowerCAmelCase					:     List[str]=False			,			_lowerCAmelCase					:     Optional[Any]=1_28			,			_lowerCAmelCase					:     int=16			,			_lowerCAmelCase					:     Tuple=False			,			_lowerCAmelCase					:     int=True			,			_lowerCAmelCase					:     int=0.05			,			_lowerCAmelCase					:     Any=10			,			_lowerCAmelCase					:     Dict=2			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     Optional[int]=10			,			_lowerCAmelCase					:     Union[str, Any]=0			,			_lowerCAmelCase					:     int="sum"			,			_lowerCAmelCase					:     Optional[int]=False			,			_lowerCAmelCase					:     str=False			,			_lowerCAmelCase					:     Optional[Any]=2_56			,			_lowerCAmelCase					:     Any=0			,			_lowerCAmelCase					:     List[Any]=1			,			_lowerCAmelCase					:     List[str]=2			,			**_lowerCAmelCase					:     Optional[int]			,			):
           super().__init__(**_lowerCAmelCase			,			pad_token_id=_lowerCAmelCase			,			bos_token_id=_lowerCAmelCase			,			eos_token_id=_lowerCAmelCase			)
           __snake_case       :				Optional[int] 			=    hidden_size
           __snake_case       :				Optional[Any] 			=    feat_extract_norm
           __snake_case       :				Optional[int] 			=    feat_extract_activation
           __snake_case       :				List[str] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    list(_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    conv_bias
           __snake_case       :				Tuple 			=    num_conv_pos_embeddings
           __snake_case       :				List[Any] 			=    num_conv_pos_embedding_groups
           __snake_case       :				Union[str, Any] 			=    len(self.conv_dim			)
           __snake_case       :				List[Any] 			=    num_hidden_layers
           __snake_case       :				Tuple 			=    intermediate_size
           __snake_case       :				Optional[int] 			=    hidden_act
           __snake_case       :				Union[str, Any] 			=    num_attention_heads
           __snake_case       :				str 			=    hidden_dropout
           __snake_case       :				Union[str, Any] 			=    attention_dropout
           __snake_case       :				Dict 			=    activation_dropout
           __snake_case       :				List[Any] 			=    feat_proj_layer_norm
           __snake_case       :				List[str] 			=    feat_proj_dropout
           __snake_case       :				List[Any] 			=    final_dropout
           __snake_case       :				str 			=    layerdrop
           __snake_case       :				List[str] 			=    layer_norm_eps
           __snake_case       :				Optional[Any] 			=    initializer_range
           __snake_case       :				Optional[int] 			=    vocab_size
           __snake_case       :				Dict 			=    do_stable_layer_norm
           __snake_case       :				Tuple 			=    use_weighted_layer_sum
           __snake_case       :				List[Any] 			=    classifier_proj_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)`, but is `len(config.conv_dim) ="""
                   f''' {len(self.conv_dim			)}`, `len(config.conv_stride) = {len(self.conv_stride			)}`,'''
                   f''' `len(config.conv_kernel) = {len(self.conv_kernel			)}`.'''			)

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

           # ctc loss
           __snake_case       :				Dict 			=    ctc_loss_reduction
           __snake_case       :				List[str] 			=    ctc_zero_infinity





       @property
       def 			snake_case__    (							self					:     Any			):
           return functools.reduce(operator.mul			,			self.conv_stride			,			1			)



