



'''simple docstring'''





import inspect
import unittest

from transformers import MobileViTConfig
from transformers.testing_utils import require_torch, 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 transformers import MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
  from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import MobileViTImageProcessor






class __A    (						A		):



 '''simple docstring'''







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


      """simple docstring"""



      _a											=						self.config_class(**self.inputs_dict )
      self.parent.assertTrue(hasattr(A				,     '''hidden_sizes''' ) )
      self.parent.assertTrue(hasattr(A				,     '''neck_hidden_sizes''' ) )
      self.parent.assertTrue(hasattr(A				,     '''num_attention_heads''' ) )






class __A    :



 '''simple docstring'''







 def __init__(self				,     A				,     A=13				,     A=32				,     A=2				,     A=3				,     A=640				,     A=4				,     A="silu"				,     A=3				,     A=32				,     A=0.1				,     A=0.1				,     A=0.1				,     A=0.02				,     A=True				,     A=True				,     A=10				,     A=None				,     )					->  str:


      """simple docstring"""



      _a											=						parent
      _a											=						batch_size
      _a											=						image_size
      _a											=						patch_size
      _a											=						num_channels
      _a											=						last_hidden_size
      _a											=						num_attention_heads
      _a											=						hidden_act
      _a											=						conv_kernel_size
      _a											=						output_stride
      _a											=						hidden_dropout_prob
      _a											=						attention_probs_dropout_prob
      _a											=						classifier_dropout_prob
      _a											=						use_labels
      _a											=						is_training
      _a											=						num_labels
      _a											=						initializer_range
      _a											=						scope



 def       a__						(self )					->  Dict:


      """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.num_labels )
           _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       a__						(self )					->  Any:


      """simple docstring"""



      return MobileViTConfig(
          image_size=self.image_size				,     patch_size=self.patch_size				,     num_channels=self.num_channels				,     num_attention_heads=self.num_attention_heads				,     hidden_act=self.hidden_act				,     conv_kernel_size=self.conv_kernel_size				,     output_stride=self.output_stride				,     hidden_dropout_prob=self.hidden_dropout_prob				,     attention_probs_dropout_prob=self.attention_probs_dropout_prob				,     classifier_dropout_prob=self.classifier_dropout_prob				,     initializer_range=self.initializer_range				,     )



 def       a__						(self				,     A				,     A				,     A				,     A )					->  Tuple:


      """simple docstring"""



      _a											=						MobileViTModel(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A )
      self.parent.assertEqual(
          result.last_hidden_state.shape				,     (
              self.batch_size,
              self.last_hidden_size,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )				,     )



 def       a__						(self				,     A				,     A				,     A				,     A )					->  Tuple:


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						MobileViTForImageClassification(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     labels=A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels) )



 def       a__						(self				,     A				,     A				,     A				,     A )					->  List[Any]:


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						MobileViTForSemanticSegmentation(A )
      model.to(A )
      model.eval()
      _a											=						model(A )
      self.parent.assertEqual(
          result.logits.shape				,     (
              self.batch_size,
              self.num_labels,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )				,     )
      _a											=						model(A				,     labels=A )
      self.parent.assertEqual(
          result.logits.shape				,     (
              self.batch_size,
              self.num_labels,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )				,     )



 def       a__						(self )					->  str:


      """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 __A    (						A     ,     A     ,     unittest.TestCase		):



 '''simple docstring'''







 __lowerCamelCase					: str    = (
     (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 __lowerCamelCase					: List[str]    = (
     {
         'feature-extraction': MobileViTModel,
         'image-classification': MobileViTForImageClassification,
         'image-segmentation': MobileViTForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 __lowerCamelCase					: str    = False
 __lowerCamelCase					: Dict    = False
 __lowerCamelCase					: Union[str, Any]    = False
 __lowerCamelCase					: List[str]    = False



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


      """simple docstring"""



      _a											=						MobileViTModelTester(self )
      _a											=						MobileViTConfigTester(self				,     config_class=A				,     has_text_modality=A )



 def       a__						(self )					->  Dict:


      """simple docstring"""



      self.config_tester.run_common_tests()



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


      """simple docstring"""



      pass



 @unittest.skip(reason='''MobileViT does not support input and output embeddings''' )
 def       a__						(self )					->  List[Any]:


      """simple docstring"""



      pass



 @unittest.skip(reason='''MobileViT does not output attentions''' )
 def       a__						(self )					->  List[Any]:


      """simple docstring"""



      pass



 def       a__						(self )					->  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(A )
           _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]				,     A )



 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' )
 def       a__						(self )					->  Tuple:


      """simple docstring"""



      pass



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A )



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


      """simple docstring"""



      def check_hidden_states_output(A				,     A				,     A ):
           _a											=						model_class(A )
           model.to(A )
           model.eval()

           with torch.no_grad():
                _a											=						model(**self._prepare_for_class(A				,     A ) )

           _a											=						outputs.hidden_states

           _a											=						5
           self.assertEqual(len(A )				,     A )

           # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
           # with the width and height being successively divided by 2.
           _a											=						2
           for i in range(len(A ) ):
                self.assertListEqual(
                    list(hidden_states[i].shape[-2:] )				,     [self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]				,     )
                divisor *= 2

           self.assertEqual(self.model_tester.output_stride				,     divisor // 2 )

      _a						,					_a											=						self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           _a											=						True
           check_hidden_states_output(A				,     A				,     A )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _a											=						True

           check_hidden_states_output(A				,     A				,     A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*A )



 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*A )



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


      """simple docstring"""



      for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _a											=						MobileViTModel.from_pretrained(A )
           self.assertIsNotNone(A )





def lowerCAmelCase						():



     """simple docstring"""



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






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



 '''simple docstring'''







 @cached_property
 def       a__						(self )					->  Tuple:


      """simple docstring"""



      return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small''' ) if is_vision_available() else None



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


      """simple docstring"""



      _a											=						MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small''' ).to(A )

      _a											=						self.default_image_processor
      _a											=						prepare_img()
      _a											=						image_processor(images=A				,     return_tensors='''pt''' ).to(A )

      # forward pass
      with torch.no_grad():
           _a											=						model(**A )

      # verify the logits
      _a											=						torch.Size((1, 1_000) )
      self.assertEqual(outputs.logits.shape				,     A )

      _a											=						torch.tensor([-1.9364, -1.2327, -0.4653] ).to(A )

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



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


      """simple docstring"""



      _a											=						MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''' )
      _a											=						model.to(A )

      _a											=						MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''' )

      _a											=						prepare_img()
      _a											=						image_processor(images=A				,     return_tensors='''pt''' ).to(A )

      # forward pass
      with torch.no_grad():
           _a											=						model(**A )
      _a											=						outputs.logits

      # verify the logits
      _a											=						torch.Size((1, 21, 32, 32) )
      self.assertEqual(logits.shape				,     A )

      _a											=						torch.tensor(
          [
              [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
              [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
              [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
          ]				,     device=A				,     )

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



 @slow
 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''' )
      _a											=						model.to(A )

      _a											=						MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''' )

      _a											=						prepare_img()
      _a											=						image_processor(images=A				,     return_tensors='''pt''' ).to(A )

      # forward pass
      with torch.no_grad():
           _a											=						model(**A )

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

      _a											=						image_processor.post_process_semantic_segmentation(outputs=A				,     target_sizes=[(50, 60)] )
      _a											=						torch.Size((50, 60) )
      self.assertEqual(segmentation[0].shape				,     A )

      _a											=						image_processor.post_process_semantic_segmentation(outputs=A )
      _a											=						torch.Size((32, 32) )
      self.assertEqual(segmentation[0].shape				,     A )








'''simple docstring'''





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


lowercase_	 =						logging.get_logger(__name__)

lowercase_	 =						{
    "google/pegasus-large": "https://huggingface.co/google/pegasus-large/resolve/main/config.json",
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}






class __A    (						A		):



 '''simple docstring'''







 __lowerCamelCase					: Union[str, Any]    = 'pegasus'
 __lowerCamelCase					: Tuple    = ['past_key_values']
 __lowerCamelCase					: List[Any]    = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}



 def __init__(self				,     A=50_265				,     A=1_024				,     A=12				,     A=4_096				,     A=16				,     A=12				,     A=4_096				,     A=16				,     A=0.0				,     A=0.0				,     A=True				,     A=True				,     A="gelu"				,     A=1_024				,     A=0.1				,     A=0.0				,     A=0.0				,     A=0.02				,     A=0				,     A=False				,     A=0				,     A=1				,     A=1				,     **A				,     )					->  str:


      """simple docstring"""



      _a											=						vocab_size
      _a											=						max_position_embeddings
      _a											=						d_model
      _a											=						encoder_ffn_dim
      _a											=						encoder_layers
      _a											=						encoder_attention_heads
      _a											=						decoder_ffn_dim
      _a											=						decoder_layers
      _a											=						decoder_attention_heads
      _a											=						dropout
      _a											=						attention_dropout
      _a											=						activation_dropout
      _a											=						activation_function
      _a											=						init_std
      _a											=						encoder_layerdrop
      _a											=						decoder_layerdrop
      _a											=						use_cache
      _a											=						encoder_layers
      _a											=						scale_embedding  # scale factor will be sqrt(d_model) if True
      super().__init__(
          pad_token_id=A				,     eos_token_id=A				,     is_encoder_decoder=A				,     decoder_start_token_id=A				,     forced_eos_token_id=A				,     **A				,     )



 @property
 def       a__						(self )					->  int:


      """simple docstring"""



      return self.encoder_attention_heads



 @property
 def       a__						(self )					->  int:


      """simple docstring"""



      return self.d_model



