

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






     '''simple docstring'''



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


     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      Optional[Any]:
      UpperCAmelCase			:      Optional[Any]		   =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

      UpperCAmelCase			:      Optional[int]		   =					None
      if self.use_labels:
       UpperCAmelCase			:      Tuple		   =					ids_tensor([self.batch_size]	,     self.type_sequence_label_size			)

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

      return config, pixel_values, labels


     def   UpperCAmelCase_			(						self					:	Union[str, Any]			)		->      List[str]:
      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					:	List[str]	,     lowercase_					:	Optional[int]	,     lowercase_					:	str	,     lowercase_					:	List[str]			)		->      Optional[Any]:
      UpperCAmelCase			:      List[str]		   =					TFSwinModel(config=lowercase_			)
      UpperCAmelCase			:      Dict		   =					model(lowercase_			)

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

      # test greyscale images
      UpperCAmelCase			:      Dict		   =					1
      UpperCAmelCase			:      str		   =					TFSwinForMaskedImageModeling(lowercase_			)

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


     def   UpperCAmelCase_			(						self					:	List[Any]	,     lowercase_					:	Tuple	,     lowercase_					:	Optional[int]	,     lowercase_					:	Any			)		->      List[str]:
      UpperCAmelCase			:      Union[str, Any]		   =					self.type_sequence_label_size
      UpperCAmelCase			:      Dict		   =					TFSwinForImageClassification(lowercase_			)
      UpperCAmelCase			:      Optional[int]		   =					model(lowercase_	,     labels=lowercase_			)
      self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.type_sequence_label_size)			)

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




     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      str:
      UpperCAmelCase			:      Optional[int]		   =					self.prepare_config_and_inputs()
      UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      List[Any]		   =					config_and_inputs
      UpperCAmelCase			:      str		   =					{'pixel_values': pixel_values}
      return config, inputs_dict




