





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_squeezebert import SqueezeBertTokenizer


__A             =       logging.get_logger(__name__)

__A             =       {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

__A             =       {
    "vocab_file": {
        "squeezebert/squeezebert-uncased": (
            "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt"
        ),
        "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt",
        "squeezebert/squeezebert-mnli-headless": (
            "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "squeezebert/squeezebert-uncased": (
            "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json"
        ),
        "squeezebert/squeezebert-mnli": (
            "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json"
        ),
        "squeezebert/squeezebert-mnli-headless": (
            "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json"
        ),
    },
}

__A             =       {
    "squeezebert/squeezebert-uncased": 512,
    "squeezebert/squeezebert-mnli": 512,
    "squeezebert/squeezebert-mnli-headless": 512,
}


__A             =       {
    "squeezebert/squeezebert-uncased": {"do_lower_case": True},
    "squeezebert/squeezebert-mnli": {"do_lower_case": True},
    "squeezebert/squeezebert-mnli-headless": {"do_lower_case": True},
}



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    VOCAB_FILES_NAMES
 lowercase_	=    PRETRAINED_VOCAB_FILES_MAP
 lowercase_	=    PRETRAINED_INIT_CONFIGURATION
 lowercase_	=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowercase_	=    SqueezeBertTokenizer

 def __init__(self       :     Any ,				UpperCAmelCase_       :     int=None ,				UpperCAmelCase_       :     List[str]=None ,				UpperCAmelCase_       :     int=True ,				UpperCAmelCase_       :     Optional[int]="[UNK]" ,				UpperCAmelCase_       :     int="[SEP]" ,				UpperCAmelCase_       :     Optional[Any]="[PAD]" ,				UpperCAmelCase_       :     Optional[int]="[CLS]" ,				UpperCAmelCase_       :     str="[MASK]" ,				UpperCAmelCase_       :     Union[str, Any]=True ,				UpperCAmelCase_       :     Optional[Any]=None ,				**UpperCAmelCase_       :     Dict ,				) ->Union[str, Any]:



   '''simple docstring'''



   super().__init__(
       UpperCAmelCase_ ,				tokenizer_file=UpperCAmelCase_ ,				do_lower_case=UpperCAmelCase_ ,				unk_token=UpperCAmelCase_ ,				sep_token=UpperCAmelCase_ ,				pad_token=UpperCAmelCase_ ,				cls_token=UpperCAmelCase_ ,				mask_token=UpperCAmelCase_ ,				tokenize_chinese_chars=UpperCAmelCase_ ,				strip_accents=UpperCAmelCase_ ,				**UpperCAmelCase_ ,				)

   lowerCamelCase__:   Dict    						=json.loads(self.backend_tokenizer.normalizer.__getstate__())
   if (
       normalizer_state.get("lowercase" ,				UpperCAmelCase_) != do_lower_case
       or normalizer_state.get("strip_accents" ,				UpperCAmelCase_) != strip_accents
       or normalizer_state.get("handle_chinese_chars" ,				UpperCAmelCase_) != tokenize_chinese_chars
   ):
     lowerCamelCase__:   Any    						=getattr(UpperCAmelCase_ ,				normalizer_state.pop("type"))
     lowerCamelCase__:   Union[str, Any]    						=do_lower_case
     lowerCamelCase__:   Optional[Any]    						=strip_accents
     lowerCamelCase__:   Optional[int]    						=tokenize_chinese_chars
     lowerCamelCase__:   int    						=normalizer_class(**UpperCAmelCase_)

   lowerCamelCase__:   Any    						=do_lower_case

 def 							SCREAMING_SNAKE_CASE_      (self       :     str ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     str=None) ->Dict:



   '''simple docstring'''



   lowerCamelCase__:   List[str]    						=[self.cls_token_id] + token_ids_a + [self.sep_token_id]

   if token_ids_a:
     output += token_ids_a + [self.sep_token_id]

   return output

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int] ,				UpperCAmelCase_       :     List[int] ,				UpperCAmelCase_       :     Optional[List[int]] = None) ->List[int]:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=[self.sep_token_id]
   lowerCamelCase__:   Tuple    						=[self.cls_token_id]
   if token_ids_a is None:
     return len(cls + token_ids_a + sep) * [0]
   return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]







 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any] ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     Optional[str] = None) ->Tuple[str]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=self._tokenizer.model.save(UpperCAmelCase_ ,				name=UpperCAmelCase_)
   return tuple(UpperCAmelCase_)








import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
     import jax

     from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
     from PIL import Image

     from transformers import BeitImageProcessor



