






import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
			import jax.numpy as jnp

			from transformers.models.distilbert.modeling_flax_distilbert import (
			    FlaxDistilBertForMaskedLM,
			    FlaxDistilBertForMultipleChoice,
			    FlaxDistilBertForQuestionAnswering,
			    FlaxDistilBertForSequenceClassification,
			    FlaxDistilBertForTokenClassification,
			    FlaxDistilBertModel,
			)




class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



						def __init__(self:					str ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					Dict=13 ,					__UpperCAmelCase:					Tuple=7 ,					__UpperCAmelCase:					Dict=True ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					int=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Tuple=99 ,					__UpperCAmelCase:					Any=32 ,					__UpperCAmelCase:					List[Any]=5 ,					__UpperCAmelCase:					Optional[Any]=4 ,					__UpperCAmelCase:					Union[str, Any]=37 ,					__UpperCAmelCase:					Any="gelu" ,					__UpperCAmelCase:					str=0.1 ,					__UpperCAmelCase:					int=0.1 ,					__UpperCAmelCase:					Any=512 ,					__UpperCAmelCase:					List[Any]=16 ,					__UpperCAmelCase:					Tuple=2 ,					__UpperCAmelCase:					List[Any]=0.02 ,					__UpperCAmelCase:					str=4 ,					)    ->     Tuple:




											'''simple docstring'''



											__a					:						Optional[Any]      =					parent
											__a					:						List[Any]      =					batch_size
											__a					:						Optional[Any]      =					seq_length
											__a					:						str      =					is_training
											__a					:						List[str]      =					use_attention_mask
											__a					:						Dict      =					use_token_type_ids
											__a					:						Optional[Any]      =					use_labels
											__a					:						Any      =					vocab_size
											__a					:						Union[str, Any]      =					hidden_size
											__a					:						int      =					num_hidden_layers
											__a					:						Tuple      =					num_attention_heads
											__a					:						List[Any]      =					intermediate_size
											__a					:						Optional[Any]      =					hidden_act
											__a					:						List[Any]      =					hidden_dropout_prob
											__a					:						Tuple      =					attention_probs_dropout_prob
											__a					:						Optional[int]      =					max_position_embeddings
											__a					:						Any      =					type_vocab_size
											__a					:						str      =					type_sequence_label_size
											__a					:						Union[str, Any]      =					initializer_range
											__a					:						Any      =					num_choices



						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Dict:




											'''simple docstring'''



											__a					:						Tuple      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						List[str]      =					None
											if self.use_attention_mask:
																__a					:						int      =					random_attention_mask([self.batch_size, self.seq_length]   )

											__a					:						List[str]      =					DistilBertConfig(
											    vocab_size=self.vocab_size ,					dim=self.hidden_size ,					n_layers=self.num_hidden_layers ,					n_heads=self.num_attention_heads ,					hidden_dim=self.intermediate_size ,					hidden_act=self.hidden_act ,					dropout=self.hidden_dropout_prob ,					attention_dropout=self.attention_probs_dropout_prob ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					tie_weights_=__UpperCAmelCase ,					)

											return config, input_ids, attention_mask



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




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a					:						List[str]      =					config_and_inputs
											__a					:						Tuple      =					{"input_ids": input_ids, "attention_mask": attention_mask}
											return config, inputs_dict




@require_flax
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (
						    (
						        FlaxDistilBertModel,
						        FlaxDistilBertForMaskedLM,
						        FlaxDistilBertForMultipleChoice,
						        FlaxDistilBertForQuestionAnswering,
						        FlaxDistilBertForSequenceClassification,
						        FlaxDistilBertForTokenClassification,
						        FlaxDistilBertForQuestionAnswering,
						    )
						    if is_flax_available()
						    else ()
						)



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




											'''simple docstring'''



											__a					:						Dict      =					FlaxDistilBertModelTester(self   )



						@slow
						def        UpperCAmelCase__					(self:					Any   )    ->     List[Any]:




											'''simple docstring'''



											for model_class_name in self.all_model_classes:
																__a					:						Optional[int]      =					model_class_name.from_pretrained("distilbert-base-uncased"   )
																__a					:						Optional[Any]      =					model(np.ones((1, 1)   )   )
																self.assertIsNotNone(__UpperCAmelCase   )




@require_flax
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											__a					:						Any      =					FlaxDistilBertModel.from_pretrained("distilbert-base-uncased"   )
											__a					:						Tuple      =					np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
											__a					:						List[Any]      =					np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
											__a					:						Dict      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase   )[0]
											__a					:						Optional[Any]      =					(1, 11, 768)
											self.assertEqual(output.shape ,					__UpperCAmelCase   )
											__a					:						Tuple      =					np.array([[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]   )

											self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] ,					__UpperCAmelCase ,					atol=1E-4   )   )













