


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





      '''simple docstring'''






      def __init__(self   :   Any       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   int=13       ,						UpperCamelCase   :   Tuple=32       ,						UpperCamelCase   :   int=2       ,						UpperCamelCase   :   Union[str, Any]=3       ,						UpperCamelCase   :   str=16       ,						UpperCamelCase   :   List[str]=[1, 2, 1]       ,						UpperCamelCase   :   Optional[int]=[2, 2, 4]       ,						UpperCamelCase   :   Union[str, Any]=2       ,						UpperCamelCase   :   Optional[Any]=2.0       ,						UpperCamelCase   :   Tuple=True       ,						UpperCamelCase   :   Tuple=0.0       ,						UpperCamelCase   :   str=0.0       ,						UpperCamelCase   :   Any=0.1       ,						UpperCamelCase   :   List[str]="gelu"       ,						UpperCamelCase   :   int=False       ,						UpperCamelCase   :   Optional[Any]=True       ,						UpperCamelCase   :   Union[str, Any]=0.02       ,						UpperCamelCase   :   Union[str, Any]=1E-5       ,						UpperCamelCase   :   Union[str, Any]=True       ,						UpperCamelCase   :   Optional[Any]=None       ,						UpperCamelCase   :   Optional[int]=True       ,						UpperCamelCase   :   Union[str, Any]=10       ,						UpperCamelCase   :   Any=8       ,						):

             '''simple docstring'''







             lowercase__				      =			parent
             lowercase__				      =			batch_size
             lowercase__				      =			image_size
             lowercase__				      =			patch_size
             lowercase__				      =			num_channels
             lowercase__				      =			embed_dim
             lowercase__				      =			depths
             lowercase__				      =			num_heads
             lowercase__				      =			window_size
             lowercase__				      =			mlp_ratio
             lowercase__				      =			qkv_bias
             lowercase__				      =			hidden_dropout_prob
             lowercase__				      =			attention_probs_dropout_prob
             lowercase__				      =			drop_path_rate
             lowercase__				      =			hidden_act
             lowercase__				      =			use_absolute_embeddings
             lowercase__				      =			patch_norm
             lowercase__				      =			layer_norm_eps
             lowercase__				      =			initializer_range
             lowercase__				      =			is_training
             lowercase__				      =			scope
             lowercase__				      =			use_labels
             lowercase__				      =			type_sequence_label_size
             lowercase__				      =			encoder_stride



      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

             lowercase__				      =			self.get_config()

             return config, pixel_values, labels



      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             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 				UpperCamelCase__		(self   :   str       ,						UpperCamelCase   :   List[str]       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			TFSwinModel(config=_UpperCAmelCase			)
             lowercase__				      =			model(_UpperCAmelCase			)

             lowercase__				      =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths			) - 1))
             lowercase__				      =			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 				UpperCamelCase__		(self   :   Dict       ,						UpperCamelCase   :   Optional[Any]       ,						UpperCamelCase   :   List[str]       ,						UpperCamelCase   :   Any			):

             '''simple docstring'''







             lowercase__				      =			TFSwinForMaskedImageModeling(config=_UpperCAmelCase			)
             lowercase__				      =			model(_UpperCAmelCase			)
             self.parent.assertEqual(
                 result.logits.shape       ,						(self.batch_size, self.num_channels, self.image_size, self.image_size)			)

             # test greyscale images
             lowercase__				      =			1
             lowercase__				      =			TFSwinForMaskedImageModeling(_UpperCAmelCase			)

             lowercase__				      =			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
             lowercase__				      =			model(_UpperCAmelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, 1, self.image_size, self.image_size)			)



      def 				UpperCamelCase__		(self   :   Any       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   List[str]       ,						UpperCamelCase   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.type_sequence_label_size
             lowercase__				      =			TFSwinForImageClassification(_UpperCAmelCase			)
             lowercase__				      =			model(_UpperCAmelCase       ,						labels=_UpperCAmelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.type_sequence_label_size)			)

             # test greyscale images
             lowercase__				      =			1
             lowercase__				      =			TFSwinForImageClassification(_UpperCAmelCase			)
             lowercase__				      =			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
             lowercase__				      =			model(_UpperCAmelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.type_sequence_label_size)			)






      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			self.prepare_config_and_inputs()
             lowercase__      ,lowercase__      ,lowercase__				      =			config_and_inputs
             lowercase__				      =			{'''pixel_values''': pixel_values}
             return config, inputs_dict






