


import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

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


if is_torch_available():
 import torch
 from torch import nn

 from transformers import MaskFormerSwinBackbone
 from transformers.models.maskformer import MaskFormerSwinModel


class 		_SCREAMING_SNAKE_CASE     :


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


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Dict							=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

       _UpperCAmelCase		:							Union[str, Any]							=     None
       if self.use_labels:
         _UpperCAmelCase		:							Any							=     ids_tensor([self.batch_size]							,					self.type_sequence_label_size    )

       _UpperCAmelCase		:							Optional[int]							=     self.get_config()

       return config, pixel_values, labels


     def        __snake_case(  self    ):
       return MaskFormerSwinConfig(
           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        __snake_case(  self							,					A_							,					A_							,					A_    ):
       _UpperCAmelCase		:							int							=     MaskFormerSwinModel(config=A_    )
       model.to(A_    )
       model.eval()
       _UpperCAmelCase		:							Optional[int]							=     model(A_    )

       _UpperCAmelCase		:							Optional[Any]							=     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
       _UpperCAmelCase		:							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							,					A_							,					A_							,					A_    ):
       _UpperCAmelCase		:							List[str]							=     MaskFormerSwinBackbone(config=A_    )
       model.to(A_    )
       model.eval()
       _UpperCAmelCase		:							Dict							=     model(A_    )

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps    )							,					len(config.out_features    )    )
       self.parent.assertListEqual(list(result.feature_maps[0].shape    )							,					[13, 16, 16, 16]    )

       # verify channels
       self.parent.assertEqual(len(model.channels    )							,					len(config.out_features    )    )
       self.parent.assertListEqual(model.channels							,					[16, 32, 64]    )

       # verify ValueError
       with self.parent.assertRaises(A_    ):
         _UpperCAmelCase		:							List[Any]							=     ["""stem"""]
         _UpperCAmelCase		:							int							=     MaskFormerSwinBackbone(config=A_    )




     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Any							=     self.prepare_config_and_inputs()
       _UpperCAmelCase,_UpperCAmelCase,_UpperCAmelCase		:							Any							=     config_and_inputs
       _UpperCAmelCase		:							Dict							=     {"""pixel_values""": pixel_values}
       return config, inputs_dict