import copy
from typing import Dict, List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase__      			=						{
    '''facebook/mask2former-swin-small-coco-instance''': (
        '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json'''
    )
    # See all Mask2Former models at https://huggingface.co/models?filter=mask2former
}

UpperCAmelCase__      			=						logging.get_logger(__name__)




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """mask2former"""
						snake_case__        =      ["""swin"""]
						snake_case__        =      {"""hidden_size""": """hidden_dim"""}



						def __init__(self:					Any ,					__UpperCAmelCase:					Optional[Dict] = None ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 1024 ,					__UpperCAmelCase:					str = "relu" ,					__UpperCAmelCase:					int = 6 ,					__UpperCAmelCase:					int = 10 ,					__UpperCAmelCase:					int = 8 ,					__UpperCAmelCase:					float = 0.0 ,					__UpperCAmelCase:					int = 2048 ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					int = 4 ,					__UpperCAmelCase:					int = 255 ,					__UpperCAmelCase:					int = 100 ,					__UpperCAmelCase:					float = 0.1 ,					__UpperCAmelCase:					float = 2.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					int = 12544 ,					__UpperCAmelCase:					float = 3.0 ,					__UpperCAmelCase:					float = 0.75 ,					__UpperCAmelCase:					float = 0.02 ,					__UpperCAmelCase:					float = 1.0 ,					__UpperCAmelCase:					bool = True ,					__UpperCAmelCase:					List[int] = [4, 8, 16, 32] ,					__UpperCAmelCase:					bool = None ,					**__UpperCAmelCase:					Any ,					)    ->     int:




											'''simple docstring'''



											if backbone_config is None:
																logger.info("`backbone_config` is `None`. Initializing the config with the default `Swin` backbone."   )
																__a					:						str      =					CONFIG_MAPPING["swin"](
																    image_size=224 ,					in_channels=3 ,					patch_size=4 ,					embed_dim=96 ,					depths=[2, 2, 18, 2] ,					num_heads=[3, 6, 12, 24] ,					window_size=7 ,					drop_path_rate=0.3 ,					use_absolute_embeddings=__UpperCAmelCase ,					out_features=["stage1", "stage2", "stage3", "stage4"] ,					)

											if isinstance(__UpperCAmelCase ,					__UpperCAmelCase   ):
																__a					:						List[Any]      =					backbone_config.pop("model_type"   )
																__a					:						List[str]      =					CONFIG_MAPPING[backbone_model_type]
																__a					:						Dict      =					config_class.from_dict(__UpperCAmelCase   )

											# verify that the backbone is supported
											if backbone_config.model_type not in self.backbones_supported:
																logger.warning_once(
																    f'Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. '
																    f'Supported model types: {",".join(self.backbones_supported   )}'   )

											__a					:						Tuple      =					backbone_config
											__a					:						Any      =					feature_size
											__a					:						int      =					mask_feature_size
											__a					:						Optional[int]      =					hidden_dim
											__a					:						int      =					encoder_feedforward_dim
											__a					:						List[str]      =					activation_function
											__a					:						Optional[Any]      =					encoder_layers
											__a					:						List[Any]      =					decoder_layers
											__a					:						Union[str, Any]      =					num_attention_heads
											__a					:						Optional[Any]      =					dropout
											__a					:						Tuple      =					dim_feedforward
											__a					:						List[str]      =					pre_norm
											__a					:						int      =					enforce_input_projection
											__a					:						List[str]      =					common_stride
											__a					:						Any      =					ignore_value
											__a					:						Union[str, Any]      =					num_queries
											__a					:						int      =					no_object_weight
											__a					:						Tuple      =					class_weight
											__a					:						int      =					mask_weight
											__a					:						Union[str, Any]      =					dice_weight
											__a					:						int      =					train_num_points
											__a					:						Any      =					oversample_ratio
											__a					:						Any      =					importance_sample_ratio
											__a					:						List[str]      =					init_std
											__a					:						List[str]      =					init_xavier_std
											__a					:						Dict      =					use_auxiliary_loss
											__a					:						List[Any]      =					feature_strides
											__a					:						Tuple      =					output_auxiliary_logits
											__a					:						List[Any]      =					decoder_layers

											super().__init__(**__UpperCAmelCase   )



						@classmethod
						def        UpperCAmelCase__					(cls:					Tuple ,					__UpperCAmelCase:					PretrainedConfig ,					**__UpperCAmelCase:					List[str]   )    ->     Any:




											'''simple docstring'''



											return cls(
											    backbone_config=__UpperCAmelCase ,					**__UpperCAmelCase ,					)



						def        UpperCAmelCase__					(self:					str   )    ->     Dict[str, any]:




											'''simple docstring'''



											__a					:						Tuple      =					copy.deepcopy(self.__dict__   )
											__a					:						List[str]      =					self.backbone_config.to_dict()
											__a					:						Any      =					self.__class__.model_type
											return output





