
import unittest

import numpy as np

from transformers import AlbertConfig, 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.albert.modeling_flax_albert import (
						    FlaxAlbertForMaskedLM,
						    FlaxAlbertForMultipleChoice,
						    FlaxAlbertForPreTraining,
						    FlaxAlbertForQuestionAnswering,
						    FlaxAlbertForSequenceClassification,
						    FlaxAlbertForTokenClassification,
						    FlaxAlbertModel,
						)





class  snake_case     (							unittest.TestCase   ):

							def __init__(self       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_=13       ,       SCREAMING_SNAKE_CASE_=7       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=99       ,       SCREAMING_SNAKE_CASE_=32       ,       SCREAMING_SNAKE_CASE_=5       ,       SCREAMING_SNAKE_CASE_=4       ,       SCREAMING_SNAKE_CASE_=37       ,       SCREAMING_SNAKE_CASE_="gelu"       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=5_12       ,       SCREAMING_SNAKE_CASE_=16       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=0.02       ,       SCREAMING_SNAKE_CASE_=4       ,       ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = parent
									SCREAMING_SNAKE_CASE_						 = batch_size
									SCREAMING_SNAKE_CASE_						 = seq_length
									SCREAMING_SNAKE_CASE_						 = is_training
									SCREAMING_SNAKE_CASE_						 = use_attention_mask
									SCREAMING_SNAKE_CASE_						 = use_token_type_ids
									SCREAMING_SNAKE_CASE_						 = use_labels
									SCREAMING_SNAKE_CASE_						 = vocab_size
									SCREAMING_SNAKE_CASE_						 = hidden_size
									SCREAMING_SNAKE_CASE_						 = num_hidden_layers
									SCREAMING_SNAKE_CASE_						 = num_attention_heads
									SCREAMING_SNAKE_CASE_						 = intermediate_size
									SCREAMING_SNAKE_CASE_						 = hidden_act
									SCREAMING_SNAKE_CASE_						 = hidden_dropout_prob
									SCREAMING_SNAKE_CASE_						 = attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE_						 = max_position_embeddings
									SCREAMING_SNAKE_CASE_						 = type_vocab_size
									SCREAMING_SNAKE_CASE_						 = type_sequence_label_size
									SCREAMING_SNAKE_CASE_						 = initializer_range
									SCREAMING_SNAKE_CASE_						 = num_choices

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = ids_tensor([self.batch_size, self.seq_length]       ,       self.vocab_size    )

									SCREAMING_SNAKE_CASE_						 = None
									if self.use_attention_mask:
											SCREAMING_SNAKE_CASE_						 = random_attention_mask([self.batch_size, self.seq_length]    )

									SCREAMING_SNAKE_CASE_						 = None
									if self.use_token_type_ids:
											SCREAMING_SNAKE_CASE_						 = ids_tensor([self.batch_size, self.seq_length]       ,       self.type_vocab_size    )

									SCREAMING_SNAKE_CASE_						 = AlbertConfig(
									    vocab_size=self.vocab_size       ,       hidden_size=self.hidden_size       ,       num_hidden_layers=self.num_hidden_layers       ,       num_attention_heads=self.num_attention_heads       ,       intermediate_size=self.intermediate_size       ,       hidden_act=self.hidden_act       ,       hidden_dropout_prob=self.hidden_dropout_prob       ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,       max_position_embeddings=self.max_position_embeddings       ,       type_vocab_size=self.type_vocab_size       ,       is_decoder=SCREAMING_SNAKE_CASE_       ,       initializer_range=self.initializer_range       ,       )

									return config, input_ids, token_type_ids, attention_mask







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_						 = config_and_inputs
									SCREAMING_SNAKE_CASE_						 = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
									return config, inputs_dict







@require_flax
class  snake_case     (							__lowercase       ,			unittest.TestCase   ):
							UpperCAmelCase__						     =      (
							    (
							        FlaxAlbertModel,
							        FlaxAlbertForPreTraining,
							        FlaxAlbertForMaskedLM,
							        FlaxAlbertForMultipleChoice,
							        FlaxAlbertForQuestionAnswering,
							        FlaxAlbertForSequenceClassification,
							        FlaxAlbertForTokenClassification,
							        FlaxAlbertForQuestionAnswering,
							    )
							    if is_flax_available()
							    else ()
							)

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = FlaxAlbertModelTester(self    )







							@slow
							def _lowercase					(self    ):

									"""simple docstring"""







									for model_class_name in self.all_model_classes:
											SCREAMING_SNAKE_CASE_						 = model_class_name.from_pretrained('''albert-base-v2'''    )
											SCREAMING_SNAKE_CASE_						 = model(np.ones((1, 1)    )    )
											self.assertIsNotNone(SCREAMING_SNAKE_CASE_    )







@require_flax
class  snake_case     (							unittest.TestCase   ):

							@slow
							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = FlaxAlbertModel.from_pretrained('''albert-base-v2'''    )
									SCREAMING_SNAKE_CASE_						 = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]    )
									SCREAMING_SNAKE_CASE_						 = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
									SCREAMING_SNAKE_CASE_						 = model(SCREAMING_SNAKE_CASE_       ,       attention_mask=SCREAMING_SNAKE_CASE_    )[0]
									SCREAMING_SNAKE_CASE_						 = (1, 11, 7_68)
									self.assertEqual(output.shape       ,       SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = np.array(
									    [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]    )

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

"""simple docstring"""




import copy
from typing import Dict, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING
from ..detr import DetrConfig
from ..swin import SwinConfig


lowerCAmelCase__							=				{
    'facebook/maskformer-swin-base-ade': (
        'https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json'
    )
    # See all MaskFormer models at https://huggingface.co/models?filter=maskformer
}

lowerCAmelCase__							=				logging.get_logger(__name__)





class  snake_case     (							__lowercase   ):
							UpperCAmelCase__						     =      '''maskformer'''
							UpperCAmelCase__						     =      {'''hidden_size''': '''mask_feature_size'''}
							UpperCAmelCase__						     =      ['''resnet''', '''swin''']
							UpperCAmelCase__						     =      ['''detr''']

							def __init__(self       ,       SCREAMING_SNAKE_CASE_ = 2_56       ,       SCREAMING_SNAKE_CASE_ = 2_56       ,       SCREAMING_SNAKE_CASE_ = 0.1       ,       SCREAMING_SNAKE_CASE_ = False       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = None       ,       SCREAMING_SNAKE_CASE_ = 0.02       ,       SCREAMING_SNAKE_CASE_ = 1.0       ,       SCREAMING_SNAKE_CASE_ = 1.0       ,       SCREAMING_SNAKE_CASE_ = 1.0       ,       SCREAMING_SNAKE_CASE_ = 20.0       ,       SCREAMING_SNAKE_CASE_ = None       ,       **SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									if backbone_config is None:
											# fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
											SCREAMING_SNAKE_CASE_						 = SwinConfig(
											    image_size=3_84       ,       in_channels=3       ,       patch_size=4       ,       embed_dim=1_28       ,       depths=[2, 2, 18, 2]       ,       num_heads=[4, 8, 16, 32]       ,       window_size=12       ,       drop_path_rate=0.3       ,       out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4''']       ,       )

									if isinstance(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):
											SCREAMING_SNAKE_CASE_						 = backbone_config.pop('''model_type'''    )
											SCREAMING_SNAKE_CASE_						 = CONFIG_MAPPING[backbone_model_type]
											SCREAMING_SNAKE_CASE_						 = config_class.from_dict(SCREAMING_SNAKE_CASE_    )

									# 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 MaskFormer. '
											    f'Supported model types: {",".join(self.backbones_supported    )}'    )

									if decoder_config is None:
											# fall back to https://huggingface.co/facebook/detr-resnet-50
											SCREAMING_SNAKE_CASE_						 = DetrConfig()
									else:
											# verify that the decoder is supported
											SCREAMING_SNAKE_CASE_						 = (
											    decoder_config.pop('''model_type'''    ) if isinstance(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ) else decoder_config.model_type
											)
											if decoder_type not in self.decoders_supported:
													raise ValueError(
													    f'Transformer Decoder {decoder_type} not supported, please use one of'
													    f' {",".join(self.decoders_supported    )}'    )
											if isinstance(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):
													SCREAMING_SNAKE_CASE_						 = CONFIG_MAPPING[decoder_type]
													SCREAMING_SNAKE_CASE_						 = config_class.from_dict(SCREAMING_SNAKE_CASE_    )

									SCREAMING_SNAKE_CASE_						 = backbone_config
									SCREAMING_SNAKE_CASE_						 = decoder_config
									# main feature dimension for the model
									SCREAMING_SNAKE_CASE_						 = fpn_feature_size
									SCREAMING_SNAKE_CASE_						 = mask_feature_size
									# initializer
									SCREAMING_SNAKE_CASE_						 = init_std
									SCREAMING_SNAKE_CASE_						 = init_xavier_std
									# Hungarian matcher && loss
									SCREAMING_SNAKE_CASE_						 = cross_entropy_weight
									SCREAMING_SNAKE_CASE_						 = dice_weight
									SCREAMING_SNAKE_CASE_						 = mask_weight
									SCREAMING_SNAKE_CASE_						 = use_auxiliary_loss
									SCREAMING_SNAKE_CASE_						 = no_object_weight
									SCREAMING_SNAKE_CASE_						 = output_auxiliary_logits

									SCREAMING_SNAKE_CASE_						 = self.decoder_config.encoder_attention_heads
									SCREAMING_SNAKE_CASE_						 = self.decoder_config.num_hidden_layers
									super().__init__(**SCREAMING_SNAKE_CASE_    )

							@classmethod
							def _lowercase					(cls       ,       SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_       ,       **SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									return cls(
									    backbone_config=SCREAMING_SNAKE_CASE_       ,       decoder_config=SCREAMING_SNAKE_CASE_       ,       **SCREAMING_SNAKE_CASE_       ,       )







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = copy.deepcopy(self.__dict__    )
									SCREAMING_SNAKE_CASE_						 = self.backbone_config.to_dict()
									SCREAMING_SNAKE_CASE_						 = self.decoder_config.to_dict()
									SCREAMING_SNAKE_CASE_						 = self.__class__.model_type
									return output