class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 def __init__(self       :     Dict ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Any=100 ,				UpperCAmelCase_       :     Optional[int]=13 ,				UpperCAmelCase_       :     Optional[int]=30 ,				UpperCAmelCase_       :     str=2 ,				UpperCAmelCase_       :     Dict=3 ,				UpperCAmelCase_       :     List[str]=True ,				UpperCAmelCase_       :     Optional[Any]=True ,				UpperCAmelCase_       :     Optional[int]=32 ,				UpperCAmelCase_       :     List[str]=5 ,				UpperCAmelCase_       :     Optional[int]=4 ,				UpperCAmelCase_       :     Optional[Any]=37 ,				UpperCAmelCase_       :     str="gelu" ,				UpperCAmelCase_       :     Optional[Any]=0.1 ,				UpperCAmelCase_       :     Union[str, Any]=0.1 ,				UpperCAmelCase_       :     int=10 ,				UpperCAmelCase_       :     List[str]=0.02 ,				UpperCAmelCase_       :     Optional[int]=3 ,				) ->int:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=parent
   lowerCamelCase__:   List[Any]    						=vocab_size
   lowerCamelCase__:   List[str]    						=batch_size
   lowerCamelCase__:   str    						=image_size
   lowerCamelCase__:   Any    						=patch_size
   lowerCamelCase__:   int    						=num_channels
   lowerCamelCase__:   str    						=is_training
   lowerCamelCase__:   Tuple    						=use_labels
   lowerCamelCase__:   Optional[int]    						=hidden_size
   lowerCamelCase__:   Union[str, Any]    						=num_hidden_layers
   lowerCamelCase__:   List[Any]    						=num_attention_heads
   lowerCamelCase__:   Optional[Any]    						=intermediate_size
   lowerCamelCase__:   Tuple    						=hidden_act
   lowerCamelCase__:   Optional[Any]    						=hidden_dropout_prob
   lowerCamelCase__:   Union[str, Any]    						=attention_probs_dropout_prob
   lowerCamelCase__:   List[Any]    						=type_sequence_label_size
   lowerCamelCase__:   List[str]    						=initializer_range

   # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
   lowerCamelCase__:   Union[str, Any]    						=(image_size // patch_size) ** 2
   lowerCamelCase__:   str    						=num_patches + 1

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

   lowerCamelCase__:   Any    						=None
   if self.use_labels:
     lowerCamelCase__:   Union[str, Any]    						=ids_tensor([self.batch_size] ,				self.type_sequence_label_size)

   lowerCamelCase__:   Dict    						=BeitConfig(
       vocab_size=self.vocab_size ,				image_size=self.image_size ,				patch_size=self.patch_size ,				num_channels=self.num_channels ,				hidden_size=self.hidden_size ,				num_hidden_layers=self.num_hidden_layers ,				num_attention_heads=self.num_attention_heads ,				intermediate_size=self.intermediate_size ,				hidden_act=self.hidden_act ,				hidden_dropout_prob=self.hidden_dropout_prob ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob ,				is_decoder=UpperCAmelCase_ ,				initializer_range=self.initializer_range ,				)

   return config, pixel_values, labels

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Optional[Any]) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=FlaxBeitModel(config=UpperCAmelCase_)
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size))

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     Optional[Any]) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=FlaxBeitForMaskedImageModeling(config=UpperCAmelCase_)
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length - 1, self.vocab_size))

 def 							SCREAMING_SNAKE_CASE_      (self       :     int ,				UpperCAmelCase_       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Any) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__:   str    						=self.type_sequence_label_size
   lowerCamelCase__:   Any    						=FlaxBeitForImageClassification(config=UpperCAmelCase_)
   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.type_sequence_label_size))

   # test greyscale images
   lowerCamelCase__:   int    						=1
   lowerCamelCase__:   int    						=FlaxBeitForImageClassification(UpperCAmelCase_)

   lowerCamelCase__:   Optional[Any]    						=floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)







 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase__
   )						,      (
       lowerCamelCase__
   )						,      (
       lowerCamelCase__
   )						,      
   ):   List[Any]    						=config_and_inputs
   lowerCamelCase__:   Dict    						={"pixel_values": pixel_values}
   return config, inputs_dict



