


'''simple docstring'''







import collections
import inspect
import unittest

from transformers import SwinvaConfig
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_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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
   from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor







class A__			:






      def __init__(					self     :Union[str, Any]	,		SCREAMING_SNAKE_CASE     :Tuple	,		SCREAMING_SNAKE_CASE     :List[str]=1_3	,		SCREAMING_SNAKE_CASE     :Dict=3_2	,		SCREAMING_SNAKE_CASE     :List[str]=2	,		SCREAMING_SNAKE_CASE     :int=3	,		SCREAMING_SNAKE_CASE     :Union[str, Any]=1_6	,		SCREAMING_SNAKE_CASE     :Optional[Any]=[1, 2, 1]	,		SCREAMING_SNAKE_CASE     :str=[2, 2, 4]	,		SCREAMING_SNAKE_CASE     :Optional[int]=2	,		SCREAMING_SNAKE_CASE     :Tuple=2.0	,		SCREAMING_SNAKE_CASE     :Union[str, Any]=True	,		SCREAMING_SNAKE_CASE     :str=0.0	,		SCREAMING_SNAKE_CASE     :Tuple=0.0	,		SCREAMING_SNAKE_CASE     :Optional[int]=0.1	,		SCREAMING_SNAKE_CASE     :List[str]="gelu"	,		SCREAMING_SNAKE_CASE     :List[Any]=False	,		SCREAMING_SNAKE_CASE     :Union[str, Any]=True	,		SCREAMING_SNAKE_CASE     :Dict=0.02	,		SCREAMING_SNAKE_CASE     :List[str]=1e-5	,		SCREAMING_SNAKE_CASE     :int=True	,		SCREAMING_SNAKE_CASE     :Any=None	,		SCREAMING_SNAKE_CASE     :int=True	,		SCREAMING_SNAKE_CASE     :Optional[Any]=1_0	,		SCREAMING_SNAKE_CASE     :int=8	,		)			->   Tuple:




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






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




             '''simple docstring'''
             _a       :					int	   =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

             _a       :					List[str]	   =None
             if self.use_labels:
                    _a       :					Optional[int]	   =ids_tensor([self.batch_size]	,		self.type_sequence_label_size      )

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

             return config, pixel_values, labels






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




             '''simple docstring'''
             return SwinvaConfig(
                 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     :int	,		SCREAMING_SNAKE_CASE     :Optional[Any]	,		SCREAMING_SNAKE_CASE     :Dict	,		SCREAMING_SNAKE_CASE     :Optional[Any]      )			->   List[Any]:




             '''simple docstring'''
             _a       :					Dict	   =SwinvaModel(config=_SCREAMING_SNAKE_CASE      )
             model.to(_SCREAMING_SNAKE_CASE      )
             model.eval()
             _a       :					Any	   =model(_SCREAMING_SNAKE_CASE      )

             _a       :					List[Any]	   =((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths      ) - 1))
             _a       :					int	   =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     :Any	,		SCREAMING_SNAKE_CASE     :Union[str, Any]	,		SCREAMING_SNAKE_CASE     :Dict	,		SCREAMING_SNAKE_CASE     :List[Any]      )			->   Dict:




             '''simple docstring'''
             _a       :					Any	   =SwinvaForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE      )
             model.to(_SCREAMING_SNAKE_CASE      )
             model.eval()
             _a       :					Tuple	   =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       :					List[Any]	   =1
             _a       :					Any	   =SwinvaForMaskedImageModeling(_SCREAMING_SNAKE_CASE      )
             model.to(_SCREAMING_SNAKE_CASE      )
             model.eval()

             _a       :					str	   =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
             _a       :					str	   =model(_SCREAMING_SNAKE_CASE      )
             self.parent.assertEqual(result.logits.shape	,		(self.batch_size, 1, self.image_size, self.image_size)      )






      def __UpperCAmelCase			(					self     :List[str]	,		SCREAMING_SNAKE_CASE     :str	,		SCREAMING_SNAKE_CASE     :Tuple	,		SCREAMING_SNAKE_CASE     :Optional[int]      )			->   List[str]:




             '''simple docstring'''
             _a       :					Optional[int]	   =self.type_sequence_label_size
             _a       :					List[Any]	   =SwinvaForImageClassification(_SCREAMING_SNAKE_CASE      )
             model.to(_SCREAMING_SNAKE_CASE      )
             model.eval()
             _a       :					Dict	   =model(_SCREAMING_SNAKE_CASE	,		labels=_SCREAMING_SNAKE_CASE      )
             self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.type_sequence_label_size)      )






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




             '''simple docstring'''
             _a       :					str	   =self.prepare_config_and_inputs()
             _a   ,       _a   ,       _a       :					Any	   =config_and_inputs
             _a       :					Dict	   ={"""pixel_values""": pixel_values}
             return config, inputs_dict







