






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      :

    """simple docstring"""




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

        '''simple docstring'''
        lowercase__     				=    parent
        lowercase__     				=    batch_size
        lowercase__     				=    image_size
        lowercase__     				=    patch_size
        lowercase__     				=    num_channels
        lowercase__     				=    embed_dim
        lowercase__     				=    depths
        lowercase__     				=    num_heads
        lowercase__     				=    window_size
        lowercase__     				=    mlp_ratio
        lowercase__     				=    qkv_bias
        lowercase__     				=    hidden_dropout_prob
        lowercase__     				=    attention_probs_dropout_prob
        lowercase__     				=    drop_path_rate
        lowercase__     				=    hidden_act
        lowercase__     				=    use_absolute_embeddings
        lowercase__     				=    patch_norm
        lowercase__     				=    layer_norm_eps
        lowercase__     				=    initializer_range
        lowercase__     				=    is_training
        lowercase__     				=    scope
        lowercase__     				=    use_labels
        lowercase__     				=    type_sequence_label_size
        lowercase__     				=    encoder_stride
        lowercase__     				=    out_features
        lowercase__     				=    out_indices




    def        lowercase__     (  self		:					List[Any]						):

        '''simple docstring'''
        lowercase__     				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

        lowercase__     				=    self.get_config()

        return config, pixel_values, labels




    def        lowercase__     (  self		:					int						):

        '''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        lowercase__     (  self		:					Optional[int],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					List[str]						):

        '''simple docstring'''
        lowercase__     				=    MaskFormerSwinModel(config=A_						)
        model.to(A_						)
        model.eval()
        lowercase__     				=    model(A_						)

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

        '''simple docstring'''
        lowercase__     				=    MaskFormerSwinBackbone(config=A_						)
        model.to(A_						)
        model.eval()
        lowercase__     				=    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_						):
            lowercase__     				=    ['''stem''']
            lowercase__     				=    MaskFormerSwinBackbone(config=A_						)




    def        lowercase__     (  self		:					List[Any]						):

        '''simple docstring'''
        lowercase__     				=    self.prepare_config_and_inputs()
        lowercase__				,		lowercase__				,		lowercase__     				=    config_and_inputs
        lowercase__     				=    {'''pixel_values''': pixel_values}
        return config, inputs_dict



@require_torch
class    _UpperCAmelCase      ( _UpperCAmelCase	,_UpperCAmelCase	,unittest.TestCase			):

    """simple docstring"""




    lowercase__       							= (
        (
            MaskFormerSwinModel,
            MaskFormerSwinBackbone,
        )
        if is_torch_available()
        else ()
    )
    lowercase__       							= {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
    lowercase__       							= False
    lowercase__       							= False
    lowercase__       							= False
    lowercase__       							= False
    lowercase__       							= False




    def        lowercase__     (  self		:					int						):

        '''simple docstring'''
        lowercase__     				=    MaskFormerSwinModelTester(self						)
        lowercase__     				=    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        lowercase__     (  self		:					Any						):

        '''simple docstring'''
        pass




    def        lowercase__     (  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        lowercase__     (  self		:					int						):

        '''simple docstring'''
        return




    def        lowercase__     (  self		:					Tuple						):

        '''simple docstring'''
        lowercase__     				=    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A_						)




    def        lowercase__     (  self		:					List[str]						):

        '''simple docstring'''
        lowercase__     				=    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*A_						)




    @unittest.skip('''Swin does not use inputs_embeds'''						)
    def        lowercase__     (  self		:					List[str]						):

        '''simple docstring'''
        pass




    @unittest.skip('''Swin does not support feedforward chunking'''						)
    def        lowercase__     (  self		:					str						):

        '''simple docstring'''
        pass




    def        lowercase__     (  self		:					List[str]						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

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




    def        lowercase__     (  self		:					Union[str, Any]						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

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

            lowercase__     				=    ['''pixel_values''']
            self.assertListEqual(arg_names[:1],						A_						)




    @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'''						)
    def        lowercase__     (  self		:					Tuple						):

        '''simple docstring'''
        pass




    @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''						)
    def        lowercase__     (  self		:					Any						):

        '''simple docstring'''
        pass




    def        lowercase__     (  self		:					Dict,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Union[str, Any]						):

        '''simple docstring'''
        lowercase__     				=    model_class(A_						)
        model.to(A_						)
        model.eval()

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

        lowercase__     				=    outputs.hidden_states

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

        lowercase__     				=    (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		:					Dict						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

        lowercase__     				=    (
            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__     				=    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"]
            lowercase__     				=    True

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




    def        lowercase__     (  self		:					Any						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__     				=    3

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

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

        for model_class in self.all_model_classes:
            lowercase__     				=    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"]
            lowercase__     				=    True
            self.check_hidden_states_output(A_,						A_,						A_,						(padded_height, padded_width)						)




    @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''						)
    def        lowercase__     (  self		:					Optional[Any]						):

        '''simple docstring'''
        pass




    @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''						)
    def        lowercase__     (  self		:					Tuple						):

        '''simple docstring'''
        pass




    @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''						)
    def        lowercase__     (  self		:					List[str]						):

        '''simple docstring'''
        pass




    def        lowercase__     (  self		:					Optional[Any]						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

        def set_nan_tensor_to_zero(lowerCamelCase		:					List[str]						):
            lowercase__     				=    0
            return t

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

                def recursive_check(lowerCamelCase		:					List[Any],						lowerCamelCase		:					List[str]						):
                    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:
            lowercase__     				=    model_class(A_						)
            model.to(A_						)
            model.eval()

            lowercase__     				=    self._prepare_for_class(A_,						A_						)
            lowercase__     				=    self._prepare_for_class(A_,						A_						)
            check_equivalence(A_,						A_,						A_						)

            lowercase__     				=    self._prepare_for_class(A_,						A_,						return_labels=A_						)
            lowercase__     				=    self._prepare_for_class(A_,						A_,						return_labels=A_						)
            check_equivalence(A_,						A_,						A_						)

            lowercase__     				=    self._prepare_for_class(A_,						A_						)
            lowercase__     				=    self._prepare_for_class(A_,						A_						)
            check_equivalence(A_,						A_,						A_,						{'''output_hidden_states''': True}						)

            lowercase__     				=    self._prepare_for_class(A_,						A_,						return_labels=A_						)
            lowercase__     				=    self._prepare_for_class(A_,						A_,						return_labels=A_						)
            check_equivalence(A_,						A_,						A_,						{'''output_hidden_states''': True}						)



@require_torch
class    _UpperCAmelCase      ( unittest.TestCase	,_UpperCAmelCase			):

    """simple docstring"""




    lowercase__       							= (MaskFormerSwinBackbone,) if is_torch_available() else ()
    lowercase__       							= MaskFormerSwinConfig




    def        lowercase__     (  self		:					Optional[Any]						):

        '''simple docstring'''
        lowercase__     				=    MaskFormerSwinModelTester(self						)




    def        lowercase__     (  self		:					Optional[int]						):

        '''simple docstring'''
        lowercase__				,		lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__     				=    inputs_dict['''pixel_values'''].shape[0]

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

            lowercase__     				=    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
            lowercase__     				=    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)
                    lowercase__				,		lowercase__				,		lowercase__     				=    hidden_state.shape
                    self.assertTrue((h_batch_size, h_n_channels),						(batch_size, n_channels)						)

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












def 						a    (		lowerCamelCase_	,     lowerCamelCase_	,     lowerCamelCase_       ):






				'''simple docstring'''

				if exponent == 1:
								return base
				if exponent % 2 == 0:
								lowercase__     				=    _modexpt(lowerCamelCase_	,     exponent // 2	,     lowerCamelCase_       ) % modulo_value
								return (x * x) % modulo_value
				else:
								return (base * _modexpt(lowerCamelCase_	,     exponent - 1	,     lowerCamelCase_       )) % modulo_value






def 						a    (		lowerCamelCase_ = 1777	,     lowerCamelCase_ = 1855	,     lowerCamelCase_ = 8       ):






				'''simple docstring'''
				lowercase__     				=    base
				for _ in range(1	,     lowerCamelCase_       ):
								lowercase__     				=    _modexpt(lowerCamelCase_	,     lowerCamelCase_	,     10**digits       )

				return result


if __name__ == "__main__":
							print(F"{solution() = }")