@require_flax
class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					,							unittest.TestCase					):







 '''simple docstring'''
 lowercase_	=    (
     (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
 )

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->None:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=FlaxBeitModelTester(self)
   lowerCamelCase__:   Any    						=ConfigTester(self ,				config_class=UpperCAmelCase_ ,				has_text_modality=UpperCAmelCase_ ,				hidden_size=37)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->List[Any]:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__						,      lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     lowerCamelCase__:   int    						=model_class(UpperCAmelCase_)
     lowerCamelCase__:   Union[str, Any]    						=inspect.signature(model.__call__)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     lowerCamelCase__:   Dict    						=[*signature.parameters.keys()]

     lowerCamelCase__:   Dict    						=["pixel_values"]
     self.assertListEqual(arg_names[:1] ,				UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__						,      lowerCamelCase__:   Dict    						=self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     with self.subTest(model_class.__name__):
       lowerCamelCase__:   List[str]    						=self._prepare_for_class(UpperCAmelCase_ ,				UpperCAmelCase_)
       lowerCamelCase__:   Any    						=model_class(UpperCAmelCase_)

       @jax.jit
       def model_jitted(UpperCAmelCase_       :     Dict ,				**UpperCAmelCase_       :     Tuple):
         return model(pixel_values=UpperCAmelCase_ ,				**UpperCAmelCase_)

       with self.subTest("JIT Enabled"):
         lowerCamelCase__:   Tuple    						=model_jitted(**UpperCAmelCase_).to_tuple()

       with self.subTest("JIT Disabled"):
         with jax.disable_jit():
           lowerCamelCase__:   Tuple    						=model_jitted(**UpperCAmelCase_).to_tuple()

       self.assertEqual(len(UpperCAmelCase_) ,				len(UpperCAmelCase_))
       for jitted_output, output in zip(UpperCAmelCase_ ,				UpperCAmelCase_):
         self.assertEqual(jitted_output.shape ,				output.shape)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->str:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Any:



   '''simple docstring'''



   for model_class_name in self.all_model_classes:
     lowerCamelCase__:   Any    						=model_class_name.from_pretrained("microsoft/beit-base-patch16-224")
     lowerCamelCase__:   Dict    						=model(np.ones((1, 3, 224, 224)))
     self.assertIsNotNone(UpperCAmelCase_)





def   lowerCAmelCase_				( )  ->	Dict:







  """simple docstring"""





  lowerCamelCase__:   Any    						=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
  return image






@require_vision
@require_flax
class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 @cached_property
 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->List[str]:



   '''simple docstring'''



   return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224") if is_vision_available() else None

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any]) ->Dict:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k")

   lowerCamelCase__:   Optional[int]    						=self.default_image_processor
   lowerCamelCase__:   int    						=prepare_img()
   lowerCamelCase__:   Optional[Any]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np").pixel_values

   # prepare bool_masked_pos
   lowerCamelCase__:   List[str]    						=np.ones((1, 196) ,				dtype=UpperCAmelCase_)

   # forward pass
   lowerCamelCase__:   List[str]    						=model(pixel_values=UpperCAmelCase_ ,				bool_masked_pos=UpperCAmelCase_)
   lowerCamelCase__:   Optional[Any]    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   str    						=(1, 196, 8_192)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   Optional[Any]    						=np.array(
       [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]])

   self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3] ,				UpperCAmelCase_ ,				atol=1E-2))

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224")

   lowerCamelCase__:   int    						=self.default_image_processor
   lowerCamelCase__:   str    						=prepare_img()
   lowerCamelCase__:   List[str]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np")

   # forward pass
   lowerCamelCase__:   Union[str, Any]    						=model(**UpperCAmelCase_)
   lowerCamelCase__:   int    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   Dict    						=(1, 1_000)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   Any    						=np.array([-1.2385, -1.0987, -1.0108])

   self.assertTrue(np.allclose(logits[0, :3] ,				UpperCAmelCase_ ,				atol=1E-4))

   lowerCamelCase__:   List[str]    						=281
   self.assertEqual(logits.argmax(-1).item() ,				UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any]) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k")

   lowerCamelCase__:   List[str]    						=self.default_image_processor
   lowerCamelCase__:   Dict    						=prepare_img()
   lowerCamelCase__:   List[Any]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np")

   # forward pass
   lowerCamelCase__:   Optional[Any]    						=model(**UpperCAmelCase_)
   lowerCamelCase__:   Any    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   Any    						=(1, 21_841)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   str    						=np.array([1.6881, -0.2787, 0.5901])

   self.assertTrue(np.allclose(logits[0, :3] ,				UpperCAmelCase_ ,				atol=1E-4))

   lowerCamelCase__:   List[str]    						=2_396
   self.assertEqual(logits.argmax(-1).item() ,				UpperCAmelCase_)