@require_torch
class 		_SCREAMING_SNAKE_CASE     (						A							,	A							,	unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    (
         (
             MaskFormerSwinModel,
             MaskFormerSwinBackbone,
         )
         if is_torch_available()
         else ()
     )
     __SCREAMING_SNAKE_CASE     					=    {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[int]							=     MaskFormerSwinModelTester(self    )
       _UpperCAmelCase		:							Any							=     ConfigTester(self							,					config_class=A_							,					embed_dim=37    )


     @require_torch_multi_gpu
     @unittest.skip(
         reason=(
             """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"""
             """ `nn.DataParallel`"""
         )    )
     def        __snake_case(  self    ):
       pass


     def        __snake_case(  self    ):
       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    ):
       return


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							str							=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_    )


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							List[Any]							=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*A_    )


     @unittest.skip("""Swin does not use inputs_embeds"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Swin does not support feedforward chunking"""    )
     def        __snake_case(  self    ):
       pass


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							str							=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         _UpperCAmelCase		:							str							=     model_class(A_    )
         self.assertIsInstance(model.get_input_embeddings()							,					(nn.Module)    )
         _UpperCAmelCase		:							Any							=     model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(A_							,					nn.Linear    )    )


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Dict							=     self.model_tester.prepare_config_and_inputs_for_common()

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

         _UpperCAmelCase		:							Tuple							=     ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]							,					A_    )


     @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone"""    )
     def        __snake_case(  self    ):
       pass


     def        __snake_case(  self							,					A_							,					A_							,					A_							,					A_    ):
       _UpperCAmelCase		:							Optional[Any]							=     model_class(A_    )
       model.to(A_    )
       model.eval()

       with torch.no_grad():
         _UpperCAmelCase		:							Tuple							=     model(**self._prepare_for_class(A_							,					A_    )    )

       _UpperCAmelCase		:							Union[str, Any]							=     outputs.hidden_states

       _UpperCAmelCase		:							List[Any]							=     getattr(
           self.model_tester							,					"""expected_num_hidden_layers"""							,					len(self.model_tester.depths    ) + 1    )
       self.assertEqual(len(A_    )							,					A_    )

       # Swin has a different seq_length
       _UpperCAmelCase		:							Optional[int]							=     (
           config.patch_size
           if isinstance(config.patch_size							,					collections.abc.Iterable    )
           else (config.patch_size, config.patch_size)
       )

       _UpperCAmelCase		:							List[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]							,					)


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Dict							=     self.model_tester.prepare_config_and_inputs_for_common()

       _UpperCAmelCase		:							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:
         _UpperCAmelCase		:							Dict							=     True
         self.check_hidden_states_output(A_							,					A_							,					A_							,					A_    )

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

         self.check_hidden_states_output(A_							,					A_							,					A_							,					A_    )


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Union[str, Any]							=     self.model_tester.prepare_config_and_inputs_for_common()
       _UpperCAmelCase		:							int							=     3

       _UpperCAmelCase		:							Optional[int]							=     (
           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)
       )
       _UpperCAmelCase		:							Dict							=     (
           config.patch_size
           if isinstance(config.patch_size							,					collections.abc.Iterable    )
           else (config.patch_size, config.patch_size)
       )

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

       for model_class in self.all_model_classes:
         _UpperCAmelCase		:							Optional[int]							=     True
         self.check_hidden_states_output(A_							,					A_							,					A_							,					(padded_height, padded_width)    )

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


     @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""    )
     def        __snake_case(  self    ):
       pass




     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Optional[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()

       def set_nan_tensor_to_zero(A_    ):
         _UpperCAmelCase		:							List[Any]							=     0
         return t

       def check_equivalence(A_							,					A_							,					A_							,					A_={}    ):
         with torch.no_grad():
           _UpperCAmelCase		:							Optional[Any]							=     model(**A_							,					return_dict=A_							,					**A_    )
           _UpperCAmelCase		:							Optional[Any]							=     model(**A_							,					return_dict=A_							,					**A_    ).to_tuple()

           def recursive_check(A_							,					A_    ):
             if isinstance(A_							,					(List, Tuple)    ):
               for tuple_iterable_value, dict_iterable_value in zip(A_							,					A_    ):
                 recursive_check(A_							,					A_    )
             elif isinstance(A_							,					A_    ):
               for tuple_iterable_value, dict_iterable_value in zip(
                   tuple_object.values()							,					dict_object.values()    ):
                 recursive_check(A_							,					A_    )
             elif tuple_object is None:
               return
             else:
               self.assertTrue(
                   torch.allclose(
                       set_nan_tensor_to_zero(A_    )							,					set_nan_tensor_to_zero(A_    )							,					atol=1e-5    )							,					msg=(
                       """Tuple and dict output are not equal. Difference:"""
                       F''' {torch.max(torch.abs(tuple_object - dict_object    )    )}. Tuple has `nan`:'''
                       F''' {torch.isnan(A_    ).any()} and `inf`: {torch.isinf(A_    )}. Dict has'''
                       F''' `nan`: {torch.isnan(A_    ).any()} and `inf`: {torch.isinf(A_    )}.'''
                   )							,					)

           recursive_check(A_							,					A_    )

       for model_class in self.all_model_classes:
         _UpperCAmelCase		:							Optional[int]							=     model_class(A_    )
         model.to(A_    )
         model.eval()

         _UpperCAmelCase		:							Any							=     self._prepare_for_class(A_							,					A_    )
         _UpperCAmelCase		:							Any							=     self._prepare_for_class(A_							,					A_    )
         check_equivalence(A_							,					A_							,					A_    )

         _UpperCAmelCase		:							Tuple							=     self._prepare_for_class(A_							,					A_							,					return_labels=A_    )
         _UpperCAmelCase		:							Optional[int]							=     self._prepare_for_class(A_							,					A_							,					return_labels=A_    )
         check_equivalence(A_							,					A_							,					A_    )

         _UpperCAmelCase		:							Tuple							=     self._prepare_for_class(A_							,					A_    )
         _UpperCAmelCase		:							Union[str, Any]							=     self._prepare_for_class(A_							,					A_    )
         check_equivalence(A_							,					A_							,					A_							,					{"""output_hidden_states""": True}    )

         _UpperCAmelCase		:							Union[str, Any]							=     self._prepare_for_class(A_							,					A_							,					return_labels=A_    )
         _UpperCAmelCase		:							Optional[Any]							=     self._prepare_for_class(A_							,					A_							,					return_labels=A_    )
         check_equivalence(A_							,					A_							,					A_							,					{"""output_hidden_states""": True}    )


@require_torch
class 		_SCREAMING_SNAKE_CASE     (						unittest.TestCase							,	A     ):
     __SCREAMING_SNAKE_CASE     					=    (MaskFormerSwinBackbone,) if is_torch_available() else ()
     __SCREAMING_SNAKE_CASE     					=    MaskFormerSwinConfig


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Any							=     MaskFormerSwinModelTester(self    )




     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							List[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()
       _UpperCAmelCase		:							int							=     inputs_dict["""pixel_values"""].shape[0]

       for backbone_class in self.all_model_classes:
         _UpperCAmelCase		:							Any							=     backbone_class(A_    )
         backbone.to(A_    )
         backbone.eval()

         _UpperCAmelCase		:							Optional[int]							=     backbone(**A_    )

         # Test default outputs and verify feature maps
         self.assertIsInstance(outputs.feature_maps							,					A_    )
         self.assertTrue(len(outputs.feature_maps    ) == len(backbone.channels    )    )
         for feature_map, n_channels in zip(outputs.feature_maps							,					backbone.channels    ):
           self.assertTrue(feature_map.shape[:2]							,					(batch_size, n_channels)    )
         self.assertIsNone(outputs.hidden_states    )
         self.assertIsNone(outputs.attentions    )

         # Test output_hidden_states=True
         _UpperCAmelCase		:							Dict							=     backbone(**A_							,					output_hidden_states=A_    )
         self.assertIsNotNone(outputs.hidden_states    )
         self.assertTrue(len(outputs.hidden_states    )							,					len(backbone.stage_names    )    )
         # We skip the stem layer
         for hidden_states, n_channels in zip(outputs.hidden_states[1:]							,					backbone.channels    ):
           for hidden_state in hidden_states:
             # Hidden states are in the format (batch_size, (height * width), n_channels)
             _UpperCAmelCase,_UpperCAmelCase,_UpperCAmelCase		:							Tuple							=     hidden_state.shape
             self.assertTrue((h_batch_size, h_n_channels)							,					(batch_size, n_channels)    )

            # Test output_attentions=True
         if self.has_attentions:
           _UpperCAmelCase		:							Dict							=     backbone(**A_							,					output_attentions=A_    )
           self.assertIsNotNone(outputs.attentions    )









import os
import string
import sys


SCREAMING_SNAKE_CASE__					:     List[str]       =     1 << 8

SCREAMING_SNAKE_CASE__					:     str       =     {
    'tab': ord('\t'),
    'newline': ord('\r'),
    'esc': 27,
    'up': 65 + ARROW_KEY_FLAG,
    'down': 66 + ARROW_KEY_FLAG,
    'right': 67 + ARROW_KEY_FLAG,
    'left': 68 + ARROW_KEY_FLAG,
    'mod_int': 91,
    'undefined': sys.maxsize,
    'interrupt': 3,
    'insert': 50,
    'delete': 51,
    'pg_up': 53,
    'pg_down': 54,
}

SCREAMING_SNAKE_CASE__					:     List[str]       =     KEYMAP['up']
SCREAMING_SNAKE_CASE__					:     Optional[int]       =     KEYMAP['left']

if sys.platform == "win32":
 SCREAMING_SNAKE_CASE__					:     Union[str, Any]       =     []
 SCREAMING_SNAKE_CASE__					:     Dict       =     {
     b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG,
     b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG,
     b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG,
     b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG,
     b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG,
     b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG,
     b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG,
     b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG,
 }

for i in range(10):
 SCREAMING_SNAKE_CASE__					:     List[str]       =     ord(str(i))




def    a__    (		):
  if os.name == "nt":
    import msvcrt

    _UpperCAmelCase		:							int							=     """mbcs"""
    # Flush the keyboard buffer
    while msvcrt.kbhit():
      msvcrt.getch()
    if len(snake_case__     ) == 0:
      # Read the keystroke
      _UpperCAmelCase		:							Any							=     msvcrt.getch()

      # If it is a prefix char, get second part
      if ch in (b"\x00", b"\xe0"):
        _UpperCAmelCase		:							str							=     ch + msvcrt.getch()
        # Translate actual Win chars to bullet char types
        try:
          _UpperCAmelCase		:							Optional[int]							=     chr(WIN_KEYMAP[cha]     )
          WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""]     )     )
          WIN_CH_BUFFER.append(snake_case__     )
          if ord(snake_case__     ) in (
              KEYMAP["insert"] - 1 << 9,
              KEYMAP["delete"] - 1 << 9,
              KEYMAP["pg_up"] - 1 << 9,
              KEYMAP["pg_down"] - 1 << 9,
          ):
            WIN_CH_BUFFER.append(chr(126     )     )
          _UpperCAmelCase		:							Any							=     chr(KEYMAP["""esc"""]     )
        except KeyError:
          _UpperCAmelCase		:							Optional[int]							=     cha[1]
      else:
        _UpperCAmelCase		:							Union[str, Any]							=     ch.decode(snake_case__     )
    else:
      _UpperCAmelCase		:							Optional[int]							=     WIN_CH_BUFFER.pop(0     )
  elif os.name == "posix":
    import termios
    import tty

    _UpperCAmelCase		:							List[str]							=     sys.stdin.fileno()
    _UpperCAmelCase		:							Optional[Any]							=     termios.tcgetattr(snake_case__     )
    try:
      tty.setraw(snake_case__     )
      _UpperCAmelCase		:							Tuple							=     sys.stdin.read(1     )
    finally:
      termios.tcsetattr(snake_case__						,   termios.TCSADRAIN						,   snake_case__     )
  return ch





def    a__    (		):
  _UpperCAmelCase		:							int							=     get_raw_chars()
  if ord(snake_case__     ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
    return char

  elif ord(snake_case__     ) == KEYMAP["esc"]:
    _UpperCAmelCase		:							int							=     get_raw_chars()
    if ord(snake_case__     ) == KEYMAP["mod_int"]:
      _UpperCAmelCase		:							Optional[Any]							=     get_raw_chars()
      if ord(snake_case__     ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(snake_case__     ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
        return chr(ord(snake_case__     ) + ARROW_KEY_FLAG     )
      else:
        return KEYMAP["undefined"]
    else:
      return get_raw_chars()

  else:
    if char in string.printable:
      return char
    else:
      return KEYMAP["undefined"]





