




'''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				:							Dict					,  SCREAMING_SNAKE_CASE_				:							Dict					,  SCREAMING_SNAKE_CASE_				:							List[str]=13					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=32					,  SCREAMING_SNAKE_CASE_				:							Dict=2					,  SCREAMING_SNAKE_CASE_				:							str=3					,  SCREAMING_SNAKE_CASE_				:							str=16					,  SCREAMING_SNAKE_CASE_				:							int=[1, 2, 1]					,  SCREAMING_SNAKE_CASE_				:							List[str]=[2, 2, 4]					,  SCREAMING_SNAKE_CASE_				:							Any=2					,  SCREAMING_SNAKE_CASE_				:							List[Any]=2.0					,  SCREAMING_SNAKE_CASE_				:							Any=True					,  SCREAMING_SNAKE_CASE_				:							List[Any]=0.0					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=0.0					,  SCREAMING_SNAKE_CASE_				:							str=0.1					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]="gelu"					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=False					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=True					,  SCREAMING_SNAKE_CASE_				:							List[Any]=0.02					,  SCREAMING_SNAKE_CASE_				:							Dict=1E-5					,  SCREAMING_SNAKE_CASE_				:							Any=True					,  SCREAMING_SNAKE_CASE_				:							List[str]=None					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=True					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=10					,  SCREAMING_SNAKE_CASE_				:							List[str]=8					,  )			->		None:







       '''simple docstring'''



       A:		Dict          =		parent
       A:		Optional[int]          =		batch_size
       A:		Dict          =		image_size
       A:		Optional[Any]          =		patch_size
       A:		Tuple          =		num_channels
       A:		int          =		embed_dim
       A:		int          =		depths
       A:		Dict          =		num_heads
       A:		Optional[int]          =		window_size
       A:		Optional[Any]          =		mlp_ratio
       A:		Any          =		qkv_bias
       A:		Any          =		hidden_dropout_prob
       A:		int          =		attention_probs_dropout_prob
       A:		Optional[Any]          =		drop_path_rate
       A:		List[str]          =		hidden_act
       A:		Optional[int]          =		use_absolute_embeddings
       A:		Any          =		patch_norm
       A:		Optional[Any]          =		layer_norm_eps
       A:		List[Any]          =		initializer_range
       A:		List[str]          =		is_training
       A:		Dict          =		scope
       A:		Dict          =		use_labels
       A:		Any          =		type_sequence_label_size
       A:		Optional[int]          =		encoder_stride



  def 					_snake_case     ( self				:							Dict	)			->		int:







       '''simple docstring'''



       A:		int          =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

       A:		int          =		None
       if self.use_labels:
            A:		Dict          =		ids_tensor([self.batch_size]					,  self.type_sequence_label_size	)

       A:		str          =		self.get_config()

       return config, pixel_values, labels



  def 					_snake_case     ( self				:							Dict	)			->		List[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 					_snake_case     ( self				:							int					,  SCREAMING_SNAKE_CASE_				:							Optional[int]					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		int:







       '''simple docstring'''



       A:		Optional[Any]          =		TFSwinModel(config=SCREAMING_SNAKE_CASE_	)
       A:		int          =		model(SCREAMING_SNAKE_CASE_	)

       A:		List[str]          =		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
       A:		List[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 					_snake_case     ( self				:							Tuple					,  SCREAMING_SNAKE_CASE_				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		Any:







       '''simple docstring'''



       A:		Dict          =		TFSwinForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_	)
       A:		str          =		model(SCREAMING_SNAKE_CASE_	)
       self.parent.assertEqual(
           result.logits.shape					,  (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

       # test greyscale images
       A:		Dict          =		1
       A:		Union[str, Any]          =		TFSwinForMaskedImageModeling(SCREAMING_SNAKE_CASE_	)

       A:		List[Any]          =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
       A:		Any          =		model(SCREAMING_SNAKE_CASE_	)
       self.parent.assertEqual(result.logits.shape					,  (self.batch_size, 1, self.image_size, self.image_size)	)



  def 					_snake_case     ( self				:							Any					,  SCREAMING_SNAKE_CASE_				:							Optional[int]					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							Dict	)			->		Optional[int]:







       '''simple docstring'''



       A:		Optional[int]          =		self.type_sequence_label_size
       A:		Any          =		TFSwinForImageClassification(SCREAMING_SNAKE_CASE_	)
       A:		Union[str, Any]          =		model(SCREAMING_SNAKE_CASE_					,  labels=SCREAMING_SNAKE_CASE_	)
       self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size)	)

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




  def 					_snake_case     ( self				:							Tuple	)			->		Any:







       '''simple docstring'''



       A:		Union[str, Any]          =		self.prepare_config_and_inputs()
       A     ,    A     ,    A:		List[str]          =		config_and_inputs
       A:		Dict          =		{'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_tf
class 						lowerCAmelCase_	(       UpperCAmelCase_       ,					UpperCAmelCase_       ,					unittest.TestCase							):







  '''simple docstring'''

  UpperCamelCase_			:      Union[str, Any]										=				(
      (
          TFSwinModel,
          TFSwinForImageClassification,
          TFSwinForMaskedImageModeling,
      )
      if is_tf_available()
      else ()
  )
  UpperCamelCase_			:      Tuple										=				(
      {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
      if is_tf_available()
      else {}
  )

  UpperCamelCase_			:      Any										=				False
  UpperCamelCase_			:      Tuple										=				False
  UpperCamelCase_			:      List[str]										=				False
  UpperCamelCase_			:      List[Any]										=				False



  def 					_snake_case     ( self				:							Dict	)			->		List[Any]:







       '''simple docstring'''



       A:		Optional[Any]          =		TFSwinModelTester(self	)
       A:		int          =		ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  embed_dim=37	)



  def 					_snake_case     ( self				:							Optional[int]	)			->		str:







       '''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 					_snake_case     ( self				:							List[Any]	)			->		Optional[Any]:







       '''simple docstring'''



       return



  def 					_snake_case     ( self				:							List[str]	)			->		Optional[int]:







       '''simple docstring'''



       A:		Tuple          =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)



  def 					_snake_case     ( self				:							Tuple	)			->		str:







       '''simple docstring'''



       A:		Optional[int]          =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_	)



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







       '''simple docstring'''



       A:		Dict          =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)



  @unittest.skip(reason='''Swin does not use inputs_embeds'''	)
  def 					_snake_case     ( self				:							Tuple	)			->		List[str]:







       '''simple docstring'''



       pass



  def 					_snake_case     ( self				:							Any	)			->		List[str]:







       '''simple docstring'''



       A     ,    A:		str          =		self.model_tester.prepare_config_and_inputs_for_common()

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



  def 					_snake_case     ( self				:							str	)			->		Optional[int]:







       '''simple docstring'''



       A     ,    A:		str          =		self.model_tester.prepare_config_and_inputs_for_common()

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

            A:		Optional[Any]          =		['''pixel_values''']
            self.assertListEqual(arg_names[:1]					,  SCREAMING_SNAKE_CASE_	)



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







       '''simple docstring'''



       A     ,    A:		List[Any]          =		self.model_tester.prepare_config_and_inputs_for_common()
       A:		Union[str, Any]          =		True

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

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

            self.assertListEqual(
                list(attentions[0].shape[-3:]	)					,  [self.model_tester.num_heads[0], window_size_squared, window_size_squared]					,  )
            A:		List[str]          =		len(SCREAMING_SNAKE_CASE_	)

            # Check attention is always last and order is fine
            A:		List[Any]          =		True
            A:		Optional[Any]          =		True
            A:		Dict          =		model_class(SCREAMING_SNAKE_CASE_	)
            A:		List[str]          =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)	)

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

            A:		List[Any]          =		outputs.attentions

            self.assertEqual(len(SCREAMING_SNAKE_CASE_	)					,  SCREAMING_SNAKE_CASE_	)

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



  def 					_snake_case     ( self				:							Union[str, Any]					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]					,  SCREAMING_SNAKE_CASE_				:							Tuple					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							List[Any]	)			->		str:







       '''simple docstring'''



       A:		Union[str, Any]          =		model_class(SCREAMING_SNAKE_CASE_	)
       A:		List[str]          =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)	)
       A:		Optional[int]          =		outputs.hidden_states

       A:		Union[str, Any]          =		getattr(
           self.model_tester					,  '''expected_num_hidden_layers'''					,  len(self.model_tester.depths	) + 1	)
       self.assertEqual(len(SCREAMING_SNAKE_CASE_	)					,  SCREAMING_SNAKE_CASE_	)

       # Swin has a different seq_length
       A:		List[str]          =		to_atuple(config.patch_size	)

       A:		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]					,  )

       A:		Any          =		outputs.reshaped_hidden_states
       self.assertEqual(len(SCREAMING_SNAKE_CASE_	)					,  SCREAMING_SNAKE_CASE_	)

       A     ,    A     ,    A     ,    A:		str          =		reshaped_hidden_states[0].shape

       A:		Union[str, Any]          =		tf.reshape(reshaped_hidden_states[0]					,  (batch_size, num_channels, height * width)	)
       A:		int          =		tf.transpose(SCREAMING_SNAKE_CASE_					,  (0, 2, 1)	)

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



  def 					_snake_case     ( self				:							Union[str, Any]	)			->		Optional[int]:







       '''simple docstring'''



       A     ,    A:		int          =		self.model_tester.prepare_config_and_inputs_for_common()

       A:		str          =		to_atuple(self.model_tester.image_size	)

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

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            A:		Tuple          =		True

            self.check_hidden_states_output(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)



  def 					_snake_case     ( self				:							Tuple	)			->		int:







       '''simple docstring'''



       A     ,    A:		str          =		self.model_tester.prepare_config_and_inputs_for_common()
       A:		List[Any]          =		3

       A:		Union[str, Any]          =		to_atuple(self.model_tester.image_size	)
       A:		Dict          =		to_atuple(config.patch_size	)

       A:		Tuple          =		image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
       A:		Dict          =		image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

       for model_class in self.all_model_classes:
            A:		List[Any]          =		True
            self.check_hidden_states_output(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  (padded_height, padded_width)	)

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




  @slow
  def 					_snake_case     ( self				:							Any	)			->		Tuple:







       '''simple docstring'''



       for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A:		Tuple          =		TFSwinModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)






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







  '''simple docstring'''

  @cached_property
  def 					_snake_case     ( self				:							Union[str, Any]	)			->		Any:







       '''simple docstring'''



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




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







       '''simple docstring'''



       A:		List[str]          =		TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''	)
       A:		List[str]          =		self.default_image_processor

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

       # forward pass
       A:		Optional[Any]          =		model(SCREAMING_SNAKE_CASE_	)

       # verify the logits
       A:		int          =		tf.TensorShape((1, 10_00)	)
       self.assertEqual(outputs.logits.shape					,  SCREAMING_SNAKE_CASE_	)
       A:		str          =		tf.constant([-0.0948, -0.6454, -0.0921]	)
       self.assertTrue(np.allclose(outputs.logits[0, :3]					,  SCREAMING_SNAKE_CASE_					,  atol=1E-4	)	)












'''simple docstring'''


from __future__ import annotations

import random






class 						lowerCAmelCase_	:







  '''simple docstring'''

  UpperCamelCase_			:      Optional[int]										=				6



  def __init__( self				:							Dict	)			->		Dict:







       '''simple docstring'''



       A:		Union[str, Any]          =		list(range(1					,  Dice.NUM_SIDES + 1	)	)




  def 					_snake_case     ( self				:							Optional[Any]	)			->		Any:







       '''simple docstring'''



       return random.choice(self.sides	)






def 					SCREAMING_SNAKE_CASE(   __lowercase		,				__lowercase = 2	)			-> list[float]:
     A:		Union[str, Any]          =		[Dice() for i in range(__lowercase	)]
     A:		Union[str, Any]          =		[0] * (len(__lowercase	) * Dice.NUM_SIDES + 1)
     for _ in range(__lowercase	):
          count_of_sum[sum(dice.roll() for dice in dices	)] += 1
     A:		Dict          =		[round((count * 1_0_0) / num_throws		,				2	) for count in count_of_sum]
     return probability[num_dice:]  # remove probability of sums that never appear


if __name__ == "__main__":
     import doctest

     doctest.testmod()






