

import collections
import inspect
import unittest

from transformers import SwinConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
   from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor



class SCREAMING_SNAKE_CASE	:
  """simple docstring"""






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






     """simple docstring"""







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







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






     """simple docstring"""







     __lowerCAmelCase  :			List[str]					    =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

     __lowerCAmelCase  :			List[str]					    =						None
     if self.use_labels:
        __lowerCAmelCase  :			Tuple					    =						ids_tensor([self.batch_size]       ,							self.type_sequence_label_size    )

     __lowerCAmelCase  :			List[str]					    =						self.get_config()

     return config, pixel_values, labels







  def        SCREAMING_SNAKE_CASE    (			self :    int    )							->	List[Any]:






     """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       ,							out_features=self.out_features       ,							out_indices=self.out_indices       ,							)







  def        SCREAMING_SNAKE_CASE    (			self :    str       ,							lowerCAmelCase :    Dict       ,							lowerCAmelCase :    Optional[Any]       ,							lowerCAmelCase :    Optional[Any]    )							->	List[str]:






     """simple docstring"""







     __lowerCAmelCase  :			int					    =						SwinModel(config=lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()
     __lowerCAmelCase  :			Union[str, Any]					    =						model(lowerCAmelCase    )

     __lowerCAmelCase  :			Tuple					    =						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
     __lowerCAmelCase  :			Optional[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        SCREAMING_SNAKE_CASE    (			self :    str       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    Optional[int]       ,							lowerCAmelCase :    str    )							->	Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :			int					    =						SwinBackbone(config=lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()
     __lowerCAmelCase  :			List[Any]					    =						model(lowerCAmelCase    )

     # verify hidden states
     self.parent.assertEqual(len(result.feature_maps    )       ,							len(config.out_features    )    )
     self.parent.assertListEqual(list(result.feature_maps[0].shape    )       ,							[self.batch_size, model.channels[0], 16, 16]    )

     # verify channels
     self.parent.assertEqual(len(model.channels    )       ,							len(config.out_features    )    )

     # verify backbone works with out_features=None
     __lowerCAmelCase  :			Dict					    =						None
     __lowerCAmelCase  :			Any					    =						SwinBackbone(config=lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()
     __lowerCAmelCase  :			List[Any]					    =						model(lowerCAmelCase    )

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps    )       ,							1    )
     self.parent.assertListEqual(list(result.feature_maps[0].shape    )       ,							[self.batch_size, model.channels[-1], 4, 4]    )

     # verify channels
     self.parent.assertEqual(len(model.channels    )       ,							1    )







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]       ,							lowerCAmelCase :    Optional[Any]       ,							lowerCAmelCase :    Optional[int]       ,							lowerCAmelCase :    Union[str, Any]    )							->	Dict:






     """simple docstring"""







     __lowerCAmelCase  :			Dict					    =						SwinForMaskedImageModeling(config=lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()
     __lowerCAmelCase  :			List[Any]					    =						model(lowerCAmelCase    )
     self.parent.assertEqual(
         result.logits.shape       ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

     # test greyscale images
     __lowerCAmelCase  :			Optional[Any]					    =						1
     __lowerCAmelCase  :			List[Any]					    =						SwinForMaskedImageModeling(lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()

     __lowerCAmelCase  :			int					    =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
     __lowerCAmelCase  :			Optional[int]					    =						model(lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, 1, self.image_size, self.image_size)    )







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






     """simple docstring"""







     __lowerCAmelCase  :			str					    =						self.type_sequence_label_size
     __lowerCAmelCase  :			List[Any]					    =						SwinForImageClassification(lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()
     __lowerCAmelCase  :			str					    =						model(lowerCAmelCase       ,							labels=lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.type_sequence_label_size)    )

     # test greyscale images
     __lowerCAmelCase  :			List[str]					    =						1
     __lowerCAmelCase  :			Tuple					    =						SwinForImageClassification(lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()

     __lowerCAmelCase  :			Tuple					    =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
     __lowerCAmelCase  :			Optional[Any]					    =						model(lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.type_sequence_label_size)    )







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]    )							->	Tuple:






     """simple docstring"""







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






@require_torch
class SCREAMING_SNAKE_CASE	(  a_   ,							a_   ,							unittest.TestCase ):
  """simple docstring"""






  lowerCamelCase    :			List[str]						      =(
      (
          SwinModel,
          SwinBackbone,
          SwinForImageClassification,
          SwinForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase    :			Any						      =(
      {"feature-extraction": SwinModel, "image-classification": SwinForImageClassification}
      if is_torch_available()
      else {}
  )
  lowerCamelCase    :			Optional[Any]						      =True

  lowerCamelCase    :			Optional[int]						      =False
  lowerCamelCase    :			Dict						      =False
  lowerCamelCase    :			Optional[Any]						      =False







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






     """simple docstring"""







     __lowerCAmelCase  :			Tuple					    =						SwinModelTester(self    )
     __lowerCAmelCase  :			Optional[int]					    =						ConfigTester(self       ,							config_class=lowerCAmelCase       ,							embed_dim=37    )







  def        SCREAMING_SNAKE_CASE    (			self :    List[Any]    )							->	Any:






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






     """simple docstring"""







     return







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






     """simple docstring"""







     __lowerCAmelCase  :			Union[str, Any]					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase    )







  @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""    )
  def        SCREAMING_SNAKE_CASE    (			self :    Any    )							->	Optional[int]:






     """simple docstring"""







     pass







  def        SCREAMING_SNAKE_CASE    (			self :    Tuple    )							->	List[str]:






     """simple docstring"""







     super().test_training_gradient_checkpointing()







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






     """simple docstring"""







     __lowerCAmelCase  :			List[str]					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*lowerCAmelCase    )







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






     """simple docstring"""







     __lowerCAmelCase  :			Union[str, Any]					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase    )







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]    )							->	Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :			List[Any]					    =						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        SCREAMING_SNAKE_CASE    (			self :    int    )							->	Optional[Any]:






     """simple docstring"""







     pass







  @unittest.skip(reason="""Swin Transformer does not use feedforward chunking"""    )
  def        SCREAMING_SNAKE_CASE    (			self :    str    )							->	int:






     """simple docstring"""







     pass







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






     """simple docstring"""







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

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			List[Any]					    =						model_class(lowerCAmelCase    )
        self.assertIsInstance(model.get_input_embeddings()       ,							(nn.Module)    )
        __lowerCAmelCase  :			List[Any]					    =						model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCAmelCase       ,							nn.Linear    )    )







  def        SCREAMING_SNAKE_CASE    (			self :    Tuple    )							->	str:






     """simple docstring"""







     __lowerCAmelCase    ,__lowerCAmelCase  :			List[Any]					    =						self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			List[Any]					    =						model_class(lowerCAmelCase    )
        __lowerCAmelCase  :			Union[str, Any]					    =						inspect.signature(model.forward    )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        __lowerCAmelCase  :			Tuple					    =						[*signature.parameters.keys()]

        __lowerCAmelCase  :			int					    =						["""pixel_values"""]
        self.assertListEqual(arg_names[:1]       ,							lowerCAmelCase    )







  def        SCREAMING_SNAKE_CASE    (			self :    List[str]    )							->	str:






     """simple docstring"""







     __lowerCAmelCase    ,__lowerCAmelCase  :			Tuple					    =						self.model_tester.prepare_config_and_inputs_for_common()
     __lowerCAmelCase  :			str					    =						True

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			str					    =						True
        __lowerCAmelCase  :			List[str]					    =						False
        __lowerCAmelCase  :			Optional[Any]					    =						True
        __lowerCAmelCase  :			Optional[Any]					    =						model_class(lowerCAmelCase    )
        model.to(lowerCAmelCase    )
        model.eval()
        with torch.no_grad():
           __lowerCAmelCase  :			str					    =						model(**self._prepare_for_class(lowerCAmelCase       ,							lowerCAmelCase    )    )
        __lowerCAmelCase  :			Union[str, Any]					    =						outputs.attentions
        __lowerCAmelCase  :			Dict					    =						len(self.model_tester.depths    )
        self.assertEqual(len(lowerCAmelCase    )       ,							lowerCAmelCase    )

        # check that output_attentions also work using config
        del inputs_dict["output_attentions"]
        __lowerCAmelCase  :			Optional[Any]					    =						True
        __lowerCAmelCase  :			Any					    =						config.window_size**2
        __lowerCAmelCase  :			Tuple					    =						model_class(lowerCAmelCase    )
        model.to(lowerCAmelCase    )
        model.eval()
        with torch.no_grad():
           __lowerCAmelCase  :			List[str]					    =						model(**self._prepare_for_class(lowerCAmelCase       ,							lowerCAmelCase    )    )
        __lowerCAmelCase  :			Optional[int]					    =						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]       ,							)
        __lowerCAmelCase  :			str					    =						len(lowerCAmelCase    )

        # Check attention is always last and order is fine
        __lowerCAmelCase  :			int					    =						True
        __lowerCAmelCase  :			int					    =						True
        __lowerCAmelCase  :			int					    =						model_class(lowerCAmelCase    )
        model.to(lowerCAmelCase    )
        model.eval()
        with torch.no_grad():
           __lowerCAmelCase  :			Dict					    =						model(**self._prepare_for_class(lowerCAmelCase       ,							lowerCAmelCase    )    )

        # also another +1 for reshaped_hidden_states
        __lowerCAmelCase  :			Optional[Any]					    =						1 if model_class.__name__ == """SwinBackbone""" else 2
        self.assertEqual(out_len + added_hidden_states       ,							len(lowerCAmelCase    )    )

        __lowerCAmelCase  :			List[Any]					    =						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        SCREAMING_SNAKE_CASE    (			self :    List[Any]       ,							lowerCAmelCase :    Dict       ,							lowerCAmelCase :    Union[str, Any]       ,							lowerCAmelCase :    Dict       ,							lowerCAmelCase :    Union[str, Any]    )							->	str:






     """simple docstring"""







     __lowerCAmelCase  :			Union[str, Any]					    =						model_class(lowerCAmelCase    )
     model.to(lowerCAmelCase    )
     model.eval()

     with torch.no_grad():
        __lowerCAmelCase  :			Any					    =						model(**self._prepare_for_class(lowerCAmelCase       ,							lowerCAmelCase    )    )

     __lowerCAmelCase  :			int					    =						outputs.hidden_states

     __lowerCAmelCase  :			int					    =						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
     __lowerCAmelCase  :			Tuple					    =						(
         config.patch_size
         if isinstance(config.patch_size       ,							collections.abc.Iterable    )
         else (config.patch_size, config.patch_size)
     )

     __lowerCAmelCase  :			Optional[int]					    =						(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]       ,							)

     if not model_class.__name__ == "SwinBackbone":
        __lowerCAmelCase  :			Optional[Any]					    =						outputs.reshaped_hidden_states
        self.assertEqual(len(lowerCAmelCase    )       ,							lowerCAmelCase    )

        __lowerCAmelCase    ,__lowerCAmelCase    ,__lowerCAmelCase    ,__lowerCAmelCase  :			Dict					    =						reshaped_hidden_states[0].shape
        __lowerCAmelCase  :			Dict					    =						(
            reshaped_hidden_states[0].view(lowerCAmelCase       ,							lowerCAmelCase       ,							height * width    ).permute(0       ,							2       ,							1    )
        )
        self.assertListEqual(
            list(reshaped_hidden_states.shape[-2:]    )       ,							[num_patches, self.model_tester.embed_dim]       ,							)







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






     """simple docstring"""







     __lowerCAmelCase    ,__lowerCAmelCase  :			Tuple					    =						self.model_tester.prepare_config_and_inputs_for_common()

     __lowerCAmelCase  :			Optional[Any]					    =						(
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size       ,							collections.abc.Iterable    )
         else (self.model_tester.image_size, self.model_tester.image_size)
     )

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			Optional[Any]					    =						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"]
        __lowerCAmelCase  :			List[Any]					    =						True

        self.check_hidden_states_output(lowerCAmelCase       ,							lowerCAmelCase       ,							lowerCAmelCase       ,							lowerCAmelCase    )







  def        SCREAMING_SNAKE_CASE    (			self :    int    )							->	Tuple:






     """simple docstring"""







     __lowerCAmelCase    ,__lowerCAmelCase  :			Optional[Any]					    =						self.model_tester.prepare_config_and_inputs_for_common()
     __lowerCAmelCase  :			Dict					    =						3

     __lowerCAmelCase  :			Dict					    =						(
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size       ,							collections.abc.Iterable    )
         else (self.model_tester.image_size, self.model_tester.image_size)
     )
     __lowerCAmelCase  :			Optional[int]					    =						(
         config.patch_size
         if isinstance(config.patch_size       ,							collections.abc.Iterable    )
         else (config.patch_size, config.patch_size)
     )

     __lowerCAmelCase  :			str					    =						image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
     __lowerCAmelCase  :			Tuple					    =						image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			int					    =						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"]
        __lowerCAmelCase  :			List[Any]					    =						True
        self.check_hidden_states_output(lowerCAmelCase       ,							lowerCAmelCase       ,							lowerCAmelCase       ,							(padded_height, padded_width)    )







  @slow
  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]    )							->	Dict:






     """simple docstring"""







     for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase  :			int					    =						SwinModel.from_pretrained(lowerCAmelCase    )
        self.assertIsNotNone(lowerCAmelCase    )







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]    )							->	Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase    ,__lowerCAmelCase  :			Optional[int]					    =						self.model_tester.prepare_config_and_inputs_for_common()

     __lowerCAmelCase  :			Tuple					    =						_config_zero_init(lowerCAmelCase    )
     for model_class in self.all_model_classes:
        __lowerCAmelCase  :			Tuple					    =						model_class(config=lowerCAmelCase    )
        for name, param in model.named_parameters():
           if "embeddings" not in name and param.requires_grad:
              self.assertIn(
                  ((param.data.mean() * 1e9).round() / 1e9).item()       ,							[0.0, 1.0]       ,							msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''       ,							)






@require_vision
@require_torch
class SCREAMING_SNAKE_CASE	(  unittest.TestCase ):
  """simple docstring"""






  @cached_property
  def        SCREAMING_SNAKE_CASE    (			self :    List[str]    )							->	str:






     """simple docstring"""







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







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






     """simple docstring"""







     __lowerCAmelCase  :			Any					    =						SwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""    ).to(lowerCAmelCase    )
     __lowerCAmelCase  :			Any					    =						self.default_image_processor

     __lowerCAmelCase  :			Optional[Any]					    =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
     __lowerCAmelCase  :			List[Any]					    =						image_processor(images=lowerCAmelCase       ,							return_tensors="""pt"""    ).to(lowerCAmelCase    )

     # forward pass
     with torch.no_grad():
        __lowerCAmelCase  :			str					    =						model(**lowerCAmelCase    )

     # verify the logits
     __lowerCAmelCase  :			Optional[Any]					    =						torch.Size((1, 10_00)    )
     self.assertEqual(outputs.logits.shape       ,							lowerCAmelCase    )
     __lowerCAmelCase  :			str					    =						torch.tensor([-0.0948, -0.6454, -0.0921]    ).to(lowerCAmelCase    )
     self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,							lowerCAmelCase       ,							atol=1e-4    )    )






@require_torch
class SCREAMING_SNAKE_CASE	(  unittest.TestCase   ,							a_ ):
  """simple docstring"""






  lowerCamelCase    :			str						      =(SwinBackbone,) if is_torch_available() else ()
  lowerCamelCase    :			List[str]						      =SwinConfig







  def        SCREAMING_SNAKE_CASE    (			self :    Optional[Any]    )							->	str:






     """simple docstring"""







     __lowerCAmelCase  :			int					    =						SwinModelTester(self    )









"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


__UpperCamelCase			:		List[Any]		    =   logging.get_logger(__name__)

__UpperCamelCase			:		Union[str, Any]		    =   {
    '''layoutlmv2-base-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json''',
    '''layoutlmv2-large-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json''',
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
       import detectrona







class 	a				(					a__   ):
 snake_case__    	=    '''layoutlmv2'''



 def __init__(			self							,    _snake_case=3_05_22							,    _snake_case=7_68							,    _snake_case=12							,    _snake_case=12							,    _snake_case=30_72							,    _snake_case="gelu"							,    _snake_case=0.1							,    _snake_case=0.1							,    _snake_case=5_12							,    _snake_case=2							,    _snake_case=0.02							,    _snake_case=1E-12							,    _snake_case=0							,    _snake_case=10_24							,    _snake_case=1_28							,    _snake_case=32							,    _snake_case=True							,    _snake_case=2_56							,    _snake_case=64							,    _snake_case=True							,    _snake_case=[7, 7, 2_56]							,    _snake_case=1_28							,    _snake_case=1_28							,    _snake_case=True							,    _snake_case=True							,    _snake_case=False							,    _snake_case=None							,    **_snake_case							,    ):







  """simple docstring"""






  super().__init__(
      vocab_size=_snake_case							,    hidden_size=_snake_case							,    num_hidden_layers=_snake_case							,    num_attention_heads=_snake_case							,    intermediate_size=_snake_case							,    hidden_act=_snake_case							,    hidden_dropout_prob=_snake_case							,    attention_probs_dropout_prob=_snake_case							,    max_position_embeddings=_snake_case							,    type_vocab_size=_snake_case							,    initializer_range=_snake_case							,    layer_norm_eps=_snake_case							,    pad_token_id=_snake_case							,    **_snake_case							,    )
  lowerCAmelCase  							=      max_ad_position_embeddings
  lowerCAmelCase  							=      max_rel_pos
  lowerCAmelCase  							=      rel_pos_bins
  lowerCAmelCase  							=      fast_qkv
  lowerCAmelCase  							=      max_rel_ad_pos
  lowerCAmelCase  							=      rel_ad_pos_bins
  lowerCAmelCase  							=      convert_sync_batchnorm
  lowerCAmelCase  							=      image_feature_pool_shape
  lowerCAmelCase  							=      coordinate_size
  lowerCAmelCase  							=      shape_size
  lowerCAmelCase  							=      has_relative_attention_bias
  lowerCAmelCase  							=      has_spatial_attention_bias
  lowerCAmelCase  							=      has_visual_segment_embedding
  lowerCAmelCase  							=      (
      detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
  )



 @classmethod
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  return {
      "MODEL.MASK_ON": True,
      "MODEL.PIXEL_STD": [57.375, 57.120, 58.395],
      "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
      "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
      "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [1_28], [2_56], [5_12]],
      "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
      "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 20_00,
      "MODEL.RPN.PRE_NMS_TOPK_TEST": 10_00,
      "MODEL.RPN.POST_NMS_TOPK_TRAIN": 10_00,
      "MODEL.POST_NMS_TOPK_TEST": 10_00,
      "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
      "MODEL.ROI_HEADS.NUM_CLASSES": 5,
      "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
      "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
      "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
      "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
      "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
      "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
      "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
      "MODEL.RESNETS.DEPTH": 1_01,
      "MODEL.RESNETS.SIZES": [[32], [64], [1_28], [2_56], [5_12]],
      "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
      "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
      "MODEL.RESNETS.NUM_GROUPS": 32,
      "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
      "MODEL.RESNETS.STRIDE_IN_1X1": False,
  }



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      detectrona.config.get_cfg()
  for k, v in self.detectrona_config_args.items():
   lowerCAmelCase  							=      k.split('.'			)
   lowerCAmelCase  							=      detectrona_config
   for attribute in attributes[:-1]:
    lowerCAmelCase  							=      getattr(_snake_case							,    _snake_case			)
   setattr(_snake_case							,    attributes[-1]							,    _snake_case			)

  return detectrona_config