@require_torch
class A__			(		a_					,	a_					,	unittest.TestCase ):
      __UpperCamelCase			:    str      			=				(
          (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
      )
      __UpperCamelCase			:    List[Any]      			=				(
          {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
          if is_torch_available()
          else {}
      )

      __UpperCamelCase			:    Union[str, Any]      			=				False
      __UpperCamelCase			:    Any      			=				False
      __UpperCamelCase			:    str      			=				False
      __UpperCamelCase			:    Union[str, Any]      			=				False






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




             '''simple docstring'''
             _a       :					Dict	   =SwinvaModelTester(self      )
             _a       :					List[Any]	   =ConfigTester(self	,		config_class=_SCREAMING_SNAKE_CASE	,		embed_dim=3_7      )






      def __UpperCAmelCase			(					self     :Optional[int]      )			->   int:




             '''simple docstring'''
             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     :Dict      )			->   List[Any]:




             '''simple docstring'''
             _a       :					Tuple	   =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE      )






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




             '''simple docstring'''
             pass






      @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""      )
      def __UpperCAmelCase			(					self     :Any      )			->   Optional[Any]:




             '''simple docstring'''
             pass






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




             '''simple docstring'''
             _a   ,       _a       :					Tuple	   =self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _a       :					List[str]	   =model_class(_SCREAMING_SNAKE_CASE      )
                    self.assertIsInstance(model.get_input_embeddings()	,		(nn.Module)      )
                    _a       :					int	   =model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE	,		nn.Linear      )      )






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




             '''simple docstring'''
             _a   ,       _a       :					Any	   =self.model_tester.prepare_config_and_inputs_for_common()

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

                    _a       :					int	   =["""pixel_values"""]
                    self.assertListEqual(arg_names[:1]	,		_SCREAMING_SNAKE_CASE      )






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




             '''simple docstring'''
             _a   ,       _a       :					str	   =self.model_tester.prepare_config_and_inputs_for_common()
             _a       :					Dict	   =True

             for model_class in self.all_model_classes:
                    _a       :					Union[str, Any]	   =True
                    _a       :					int	   =False
                    _a       :					Tuple	   =True
                    _a       :					List[Any]	   =model_class(_SCREAMING_SNAKE_CASE      )
                    model.to(_SCREAMING_SNAKE_CASE      )
                    model.eval()
                    with torch.no_grad():
                           _a       :					str	   =model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE      )      )
                    _a       :					Dict	   =outputs.attentions
                    _a       :					Dict	   =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       :					List[str]	   =True
                    _a       :					Optional[Any]	   =config.window_size**2
                    _a       :					Tuple	   =model_class(_SCREAMING_SNAKE_CASE      )
                    model.to(_SCREAMING_SNAKE_CASE      )
                    model.eval()
                    with torch.no_grad():
                           _a       :					Dict	   =model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE      )      )
                    _a       :					List[str]	   =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       :					str	   =len(_SCREAMING_SNAKE_CASE      )

                    # Check attention is always last and order is fine
                    _a       :					str	   =True
                    _a       :					int	   =True
                    _a       :					List[str]	   =model_class(_SCREAMING_SNAKE_CASE      )
                    model.to(_SCREAMING_SNAKE_CASE      )
                    model.eval()
                    with torch.no_grad():
                           _a       :					List[Any]	   =model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE      )      )

                    if hasattr(self.model_tester	,		"""num_hidden_states_types"""      ):
                           _a       :					Any	   =self.model_tester.num_hidden_states_types
                    else:
                           # also another +1 for reshaped_hidden_states
                           _a       :					Dict	   =2
                    self.assertEqual(out_len + added_hidden_states	,		len(_SCREAMING_SNAKE_CASE      )      )

                    _a       :					Optional[int]	   =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 __UpperCAmelCase			(					self     :str	,		SCREAMING_SNAKE_CASE     :Optional[Any]	,		SCREAMING_SNAKE_CASE     :Optional[Any]	,		SCREAMING_SNAKE_CASE     :Optional[int]	,		SCREAMING_SNAKE_CASE     :Any      )			->   Union[str, Any]:




             '''simple docstring'''
             _a       :					Tuple	   =model_class(_SCREAMING_SNAKE_CASE      )
             model.to(_SCREAMING_SNAKE_CASE      )
             model.eval()

             with torch.no_grad():
                    _a       :					List[Any]	   =model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE      )      )

             _a       :					Dict	   =outputs.hidden_states

             _a       :					Dict	   =getattr(
                 self.model_tester	,		"""expected_num_hidden_layers"""	,		len(self.model_tester.depths      ) + 1      )
             self.assertEqual(len(_SCREAMING_SNAKE_CASE      )	,		_SCREAMING_SNAKE_CASE      )

             # Swinv2 has a different seq_length
             _a       :					Any	   =(
                 config.patch_size
                 if isinstance(config.patch_size	,		collections.abc.Iterable      )
                 else (config.patch_size, config.patch_size)
             )

             _a       :					Tuple	   =(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       :					Union[str, Any]	   =outputs.reshaped_hidden_states
             self.assertEqual(len(_SCREAMING_SNAKE_CASE      )	,		_SCREAMING_SNAKE_CASE      )

             _a   ,       _a   ,       _a   ,       _a       :					Any	   =reshaped_hidden_states[0].shape
             _a       :					Optional[int]	   =(
                 reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		height * width      ).permute(0	,		2	,		1      )
             )
             self.assertListEqual(
                 list(reshaped_hidden_states.shape[-2:]      )	,		[num_patches, self.model_tester.embed_dim]	,		)






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




             '''simple docstring'''
             _a   ,       _a       :					List[Any]	   =self.model_tester.prepare_config_and_inputs_for_common()

             _a       :					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)
             )

             for model_class in self.all_model_classes:
                    _a       :					str	   =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       :					List[str]	   =True

                    self.check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE      )






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




             '''simple docstring'''
             _a   ,       _a       :					Optional[int]	   =self.model_tester.prepare_config_and_inputs_for_common()
             _a       :					List[Any]	   =3

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

             _a       :					str	   =image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
             _a       :					List[str]	   =image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

             for model_class in self.all_model_classes:
                    _a       :					Tuple	   =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       :					str	   =True
                    self.check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		(padded_height, padded_width)      )






      def __UpperCAmelCase			(					self     :Any      )			->   str:




             '''simple docstring'''
             _a       :					Any	   =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE      )






      def __UpperCAmelCase			(					self     :Optional[int]      )			->   int:




             '''simple docstring'''
             _a       :					str	   =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE      )






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




             '''simple docstring'''

             for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _a       :					Optional[Any]	   =SwinvaModel.from_pretrained(_SCREAMING_SNAKE_CASE      )
                    self.assertIsNotNone(_SCREAMING_SNAKE_CASE      )






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




             '''simple docstring'''
             _a   ,       _a       :					List[Any]	   =self.model_tester.prepare_config_and_inputs_for_common()

             _a       :					List[Any]	   =_config_zero_init(_SCREAMING_SNAKE_CASE      )
             for model_class in self.all_model_classes:
                    _a       :					Optional[int]	   =model_class(config=_SCREAMING_SNAKE_CASE      )
                    for name, param in model.named_parameters():
                           if "embeddings" not in name and "logit_scale" 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 A__			(		unittest.TestCase ):






      @cached_property
      def __UpperCAmelCase			(					self     :Tuple      )			->   Any:




             '''simple docstring'''
             return (
                 AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""      )
                 if is_vision_available()
                 else None
             )






      @slow
      def __UpperCAmelCase			(					self     :Tuple      )			->   str:




             '''simple docstring'''
             _a       :					List[str]	   =SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""      ).to(
                 _SCREAMING_SNAKE_CASE      )
             _a       :					List[str]	   =self.default_image_processor

             _a       :					Union[str, Any]	   =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
             _a       :					Optional[int]	   =image_processor(images=_SCREAMING_SNAKE_CASE	,		return_tensors="""pt"""      ).to(_SCREAMING_SNAKE_CASE      )

             # forward pass
             with torch.no_grad():
                    _a       :					str	   =model(**_SCREAMING_SNAKE_CASE      )

             # verify the logits
             _a       :					List[str]	   =torch.Size((1, 1_0_0_0)      )
             self.assertEqual(outputs.logits.shape	,		_SCREAMING_SNAKE_CASE      )
             _a       :					Any	   =torch.tensor([-0.3_947, -0.4_306, 0.0_026]      ).to(_SCREAMING_SNAKE_CASE      )
             self.assertTrue(torch.allclose(outputs.logits[0, :3]	,		_SCREAMING_SNAKE_CASE	,		atol=1e-4      )      )





'''simple docstring'''



def       snake_case   (	snake_case      : dict			) ->  set:


    """simple docstring"""


    lowerCAmelCase 						=							set()
    # edges = list of graph's edges
    lowerCAmelCase 						=							get_edges(snake_case			)

    # While there are still elements in edges list, take an arbitrary edge
    # (from_node, to_node) and add his extremity to chosen_vertices and then
    # remove all arcs adjacent to the from_node and to_node
    while edges:
        lowerCAmelCase						,   lowerCAmelCase 						=							edges.pop()
        chosen_vertices.add(snake_case			)
        chosen_vertices.add(snake_case			)
        for edge in edges.copy():
            if from_node in edge or to_node in edge:
                edges.discard(snake_case			)
    return chosen_vertices


def       snake_case   (	snake_case      : dict			) ->  set:


    """simple docstring"""


    lowerCAmelCase 						=							set()
    for from_node, to_nodes in graph.items():
        for to_node in to_nodes:
            edges.add((from_node, to_node)			)
    return edges


if __name__ == "__main__":
     import doctest

     doctest.testmod()

     # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]}
     # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")





