




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 			a     :

     '''simple docstring'''





     def __init__(	self   :			Dict					,    __snake_case   :			List[Any]					,    __snake_case   :			Optional[int]=13					,    __snake_case   :			List[str]=32					,    __snake_case   :			List[Any]=2					,    __snake_case   :			List[Any]=3					,    __snake_case   :			List[str]=16					,    __snake_case   :			List[str]=[1, 2, 1]					,    __snake_case   :			Union[str, Any]=[2, 2, 4]					,    __snake_case   :			Dict=2					,    __snake_case   :			List[str]=2.0					,    __snake_case   :			Optional[Any]=True					,    __snake_case   :			Dict=0.0					,    __snake_case   :			Optional[int]=0.0					,    __snake_case   :			List[Any]=0.1					,    __snake_case   :			List[Any]="gelu"					,    __snake_case   :			Any=False					,    __snake_case   :			Tuple=True					,    __snake_case   :			str=0.02					,    __snake_case   :			List[Any]=1E-5					,    __snake_case   :			List[str]=True					,    __snake_case   :			List[str]=None					,    __snake_case   :			str=True					,    __snake_case   :			Optional[Any]=10					,    __snake_case   :			str=8					,    ):
         UpperCAmelCase_							 =     parent
         UpperCAmelCase_							 =     batch_size
         UpperCAmelCase_							 =     image_size
         UpperCAmelCase_							 =     patch_size
         UpperCAmelCase_							 =     num_channels
         UpperCAmelCase_							 =     embed_dim
         UpperCAmelCase_							 =     depths
         UpperCAmelCase_							 =     num_heads
         UpperCAmelCase_							 =     window_size
         UpperCAmelCase_							 =     mlp_ratio
         UpperCAmelCase_							 =     qkv_bias
         UpperCAmelCase_							 =     hidden_dropout_prob
         UpperCAmelCase_							 =     attention_probs_dropout_prob
         UpperCAmelCase_							 =     drop_path_rate
         UpperCAmelCase_							 =     hidden_act
         UpperCAmelCase_							 =     use_absolute_embeddings
         UpperCAmelCase_							 =     patch_norm
         UpperCAmelCase_							 =     layer_norm_eps
         UpperCAmelCase_							 =     initializer_range
         UpperCAmelCase_							 =     is_training
         UpperCAmelCase_							 =     scope
         UpperCAmelCase_							 =     use_labels
         UpperCAmelCase_							 =     type_sequence_label_size
         UpperCAmelCase_							 =     encoder_stride
     def   lowerCamelCase_   (	self   :			Any				):
         UpperCAmelCase_							 =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

         UpperCAmelCase_							 =     self.get_config()

         return config, pixel_values, labels
     def   lowerCamelCase_   (	self   :			int				):
         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   lowerCamelCase_   (	self   :			Union[str, Any]					,    __snake_case   :			Dict					,    __snake_case   :			int					,    __snake_case   :			List[Any]				):
         UpperCAmelCase_							 =     TFSwinModel(config=_lowerCamelCase				)
         UpperCAmelCase_							 =     model(_lowerCamelCase				)

         UpperCAmelCase_							 =     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
         UpperCAmelCase_							 =     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   lowerCamelCase_   (	self   :			Optional[int]					,    __snake_case   :			int					,    __snake_case   :			str					,    __snake_case   :			List[Any]				):
         UpperCAmelCase_							 =     TFSwinForMaskedImageModeling(config=_lowerCamelCase				)
         UpperCAmelCase_							 =     model(_lowerCamelCase				)
         self.parent.assertEqual(
             result.logits.shape					,    (self.batch_size, self.num_channels, self.image_size, self.image_size)				)

         # test greyscale images
         UpperCAmelCase_							 =     1
         UpperCAmelCase_							 =     TFSwinForMaskedImageModeling(_lowerCamelCase				)

         UpperCAmelCase_							 =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
         UpperCAmelCase_							 =     model(_lowerCamelCase				)
         self.parent.assertEqual(result.logits.shape					,    (self.batch_size, 1, self.image_size, self.image_size)				)
     def   lowerCamelCase_   (	self   :			int					,    __snake_case   :			List[str]					,    __snake_case   :			Tuple					,    __snake_case   :			Tuple				):
         UpperCAmelCase_							 =     self.type_sequence_label_size
         UpperCAmelCase_							 =     TFSwinForImageClassification(_lowerCamelCase				)
         UpperCAmelCase_							 =     model(_lowerCamelCase					,    labels=_lowerCamelCase				)
         self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.type_sequence_label_size)				)

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




     def   lowerCamelCase_   (	self   :			str				):
         UpperCAmelCase_							 =     self.prepare_config_and_inputs()
         UpperCAmelCase_							 =     config_and_inputs
         UpperCAmelCase_							 =     {'''pixel_values''': pixel_values}
         return config, inputs_dict



