


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 _lowerCamelCase:
       def __init__(   self, lowerCamelCase, lowerCamelCase=13, lowerCamelCase=32, lowerCamelCase=2, lowerCamelCase=3, lowerCamelCase=16, lowerCamelCase=[1, 2, 1], lowerCamelCase=[2, 2, 4], lowerCamelCase=2, lowerCamelCase=2.0, lowerCamelCase=True, lowerCamelCase=0.0, lowerCamelCase=0.0, lowerCamelCase=0.1, lowerCamelCase="gelu", lowerCamelCase=False, lowerCamelCase=True, lowerCamelCase=0.0_2, lowerCamelCase=1E-5, lowerCamelCase=True, lowerCamelCase=None, lowerCamelCase=True, lowerCamelCase=10, lowerCamelCase=8, lowerCamelCase=["stage1", "stage2", "stage3"], lowerCamelCase=[1, 2, 3], )			->  List[Any]:




           """simple docstring"""

           _lowercase			:				Any 						=						parent
           _lowercase			:				Any 						=						batch_size
           _lowercase			:				List[Any] 						=						image_size
           _lowercase			:				List[Any] 						=						patch_size
           _lowercase			:				List[Any] 						=						num_channels
           _lowercase			:				List[Any] 						=						embed_dim
           _lowercase			:				List[Any] 						=						depths
           _lowercase			:				int 						=						num_heads
           _lowercase			:				List[str] 						=						window_size
           _lowercase			:				str 						=						mlp_ratio
           _lowercase			:				Dict 						=						qkv_bias
           _lowercase			:				Tuple 						=						hidden_dropout_prob
           _lowercase			:				Any 						=						attention_probs_dropout_prob
           _lowercase			:				List[str] 						=						drop_path_rate
           _lowercase			:				List[Any] 						=						hidden_act
           _lowercase			:				List[Any] 						=						use_absolute_embeddings
           _lowercase			:				str 						=						patch_norm
           _lowercase			:				List[str] 						=						layer_norm_eps
           _lowercase			:				Any 						=						initializer_range
           _lowercase			:				List[Any] 						=						is_training
           _lowercase			:				str 						=						scope
           _lowercase			:				Dict 						=						use_labels
           _lowercase			:				Optional[int] 						=						type_sequence_label_size
           _lowercase			:				str 						=						encoder_stride
           _lowercase			:				Tuple 						=						out_features
           _lowercase			:				Optional[Any] 						=						out_indices
       def      UpperCamelCase	(   self)			->  List[str]:




           """simple docstring"""

           _lowercase			:				Optional[Any] 						=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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

           return config, pixel_values, labels
       def      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           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      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase)			->  str:




           """simple docstring"""

           _lowercase			:				int 						=						MaskFormerSwinModel(config=lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()
           _lowercase			:				List[Any] 						=						model(lowerCamelCase)

           _lowercase			:				Optional[int] 						=						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
           _lowercase			:				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      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase)			->  Union[str, Any]:




           """simple docstring"""

           _lowercase			:				Optional[Any] 						=						MaskFormerSwinBackbone(config=lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()
           _lowercase			:				Optional[int] 						=						model(lowerCamelCase)

           # 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(lowerCamelCase):
               _lowercase			:				Tuple 						=						['stem']
               _lowercase			:				Union[str, Any] 						=						MaskFormerSwinBackbone(config=lowerCamelCase)




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




           """simple docstring"""

           _lowercase			:				Union[str, Any] 						=						self.prepare_config_and_inputs()
           _lowercase						,							_lowercase						,							_lowercase			:				List[Any] 						=						config_and_inputs
           _lowercase			:				int 						=						{'pixel_values': pixel_values}
           return config, inputs_dict






@require_torch
class _lowerCamelCase(		_a,					_a,					unittest.TestCase  ):
       lowercase_					:      str       					=     (
           (
               MaskFormerSwinModel,
               MaskFormerSwinBackbone,
           )
           if is_torch_available()
           else ()
       )
       lowercase_					:      Optional[Any]       					=     {"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
       lowercase_					:      Optional[Any]       					=     False
       lowercase_					:      Optional[Any]       					=     False
       lowercase_					:      Tuple       					=     False
       lowercase_					:      Tuple       					=     False
       lowercase_					:      Tuple       					=     False
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				Union[str, Any] 						=						MaskFormerSwinModelTester(self)
           _lowercase			:				List[str] 						=						ConfigTester(self, config_class=lowerCamelCase, 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      UpperCamelCase	(   self)			->  int:




           """simple docstring"""

           pass
       def      UpperCamelCase	(   self)			->  str:




           """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      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

           return
       def      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           _lowercase			:				Dict 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase)
       def      UpperCamelCase	(   self)			->  List[Any]:




           """simple docstring"""

           _lowercase			:				Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*lowerCamelCase)
       @unittest.skip('Swin does not use inputs_embeds')
       def      UpperCamelCase	(   self)			->  Dict:




           """simple docstring"""

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




           """simple docstring"""

           pass
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase						,							_lowercase			:				Any 						=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               _lowercase			:				List[str] 						=						model_class(lowerCamelCase)
               self.assertIsInstance(model.get_input_embeddings(), (nn.Module))
               _lowercase			:				List[str] 						=						model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowerCamelCase, nn.Linear))
       def      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

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

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

               _lowercase			:				Optional[int] 						=						['pixel_values']
               self.assertListEqual(arg_names[:1], lowerCamelCase)
       @unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions')
       def      UpperCamelCase	(   self)			->  str:




           """simple docstring"""

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




           """simple docstring"""

           pass
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase)			->  Tuple:




           """simple docstring"""

           _lowercase			:				Optional[int] 						=						model_class(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           with torch.no_grad():
               _lowercase			:				List[str] 						=						model(**self._prepare_for_class(lowerCamelCase, lowerCamelCase))

           _lowercase			:				str 						=						outputs.hidden_states

           _lowercase			:				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
           _lowercase			:				List[str] 						=						(
               config.patch_size
               if isinstance(config.patch_size, collections.abc.Iterable)
               else (config.patch_size, config.patch_size)
           )

           _lowercase			:				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], )
       def      UpperCamelCase	(   self)			->  List[str]:




           """simple docstring"""

           _lowercase						,							_lowercase			:				int 						=						self.model_tester.prepare_config_and_inputs_for_common()

           _lowercase			:				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:
               _lowercase			:				str 						=						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"]
               _lowercase			:				Optional[int] 						=						True

               self.check_hidden_states_output(lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase)
       def      UpperCamelCase	(   self)			->  str:




           """simple docstring"""

           _lowercase						,							_lowercase			:				Any 						=						self.model_tester.prepare_config_and_inputs_for_common()
           _lowercase			:				List[str] 						=						3

           _lowercase			:				str 						=						(
               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)
           )
           _lowercase			:				List[str] 						=						(
               config.patch_size
               if isinstance(config.patch_size, collections.abc.Iterable)
               else (config.patch_size, config.patch_size)
           )

           _lowercase			:				Optional[Any] 						=						image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           _lowercase			:				Dict 						=						image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

           for model_class in self.all_model_classes:
               _lowercase			:				Any 						=						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"]
               _lowercase			:				Any 						=						True
               self.check_hidden_states_output(lowerCamelCase, lowerCamelCase, lowerCamelCase, (padded_height, padded_width))
       @unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints')
       def      UpperCamelCase	(   self)			->  List[str]:




           """simple docstring"""

           pass
       @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin')
       def      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

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




           """simple docstring"""

           pass




       def      UpperCamelCase	(   self)			->  Tuple:




           """simple docstring"""

           _lowercase						,							_lowercase			:				Dict 						=						self.model_tester.prepare_config_and_inputs_for_common()

           def set_nan_tensor_to_zero(lowerCamelCase):
               _lowercase			:				str 						=						0
               return t

           def check_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase={}):
               with torch.no_grad():
                   _lowercase			:				List[str] 						=						model(**lowerCamelCase, return_dict=lowerCamelCase, **lowerCamelCase)
                   _lowercase			:				Optional[Any] 						=						model(**lowerCamelCase, return_dict=lowerCamelCase, **lowerCamelCase).to_tuple()

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

                   recursive_check(lowerCamelCase, lowerCamelCase)

           for model_class in self.all_model_classes:
               _lowercase			:				str 						=						model_class(lowerCamelCase)
               model.to(lowerCamelCase)
               model.eval()

               _lowercase			:				Optional[Any] 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase)
               _lowercase			:				Dict 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase)
               check_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase)

               _lowercase			:				Tuple 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase, return_labels=lowerCamelCase)
               _lowercase			:				Optional[int] 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase, return_labels=lowerCamelCase)
               check_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase)

               _lowercase			:				str 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase)
               _lowercase			:				List[Any] 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase)
               check_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase, {'output_hidden_states': True})

               _lowercase			:				List[str] 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase, return_labels=lowerCamelCase)
               _lowercase			:				List[str] 						=						self._prepare_for_class(lowerCamelCase, lowerCamelCase, return_labels=lowerCamelCase)
               check_equivalence(lowerCamelCase, lowerCamelCase, lowerCamelCase, {'output_hidden_states': True})






@require_torch
class _lowerCamelCase(		unittest.TestCase,					_a  ):
       lowercase_					:      Dict       					=     (MaskFormerSwinBackbone,) if is_torch_available() else ()
       lowercase_					:      int       					=     MaskFormerSwinConfig
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				List[str] 						=						MaskFormerSwinModelTester(self)




       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase						,							_lowercase			:				List[str] 						=						self.model_tester.prepare_config_and_inputs_for_common()
           _lowercase			:				int 						=						inputs_dict['pixel_values'].shape[0]

           for backbone_class in self.all_model_classes:
               _lowercase			:				Tuple 						=						backbone_class(lowerCamelCase)
               backbone.to(lowerCamelCase)
               backbone.eval()

               _lowercase			:				List[str] 						=						backbone(**lowerCamelCase)

               # Test default outputs and verify feature maps
               self.assertIsInstance(outputs.feature_maps, lowerCamelCase)
               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
               _lowercase			:				Any 						=						backbone(**lowerCamelCase, output_hidden_states=lowerCamelCase)
               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)
                       _lowercase						,							_lowercase						,							_lowercase			:				Optional[Any] 						=						hidden_state.shape
                       self.assertTrue((h_batch_size, h_n_channels), (batch_size, n_channels))

            # Test output_attentions=True
               if self.has_attentions:
                   _lowercase			:				str 						=						backbone(**lowerCamelCase, output_attentions=lowerCamelCase)
                   self.assertIsNotNone(outputs.attentions)






from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands



def 							UpperCamelCase_( )       ->		List[Any]:
    _lowercase			:				int 						=						ArgumentParser('Transformers CLI tool'				,					usage='transformers-cli <command> [<args>]'					)
    _lowercase			:				Optional[Any] 						=						parser.add_subparsers(help='transformers-cli command helpers'					)

    # Register commands
    ConvertCommand.register_subcommand(lowerCamelCase_					)
    DownloadCommand.register_subcommand(lowerCamelCase_					)
    EnvironmentCommand.register_subcommand(lowerCamelCase_					)
    RunCommand.register_subcommand(lowerCamelCase_					)
    ServeCommand.register_subcommand(lowerCamelCase_					)
    UserCommands.register_subcommand(lowerCamelCase_					)
    AddNewModelCommand.register_subcommand(lowerCamelCase_					)
    AddNewModelLikeCommand.register_subcommand(lowerCamelCase_					)
    LfsCommands.register_subcommand(lowerCamelCase_					)
    PTtoTFCommand.register_subcommand(lowerCamelCase_					)

    # Let's go
    _lowercase			:				Any 						=						parser.parse_args()

    if not hasattr(lowerCamelCase_				,					'func'					):
        parser.print_help()
        exit(1					)

    # Run
    _lowercase			:				Optional[int] 						=						args.func(lowerCamelCase_					)
    service.run()


if __name__ == "__main__":
 main()


