






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    __lowercase   :







     '''simple docstring'''




     def __init__(					self							:      int			,						UpperCamelCase_							:      Optional[int]			,						UpperCamelCase_							:      Optional[int]=100			,						UpperCamelCase_							:      Tuple=13			,						UpperCamelCase_							:      Tuple=30			,						UpperCamelCase_							:      List[Any]=2			,						UpperCamelCase_							:      int=3			,						UpperCamelCase_							:      List[Any]=True			,						UpperCamelCase_							:      Tuple=True			,						UpperCamelCase_							:      int=32			,						UpperCamelCase_							:      Union[str, Any]=4			,						UpperCamelCase_							:      Dict=4			,						UpperCamelCase_							:      Optional[int]=37			,						UpperCamelCase_							:      Union[str, Any]="gelu"			,						UpperCamelCase_							:      Tuple=0.1			,						UpperCamelCase_							:      Tuple=0.1			,						UpperCamelCase_							:      Dict=10			,						UpperCamelCase_							:      Tuple=0.02			,						UpperCamelCase_							:      str=3			,						UpperCamelCase_							:      str=None			,						UpperCamelCase_							:      List[Any]=[0, 1, 2, 3]			,						):


         """simple docstring"""



         __A       =						parent
         __A       =						100
         __A       =						batch_size
         __A       =						image_size
         __A       =						patch_size
         __A       =						num_channels
         __A       =						is_training
         __A       =						use_labels
         __A       =						hidden_size
         __A       =						num_hidden_layers
         __A       =						num_attention_heads
         __A       =						intermediate_size
         __A       =						hidden_act
         __A       =						hidden_dropout_prob
         __A       =						attention_probs_dropout_prob
         __A       =						type_sequence_label_size
         __A       =						initializer_range
         __A       =						scope
         __A       =						out_indices
         __A       =						num_labels

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




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         __A       =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

         __A       =						None
         __A       =						None
         if self.use_labels:
             __A       =						ids_tensor([self.batch_size]			,						self.type_sequence_label_size					)
             __A       =						ids_tensor([self.batch_size, self.image_size, self.image_size]			,						self.num_labels					)

         __A       =						self.get_config()

         return config, pixel_values, labels, pixel_labels




     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         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=UpperCamelCase_			,						initializer_range=self.initializer_range			,						out_indices=self.out_indices			,						)




     def 		lowerCAmelCase_  (					self							:      str			,						UpperCamelCase_							:      Union[str, Any]			,						UpperCamelCase_							:      List[str]			,						UpperCamelCase_							:      int			,						UpperCamelCase_							:      str					):


         """simple docstring"""



         __A       =						BeitModel(config=UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         __A       =						model(UpperCamelCase_					)
         self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)					)




     def 		lowerCAmelCase_  (					self							:      List[Any]			,						UpperCamelCase_							:      Optional[int]			,						UpperCamelCase_							:      Any			,						UpperCamelCase_							:      Optional[int]			,						UpperCamelCase_							:      Dict					):


         """simple docstring"""



         __A       =						BeitForMaskedImageModeling(config=UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         __A       =						model(UpperCamelCase_					)
         self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length - 1, self.vocab_size)					)




     def 		lowerCAmelCase_  (					self							:      List[str]			,						UpperCamelCase_							:      int			,						UpperCamelCase_							:      str			,						UpperCamelCase_							:      Optional[int]			,						UpperCamelCase_							:      Union[str, Any]					):


         """simple docstring"""



         __A       =						self.type_sequence_label_size
         __A       =						BeitForImageClassification(UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         __A       =						model(UpperCamelCase_			,						labels=UpperCamelCase_					)
         self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)					)

         # test greyscale images
         __A       =						1
         __A       =						BeitForImageClassification(UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()

         __A       =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
         __A       =						model(UpperCamelCase_			,						labels=UpperCamelCase_					)
         self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)					)




     def 		lowerCAmelCase_  (					self							:      Any			,						UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      Optional[Any]			,						UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      Union[str, Any]					):


         """simple docstring"""



         __A       =						self.num_labels
         __A       =						BeitForSemanticSegmentation(UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         __A       =						model(UpperCamelCase_					)
         self.parent.assertEqual(
             result.logits.shape			,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)
         __A       =						model(UpperCamelCase_			,						labels=UpperCamelCase_					)
         self.parent.assertEqual(
             result.logits.shape			,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)





     def 		lowerCAmelCase_  (					self							:      int					):


         """simple docstring"""



         __A       =						self.prepare_config_and_inputs()
         __A     ,						__A     ,						__A     ,						__A       =						config_and_inputs
         __A       =						{"""pixel_values""": pixel_values}
         return config, inputs_dict


@require_torch
class    __lowercase   (						lowercase_      ,   lowercase_      ,   unittest.TestCase	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					(
         (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE     =					(
         {
             "feature-extraction": BeitModel,
             "image-classification": BeitForImageClassification,
             "image-segmentation": BeitForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False




     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						BeitModelTester(self					)
         __A       =						ConfigTester(self			,						config_class=UpperCamelCase_			,						has_text_modality=UpperCamelCase_			,						hidden_size=37					)




     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         self.config_tester.run_common_tests()




     @unittest.skip(reason="""BEiT does not use inputs_embeds"""					)
     def 		lowerCAmelCase_  (					self							:      Optional[Any]					):


         """simple docstring"""



         pass




     @require_torch_multi_gpu
     @unittest.skip(reason="""BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""					)
     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_  (					self							:      Optional[Any]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A       =						model_class(UpperCamelCase_					)
             self.assertIsInstance(model.get_input_embeddings()			,						(nn.Module)					)
             __A       =						model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_			,						nn.Linear					)					)




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A       =						model_class(UpperCamelCase_					)
             __A       =						inspect.signature(model.forward					)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             __A       =						[*signature.parameters.keys()]

             __A       =						["""pixel_values"""]
             self.assertListEqual(arg_names[:1]			,						UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Tuple					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Optional[Any]					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         if not self.model_tester.is_training:
             return

         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()
         __A       =						True

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

             __A       =						model_class(UpperCamelCase_					)
             model.to(UpperCamelCase_					)
             model.train()
             __A       =						self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_			,						return_labels=UpperCamelCase_					)
             __A       =						model(**UpperCamelCase_					).loss
             loss.backward()




     def 		lowerCAmelCase_  (					self							:      Optional[Any]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()
         if not self.model_tester.is_training:
             return

         __A       =						False
         __A       =						True

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

             __A       =						model_class(UpperCamelCase_					)
             model.gradient_checkpointing_enable()
             model.to(UpperCamelCase_					)
             model.train()
             __A       =						self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_			,						return_labels=UpperCamelCase_					)
             __A       =						model(**UpperCamelCase_					).loss
             loss.backward()




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

         __A       =						_config_zero_init(UpperCamelCase_					)
         for model_class in self.all_model_classes:
             __A       =						model_class(config=UpperCamelCase_					)
             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 		lowerCAmelCase_  (					self							:      Tuple					):


         """simple docstring"""



         for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __A       =						BeitModel.from_pretrained(UpperCamelCase_					)
             self.assertIsNotNone(UpperCamelCase_					)



def       _SCREAMING_SNAKE_CASE    (			)    ->   int:





    """simple docstring"""

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






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







     '''simple docstring'''




     @cached_property
     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



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




     @slow
     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""					).to(UpperCamelCase_					)

         __A       =						self.default_image_processor
         __A       =						prepare_img()
         __A       =						image_processor(images=UpperCamelCase_			,						return_tensors="""pt"""					).pixel_values.to(UpperCamelCase_					)

         # prepare bool_masked_pos
         __A       =						torch.ones((1, 196)			,						dtype=torch.bool					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(pixel_values=UpperCamelCase_			,						bool_masked_pos=UpperCamelCase_					)
         __A       =						outputs.logits

         # verify the logits
         __A       =						torch.Size((1, 196, 8_192)					)
         self.assertEqual(logits.shape			,						UpperCamelCase_					)

         __A       =						torch.tensor(
             [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]					).to(UpperCamelCase_					)

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




     @slow
     def 		lowerCAmelCase_  (					self							:      Tuple					):


         """simple docstring"""



         __A       =						BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""					).to(UpperCamelCase_					)

         __A       =						self.default_image_processor
         __A       =						prepare_img()
         __A       =						image_processor(images=UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)
         __A       =						outputs.logits

         # verify the logits
         __A       =						torch.Size((1, 1_000)					)
         self.assertEqual(logits.shape			,						UpperCamelCase_					)

         __A       =						torch.tensor([-1.2385, -1.0987, -1.0108]					).to(UpperCamelCase_					)

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

         __A       =						281
         self.assertEqual(logits.argmax(-1					).item()			,						UpperCamelCase_					)




     @slow
     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         __A       =						BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""					).to(
             UpperCamelCase_					)

         __A       =						self.default_image_processor
         __A       =						prepare_img()
         __A       =						image_processor(images=UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)
         __A       =						outputs.logits

         # verify the logits
         __A       =						torch.Size((1, 21_841)					)
         self.assertEqual(logits.shape			,						UpperCamelCase_					)

         __A       =						torch.tensor([1.6881, -0.2787, 0.5901]					).to(UpperCamelCase_					)

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

         __A       =						2_396
         self.assertEqual(logits.argmax(-1					).item()			,						UpperCamelCase_					)




     @slow
     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""					)
         __A       =						model.to(UpperCamelCase_					)

         __A       =						BeitImageProcessor(do_resize=UpperCamelCase_			,						size=640			,						do_center_crop=UpperCamelCase_					)

         __A       =						load_dataset("""hf-internal-testing/fixtures_ade20k"""			,						split="""test"""					)
         __A       =						Image.open(ds[0]["""file"""]					)
         __A       =						image_processor(images=UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)
         __A       =						outputs.logits

         # verify the logits
         __A       =						torch.Size((1, 150, 160, 160)					)
         self.assertEqual(logits.shape			,						UpperCamelCase_					)

         __A       =						version.parse(PIL.__version__					) < version.parse("""9.0.0"""					)

         if is_pillow_less_than_a:
             __A       =						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=UpperCamelCase_			,						)
         else:
             __A       =						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=UpperCamelCase_			,						)

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





     @slow
     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""					)
         __A       =						model.to(UpperCamelCase_					)

         __A       =						BeitImageProcessor(do_resize=UpperCamelCase_			,						size=640			,						do_center_crop=UpperCamelCase_					)

         __A       =						load_dataset("""hf-internal-testing/fixtures_ade20k"""			,						split="""test"""					)
         __A       =						Image.open(ds[0]["""file"""]					)
         __A       =						image_processor(images=UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)

         __A       =						outputs.logits.detach().cpu()

         __A       =						image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase_			,						target_sizes=[(500, 300)]					)
         __A       =						torch.Size((500, 300)					)
         self.assertEqual(segmentation[0].shape			,						UpperCamelCase_					)

         __A       =						image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase_					)
         __A       =						torch.Size((160, 160)					)
         self.assertEqual(segmentation[0].shape			,						UpperCamelCase_					)












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


__a   :							Dict						= logging.get_logger(__name__)

__a   :							Dict						= {
    "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json",
}


class    __lowercase   (						lowercase_	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					"gpt_bigcode"
     SCREAMING_SNAKE_CASE     =					["past_key_values"]
     SCREAMING_SNAKE_CASE     =					{
         "hidden_size": "n_embd",
         "max_position_embeddings": "n_positions",
         "num_attention_heads": "n_head",
         "num_hidden_layers": "n_layer",
     }




     def __init__(					self							:      List[str]			,						UpperCamelCase_							:      Dict=50_257			,						UpperCamelCase_							:      int=1_024			,						UpperCamelCase_							:      List[Any]=768			,						UpperCamelCase_							:      str=12			,						UpperCamelCase_							:      Union[str, Any]=12			,						UpperCamelCase_							:      Optional[Any]=None			,						UpperCamelCase_							:      Tuple="gelu_pytorch_tanh"			,						UpperCamelCase_							:      Optional[Any]=0.1			,						UpperCamelCase_							:      List[str]=0.1			,						UpperCamelCase_							:      Optional[int]=0.1			,						UpperCamelCase_							:      int=1e-5			,						UpperCamelCase_							:      Tuple=0.02			,						UpperCamelCase_							:      Any=True			,						UpperCamelCase_							:      List[Any]=True			,						UpperCamelCase_							:      Dict=50_256			,						UpperCamelCase_							:      Optional[Any]=50_256			,						UpperCamelCase_							:      int=True			,						UpperCamelCase_							:      List[Any]=True			,						UpperCamelCase_							:      List[Any]=True			,						**UpperCamelCase_							:      int			,						):


         """simple docstring"""



         __A       =						vocab_size
         __A       =						n_positions
         __A       =						n_embd
         __A       =						n_layer
         __A       =						n_head
         __A       =						n_inner
         __A       =						activation_function
         __A       =						resid_pdrop
         __A       =						embd_pdrop
         __A       =						attn_pdrop
         __A       =						layer_norm_epsilon
         __A       =						initializer_range
         __A       =						scale_attn_weights
         __A       =						use_cache
         __A       =						attention_softmax_in_fpaa
         __A       =						scale_attention_softmax_in_fpaa
         __A       =						multi_query

         __A       =						bos_token_id
         __A       =						eos_token_id

         super().__init__(bos_token_id=UpperCamelCase_			,						eos_token_id=UpperCamelCase_			,						**UpperCamelCase_					)