@require_tf
class   __lowerCAmelCase						(lowerCAmelCase__  ,					lowerCAmelCase__  ,					unittest.TestCase					):





      '''simple docstring'''






      lowerCAmelCase__ :					List[Any]   =							(
          (
              TFSwinModel,
              TFSwinForImageClassification,
              TFSwinForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__ :					Optional[Any]   =							(
          {"feature-extraction": TFSwinModel, "image-classification": TFSwinForImageClassification}
          if is_tf_available()
          else {}
      )

      lowerCAmelCase__ :					int   =							False
      lowerCAmelCase__ :					List[Any]   =							False
      lowerCAmelCase__ :					Tuple   =							False
      lowerCAmelCase__ :					Tuple   =							False



      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             lowercase__				      =			TFSwinModelTester(self			)
             lowercase__				      =			ConfigTester(self       ,						config_class=_UpperCAmelCase       ,						embed_dim=37			)



      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             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 				UpperCamelCase__		(self   :   Dict			):

             '''simple docstring'''







             return



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_UpperCAmelCase			)



      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase			)



      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             lowercase__				      =			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 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             pass



      def 				UpperCamelCase__		(self   :   Any			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__				      =			model_class(_UpperCAmelCase			)
                    self.assertIsInstance(model.get_input_embeddings()       ,						tf.keras.layers.Layer			)
                    lowercase__				      =			model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(_UpperCAmelCase       ,						tf.keras.layers.Dense			)			)



      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__				      =			model_class(_UpperCAmelCase			)
                    lowercase__				      =			inspect.signature(model.call			)
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    lowercase__				      =			[*signature.parameters.keys()]

                    lowercase__				      =			['''pixel_values''']
                    self.assertListEqual(arg_names[:1]       ,						_UpperCAmelCase			)



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()
             lowercase__				      =			True

             for model_class in self.all_model_classes:
                    lowercase__				      =			True
                    lowercase__				      =			False
                    lowercase__				      =			True
                    lowercase__				      =			model_class(_UpperCAmelCase			)
                    lowercase__				      =			model(**self._prepare_for_class(_UpperCAmelCase       ,						_UpperCAmelCase			)			)
                    lowercase__				      =			outputs.attentions
                    lowercase__				      =			len(self.model_tester.depths			)
                    self.assertEqual(len(_UpperCAmelCase			)       ,						_UpperCAmelCase			)

                    # check that output_attentions also work using config
                    del inputs_dict["output_attentions"]
                    lowercase__				      =			True
                    lowercase__				      =			config.window_size**2
                    lowercase__				      =			model_class(_UpperCAmelCase			)
                    lowercase__				      =			model(**self._prepare_for_class(_UpperCAmelCase       ,						_UpperCAmelCase			)			)
                    lowercase__				      =			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]       ,						)
                    lowercase__				      =			len(_UpperCAmelCase			)

                    # Check attention is always last and order is fine
                    lowercase__				      =			True
                    lowercase__				      =			True
                    lowercase__				      =			model_class(_UpperCAmelCase			)
                    lowercase__				      =			model(**self._prepare_for_class(_UpperCAmelCase       ,						_UpperCAmelCase			)			)

                    if hasattr(self.model_tester       ,						'''num_hidden_states_types'''			):
                           lowercase__				      =			self.model_tester.num_hidden_states_types
                    else:
                           # also another +1 for reshaped_hidden_states
                           lowercase__				      =			2
                    self.assertEqual(out_len + added_hidden_states       ,						len(_UpperCAmelCase			)			)

                    lowercase__				      =			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 				UpperCamelCase__		(self   :   List[str]       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			model_class(_UpperCAmelCase			)
             lowercase__				      =			model(**self._prepare_for_class(_UpperCAmelCase       ,						_UpperCAmelCase			)			)
             lowercase__				      =			outputs.hidden_states

             lowercase__				      =			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
             lowercase__				      =			to_atuple(config.patch_size			)

             lowercase__				      =			(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]       ,						)

             lowercase__				      =			outputs.reshaped_hidden_states
             self.assertEqual(len(_UpperCAmelCase			)       ,						_UpperCAmelCase			)

             lowercase__      ,lowercase__      ,lowercase__      ,lowercase__				      =			reshaped_hidden_states[0].shape

             lowercase__				      =			tf.reshape(reshaped_hidden_states[0]       ,						(batch_size, num_channels, height * width)			)
             lowercase__				      =			tf.transpose(_UpperCAmelCase       ,						(0, 2, 1)			)

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



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()

             lowercase__				      =			to_atuple(self.model_tester.image_size			)

             for model_class in self.all_model_classes:
                    lowercase__				      =			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"]
                    lowercase__				      =			True

                    self.check_hidden_states_output(_UpperCAmelCase       ,						_UpperCAmelCase       ,						_UpperCAmelCase       ,						_UpperCAmelCase			)



      def 				UpperCamelCase__		(self   :   List[str]			):

             '''simple docstring'''







             lowercase__      ,lowercase__				      =			self.model_tester.prepare_config_and_inputs_for_common()
             lowercase__				      =			3

             lowercase__				      =			to_atuple(self.model_tester.image_size			)
             lowercase__				      =			to_atuple(config.patch_size			)

             lowercase__				      =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
             lowercase__				      =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

             for model_class in self.all_model_classes:
                    lowercase__				      =			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"]
                    lowercase__				      =			True
                    self.check_hidden_states_output(_UpperCAmelCase       ,						_UpperCAmelCase       ,						_UpperCAmelCase       ,						(padded_height, padded_width)			)






      @slow
      def 				UpperCamelCase__		(self   :   List[Any]			):

             '''simple docstring'''







             for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__				      =			TFSwinModel.from_pretrained(_UpperCAmelCase			)
                    self.assertIsNotNone(_UpperCAmelCase			)






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





      '''simple docstring'''






      @cached_property
      def 				UpperCamelCase__		(self   :   Any			):

             '''simple docstring'''







             return (
                 AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''			)
                 if is_vision_available()
                 else None
             )






      @slow
      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''			)
             lowercase__				      =			self.default_image_processor

             lowercase__				      =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
             lowercase__				      =			image_processor(images=_UpperCAmelCase       ,						return_tensors='''tf'''			)

             # forward pass
             lowercase__				      =			model(_UpperCAmelCase			)

             # verify the logits
             lowercase__				      =			tf.TensorShape((1, 1000)			)
             self.assertEqual(outputs.logits.shape       ,						_UpperCAmelCase			)
             lowercase__				      =			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_sentencepiece_available, is_torch_available


lowerCamelCase							:       Tuple  						=     {'configuration_bert_generation': ['BertGenerationConfig']}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase							:       Union[str, Any]  						=     ['BertGenerationTokenizer']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase							:       Dict  						=     [
     'BertGenerationDecoder',
     'BertGenerationEncoder',
     'BertGenerationPreTrainedModel',
     'load_tf_weights_in_bert_generation',
 ]


if TYPE_CHECKING:
 from .configuration_bert_generation import BertGenerationConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_bert_generation import BertGenerationTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_bert_generation import (
      BertGenerationDecoder,
      BertGenerationEncoder,
      BertGenerationPreTrainedModel,
      load_tf_weights_in_bert_generation,
  )

else:
 import sys

 lowerCamelCase							:       int  						=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