@require_tf
class 			a     (   __UpperCAmelCase	,   __UpperCAmelCase	,   unittest.TestCase			):

     '''simple docstring'''





     lowerCAmelCase				:       int   		=  (
         (
             TFSwinModel,
             TFSwinForImageClassification,
             TFSwinForMaskedImageModeling,
         )
         if is_tf_available()
         else ()
     )
     lowerCAmelCase				:       int   		=  (
         {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
         if is_tf_available()
         else {}
     )

     lowerCAmelCase				:       Tuple   		=  False
     lowerCAmelCase				:       Optional[Any]   		=  False
     lowerCAmelCase				:       str   		=  False
     lowerCAmelCase				:       Any   		=  False
     def   lowerCamelCase_   (	self   :			Union[str, Any]				):
         UpperCAmelCase_							 =     TFSwinModelTester(self				)
         UpperCAmelCase_							 =     ConfigTester(self					,    config_class=_lowerCamelCase					,    embed_dim=37				)
     def   lowerCamelCase_   (	self   :			str				):
         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   lowerCamelCase_   (	self   :			int				):
         return
     def   lowerCamelCase_   (	self   :			Any				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase				)
     def   lowerCamelCase_   (	self   :			List[Any]				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase				)
     def   lowerCamelCase_   (	self   :			List[str]				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase				)
     @unittest.skip(reason='''Swin does not use inputs_embeds'''				)
     def   lowerCamelCase_   (	self   :			Tuple				):
         pass
     def   lowerCamelCase_   (	self   :			str				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             UpperCAmelCase_							 =     model_class(_lowerCamelCase				)
             self.assertIsInstance(model.get_input_embeddings()					,    tf.keras.layers.Layer				)
             UpperCAmelCase_							 =     model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCamelCase					,    tf.keras.layers.Dense				)				)
     def   lowerCamelCase_   (	self   :			Tuple				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs_for_common()

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

             UpperCAmelCase_							 =     ['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,    _lowerCamelCase				)
     def   lowerCamelCase_   (	self   :			Any				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase_							 =     True

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

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             UpperCAmelCase_							 =     True
             UpperCAmelCase_							 =     config.window_size**2
             UpperCAmelCase_							 =     model_class(_lowerCamelCase				)
             UpperCAmelCase_							 =     model(**self._prepare_for_class(_lowerCamelCase					,    _lowerCamelCase				)				)
             UpperCAmelCase_							 =     outputs.attentions
             self.assertEqual(len(_lowerCamelCase				)					,    _lowerCamelCase				)

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

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

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

             UpperCAmelCase_							 =     outputs.attentions

             self.assertEqual(len(_lowerCamelCase				)					,    _lowerCamelCase				)

             self.assertListEqual(
                 list(self_attentions[0].shape[-3:]				)					,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]					,    )
     def   lowerCamelCase_   (	self   :			Optional[Any]					,    __snake_case   :			Any					,    __snake_case   :			Optional[Any]					,    __snake_case   :			Union[str, Any]					,    __snake_case   :			Optional[int]				):
         UpperCAmelCase_							 =     model_class(_lowerCamelCase				)
         UpperCAmelCase_							 =     model(**self._prepare_for_class(_lowerCamelCase					,    _lowerCamelCase				)				)
         UpperCAmelCase_							 =     outputs.hidden_states

         UpperCAmelCase_							 =     getattr(
             self.model_tester					,    '''expected_num_hidden_layers'''					,    len(self.model_tester.depths				) + 1				)
         self.assertEqual(len(_lowerCamelCase				)					,    _lowerCamelCase				)

         # Swin has a different seq_length
         UpperCAmelCase_							 =     to_atuple(config.patch_size				)

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

         UpperCAmelCase_							 =     outputs.reshaped_hidden_states
         self.assertEqual(len(_lowerCamelCase				)					,    _lowerCamelCase				)

         UpperCAmelCase_							 =     reshaped_hidden_states[0].shape

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

         self.assertListEqual(
             list(reshaped_hidden_states.shape[-2:]				)					,    [num_patches, self.model_tester.embed_dim]					,    )
     def   lowerCamelCase_   (	self   :			Optional[Any]				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase_							 =     to_atuple(self.model_tester.image_size				)

         for model_class in self.all_model_classes:
             UpperCAmelCase_							 =     True
             self.check_hidden_states_output(_lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase				)

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

             self.check_hidden_states_output(_lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase				)
     def   lowerCamelCase_   (	self   :			Any				):
         UpperCAmelCase_							 =     self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase_							 =     3

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

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

         for model_class in self.all_model_classes:
             UpperCAmelCase_							 =     True
             self.check_hidden_states_output(_lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    (padded_height, padded_width)				)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCAmelCase_							 =     True
             self.check_hidden_states_output(_lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    (padded_height, padded_width)				)




     @slow
     def   lowerCamelCase_   (	self   :			List[str]				):
         for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_							 =     TFSwinModel.from_pretrained(_lowerCamelCase				)
             self.assertIsNotNone(_lowerCamelCase				)



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

     '''simple docstring'''





     @cached_property
     def   lowerCamelCase_   (	self   :			List[str]				):
         return (
             AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''				)
             if is_vision_available()
             else None
         )




     @slow
     def   lowerCamelCase_   (	self   :			Optional[int]				):
         UpperCAmelCase_							 =     TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''				)
         UpperCAmelCase_							 =     self.default_image_processor

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

         # forward pass
         UpperCAmelCase_							 =     model(_lowerCamelCase				)

         # verify the logits
         UpperCAmelCase_							 =     tf.TensorShape((1, 10_00)				)
         self.assertEqual(outputs.logits.shape					,    _lowerCamelCase				)
         UpperCAmelCase_							 =     tf.constant([-0.0_948, -0.6_454, -0.0_921]				)
         self.assertTrue(np.allclose(outputs.logits[0, :3]					,    _lowerCamelCase					,    atol=1E-4				)				)












"""simple docstring"""





from ctypes import c_float, sizeof
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union


if TYPE_CHECKING:
    from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore





class 			snake_case		(   __UpperCAmelCase   ):
      lowerCamelCase__						= c_float

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       return sizeof(self.value						)







def 		lowerCAmelCase_	(  lowercase_   :							int ,      lowercase_   :							int ,      lowercase_   :							int = 0		):

 '''simple docstring'''






 if dimension <= 0:
  __SCREAMING_SNAKE_CASE :			str												=       fixed_dimension

 dimension -= num_token_to_add
 return dimension




def 		lowerCAmelCase_	(  lowercase_   :							int ,      lowercase_   :							ParameterFormat		):

 '''simple docstring'''






 return num_parameters * dtype.size






def 		lowerCAmelCase_	(  lowercase_   :							str		):

 '''simple docstring'''






 from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore

 try:
  return AutoProcessor.from_pretrained(lowercase_		)
 except (ValueError, OSError, KeyError):
  __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			int												=       None, None
  try:
   __SCREAMING_SNAKE_CASE :			Optional[Any]												=       AutoTokenizer.from_pretrained(lowercase_		)
  except (OSError, KeyError):
   pass
  try:
   __SCREAMING_SNAKE_CASE :			Optional[Any]												=       AutoFeatureExtractor.from_pretrained(lowercase_		)
  except (OSError, KeyError):
   pass

  if tokenizer is not None and feature_extractor is not None:
   raise ValueError(
       F'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''		)
  elif tokenizer is None and feature_extractor is None:
   return None
  elif tokenizer is not None:
   return tokenizer
  else:
   return feature_extractor