@require_tf
class 			A_					(							_snake_case			,  _snake_case			,  unittest.TestCase				):






     '''simple docstring'''



     UpperCAmelCase_		:      Dict    						=    (
         (
             TFSwinModel,
             TFSwinForImageClassification,
             TFSwinForMaskedImageModeling,
         )
         if is_tf_available()
         else ()
     )
     UpperCAmelCase_		:      str    						=    (
         {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
         if is_tf_available()
         else {}
     )

     UpperCAmelCase_		:      Optional[int]    						=    False
     UpperCAmelCase_		:      str    						=    False
     UpperCAmelCase_		:      List[Any]    						=    False
     UpperCAmelCase_		:      Dict    						=    False


     def   UpperCAmelCase_			(						self					:	str			)		->      Optional[int]:
      UpperCAmelCase			:      List[Any]		   =					TFSwinModelTester(self			)
      UpperCAmelCase			:      Optional[int]		   =					ConfigTester(self	,     config_class=lowercase_	,     embed_dim=37			)


     def   UpperCAmelCase_			(						self					:	Dict			)		->      Any:
      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					:	str			)		->      Tuple:
      return


     def   UpperCAmelCase_			(						self					:	str			)		->      Optional[Any]:
      UpperCAmelCase			:      List[Any]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowercase_			)


     def   UpperCAmelCase_			(						self					:	Dict			)		->      str:
      UpperCAmelCase			:      Optional[int]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_			)


     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      Optional[Any]:
      UpperCAmelCase			:      Optional[Any]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowercase_			)


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


     def   UpperCAmelCase_			(						self					:	Dict			)		->      str:
      UpperCAmelCase   ,					UpperCAmelCase			:      Optional[Any]		   =					self.model_tester.prepare_config_and_inputs_for_common()

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


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

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

       UpperCAmelCase			:      Tuple		   =					['pixel_values']
       self.assertListEqual(arg_names[:1]	,     lowercase_			)


     def   UpperCAmelCase_			(						self					:	Optional[Any]			)		->      Union[str, Any]:
      UpperCAmelCase   ,					UpperCAmelCase			:      int		   =					self.model_tester.prepare_config_and_inputs_for_common()
      UpperCAmelCase			:      Optional[int]		   =					True

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

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

       self.assertListEqual(
           list(attentions[0].shape[-3:]			)	,     [self.model_tester.num_heads[0], window_size_squared, window_size_squared]	,     )
       UpperCAmelCase			:      Optional[int]		   =					len(lowercase_			)

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

       if hasattr(self.model_tester	,     'num_hidden_states_types'			):
        UpperCAmelCase			:      Optional[int]		   =					self.model_tester.num_hidden_states_types
       else:
        # also another +1 for reshaped_hidden_states
        UpperCAmelCase			:      Tuple		   =					2
       self.assertEqual(out_len + added_hidden_states	,     len(lowercase_			)			)

       UpperCAmelCase			:      Union[str, Any]		   =					outputs.attentions

       self.assertEqual(len(lowercase_			)	,     lowercase_			)

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


     def   UpperCAmelCase_			(						self					:	Optional[int]	,     lowercase_					:	str	,     lowercase_					:	Optional[Any]	,     lowercase_					:	Any	,     lowercase_					:	Tuple			)		->      Union[str, Any]:
      UpperCAmelCase			:      Tuple		   =					model_class(lowercase_			)
      UpperCAmelCase			:      Optional[Any]		   =					model(**self._prepare_for_class(lowercase_	,     lowercase_			)			)
      UpperCAmelCase			:      List[str]		   =					outputs.hidden_states

      UpperCAmelCase			:      Any		   =					getattr(
          self.model_tester	,     'expected_num_hidden_layers'	,     len(self.model_tester.depths			) + 1			)
      self.assertEqual(len(lowercase_			)	,     lowercase_			)

      # Swin has a different seq_length
      UpperCAmelCase			:      Union[str, Any]		   =					to_atuple(config.patch_size			)

      UpperCAmelCase			:      Dict		   =					(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			:      Dict		   =					outputs.reshaped_hidden_states
      self.assertEqual(len(lowercase_			)	,     lowercase_			)

      UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      Optional[int]		   =					reshaped_hidden_states[0].shape

      UpperCAmelCase			:      List[str]		   =					tf.reshape(reshaped_hidden_states[0]	,     (batch_size, num_channels, height * width)			)
      UpperCAmelCase			:      Optional[Any]		   =					tf.transpose(lowercase_	,     (0, 2, 1)			)

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


     def   UpperCAmelCase_			(						self					:	List[str]			)		->      Dict:
      UpperCAmelCase   ,					UpperCAmelCase			:      Dict		   =					self.model_tester.prepare_config_and_inputs_for_common()

      UpperCAmelCase			:      Tuple		   =					to_atuple(self.model_tester.image_size			)

      for model_class in self.all_model_classes:
       UpperCAmelCase			:      Dict		   =					True
       self.check_hidden_states_output(lowercase_	,     lowercase_	,     lowercase_	,     lowercase_			)

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

       self.check_hidden_states_output(lowercase_	,     lowercase_	,     lowercase_	,     lowercase_			)


     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      Dict:
      UpperCAmelCase   ,					UpperCAmelCase			:      Optional[Any]		   =					self.model_tester.prepare_config_and_inputs_for_common()
      UpperCAmelCase			:      Optional[Any]		   =					3

      UpperCAmelCase			:      List[Any]		   =					to_atuple(self.model_tester.image_size			)
      UpperCAmelCase			:      List[Any]		   =					to_atuple(config.patch_size			)

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

      for model_class in self.all_model_classes:
       UpperCAmelCase			:      Tuple		   =					True
       self.check_hidden_states_output(lowercase_	,     lowercase_	,     lowercase_	,     (padded_height, padded_width)			)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCAmelCase			:      int		   =					True
       self.check_hidden_states_output(lowercase_	,     lowercase_	,     lowercase_	,     (padded_height, padded_width)			)




     @slow
     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      str:
      for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase			:      Any		   =					TFSwinModel.from_pretrained(lowercase_			)
       self.assertIsNotNone(lowercase_			)




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






     '''simple docstring'''



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




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

      UpperCAmelCase			:      Union[str, Any]		   =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
      UpperCAmelCase			:      int		   =					image_processor(images=lowercase_	,     return_tensors='tf'			)

      # forward pass
      UpperCAmelCase			:      Dict		   =					model(lowercase_			)

      # verify the logits
      UpperCAmelCase			:      Union[str, Any]		   =					tf.TensorShape((1, 1_000)			)
      self.assertEqual(outputs.logits.shape	,     lowercase_			)
      UpperCAmelCase			:      Optional[int]		   =					tf.constant([-0.0948, -0.6454, -0.0921]			)
      self.assertTrue(np.allclose(outputs.logits[0, :3]	,     lowercase_	,     atol=1E-4			)			)







'''simple docstring'''

from __future__ import annotations

from collections.abc import Callable




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_ = 1_00    ,			):
 UpperCAmelCase			:      Any		   =					x_start
 UpperCAmelCase			:      int		   =					fnc(UpperCAmelCase_      )
 UpperCAmelCase			:      str		   =					0.0

 for _ in range(UpperCAmelCase_      ):
  # Approximates small segments of curve as linear and solve
  # for trapezoidal area
  UpperCAmelCase			:      List[Any]		   =					(x_end - x_start) / steps + xa
  UpperCAmelCase			:      int		   =					fnc(UpperCAmelCase_      )
  area += abs(fxa + fxa      ) * (xa - xa) / 2

  # Increment step
  UpperCAmelCase			:      List[str]		   =					xa
  UpperCAmelCase			:      List[Any]		   =					fxa
 return area


if __name__ == "__main__":




 def  UpperCamelCase(	UpperCAmelCase_      ):
  return x**3

 print("f(x) = x^3")
 print("The area between the curve, x = -10, x = 10 and the x axis is:")
 lowercase__            =     10
 while i <= 100000:
  lowercase__            =     trapezoidal_area(f, -5, 5, i)
  print(f'''with {i} steps: {area}''')
  i *= 10




