




'''simple docstring'''



import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
      from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import ViTImageProcessor
class UpperCAmelCase_					:




      """simple docstring"""




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


       '''simple docstring'''

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

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






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


       '''simple docstring'''

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

       lowercase					:   Optional[int]				  =None
       if self.use_labels:
        lowercase					:   List[Any]				  =ids_tensor([self.batch_size]     ,      self.type_sequence_label_size		)

       lowercase					:   Optional[Any]				  =self.get_config()

       return config, pixel_values, labels






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


       '''simple docstring'''

       return ViTConfig(
           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=SCREAMING_SNAKE_CASE_     ,      initializer_range=self.initializer_range     ,      encoder_stride=self.encoder_stride     ,      )






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


       '''simple docstring'''

       lowercase					:   Any				  =ViTModel(config=SCREAMING_SNAKE_CASE_		)
       model.to(SCREAMING_SNAKE_CASE_		)
       model.eval()
       lowercase					:   Optional[Any]				  =model(SCREAMING_SNAKE_CASE_		)
       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






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


       '''simple docstring'''

       lowercase					:   Tuple				  =ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_		)
       model.to(SCREAMING_SNAKE_CASE_		)
       model.eval()
       lowercase					:   Dict				  =model(SCREAMING_SNAKE_CASE_		)
       self.parent.assertEqual(
           result.reconstruction.shape     ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

       # test greyscale images
       lowercase					:   int				  =1
       lowercase					:   Any				  =ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE_		)
       model.to(SCREAMING_SNAKE_CASE_		)
       model.eval()

       lowercase					:   Dict				  =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       lowercase					:   Optional[Any]				  =model(SCREAMING_SNAKE_CASE_		)
       self.parent.assertEqual(result.reconstruction.shape     ,      (self.batch_size, 1, self.image_size, self.image_size)		)






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


       '''simple docstring'''

       lowercase					:   str				  =self.type_sequence_label_size
       lowercase					:   Union[str, Any]				  =ViTForImageClassification(SCREAMING_SNAKE_CASE_		)
       model.to(SCREAMING_SNAKE_CASE_		)
       model.eval()
       lowercase					:   Optional[Any]				  =model(SCREAMING_SNAKE_CASE_     ,      labels=SCREAMING_SNAKE_CASE_		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.type_sequence_label_size)		)

       # test greyscale images
       lowercase					:   int				  =1
       lowercase					:   Any				  =ViTForImageClassification(SCREAMING_SNAKE_CASE_		)
       model.to(SCREAMING_SNAKE_CASE_		)
       model.eval()

       lowercase					:   Optional[int]				  =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       lowercase					:   int				  =model(SCREAMING_SNAKE_CASE_		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.type_sequence_label_size)		)






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


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.prepare_config_and_inputs()
       (
           (
           lowercase 
       )  ,		(
           lowercase 
       )  ,		(
           lowercase 
       )  ,		 
       )					:   Dict				  =config_and_inputs
       lowercase					:   int				  ={'''pixel_values''': pixel_values}
       return config, inputs_dict





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




      """simple docstring"""




      UpperCamelCase_								=			(
          (
              ViTModel,
              ViTForImageClassification,
              ViTForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase_								=			(
          {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
          if is_torch_available()
          else {}
      )
      UpperCamelCase_								=			True

      UpperCamelCase_								=			False
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False






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


       '''simple docstring'''

       lowercase					:   int				  =ViTModelTester(self		)
       lowercase					:   List[Any]				  =ConfigTester(self     ,      config_class=SCREAMING_SNAKE_CASE_     ,      has_text_modality=SCREAMING_SNAKE_CASE_     ,      hidden_size=37		)






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


       '''simple docstring'''

       self.config_tester.run_common_tests()






      @unittest.skip(reason='''ViT does not use inputs_embeds'''		)
      def 				A__      ( self			:		Tuple		)  ->  List[Any]:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		Any		)  ->  Any:


       '''simple docstring'''

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

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






      def 				A__      ( self			:		str		)  ->  Any:


       '''simple docstring'''

       lowercase  ,		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(SCREAMING_SNAKE_CASE_		)
        lowercase					:   str				  =inspect.signature(model.forward		)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        lowercase					:   Optional[Any]				  =[*signature.parameters.keys()]

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






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


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_		)






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


       '''simple docstring'''

       lowercase					:   List[str]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_		)






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


       '''simple docstring'''

       lowercase					:   Tuple				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_		)






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


       '''simple docstring'''

       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase					:   Union[str, Any]				  =ViTModel.from_pretrained(SCREAMING_SNAKE_CASE_		)
        self.assertIsNotNone(SCREAMING_SNAKE_CASE_		)





