



"""simple docstring"""







from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
     import tensorflow as tf

     from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor







class 				UpperCamelCase__       :




      """simple docstring"""




      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=1_3   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=3_0   ,      SCREAMING_SNAKE_CASE_    :				List[str]=2   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=3   ,      SCREAMING_SNAKE_CASE_    :				Tuple=True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=True   ,      SCREAMING_SNAKE_CASE_    :				Dict=3_2   ,      SCREAMING_SNAKE_CASE_    :				List[str]=2   ,      SCREAMING_SNAKE_CASE_    :				int=4   ,      SCREAMING_SNAKE_CASE_    :				Any=3_7   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]="gelu"   ,      SCREAMING_SNAKE_CASE_    :				int=0.1   ,      SCREAMING_SNAKE_CASE_    :				Any=0.1   ,      SCREAMING_SNAKE_CASE_    :				str=1_0   ,      SCREAMING_SNAKE_CASE_    :				int=0.02   ,      SCREAMING_SNAKE_CASE_    :				int=3   ,      SCREAMING_SNAKE_CASE_    :				Dict=None   ,      ):
        lowerCAmelCase_       :    Any								=			parent
        lowerCAmelCase_       :    List[str]								=			batch_size
        lowerCAmelCase_       :    Tuple								=			image_size
        lowerCAmelCase_       :    str								=			patch_size
        lowerCAmelCase_       :    List[Any]								=			num_channels
        lowerCAmelCase_       :    int								=			is_training
        lowerCAmelCase_       :    Union[str, Any]								=			use_labels
        lowerCAmelCase_       :    Dict								=			hidden_size
        lowerCAmelCase_       :    Optional[int]								=			num_hidden_layers
        lowerCAmelCase_       :    Optional[Any]								=			num_attention_heads
        lowerCAmelCase_       :    List[str]								=			intermediate_size
        lowerCAmelCase_       :    Optional[int]								=			hidden_act
        lowerCAmelCase_       :    List[str]								=			hidden_dropout_prob
        lowerCAmelCase_       :    Any								=			attention_probs_dropout_prob
        lowerCAmelCase_       :    int								=			type_sequence_label_size
        lowerCAmelCase_       :    str								=			initializer_range
        lowerCAmelCase_       :    int								=			scope

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        lowerCAmelCase_       :    List[Any]								=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

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

        return config, pixel_values, labels



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        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   ,      )



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				str	):
        lowerCAmelCase_       :    List[Any]								=			TFViTModel(config=SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    int								=			model(SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, self.seq_length, self.hidden_size)	)

        # Test with an image with different size than the one specified in config.
        lowerCAmelCase_       :    Dict								=			self.image_size // 2
        lowerCAmelCase_       :    str								=			pixel_values[:, :, :image_size, :image_size]
        lowerCAmelCase_       :    Optional[Any]								=			model(SCREAMING_SNAKE_CASE_   ,      interpolate_pos_encoding=SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Union[str, Any]								=			(image_size // self.patch_size) ** 2 + 1
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, seq_length, self.hidden_size)	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				Any   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]	):
        lowerCAmelCase_       :    List[str]								=			self.type_sequence_label_size
        lowerCAmelCase_       :    Optional[int]								=			TFViTForImageClassification(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    List[Any]								=			model(SCREAMING_SNAKE_CASE_   ,      labels=SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.type_sequence_label_size)	)

        # Test with an image with different size than the one specified in config.
        lowerCAmelCase_       :    str								=			self.image_size // 2
        lowerCAmelCase_       :    Any								=			pixel_values[:, :, :image_size, :image_size]
        lowerCAmelCase_       :    List[str]								=			model(SCREAMING_SNAKE_CASE_   ,      interpolate_pos_encoding=SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.type_sequence_label_size)	)

        # test greyscale images
        lowerCAmelCase_       :    List[str]								=			1
        lowerCAmelCase_       :    int								=			TFViTForImageClassification(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Dict								=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
        lowerCAmelCase_       :    str								=			model(SCREAMING_SNAKE_CASE_	)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.type_sequence_label_size)	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        lowerCAmelCase_       :    str								=			self.prepare_config_and_inputs()
        lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_       :    Union[str, Any]								=			config_and_inputs
        lowerCAmelCase_       :    int								=			{'pixel_values': pixel_values}
        return config, inputs_dict







@require_tf
class 				UpperCamelCase__       (     lowercase_, lowercase_, unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
      _SCREAMING_SNAKE_CASE      =					(
          {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
          if is_tf_available()
          else {}
      )

      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					False



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_       :    Optional[int]								=			TFViTModelTester(self	)
        lowerCAmelCase_       :    Optional[int]								=			ConfigTester(self   ,      config_class=SCREAMING_SNAKE_CASE_   ,      has_text_modality=SCREAMING_SNAKE_CASE_   ,      hidden_size=3_7	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        self.config_tester.run_common_tests()



      @unittest.skip(reason='ViT does not use inputs_embeds'	)
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        pass



      @unittest.skip(reason='ViT does not use inputs_embeds'	)
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        pass



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_						,lowerCAmelCase_       :    Dict								=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          lowerCAmelCase_       :    Optional[int]								=			model_class(SCREAMING_SNAKE_CASE_	)
          self.assertIsInstance(model.get_input_embeddings()   ,      (tf.keras.layers.Layer)	)
          lowerCAmelCase_       :    List[str]								=			model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_   ,      tf.keras.layers.Layer	)	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple	):
        lowerCAmelCase_						,lowerCAmelCase_       :    List[str]								=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          lowerCAmelCase_       :    Union[str, Any]								=			model_class(SCREAMING_SNAKE_CASE_	)
          lowerCAmelCase_       :    str								=			inspect.signature(model.call	)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          lowerCAmelCase_       :    Dict								=			[*signature.parameters.keys()]

          lowerCAmelCase_       :    List[str]								=			['pixel_values']
          self.assertListEqual(arg_names[:1]   ,      SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]	):
        lowerCAmelCase_       :    List[str]								=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_       :    Optional[int]								=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)





      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple	):
        lowerCAmelCase_       :    List[Any]								=			TFViTModel.from_pretrained('google/vit-base-patch16-224'	)
        self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)




