






'''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 UpperCAmelCase_					:




      """simple docstring"""




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


       '''simple docstring'''

       lowercase					:   Any				  =parent
       lowercase					:   Tuple				  =100
       lowercase					:   Optional[int]				  =batch_size
       lowercase					:   Optional[int]				  =image_size
       lowercase					:   List[Any]				  =patch_size
       lowercase					:   Union[str, Any]				  =num_channels
       lowercase					:   Tuple				  =is_training
       lowercase					:   Union[str, Any]				  =use_labels
       lowercase					:   Any				  =hidden_size
       lowercase					:   Optional[int]				  =num_hidden_layers
       lowercase					:   List[str]				  =num_attention_heads
       lowercase					:   int				  =intermediate_size
       lowercase					:   Optional[int]				  =hidden_act
       lowercase					:   Dict				  =hidden_dropout_prob
       lowercase					:   Optional[Any]				  =attention_probs_dropout_prob
       lowercase					:   Optional[Any]				  =type_sequence_label_size
       lowercase					:   str				  =initializer_range
       lowercase					:   Union[str, Any]				  =scope
       lowercase					:   Union[str, Any]				  =out_indices
       lowercase					:   List[str]				  =num_labels

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






      def 				A__      ( self			:		List[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Dict				  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

       lowercase					:   int				  =None
       lowercase					:   Union[str, Any]				  =None
       if self.use_labels:
        lowercase					:   Tuple				  =ids_tensor([self.batch_size]     ,      self.type_sequence_label_size		)
        lowercase					:   str				  =ids_tensor([self.batch_size, self.image_size, self.image_size]     ,      self.num_labels		)

       lowercase					:   str				  =self.get_config()

       return config, pixel_values, labels, pixel_labels






      def 				A__      ( self			:		Union[str, Any]		)  ->  List[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=snake_case__     ,      initializer_range=self.initializer_range     ,      out_indices=self.out_indices     ,      )






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		List[str]		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =BeitModel(config=snake_case__		)
       model.to(snake_case__		)
       model.eval()
       lowercase					:   List[str]				  =model(snake_case__		)
       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






      def 				A__      ( self			:		Dict     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		str		)  ->  Any:


       '''simple docstring'''

       lowercase					:   int				  =BeitForMaskedImageModeling(config=snake_case__		)
       model.to(snake_case__		)
       model.eval()
       lowercase					:   Dict				  =model(snake_case__		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length - 1, self.vocab_size)		)






      def 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		List[Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =self.type_sequence_label_size
       lowercase					:   int				  =BeitForImageClassification(snake_case__		)
       model.to(snake_case__		)
       model.eval()
       lowercase					:   List[Any]				  =model(snake_case__     ,      labels=snake_case__		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.type_sequence_label_size)		)

       # test greyscale images
       lowercase					:   Optional[Any]				  =1
       lowercase					:   int				  =BeitForImageClassification(snake_case__		)
       model.to(snake_case__		)
       model.eval()

       lowercase					:   Dict				  =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       lowercase					:   List[Any]				  =model(snake_case__     ,      labels=snake_case__		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.type_sequence_label_size)		)






      def 				A__      ( self			:		Tuple     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Any		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Dict				  =self.num_labels
       lowercase					:   str				  =BeitForSemanticSegmentation(snake_case__		)
       model.to(snake_case__		)
       model.eval()
       lowercase					:   List[str]				  =model(snake_case__		)
       self.parent.assertEqual(
           result.logits.shape     ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
       lowercase					:   Union[str, Any]				  =model(snake_case__     ,      labels=snake_case__		)
       self.parent.assertEqual(
           result.logits.shape     ,      (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)






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


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.prepare_config_and_inputs()
       lowercase					:   str				  =config_and_inputs
       lowercase					:   Optional[int]				  ={"pixel_values": pixel_values}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_					(    SCREAMING_SNAKE_CASE__				,       SCREAMING_SNAKE_CASE__				,       unittest.TestCase				):




      """simple docstring"""




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

      UpperCamelCase_								=			False
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False






      def 				A__      ( self			:		str		)  ->  int:


       '''simple docstring'''

       lowercase					:   Any				  =BeitModelTester(self		)
       lowercase					:   List[str]				  =ConfigTester(self     ,      config_class=snake_case__     ,      has_text_modality=snake_case__     ,      hidden_size=37		)






      def 				A__      ( self			:		Any		)  ->  Any:


       '''simple docstring'''

       self.config_tester.run_common_tests()






      @unittest.skip(reason='''BEiT does not use inputs_embeds'''		)
      def 				A__      ( self			:		Any		)  ->  Union[str, 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 				A__      ( self			:		Dict		)  ->  Dict:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		int		)  ->  Union[str, Any]:


       '''simple docstring'''

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

       for model_class in self.all_model_classes:
        lowercase					:   Optional[int]				  =model_class(snake_case__		)
        self.assertIsInstance(model.get_input_embeddings()     ,      (nn.Module)		)
        lowercase					:   str				  =model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(snake_case__     ,      nn.Linear		)		)






      def 				A__      ( self			:		Tuple		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs_for_common()

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

        lowercase					:   Optional[int]				  =["pixel_values"]
        self.assertListEqual(arg_names[:1]     ,      snake_case__		)






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


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case__		)






      def 				A__      ( self			:		Dict		)  ->  int:


       '''simple docstring'''

       lowercase					:   Any				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*snake_case__		)






      def 				A__      ( self			:		Tuple		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*snake_case__		)






      def 				A__      ( self			:		str		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*snake_case__		)






      def 				A__      ( self			:		Union[str, Any]		)  ->  Optional[int]:


       '''simple docstring'''

       if not self.model_tester.is_training:
        return

       lowercase					:   Any				  =self.model_tester.prepare_config_and_inputs_for_common()
       lowercase					:   Tuple				  =True

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

        lowercase					:   str				  =model_class(snake_case__		)
        model.to(snake_case__		)
        model.train()
        lowercase					:   Tuple				  =self._prepare_for_class(snake_case__     ,      snake_case__     ,      return_labels=snake_case__		)
        lowercase					:   Union[str, Any]				  =model(**snake_case__		).loss
        loss.backward()






      def 				A__      ( self			:		int		)  ->  str:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
        return

       lowercase					:   Optional[Any]				  =False
       lowercase					:   Any				  =True

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

        lowercase					:   int				  =model_class(snake_case__		)
        model.gradient_checkpointing_enable()
        model.to(snake_case__		)
        model.train()
        lowercase					:   Union[str, Any]				  =self._prepare_for_class(snake_case__     ,      snake_case__     ,      return_labels=snake_case__		)
        lowercase					:   Tuple				  =model(**snake_case__		).loss
        loss.backward()






      def 				A__      ( self			:		int		)  ->  str:


       '''simple docstring'''

       lowercase					:   int				  =self.model_tester.prepare_config_and_inputs_for_common()

       lowercase					:   Tuple				  =_config_zero_init(snake_case__		)
       for model_class in self.all_model_classes:
        lowercase					:   List[Any]				  =model_class(config=snake_case__		)
        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 				A__      ( self			:		Tuple		)  ->  Union[str, Any]:


       '''simple docstring'''

       for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase					:   Tuple				  =BeitModel.from_pretrained(snake_case__		)
        self.assertIsNotNone(snake_case__		)





def 							lowercase_				(    )       ->      Union[str, Any]:



 """simple docstring"""




 lowercase					:   Dict				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
 return image





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




      """simple docstring"""




      @cached_property
      def 				A__      ( self			:		List[str]		)  ->  Optional[int]:


       '''simple docstring'''

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






      @slow
      def 				A__      ( self			:		Optional[Any]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   List[str]				  =BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''		).to(snake_case__		)

       lowercase					:   Optional[Any]				  =self.default_image_processor
       lowercase					:   Dict				  =prepare_img()
       lowercase					:   Optional[Any]				  =image_processor(images=snake_case__     ,      return_tensors='''pt'''		).pixel_values.to(snake_case__		)

       # prepare bool_masked_pos
       lowercase					:   str				  =torch.ones((1, 196)     ,      dtype=torch.bool		).to(snake_case__		)

       # forward pass
       with torch.no_grad():
        lowercase					:   int				  =model(pixel_values=snake_case__     ,      bool_masked_pos=snake_case__		)
       lowercase					:   Any				  =outputs.logits

       # verify the logits
       lowercase					:   str				  =torch.Size((1, 196, 8192)		)
       self.assertEqual(logits.shape     ,      snake_case__		)

       lowercase					:   List[Any]				  =torch.tensor(
           [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]]		).to(snake_case__		)

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






      @slow
      def 				A__      ( self			:		List[Any]		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''		).to(snake_case__		)

       lowercase					:   Tuple				  =self.default_image_processor
       lowercase					:   int				  =prepare_img()
       lowercase					:   Tuple				  =image_processor(images=snake_case__     ,      return_tensors='''pt'''		).to(snake_case__		)

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

       # verify the logits
       lowercase					:   Union[str, Any]				  =torch.Size((1, 1000)		)
       self.assertEqual(logits.shape     ,      snake_case__		)

       lowercase					:   int				  =torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]		).to(snake_case__		)

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

       lowercase					:   str				  =281
       self.assertEqual(logits.argmax(-1		).item()     ,      snake_case__		)






      @slow
      def 				A__      ( self			:		Dict		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   List[Any]				  =BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''		).to(
           snake_case__		)

       lowercase					:   List[str]				  =self.default_image_processor
       lowercase					:   str				  =prepare_img()
       lowercase					:   Optional[int]				  =image_processor(images=snake_case__     ,      return_tensors='''pt'''		).to(snake_case__		)

       # forward pass
       with torch.no_grad():
        lowercase					:   int				  =model(**snake_case__		)
       lowercase					:   Any				  =outputs.logits

       # verify the logits
       lowercase					:   List[str]				  =torch.Size((1, 2_1841)		)
       self.assertEqual(logits.shape     ,      snake_case__		)

       lowercase					:   Tuple				  =torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]		).to(snake_case__		)

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

       lowercase					:   Tuple				  =2396
       self.assertEqual(logits.argmax(-1		).item()     ,      snake_case__		)






      @slow
      def 				A__      ( self			:		str		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Any				  =BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
       lowercase					:   Optional[int]				  =model.to(snake_case__		)

       lowercase					:   Tuple				  =BeitImageProcessor(do_resize=snake_case__     ,      size=640     ,      do_center_crop=snake_case__		)

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

       # forward pass
       with torch.no_grad():
        lowercase					:   Tuple				  =model(**snake_case__		)
       lowercase					:   List[str]				  =outputs.logits

       # verify the logits
       lowercase					:   Dict				  =torch.Size((1, 150, 160, 160)		)
       self.assertEqual(logits.shape     ,      snake_case__		)

       lowercase					:   Tuple				  =version.parse(PIL.__version__		) < version.parse('''9.0.0'''		)

       if is_pillow_less_than_a:
        lowercase					:   List[Any]				  =torch.tensor(
            [
                [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]],
                [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]],
                [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]],
            ]     ,      device=snake_case__     ,      )
       else:
        lowercase					:   Optional[Any]				  =torch.tensor(
            [
                [[-4.8_9_6_0, -2.3_6_8_8, -3.0_3_5_5], [-2.8_4_7_8, -0.9_8_3_6, -1.7_4_1_8], [-2.9_4_4_9, -1.3_3_3_2, -2.1_4_5_6]],
                [[-5.8_0_8_1, -3.4_1_2_4, -4.1_0_0_6], [-3.8_5_6_1, -2.2_0_8_1, -3.0_3_2_3], [-3.8_3_6_5, -2.4_6_0_1, -3.3_6_6_9]],
                [[-0.0_3_0_9, 3.9_8_6_8, 4.0_5_4_0], [2.9_6_4_0, 4.6_8_7_7, 4.9_9_7_6], [3.2_0_8_1, 4.7_6_9_0, 4.9_9_4_2]],
            ]     ,      device=snake_case__     ,      )

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






      @slow
      def 				A__      ( self			:		Optional[Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Dict				  =BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
       lowercase					:   int				  =model.to(snake_case__		)

       lowercase					:   Union[str, Any]				  =BeitImageProcessor(do_resize=snake_case__     ,      size=640     ,      do_center_crop=snake_case__		)

       lowercase					:   int				  =load_dataset('''hf-internal-testing/fixtures_ade20k'''     ,      split='''test'''		)
       lowercase					:   str				  =Image.open(ds[0]['''file''']		)
       lowercase					:   Optional[int]				  =image_processor(images=snake_case__     ,      return_tensors='''pt'''		).to(snake_case__		)

       # forward pass
       with torch.no_grad():
        lowercase					:   Optional[Any]				  =model(**snake_case__		)

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

       lowercase					:   List[str]				  =image_processor.post_process_semantic_segmentation(outputs=snake_case__     ,      target_sizes=[(500, 300)]		)
       lowercase					:   List[str]				  =torch.Size((500, 300)		)
       self.assertEqual(segmentation[0].shape     ,      snake_case__		)

       lowercase					:   str				  =image_processor.post_process_semantic_segmentation(outputs=snake_case__		)
       lowercase					:   str				  =torch.Size((160, 160)		)
       self.assertEqual(segmentation[0].shape     ,      snake_case__		)











'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers.models.distilbert.modeling_tf_distilbert import (
          TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFDistilBertForMaskedLM,
          TFDistilBertForMultipleChoice,
          TFDistilBertForQuestionAnswering,
          TFDistilBertForSequenceClassification,
          TFDistilBertForTokenClassification,
          TFDistilBertModel,
      )
class UpperCAmelCase_					:




      """simple docstring"""




      def __init__( self			:		Tuple     ,      UpperCAmelCase			:		List[str]     ,      )  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   int				  =parent
       lowercase					:   Any				  =13
       lowercase					:   Any				  =7
       lowercase					:   Optional[int]				  =True
       lowercase					:   Optional[int]				  =True
       lowercase					:   Tuple				  =False
       lowercase					:   Optional[Any]				  =True
       lowercase					:   Dict				  =99
       lowercase					:   Union[str, Any]				  =32
       lowercase					:   Union[str, Any]				  =2
       lowercase					:   Union[str, Any]				  =4
       lowercase					:   List[str]				  =37
       lowercase					:   str				  ='''gelu'''
       lowercase					:   Dict				  =0.1
       lowercase					:   List[Any]				  =0.1
       lowercase					:   List[str]				  =512
       lowercase					:   Optional[int]				  =16
       lowercase					:   Optional[Any]				  =2
       lowercase					:   List[str]				  =0.0_2
       lowercase					:   Any				  =3
       lowercase					:   Optional[Any]				  =4
       lowercase					:   int				  =None






      def 				A__      ( self			:		List[str]		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Tuple				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size		)

       lowercase					:   Any				  =None
       if self.use_input_mask:
        lowercase					:   List[str]				  =random_attention_mask([self.batch_size, self.seq_length]		)

       lowercase					:   Union[str, Any]				  =None
       lowercase					:   Any				  =None
       lowercase					:   str				  =None
       if self.use_labels:
        lowercase					:   Union[str, Any]				  =ids_tensor([self.batch_size]     ,      self.type_sequence_label_size		)
        lowercase					:   Tuple				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.num_labels		)
        lowercase					:   List[Any]				  =ids_tensor([self.batch_size]     ,      self.num_choices		)

       lowercase					:   Dict				  =DistilBertConfig(
           vocab_size=self.vocab_size     ,      dim=self.hidden_size     ,      n_layers=self.num_hidden_layers     ,      n_heads=self.num_attention_heads     ,      hidden_dim=self.intermediate_size     ,      hidden_act=self.hidden_act     ,      dropout=self.hidden_dropout_prob     ,      attention_dropout=self.attention_probs_dropout_prob     ,      max_position_embeddings=self.max_position_embeddings     ,      initializer_range=self.initializer_range     ,      )

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Optional[Any]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   int				  =TFDistilBertModel(config=UpperCAmelCase		)
       lowercase					:   int				  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}

       lowercase					:   List[str]				  =model(UpperCAmelCase		)

       lowercase					:   str				  =[input_ids, input_mask]

       lowercase					:   Tuple				  =model(UpperCAmelCase		)

       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






      def 				A__      ( self			:		List[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Optional[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   List[str]				  =TFDistilBertForMaskedLM(config=UpperCAmelCase		)
       lowercase					:   int				  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}
       lowercase					:   Union[str, Any]				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)		)






      def 				A__      ( self			:		List[Any]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[int]		)  ->  Any:


       '''simple docstring'''

       lowercase					:   str				  =TFDistilBertForQuestionAnswering(config=UpperCAmelCase		)
       lowercase					:   int				  ={
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
       }
       lowercase					:   List[str]				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.start_logits.shape     ,      (self.batch_size, self.seq_length)		)
       self.parent.assertEqual(result.end_logits.shape     ,      (self.batch_size, self.seq_length)		)






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Dict		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Dict				  =self.num_labels
       lowercase					:   Optional[Any]				  =TFDistilBertForSequenceClassification(UpperCAmelCase		)
       lowercase					:   str				  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}
       lowercase					:   Union[str, Any]				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_labels)		)






      def 				A__      ( self			:		int     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Dict		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self.num_choices
       lowercase					:   Optional[int]				  =TFDistilBertForMultipleChoice(UpperCAmelCase		)
       lowercase					:   Optional[Any]				  =tf.tile(tf.expand_dims(UpperCAmelCase     ,      1		)     ,      (1, self.num_choices, 1)		)
       lowercase					:   List[Any]				  =tf.tile(tf.expand_dims(UpperCAmelCase     ,      1		)     ,      (1, self.num_choices, 1)		)
       lowercase					:   Tuple				  ={
           '''input_ids''': multiple_choice_inputs_ids,
           '''attention_mask''': multiple_choice_input_mask,
       }
       lowercase					:   Tuple				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_choices)		)






      def 				A__      ( self			:		List[Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Union[str, Any]		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Dict				  =self.num_labels
       lowercase					:   Tuple				  =TFDistilBertForTokenClassification(UpperCAmelCase		)
       lowercase					:   Optional[Any]				  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}
       lowercase					:   str				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.num_labels)		)






      def 				A__      ( self			:		List[str]		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   int				  =self.prepare_config_and_inputs()
       ((lowercase)  ,		(lowercase)  ,		(lowercase)  ,		(lowercase)  ,		(lowercase)  ,		(lowercase))					:   Union[str, Any]				  =config_and_inputs
       lowercase					:   Tuple				  ={'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict





@require_tf
class UpperCAmelCase_					(    __A				,       __A				,       unittest.TestCase				):




      """simple docstring"""




      UpperCamelCase_								=			(
          (
              TFDistilBertModel,
              TFDistilBertForMaskedLM,
              TFDistilBertForQuestionAnswering,
              TFDistilBertForSequenceClassification,
              TFDistilBertForTokenClassification,
              TFDistilBertForMultipleChoice,
          )
          if is_tf_available()
          else None
      )
      UpperCamelCase_								=			(
          {
              '''feature-extraction''': TFDistilBertModel,
              '''fill-mask''': TFDistilBertForMaskedLM,
              '''question-answering''': TFDistilBertForQuestionAnswering,
              '''text-classification''': TFDistilBertForSequenceClassification,
              '''token-classification''': TFDistilBertForTokenClassification,
              '''zero-shot''': TFDistilBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False






      def 				A__      ( self			:		Dict		)  ->  str:


       '''simple docstring'''

       lowercase					:   str				  =TFDistilBertModelTester(self		)
       lowercase					:   int				  =ConfigTester(self     ,      config_class=UpperCAmelCase     ,      dim=37		)






      def 				A__      ( self			:		Union[str, Any]		)  ->  Dict:


       '''simple docstring'''

       self.config_tester.run_common_tests()






      def 				A__      ( self			:		Optional[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase		)






      def 				A__      ( self			:		Tuple		)  ->  Any:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase					:   Any				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase		)






      def 				A__      ( self			:		Any		)  ->  str:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase		)






      def 				A__      ( self			:		str		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase		)






      @slow
      def 				A__      ( self			:		List[Any]		)  ->  Dict:


       '''simple docstring'''

       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
        lowercase					:   Union[str, Any]				  =TFDistilBertModel.from_pretrained(UpperCAmelCase		)
        self.assertIsNotNone(UpperCAmelCase		)





@require_tf
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      @slow
      def 				A__      ( self			:		List[str]		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''		)
       lowercase					:   Tuple				  =tf.constant([[0, 1, 2, 3, 4, 5]]		)
       lowercase					:   List[Any]				  =model(UpperCAmelCase		)[0]

       lowercase					:   str				  =[1, 6, 768]
       self.assertEqual(output.shape     ,      UpperCAmelCase		)

       lowercase					:   Optional[int]				  =tf.constant(
           [
               [
                   [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                   [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                   [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]     ,      UpperCAmelCase     ,      atol=1e-4		)



