




'''simple docstring'''





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
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.models.swin.modeling_tf_swin import (
           TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
           TFSwinForImageClassification,
           TFSwinForMaskedImageModeling,
           TFSwinModel,
       )


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor






class     lowercase  :






      def __init__(self			,	a__			,	a__=1_3			,	a__=3_2			,	a__=2			,	a__=3			,	a__=1_6			,	a__=[1, 2, 1]			,	a__=[2, 2, 4]			,	a__=2			,	a__=2.0			,	a__=True			,	a__=0.0			,	a__=0.0			,	a__=0.1			,	a__="gelu"			,	a__=False			,	a__=True			,	a__=0.02			,	a__=1E-5			,	a__=True			,	a__=None			,	a__=True			,	a__=1_0			,	a__=8			,	)							-> List[Any]:
         SCREAMING_SNAKE_CASE__      :					str							    =			parent
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			batch_size
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			image_size
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			patch_size
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			num_channels
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			embed_dim
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			depths
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			num_heads
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			window_size
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			mlp_ratio
         SCREAMING_SNAKE_CASE__      :					Dict							    =			qkv_bias
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			hidden_dropout_prob
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			attention_probs_dropout_prob
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			drop_path_rate
         SCREAMING_SNAKE_CASE__      :					str							    =			hidden_act
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			use_absolute_embeddings
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			patch_norm
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			layer_norm_eps
         SCREAMING_SNAKE_CASE__      :					Dict							    =			initializer_range
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			is_training
         SCREAMING_SNAKE_CASE__      :					Dict							    =			scope
         SCREAMING_SNAKE_CASE__      :					Any							    =			use_labels
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			type_sequence_label_size
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			encoder_stride






      def  A__(self       )							-> Any:
         SCREAMING_SNAKE_CASE__      :					Any							    =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

         SCREAMING_SNAKE_CASE__      :					Any							    =			None
         if self.use_labels:
            SCREAMING_SNAKE_CASE__      :					Any							    =			ids_tensor([self.batch_size]			,	self.type_sequence_label_size       )

         SCREAMING_SNAKE_CASE__      :					Tuple							    =			self.get_config()

         return config, pixel_values, labels






      def  A__(self       )							-> Optional[Any]:
         return SwinConfig(
             image_size=self.image_size			,	patch_size=self.patch_size			,	num_channels=self.num_channels			,	embed_dim=self.embed_dim			,	depths=self.depths			,	num_heads=self.num_heads			,	window_size=self.window_size			,	mlp_ratio=self.mlp_ratio			,	qkv_bias=self.qkv_bias			,	hidden_dropout_prob=self.hidden_dropout_prob			,	attention_probs_dropout_prob=self.attention_probs_dropout_prob			,	drop_path_rate=self.drop_path_rate			,	hidden_act=self.hidden_act			,	use_absolute_embeddings=self.use_absolute_embeddings			,	path_norm=self.patch_norm			,	layer_norm_eps=self.layer_norm_eps			,	initializer_range=self.initializer_range			,	encoder_stride=self.encoder_stride			,	)






      def  A__(self			,	a__			,	a__			,	a__       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Any							    =			TFSwinModel(config=__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			model(__UpperCamelCase       )

         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
         SCREAMING_SNAKE_CASE__      :					str							    =			int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

         self.parent.assertEqual(result.last_hidden_state.shape			,	(self.batch_size, expected_seq_len, expected_dim)       )






      def  A__(self			,	a__			,	a__			,	a__       )							-> int:
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			TFSwinForMaskedImageModeling(config=__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					Dict							    =			model(__UpperCamelCase       )
         self.parent.assertEqual(
             result.logits.shape			,	(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

         # test greyscale images
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			1
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			TFSwinForMaskedImageModeling(__UpperCamelCase       )

         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
         SCREAMING_SNAKE_CASE__      :					str							    =			model(__UpperCamelCase       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, 1, self.image_size, self.image_size)       )






      def  A__(self			,	a__			,	a__			,	a__       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			self.type_sequence_label_size
         SCREAMING_SNAKE_CASE__      :					int							    =			TFSwinForImageClassification(__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			model(__UpperCamelCase			,	labels=__UpperCamelCase       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.type_sequence_label_size)       )

         # test greyscale images
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			1
         SCREAMING_SNAKE_CASE__      :					Any							    =			TFSwinForImageClassification(__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
         SCREAMING_SNAKE_CASE__      :					str							    =			model(__UpperCamelCase       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.type_sequence_label_size)       )






      def  A__(self       )							-> str:
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			self.prepare_config_and_inputs()
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					str							    =			config_and_inputs
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			{'''pixel_values''': pixel_values}
         return config, inputs_dict






@require_tf
class     lowercase  ( __lowerCAmelCase					,      __lowerCAmelCase					,      unittest.TestCase):
      __lowercase					=	(
          (
              TFSwinModel,
              TFSwinForImageClassification,
              TFSwinForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      __lowercase					=	(
          {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
          if is_tf_available()
          else {}
      )

      __lowercase					=	False
      __lowercase					=	False
      __lowercase					=	False
      __lowercase					=	False






      def  A__(self       )							-> List[str]:
         SCREAMING_SNAKE_CASE__      :					Any							    =			TFSwinModelTester(self       )
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			ConfigTester(self			,	config_class=__UpperCamelCase			,	embed_dim=3_7       )






      def  A__(self       )							-> Optional[int]:
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()






      def  A__(self       )							-> Any:
         return






      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCamelCase       )






      def  A__(self       )							-> List[str]:
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase       )






      def  A__(self       )							-> Dict:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase       )






      @unittest.skip(reason='''Swin does not use inputs_embeds'''       )
      def  A__(self       )							-> Dict:
         pass






      def  A__(self       )							-> List[Any]:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					Any							    =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			model_class(__UpperCamelCase       )
            self.assertIsInstance(model.get_input_embeddings()			,	tf.keras.layers.Layer       )
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(__UpperCamelCase			,	tf.keras.layers.Dense       )       )






      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					Dict							    =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			model_class(__UpperCamelCase       )
            SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			inspect.signature(model.call       )
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            SCREAMING_SNAKE_CASE__      :					Dict							    =			[*signature.parameters.keys()]

            SCREAMING_SNAKE_CASE__      :					str							    =			['''pixel_values''']
            self.assertListEqual(arg_names[:1]			,	__UpperCamelCase       )






      def  A__(self       )							-> Optional[Any]:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					List[str]							    =			self.model_tester.prepare_config_and_inputs_for_common()
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			True

         for model_class in self.all_model_classes:
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			True
            SCREAMING_SNAKE_CASE__      :					Any							    =			False
            SCREAMING_SNAKE_CASE__      :					int							    =			True
            SCREAMING_SNAKE_CASE__      :					Dict							    =			model_class(__UpperCamelCase       )
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			model(**self._prepare_for_class(__UpperCamelCase			,	__UpperCamelCase       )       )
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			outputs.attentions
            SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			len(self.model_tester.depths       )
            self.assertEqual(len(__UpperCamelCase       )			,	__UpperCamelCase       )

            # check that output_attentions also work using config
            del inputs_dict["output_attentions"]
            SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			True
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			config.window_size**2
            SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			model_class(__UpperCamelCase       )
            SCREAMING_SNAKE_CASE__      :					str							    =			model(**self._prepare_for_class(__UpperCamelCase			,	__UpperCamelCase       )       )
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			outputs.attentions
            self.assertEqual(len(__UpperCamelCase       )			,	__UpperCamelCase       )

            self.assertListEqual(
                list(attentions[0].shape[-3:]       )			,	[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,	)
            SCREAMING_SNAKE_CASE__      :					Dict							    =			len(__UpperCamelCase       )

            # Check attention is always last and order is fine
            SCREAMING_SNAKE_CASE__      :					Dict							    =			True
            SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			True
            SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			model_class(__UpperCamelCase       )
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			model(**self._prepare_for_class(__UpperCamelCase			,	__UpperCamelCase       )       )

            if hasattr(self.model_tester			,	'''num_hidden_states_types'''       ):
               SCREAMING_SNAKE_CASE__      :					List[str]							    =			self.model_tester.num_hidden_states_types
            else:
               # also another +1 for reshaped_hidden_states
               SCREAMING_SNAKE_CASE__      :					Any							    =			2
            self.assertEqual(out_len + added_hidden_states			,	len(__UpperCamelCase       )       )

            SCREAMING_SNAKE_CASE__      :					int							    =			outputs.attentions

            self.assertEqual(len(__UpperCamelCase       )			,	__UpperCamelCase       )

            self.assertListEqual(
                list(self_attentions[0].shape[-3:]       )			,	[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,	)






      def  A__(self			,	a__			,	a__			,	a__			,	a__       )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			model_class(__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			model(**self._prepare_for_class(__UpperCamelCase			,	__UpperCamelCase       )       )
         SCREAMING_SNAKE_CASE__      :					Dict							    =			outputs.hidden_states

         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			getattr(
             self.model_tester			,	'''expected_num_hidden_layers'''			,	len(self.model_tester.depths       ) + 1       )
         self.assertEqual(len(__UpperCamelCase       )			,	__UpperCamelCase       )

         # Swin has a different seq_length
         SCREAMING_SNAKE_CASE__      :					Any							    =			to_atuple(config.patch_size       )

         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

         self.assertListEqual(
             list(hidden_states[0].shape[-2:]       )			,	[num_patches, self.model_tester.embed_dim]			,	)

         SCREAMING_SNAKE_CASE__      :					List[str]							    =			outputs.reshaped_hidden_states
         self.assertEqual(len(__UpperCamelCase       )			,	__UpperCamelCase       )

         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			reshaped_hidden_states[0].shape

         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			tf.reshape(reshaped_hidden_states[0]			,	(batch_size, num_channels, height * width)       )
         SCREAMING_SNAKE_CASE__      :					Dict							    =			tf.transpose(__UpperCamelCase			,	(0, 2, 1)       )

         self.assertListEqual(
             list(reshaped_hidden_states.shape[-2:]       )			,	[num_patches, self.model_tester.embed_dim]			,	)






      def  A__(self       )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			self.model_tester.prepare_config_and_inputs_for_common()

         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			to_atuple(self.model_tester.image_size       )

         for model_class in self.all_model_classes:
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			True
            self.check_hidden_states_output(__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			True

            self.check_hidden_states_output(__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase       )






      def  A__(self       )							-> Any:
         SCREAMING_SNAKE_CASE__    ,					SCREAMING_SNAKE_CASE__      :					List[str]							    =			self.model_tester.prepare_config_and_inputs_for_common()
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			3

         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			to_atuple(self.model_tester.image_size       )
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			to_atuple(config.patch_size       )

         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

         for model_class in self.all_model_classes:
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			True
            self.check_hidden_states_output(__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase			,	(padded_height, padded_width)       )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            SCREAMING_SNAKE_CASE__      :					List[str]							    =			True
            self.check_hidden_states_output(__UpperCamelCase			,	__UpperCamelCase			,	__UpperCamelCase			,	(padded_height, padded_width)       )






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






         for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			TFSwinModel.from_pretrained(__UpperCamelCase       )
            self.assertIsNotNone(__UpperCamelCase       )






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






      @cached_property
      def  A__(self       )							-> Any:
         return (
             AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''       )
             if is_vision_available()
             else None
         )






      @slow
      def  A__(self       )							-> Tuple:
         SCREAMING_SNAKE_CASE__      :					Any							    =			TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''       )
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			self.default_image_processor

         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
         SCREAMING_SNAKE_CASE__      :					Dict							    =			image_processor(images=__UpperCamelCase			,	return_tensors='''tf'''       )

         # forward pass
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			model(__UpperCamelCase       )

         # verify the logits
         SCREAMING_SNAKE_CASE__      :					Dict							    =			tf.TensorShape((1, 1_0_0_0)       )
         self.assertEqual(outputs.logits.shape			,	__UpperCamelCase       )
         SCREAMING_SNAKE_CASE__      :					Dict							    =			tf.constant([-0.09_48, -0.64_54, -0.09_21]       )
         self.assertTrue(np.allclose(outputs.logits[0, :3]			,	__UpperCamelCase			,	atol=1E-4       )       )







'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


a__         =     {'''configuration_cvt''': ['''CVT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CvtConfig''']}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__         =     [
		    '''CVT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''CvtForImageClassification''',
		    '''CvtModel''',
		    '''CvtPreTrainedModel''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__         =     [
		    '''TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFCvtForImageClassification''',
		    '''TFCvtModel''',
		    '''TFCvtPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_cvt import CVT_PRETRAINED_CONFIG_ARCHIVE_MAP, CvtConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_cvt import (
				    CVT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    CvtForImageClassification,
				    CvtModel,
				    CvtPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_cvt import (
				    TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFCvtForImageClassification,
				    TFCvtModel,
				    TFCvtPreTrainedModel,
				)


else:
		import sys

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