






'''simple docstring'''
import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      MODEL_MAPPING,
      BeitForImageClassification,
      BeitForMaskedImageModeling,
      BeitForSemanticSegmentation,
      BeitModel,
  )
  from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  import PIL
  from PIL import Image

  from transformers import BeitImageProcessor






class      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							Optional[Any]					,      __lowercase	:							int					,      __lowercase	:							List[Any]=1_00					,      __lowercase	:							Optional[int]=13					,      __lowercase	:							Union[str, Any]=30					,      __lowercase	:							List[Any]=2					,      __lowercase	:							Any=3					,      __lowercase	:							List[Any]=True					,      __lowercase	:							Any=True					,      __lowercase	:							Optional[Any]=32					,      __lowercase	:							Tuple=4					,      __lowercase	:							str=4					,      __lowercase	:							Optional[Any]=37					,      __lowercase	:							Union[str, Any]="gelu"					,      __lowercase	:							List[str]=0.1					,      __lowercase	:							Union[str, Any]=0.1					,      __lowercase	:							Any=10					,      __lowercase	:							Optional[Any]=0.02					,      __lowercase	:							Union[str, Any]=3					,      __lowercase	:							Union[str, Any]=None					,      __lowercase	:							Tuple=[0, 1, 2, 3]					,      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				Dict									=parent
       SCREAMING_SNAKE_CASE__     :				Dict									=1_00
       SCREAMING_SNAKE_CASE__     :				Dict									=batch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_size
       SCREAMING_SNAKE_CASE__     :				str									=patch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=num_channels
       SCREAMING_SNAKE_CASE__     :				List[Any]									=is_training
       SCREAMING_SNAKE_CASE__     :				Any									=use_labels
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=hidden_size
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=num_hidden_layers
       SCREAMING_SNAKE_CASE__     :				int									=num_attention_heads
       SCREAMING_SNAKE_CASE__     :				List[str]									=intermediate_size
       SCREAMING_SNAKE_CASE__     :				List[str]									=hidden_act
       SCREAMING_SNAKE_CASE__     :				str									=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__     :				Any									=type_sequence_label_size
       SCREAMING_SNAKE_CASE__     :				List[Any]									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=scope
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=out_indices
       SCREAMING_SNAKE_CASE__     :				Any									=num_labels

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




 def      __magic_name__	(      self	:							Tuple      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				int									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

       SCREAMING_SNAKE_CASE__     :				Dict									=None
       SCREAMING_SNAKE_CASE__     :				List[Any]									=None
       if self.use_labels:
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=ids_tensor([self.batch_size]					,      self.type_sequence_label_size      )
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=ids_tensor([self.batch_size, self.image_size, self.image_size]					,      self.num_labels      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.get_config()

       return config, pixel_values, labels, pixel_labels




 def      __magic_name__	(      self	:							Any      )	->						int:
       return 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=__lowercase					,      initializer_range=self.initializer_range					,      out_indices=self.out_indices					,      )




 def      __magic_name__	(      self	:							List[Any]					,      __lowercase	:							Tuple					,      __lowercase	:							List[Any]					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Any      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=BeitModel(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(__lowercase      )
       self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)      )




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							List[str]					,      __lowercase	:							str					,      __lowercase	:							List[str]					,      __lowercase	:							str      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				str									=BeitForMaskedImageModeling(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(__lowercase      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length - 1, self.vocab_size)      )




 def      __magic_name__	(      self	:							Tuple					,      __lowercase	:							Optional[int]					,      __lowercase	:							Any					,      __lowercase	:							Any					,      __lowercase	:							Tuple      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=self.type_sequence_label_size
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=BeitForImageClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[str]									=model(__lowercase					,      labels=__lowercase      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)      )

       # test greyscale images
       SCREAMING_SNAKE_CASE__     :				Tuple									=1
       SCREAMING_SNAKE_CASE__     :				Tuple									=BeitForImageClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       SCREAMING_SNAKE_CASE__     :				List[Any]									=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(__lowercase					,      labels=__lowercase      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)      )




 def      __magic_name__	(      self	:							List[str]					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Optional[int]					,      __lowercase	:							List[str]					,      __lowercase	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[str]									=self.num_labels
       SCREAMING_SNAKE_CASE__     :				str									=BeitForSemanticSegmentation(__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(__lowercase      )
       self.parent.assertEqual(
           result.logits.shape					,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(__lowercase					,      labels=__lowercase      )
       self.parent.assertEqual(
           result.logits.shape					,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )




 def      __magic_name__	(      self	:							int      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				str									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Tuple									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				List[Any]									={'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase	,				lowerCamelCase	,				unittest.TestCase				):
 snake_case_  							=							(
     (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 snake_case_  							=							(
     {
         """feature-extraction""": BeitModel,
         """image-classification""": BeitForImageClassification,
         """image-segmentation""": BeitForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False




 def      __magic_name__	(      self	:							int      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=BeitModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				str									=ConfigTester(self					,      config_class=__lowercase					,      has_text_modality=__lowercase					,      hidden_size=37      )




 def      __magic_name__	(      self	:							int      )	->						int:
       self.config_tester.run_common_tests()




 @unittest.skip(reason='''BEiT does not use inputs_embeds'''      )
 def      __magic_name__	(      self	:							Optional[Any]      )	->						Optional[Any]:
       pass




 @require_torch_multi_gpu
 @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''      )
 def      __magic_name__	(      self	:							Tuple      )	->						Any:
       pass




 def      __magic_name__	(      self	:							Optional[int]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=model_class(__lowercase      )
             self.assertIsInstance(model.get_input_embeddings()					,      (nn.Module)      )
             SCREAMING_SNAKE_CASE__     :				Dict									=model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__lowercase					,      nn.Linear      )      )




 def      __magic_name__	(      self	:							Dict      )	->						Tuple:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model_class(__lowercase      )
             SCREAMING_SNAKE_CASE__     :				str									=inspect.signature(model.forward      )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             SCREAMING_SNAKE_CASE__     :				List[str]									=[*signature.parameters.keys()]

             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,      __lowercase      )




 def      __magic_name__	(      self	:							str      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase      )




 def      __magic_name__	(      self	:							Dict      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*__lowercase      )




 def      __magic_name__	(      self	:							Optional[Any]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				str									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowercase      )




 def      __magic_name__	(      self	:							Optional[Any]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Any									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*__lowercase      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[Any]:
       if not self.model_tester.is_training:
             return

       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=True

       for model_class in self.all_model_classes:
             # we don't test BeitForMaskedImageModeling
             if model_class in [*get_values(__lowercase      ), BeitForMaskedImageModeling]:
                   continue

             SCREAMING_SNAKE_CASE__     :				Tuple									=model_class(__lowercase      )
             model.to(__lowercase      )
             model.train()
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=self._prepare_for_class(__lowercase					,      __lowercase					,      return_labels=__lowercase      )
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(**__lowercase      ).loss
             loss.backward()




 def      __magic_name__	(      self	:							Optional[int]      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
             return

       SCREAMING_SNAKE_CASE__     :				int									=False
       SCREAMING_SNAKE_CASE__     :				str									=True

       for model_class in self.all_model_classes:
             # we don't test BeitForMaskedImageModeling
             if (
                 model_class in [*get_values(__lowercase      ), BeitForMaskedImageModeling]
                 or not model_class.supports_gradient_checkpointing
             ):
                   continue

             SCREAMING_SNAKE_CASE__     :				Optional[int]									=model_class(__lowercase      )
             model.gradient_checkpointing_enable()
             model.to(__lowercase      )
             model.train()
             SCREAMING_SNAKE_CASE__     :				str									=self._prepare_for_class(__lowercase					,      __lowercase					,      return_labels=__lowercase      )
             SCREAMING_SNAKE_CASE__     :				Dict									=model(**__lowercase      ).loss
             loss.backward()




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[str]:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.prepare_config_and_inputs_for_common()

       SCREAMING_SNAKE_CASE__     :				List[str]									=_config_zero_init(__lowercase      )
       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				str									=model_class(config=__lowercase      )
             for name, param in model.named_parameters():
                   # we skip lambda parameters as these require special initial values
                   # determined by config.layer_scale_init_value
                   if "lambda" in name:
                         continue
                   if param.requires_grad:
                         self.assertIn(
                             ((param.data.mean() * 1e9).round() / 1e9).item()					,      [0.0, 1.0]					,      msg=F"Parameter {name} of model {model_class} seems not properly initialized"					,      )




 @slow
 def      __magic_name__	(      self	:							Dict      )	->						str:
       for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=BeitModel.from_pretrained(__lowercase      )
             self.assertIsNotNone(__lowercase      )





def  _a(  ):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
      return image







@require_torch
@require_vision
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @cached_property
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''      ) if is_vision_available() else None




 @slow
 def      __magic_name__	(      self	:							Any      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Any									=BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''      ).to(__lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				List[str]									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				Any									=image_processor(images=__lowercase					,      return_tensors='''pt'''      ).pixel_values.to(__lowercase      )

       # prepare bool_masked_pos
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=torch.ones((1, 1_96)					,      dtype=torch.bool      ).to(__lowercase      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				List[Any]									=model(pixel_values=__lowercase					,      bool_masked_pos=__lowercase      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				str									=torch.Size((1, 1_96, 81_92)      )
       self.assertEqual(logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				str									=torch.tensor(
           [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]      ).to(__lowercase      )

       self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]					,      __lowercase					,      atol=1e-2      )      )




 @slow
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				List[str]									=BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''      ).to(__lowercase      )

       SCREAMING_SNAKE_CASE__     :				int									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				int									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				int									=image_processor(images=__lowercase					,      return_tensors='''pt'''      ).to(__lowercase      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				Any									=model(**__lowercase      )
       SCREAMING_SNAKE_CASE__     :				str									=outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				Tuple									=torch.Size((1, 10_00)      )
       self.assertEqual(logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				Tuple									=torch.tensor([-1.2385, -1.0987, -1.0108]      ).to(__lowercase      )

       self.assertTrue(torch.allclose(logits[0, :3]					,      __lowercase					,      atol=1e-4      )      )

       SCREAMING_SNAKE_CASE__     :				Any									=2_81
       self.assertEqual(logits.argmax(-1      ).item()					,      __lowercase      )




 @slow
 def      __magic_name__	(      self	:							Optional[int]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Dict									=BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''      ).to(
           __lowercase      )

       SCREAMING_SNAKE_CASE__     :				Tuple									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				int									=prepare_img()
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=image_processor(images=__lowercase					,      return_tensors='''pt'''      ).to(__lowercase      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model(**__lowercase      )
       SCREAMING_SNAKE_CASE__     :				int									=outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				int									=torch.Size((1, 2_18_41)      )
       self.assertEqual(logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				Any									=torch.tensor([1.6881, -0.2787, 0.5901]      ).to(__lowercase      )

       self.assertTrue(torch.allclose(logits[0, :3]					,      __lowercase					,      atol=1e-4      )      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=23_96
       self.assertEqual(logits.argmax(-1      ).item()					,      __lowercase      )




 @slow
 def      __magic_name__	(      self	:							Optional[int]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=model.to(__lowercase      )

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=BeitImageProcessor(do_resize=__lowercase					,      size=6_40					,      do_center_crop=__lowercase      )

       SCREAMING_SNAKE_CASE__     :				Dict									=load_dataset('''hf-internal-testing/fixtures_ade20k'''					,      split='''test'''      )
       SCREAMING_SNAKE_CASE__     :				int									=Image.open(ds[0]['''file''']      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_processor(images=__lowercase					,      return_tensors='''pt'''      ).to(__lowercase      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				Any									=model(**__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				Tuple									=torch.Size((1, 1_50, 1_60, 1_60)      )
       self.assertEqual(logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=version.parse(PIL.__version__      ) < version.parse('''9.0.0'''      )

       if is_pillow_less_than_a:
             SCREAMING_SNAKE_CASE__     :				int									=torch.tensor(
                 [
                     [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
                     [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
                     [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
                 ]					,      device=__lowercase					,      )
       else:
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=torch.tensor(
                 [
                     [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
                     [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
                     [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
                 ]					,      device=__lowercase					,      )

       self.assertTrue(torch.allclose(logits[0, :3, :3, :3]					,      __lowercase					,      atol=1e-4      )      )




 @slow
 def      __magic_name__	(      self	:							Dict      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model.to(__lowercase      )

       SCREAMING_SNAKE_CASE__     :				str									=BeitImageProcessor(do_resize=__lowercase					,      size=6_40					,      do_center_crop=__lowercase      )

       SCREAMING_SNAKE_CASE__     :				Dict									=load_dataset('''hf-internal-testing/fixtures_ade20k'''					,      split='''test'''      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=Image.open(ds[0]['''file''']      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=image_processor(images=__lowercase					,      return_tensors='''pt'''      ).to(__lowercase      )

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=model(**__lowercase      )

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=outputs.logits.detach().cpu()

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_processor.post_process_semantic_segmentation(outputs=__lowercase					,      target_sizes=[(5_00, 3_00)]      )
       SCREAMING_SNAKE_CASE__     :				Any									=torch.Size((5_00, 3_00)      )
       self.assertEqual(segmentation[0].shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				Any									=image_processor.post_process_semantic_segmentation(outputs=__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=torch.Size((1_60, 1_60)      )
       self.assertEqual(segmentation[0].shape					,      __lowercase      )







'''simple docstring'''
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


a_					= logging.get_logger(__name__)

a_					= {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

a_					= {
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

a_					= {
    'bert-base-uncased': 5_1_2,
    'bert-large-uncased': 5_1_2,
    'bert-base-cased': 5_1_2,
    'bert-large-cased': 5_1_2,
    'bert-base-multilingual-uncased': 5_1_2,
    'bert-base-multilingual-cased': 5_1_2,
    'bert-base-chinese': 5_1_2,
    'bert-base-german-cased': 5_1_2,
    'bert-large-uncased-whole-word-masking': 5_1_2,
    'bert-large-cased-whole-word-masking': 5_1_2,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 5_1_2,
    'bert-large-cased-whole-word-masking-finetuned-squad': 5_1_2,
    'bert-base-cased-finetuned-mrpc': 5_1_2,
    'bert-base-german-dbmdz-cased': 5_1_2,
    'bert-base-german-dbmdz-uncased': 5_1_2,
    'TurkuNLP/bert-base-finnish-cased-v1': 5_1_2,
    'TurkuNLP/bert-base-finnish-uncased-v1': 5_1_2,
    'wietsedv/bert-base-dutch-cased': 5_1_2,
}

a_					= {
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}






class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):
 snake_case_  							=							VOCAB_FILES_NAMES
 snake_case_  							=							PRETRAINED_VOCAB_FILES_MAP
 snake_case_  							=							PRETRAINED_INIT_CONFIGURATION
 snake_case_  							=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 snake_case_  							=							BertTokenizer




 def __init__(      self	:							int					,      __lowercase	:							Union[str, Any]=None					,      __lowercase	:							Tuple=None					,      __lowercase	:							str=True					,      __lowercase	:							Optional[Any]="[UNK]"					,      __lowercase	:							Tuple="[SEP]"					,      __lowercase	:							Any="[PAD]"					,      __lowercase	:							List[Any]="[CLS]"					,      __lowercase	:							Union[str, Any]="[MASK]"					,      __lowercase	:							Tuple=True					,      __lowercase	:							str=None					,      **__lowercase	:							Any					,      )	->						Optional[Any]:
       super().__init__(
           __lowercase					,      tokenizer_file=__lowercase					,      do_lower_case=__lowercase					,      unk_token=__lowercase					,      sep_token=__lowercase					,      pad_token=__lowercase					,      cls_token=__lowercase					,      mask_token=__lowercase					,      tokenize_chinese_chars=__lowercase					,      strip_accents=__lowercase					,      **__lowercase					,      )

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

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=do_lower_case




 def      __magic_name__	(      self	:							int					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Union[str, Any]=None      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[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      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							List[int]					,      __lowercase	:							Optional[List[int]] = None      )	->						List[int]:
       SCREAMING_SNAKE_CASE__     :				List[str]									=[self.sep_token_id]
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[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      __magic_name__	(      self	:							List[Any]					,      __lowercase	:							str					,      __lowercase	:							Optional[str] = None      )	->						Tuple[str]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self._tokenizer.model.save(__lowercase					,      name=__lowercase      )
       return tuple(__lowercase      )