def     UpperCamelCase_   (   )						->       Any:

  """simple docstring"""




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







@require_tf
@require_vision
class 				UpperCamelCase__       (     unittest.TestCase  ):




      """simple docstring"""




      @cached_property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'	) if is_vision_available() else None





      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        lowerCAmelCase_       :    Dict								=			TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'	)

        lowerCAmelCase_       :    str								=			self.default_image_processor
        lowerCAmelCase_       :    Optional[int]								=			prepare_img()
        lowerCAmelCase_       :    Dict								=			image_processor(images=SCREAMING_SNAKE_CASE_   ,      return_tensors='tf'	)

        # forward pass
        lowerCAmelCase_       :    List[str]								=			model(**SCREAMING_SNAKE_CASE_	)

        # verify the logits
        lowerCAmelCase_       :    List[str]								=			tf.TensorShape((1, 1_0_0_0)	)
        self.assertEqual(outputs.logits.shape   ,      SCREAMING_SNAKE_CASE_	)

        lowerCAmelCase_       :    List[Any]								=			tf.constant([-0.27_44, 0.82_15, -0.08_36]	)

        tf.debugging.assert_near(outputs.logits[0, :3]   ,      SCREAMING_SNAKE_CASE_   ,      atol=1E-4	)










"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowercase__		:    Optional[int]				   =     logging.get_logger(__name__)







class 				UpperCamelCase__       (     lowercase_, lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""maskformer-swin"""

      _SCREAMING_SNAKE_CASE      =					{
          """num_attention_heads""": """num_heads""",
          """num_hidden_layers""": """num_layers""",
      }



      def __init__(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2_2_4   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=4   ,      SCREAMING_SNAKE_CASE_    :				int=3   ,      SCREAMING_SNAKE_CASE_    :				Dict=9_6   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=[2, 2, 6, 2]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=[3, 6, 1_2, 2_4]   ,      SCREAMING_SNAKE_CASE_    :				List[str]=7   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=4.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=True   ,      SCREAMING_SNAKE_CASE_    :				Tuple=0.0   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=0.0   ,      SCREAMING_SNAKE_CASE_    :				int=0.1   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]="gelu"   ,      SCREAMING_SNAKE_CASE_    :				Dict=False   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=0.02   ,      SCREAMING_SNAKE_CASE_    :				int=1E-5   ,      SCREAMING_SNAKE_CASE_    :				Dict=None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=None   ,      **SCREAMING_SNAKE_CASE_    :				str   ,      ):
        super().__init__(**SCREAMING_SNAKE_CASE_	)

        lowerCAmelCase_       :    Dict								=			image_size
        lowerCAmelCase_       :    Optional[Any]								=			patch_size
        lowerCAmelCase_       :    Optional[int]								=			num_channels
        lowerCAmelCase_       :    List[str]								=			embed_dim
        lowerCAmelCase_       :    Dict								=			depths
        lowerCAmelCase_       :    Optional[Any]								=			len(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Tuple								=			num_heads
        lowerCAmelCase_       :    List[str]								=			window_size
        lowerCAmelCase_       :    Any								=			mlp_ratio
        lowerCAmelCase_       :    Any								=			qkv_bias
        lowerCAmelCase_       :    List[Any]								=			hidden_dropout_prob
        lowerCAmelCase_       :    List[Any]								=			attention_probs_dropout_prob
        lowerCAmelCase_       :    Tuple								=			drop_path_rate
        lowerCAmelCase_       :    List[str]								=			hidden_act
        lowerCAmelCase_       :    Any								=			use_absolute_embeddings
        lowerCAmelCase_       :    Optional[Any]								=			layer_norm_eps
        lowerCAmelCase_       :    str								=			initializer_range
        # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
        # this indicates the channel dimension after the last stage of the model
        lowerCAmelCase_       :    List[str]								=			int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE_	) - 1)	)
        lowerCAmelCase_       :    List[Any]								=			['stem'] + [F"stage{idx}" for idx in range(1   ,      len(SCREAMING_SNAKE_CASE_	) + 1	)]
        lowerCAmelCase_						,lowerCAmelCase_       :    Tuple								=			get_aligned_output_features_output_indices(
            out_features=SCREAMING_SNAKE_CASE_   ,      out_indices=SCREAMING_SNAKE_CASE_   ,      stage_names=self.stage_names	)