def 							lowercase_				(    )       ->      Dict:



 """simple docstring"""




 lowercase					:   Optional[int]				  =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			:		Optional[Any]		)  ->  str:


       '''simple docstring'''

       return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''		) if is_vision_available() else None






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


       '''simple docstring'''

       lowercase					:   str				  =ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''		).to(SCREAMING_SNAKE_CASE_		)

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

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

       # verify the logits
       lowercase					:   Dict				  =torch.Size((1, 1000)		)
       self.assertEqual(outputs.logits.shape     ,      SCREAMING_SNAKE_CASE_		)

       lowercase					:   str				  =torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]		).to(SCREAMING_SNAKE_CASE_		)

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






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


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =ViTModel.from_pretrained('''facebook/dino-vits8'''		).to(SCREAMING_SNAKE_CASE_		)

       lowercase					:   Optional[Any]				  =ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''     ,      size=480		)
       lowercase					:   str				  =prepare_img()
       lowercase					:   int				  =image_processor(images=SCREAMING_SNAKE_CASE_     ,      return_tensors='''pt'''		)
       lowercase					:   Optional[int]				  =inputs.pixel_values.to(SCREAMING_SNAKE_CASE_		)

       # forward pass
       with torch.no_grad():
        lowercase					:   Dict				  =model(SCREAMING_SNAKE_CASE_     ,      interpolate_pos_encoding=SCREAMING_SNAKE_CASE_		)

       # verify the logits
       lowercase					:   int				  =torch.Size((1, 3601, 384)		)
       self.assertEqual(outputs.last_hidden_state.shape     ,      SCREAMING_SNAKE_CASE_		)

       lowercase					:   Optional[Any]				  =torch.tensor(
           [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]		).to(SCREAMING_SNAKE_CASE_		)

       self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]     ,      SCREAMING_SNAKE_CASE_     ,      atol=1e-4		)		)






      @slow
      @require_accelerate
      @require_torch_gpu
      def 				A__      ( self			:		Tuple		)  ->  int:


       '''simple docstring'''

       lowercase					:   List[Any]				  =ViTModel.from_pretrained('''facebook/dino-vits8'''     ,      torch_dtype=torch.floataa     ,      device_map='''auto'''		)
       lowercase					:   List[str]				  =self.default_image_processor

       lowercase					:   Optional[int]				  =prepare_img()
       lowercase					:   Tuple				  =image_processor(images=SCREAMING_SNAKE_CASE_     ,      return_tensors='''pt'''		)
       lowercase					:   Optional[Any]				  =inputs.pixel_values.to(SCREAMING_SNAKE_CASE_		)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
        lowercase					:   List[Any]				  =model(SCREAMING_SNAKE_CASE_		)











'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE						=	{
    'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig']
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	['RemBertTokenizer']

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	['RemBertTokenizerFast']

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'RemBertForCausalLM',
          'RemBertForMaskedLM',
          'RemBertForMultipleChoice',
          'RemBertForQuestionAnswering',
          'RemBertForSequenceClassification',
          'RemBertForTokenClassification',
          'RemBertLayer',
          'RemBertModel',
          'RemBertPreTrainedModel',
          'load_tf_weights_in_rembert',
      ]


try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
          'TFRemBertForCausalLM',
          'TFRemBertForMaskedLM',
          'TFRemBertForMultipleChoice',
          'TFRemBertForQuestionAnswering',
          'TFRemBertForSequenceClassification',
          'TFRemBertForTokenClassification',
          'TFRemBertLayer',
          'TFRemBertModel',
          'TFRemBertPreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert import RemBertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert_fast import RemBertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_rembert import (
                REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                RemBertForCausalLM,
                RemBertForMaskedLM,
                RemBertForMultipleChoice,
                RemBertForQuestionAnswering,
                RemBertForSequenceClassification,
                RemBertForTokenClassification,
                RemBertLayer,
                RemBertModel,
                RemBertPreTrainedModel,
                load_tf_weights_in_rembert,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_rembert import (
                TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFRemBertForCausalLM,
                TFRemBertForMaskedLM,
                TFRemBertForMultipleChoice,
                TFRemBertForQuestionAnswering,
                TFRemBertForSequenceClassification,
                TFRemBertForTokenClassification,
                TFRemBertLayer,
                TFRemBertModel,
                TFRemBertPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



