

'''simple docstring'''






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 			UpperCAmelCase					:




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




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				List[str]:
            _lowerCAmelCase												=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

            _lowerCAmelCase												=							None
            if self.use_labels:
                  _lowerCAmelCase												=							ids_tensor([self.batch_size]      ,      self.type_sequence_label_size  )

            _lowerCAmelCase												=							self.get_config()

            return config, pixel_values, labels




      def     lowercase__   (      self      :			int  )    ->				Any:
            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     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Optional[Any]  )    ->				Dict:
            _lowerCAmelCase												=							MaskFormerSwinModel(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths  ) - 1))
            _lowerCAmelCase												=							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     lowercase__   (      self      :			Optional[Any]      ,      __snake_case      :			Any      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Optional[int]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							MaskFormerSwinBackbone(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case  )

            # 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(__snake_case  ):
                  _lowerCAmelCase												=							["""stem"""]
                  _lowerCAmelCase												=							MaskFormerSwinBackbone(config=__snake_case  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				int:
            _lowerCAmelCase												=							self.prepare_config_and_inputs()
            _lowerCAmelCase					,							_lowerCAmelCase					,							_lowerCAmelCase												=							config_and_inputs
            _lowerCAmelCase												=							{"""pixel_values""": pixel_values}
            return config, inputs_dict




@require_torch
class 			UpperCAmelCase					(      snake_case_							,		snake_case_							,		unittest.TestCase       ):
      _lowercase:      str											=       (
          (
              MaskFormerSwinModel,
              MaskFormerSwinBackbone,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:      List[Any]											=       {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
      _lowercase:      Optional[Any]											=       False
      _lowercase:      List[str]											=       False
      _lowercase:      Optional[Any]											=       False
      _lowercase:      str											=       False
      _lowercase:      Union[str, Any]											=       False




      def     lowercase__   (      self      :			Optional[Any]  )    ->				str:
            _lowerCAmelCase												=							MaskFormerSwinModelTester(self  )
            _lowerCAmelCase												=							ConfigTester(self      ,      config_class=__snake_case      ,      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     lowercase__   (      self      :			Tuple  )    ->				str:
            pass




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




      def     lowercase__   (      self      :			Tuple  )    ->				Tuple:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__snake_case  )




      def     lowercase__   (      self      :			Optional[int]  )    ->				str:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_backbone(*__snake_case  )




      @unittest.skip("""Swin does not use inputs_embeds"""  )
      def     lowercase__   (      self      :			str  )    ->				List[str]:
            pass




      @unittest.skip("""Swin does not support feedforward chunking"""  )
      def     lowercase__   (      self      :			Any  )    ->				Union[str, Any]:
            pass




      def     lowercase__   (      self      :			Optional[Any]  )    ->				List[str]:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

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




      def     lowercase__   (      self      :			str  )    ->				Any:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

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

                  _lowerCAmelCase												=							["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]      ,      __snake_case  )




      @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions"""  )
      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Any:
            pass




      @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone"""  )
      def     lowercase__   (      self      :			List[str]  )    ->				Any:
            pass




      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			str      ,      __snake_case      :			str      ,      __snake_case      :			Optional[int]  )    ->				Optional[int]:
            _lowerCAmelCase												=							model_class(__snake_case  )
            model.to(__snake_case  )
            model.eval()

            with torch.no_grad():
                  _lowerCAmelCase												=							model(**self._prepare_for_class(__snake_case      ,      __snake_case  )  )

            _lowerCAmelCase												=							outputs.hidden_states

            _lowerCAmelCase												=							getattr(
                self.model_tester      ,      """expected_num_hidden_layers"""      ,      len(self.model_tester.depths  ) + 1  )
            self.assertEqual(len(__snake_case  )      ,      __snake_case  )

            # Swin has a different seq_length
            _lowerCAmelCase												=							(
                config.patch_size
                if isinstance(config.patch_size      ,      collections.abc.Iterable  )
                else (config.patch_size, config.patch_size)
            )

            _lowerCAmelCase												=							(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     lowercase__   (      self      :			List[str]  )    ->				Any:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

            _lowerCAmelCase												=							(
                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												=							True
                  self.check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case      ,      __snake_case  )

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  _lowerCAmelCase												=							True

                  self.check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case      ,      __snake_case  )




      def     lowercase__   (      self      :			Any  )    ->				Any:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase												=							3

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

            _lowerCAmelCase												=							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
            _lowerCAmelCase												=							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

            for model_class in self.all_model_classes:
                  _lowerCAmelCase												=							True
                  self.check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case      ,      (padded_height, padded_width)  )

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  _lowerCAmelCase												=							True
                  self.check_hidden_states_output(__snake_case      ,      __snake_case      ,      __snake_case      ,      (padded_height, padded_width)  )




      @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints"""  )
      def     lowercase__   (      self      :			List[Any]  )    ->				Any:
            pass




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




      @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""  )
      def     lowercase__   (      self      :			Dict  )    ->				Optional[int]:
            pass




      def     lowercase__   (      self      :			List[str]  )    ->				Tuple:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()

            def set_nan_tensor_to_zero(__snake_case      :			List[str]  ):
                  _lowerCAmelCase												=							0
                  return t

            def check_equivalence(__snake_case      :			Any      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Dict={}  ):
                  with torch.no_grad():
                        _lowerCAmelCase												=							model(**__snake_case      ,      return_dict=__snake_case      ,      **__snake_case  )
                        _lowerCAmelCase												=							model(**__snake_case      ,      return_dict=__snake_case      ,      **__snake_case  ).to_tuple()

                        def recursive_check(__snake_case      :			int      ,      __snake_case      :			Any  ):
                              if isinstance(__snake_case      ,      (List, Tuple)  ):
                                    for tuple_iterable_value, dict_iterable_value in zip(__snake_case      ,      __snake_case  ):
                                          recursive_check(__snake_case      ,      __snake_case  )
                              elif isinstance(__snake_case      ,      __snake_case  ):
                                    for tuple_iterable_value, dict_iterable_value in zip(
                                        tuple_object.values()      ,      dict_object.values()  ):
                                          recursive_check(__snake_case      ,      __snake_case  )
                              elif tuple_object is None:
                                    return
                              else:
                                    self.assertTrue(
                                        torch.allclose(
                                            set_nan_tensor_to_zero(__snake_case  )      ,      set_nan_tensor_to_zero(__snake_case  )      ,      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(__snake_case  ).any()} and `inf`: {torch.isinf(__snake_case  )}. Dict has"
                                            f" `nan`: {torch.isnan(__snake_case  ).any()} and `inf`: {torch.isinf(__snake_case  )}."
                                        )      ,      )

                        recursive_check(__snake_case      ,      __snake_case  )

            for model_class in self.all_model_classes:
                  _lowerCAmelCase												=							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()

                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case  )
                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case  )
                  check_equivalence(__snake_case      ,      __snake_case      ,      __snake_case  )

                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )
                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )
                  check_equivalence(__snake_case      ,      __snake_case      ,      __snake_case  )

                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case  )
                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case  )
                  check_equivalence(__snake_case      ,      __snake_case      ,      __snake_case      ,      {"""output_hidden_states""": True}  )

                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )
                  _lowerCAmelCase												=							self._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )
                  check_equivalence(__snake_case      ,      __snake_case      ,      __snake_case      ,      {"""output_hidden_states""": True}  )




@require_torch
class 			UpperCAmelCase					(      unittest.TestCase							,		snake_case_       ):
      _lowercase:      int											=       (MaskFormerSwinBackbone,) if is_torch_available() else ()
      _lowercase:      Dict											=       MaskFormerSwinConfig




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				str:
            _lowerCAmelCase												=							MaskFormerSwinModelTester(self  )




      def     lowercase__   (      self      :			str  )    ->				Tuple:
            _lowerCAmelCase					,							_lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase												=							inputs_dict["""pixel_values"""].shape[0]

            for backbone_class in self.all_model_classes:
                  _lowerCAmelCase												=							backbone_class(__snake_case  )
                  backbone.to(__snake_case  )
                  backbone.eval()

                  _lowerCAmelCase												=							backbone(**__snake_case  )

                  # Test default outputs and verify feature maps
                  self.assertIsInstance(outputs.feature_maps      ,      __snake_case  )
                  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
                  _lowerCAmelCase												=							backbone(**__snake_case      ,      output_hidden_states=__snake_case  )
                  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)
                              _lowerCAmelCase					,							_lowerCAmelCase					,							_lowerCAmelCase												=							hidden_state.shape
                              self.assertTrue((h_batch_size, h_n_channels)      ,      (batch_size, n_channels)  )

            # Test output_attentions=True
                  if self.has_attentions:
                        _lowerCAmelCase												=							backbone(**__snake_case      ,      output_attentions=__snake_case  )
                        self.assertIsNotNone(outputs.attentions  )













'''simple docstring'''






# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

A__    :       List[str]              =TypeVar('''T''')




class 			UpperCAmelCase					(      Generic[T]       ):




      def __init__(      self      :			Tuple      ,      __snake_case      :			bool = True  )    ->				None:
            _lowerCAmelCase												=							{}  # dictionary of lists
            _lowerCAmelCase												=							directed




      def     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			T      ,      __snake_case      :			T  )    ->				GraphAdjacencyList[T]:

            if not self.directed:  # For undirected graphs
                  # if both source vertex and destination vertex are both present in the
                  # adjacency list, add destination vertex to source vertex list of adjacent
                  # vertices and add source vertex to destination vertex list of adjacent
                  # vertices.
                  if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        self.adj_list[destination_vertex].append(__snake_case  )
                  # if only source vertex is present in adjacency list, add destination vertex
                  # to source vertex list of adjacent vertices, then create a new vertex with
                  # destination vertex as key and assign a list containing the source vertex
                  # as it's first adjacent vertex.
                  elif source_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[source_vertex]
                  # if only destination vertex is present in adjacency list, add source vertex
                  # to destination vertex list of adjacent vertices, then create a new vertex
                  # with source vertex as key and assign a list containing the source vertex
                  # as it's first adjacent vertex.
                  elif destination_vertex in self.adj_list:
                        self.adj_list[destination_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[destination_vertex]
                  # if both source vertex and destination vertex are not present in adjacency
                  # list, create a new vertex with source vertex as key and assign a list
                  # containing the destination vertex as it's first adjacent vertex also
                  # create a new vertex with destination vertex as key and assign a list
                  # containing the source vertex as it's first adjacent vertex.
                  else:
                        _lowerCAmelCase												=							[destination_vertex]
                        _lowerCAmelCase												=							[source_vertex]
            else:  # For directed graphs
                  # if both source vertex and destination vertex are present in adjacency
                  # list, add destination vertex to source vertex list of adjacent vertices.
                  if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                  # if only source vertex is present in adjacency list, add destination
                  # vertex to source vertex list of adjacent vertices and create a new vertex
                  # with destination vertex as key, which has no adjacent vertex
                  elif source_vertex in self.adj_list:
                        self.adj_list[source_vertex].append(__snake_case  )
                        _lowerCAmelCase												=							[]
                  # if only destination vertex is present in adjacency list, create a new
                  # vertex with source vertex as key and assign a list containing destination
                  # vertex as first adjacent vertex
                  elif destination_vertex in self.adj_list:
                        _lowerCAmelCase												=							[destination_vertex]
                  # if both source vertex and destination vertex are not present in adjacency
                  # list, create a new vertex with source vertex as key and a list containing
                  # destination vertex as it's first adjacent vertex. Then create a new vertex
                  # with destination vertex as key, which has no adjacent vertex
                  else:
                        _lowerCAmelCase												=							[destination_vertex]
                        _lowerCAmelCase												=							[]

            return self




      def __repr__(      self      :			int  )    ->				str:
            return pformat(self.adj_list  )






