






import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import BertGenerationDecoder, BertGenerationEncoder




class 							snake_case_       :

						"""simple docstring"""



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




											'''simple docstring'''



											__a					:						List[Any]      =					parent
											__a					:						int      =					batch_size
											__a					:						List[str]      =					seq_length
											__a					:						int      =					is_training
											__a					:						List[Any]      =					use_input_mask
											__a					:						Optional[Any]      =					vocab_size
											__a					:						int      =					hidden_size
											__a					:						List[Any]      =					num_hidden_layers
											__a					:						Union[str, Any]      =					num_attention_heads
											__a					:						Tuple      =					intermediate_size
											__a					:						List[Any]      =					hidden_act
											__a					:						List[str]      =					hidden_dropout_prob
											__a					:						Tuple      =					attention_probs_dropout_prob
											__a					:						Optional[int]      =					max_position_embeddings
											__a					:						Any      =					initializer_range
											__a					:						List[str]      =					use_labels
											__a					:						Any      =					scope



						def        UpperCAmelCase__					(self:					Dict   )    ->     int:




											'''simple docstring'''



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

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

											if self.use_labels:
																__a					:						int      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						Union[str, Any]      =					self.get_config()

											return config, input_ids, input_mask, token_labels



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




											'''simple docstring'''



											return BertGenerationConfig(
											    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 ,					is_decoder=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					)



						def        UpperCAmelCase__					(self:					int   )    ->     Any:




											'''simple docstring'''



											(
											    (
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			
											)					:						str      =					self.prepare_config_and_inputs()

											__a					:						Union[str, Any]      =					True
											__a					:						Union[str, Any]      =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
											__a					:						List[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					vocab_size=2   )

											return (
											    config,
											    input_ids,
											    input_mask,
											    token_labels,
											    encoder_hidden_states,
											    encoder_attention_mask,
											)



						def        UpperCAmelCase__					(self:					int ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Optional[int] ,					**__UpperCAmelCase:					Union[str, Any] ,					)    ->     List[str]:




											'''simple docstring'''



											__a					:						Optional[int]      =					BertGenerationEncoder(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase   )
											__a					:						str      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					Union[str, Any] ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Optional[Any] ,					**__UpperCAmelCase:					Dict ,					)    ->     List[str]:




											'''simple docstring'''



											__a					:						Tuple      =					True
											__a					:						Optional[Any]      =					BertGenerationEncoder(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					)
											__a					:						str      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					)
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					Optional[Any] ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					List[Any] ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					List[Any] ,					**__UpperCAmelCase:					Union[str, Any] ,					)    ->     str:




											'''simple docstring'''



											__a					:						int      =					True
											__a					:						Union[str, Any]      =					True
											__a					:						str      =					BertGenerationDecoder(config=__UpperCAmelCase   ).to(__UpperCAmelCase   ).eval()

											# first forward pass
											__a					:						Tuple      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					use_cache=__UpperCAmelCase ,					)
											__a					:						str      =					outputs.past_key_values

											# create hypothetical multiple next token and extent to next_input_ids
											__a					:						Any      =					ids_tensor((self.batch_size, 3) ,					config.vocab_size   )
											__a					:						List[str]      =					ids_tensor((self.batch_size, 3) ,					vocab_size=2   )

											# append to next input_ids and
											__a					:						int      =					torch.cat([input_ids, next_tokens] ,					dim=-1   )
											__a					:						List[str]      =					torch.cat([input_mask, next_mask] ,					dim=-1   )

											__a					:						Optional[Any]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					)["hidden_states"][0]
											__a					:						Union[str, Any]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					past_key_values=__UpperCAmelCase ,					output_hidden_states=__UpperCAmelCase ,					)["hidden_states"][0]

											# select random slice
											__a					:						Union[str, Any]      =					ids_tensor((1,) ,					output_from_past.shape[-1]   ).item()
											__a					:						List[str]      =					output_from_no_past[:, -3:, random_slice_idx].detach()
											__a					:						List[Any]      =					output_from_past[:, :, random_slice_idx].detach()

											self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

											# test that outputs are equal for slice
											self.parent.assertTrue(torch.allclose(__UpperCAmelCase ,					__UpperCAmelCase ,					atol=1E-3   )   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					str ,					*__UpperCAmelCase:					Optional[int] ,					)    ->     str:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					BertGenerationDecoder(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Any      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)   )



						def        UpperCAmelCase__					(self:					Dict   )    ->     int:




											'''simple docstring'''



											__a      ,			__a      ,			__a      ,			__a					:						Optional[int]      =					self.prepare_config_and_inputs()
											__a					:						Dict      =					{"input_ids": input_ids, "attention_mask": input_mask}
											return config, inputs_dict




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

						"""simple docstring"""



						snake_case__        =      (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
						snake_case__        =      (BertGenerationDecoder,) if is_torch_available() else ()
						snake_case__        =      (
						    {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
						    if is_torch_available()
						    else {}
						)



						def        UpperCAmelCase__					(self:					int   )    ->     Tuple:




											'''simple docstring'''



											__a					:						Optional[Any]      =					BertGenerationEncoderTester(self   )
											__a					:						int      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					int   )    ->     int:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Any:




											'''simple docstring'''



											__a					:						Tuple      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



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




											'''simple docstring'''



											__a      ,			__a      ,			__a      ,			__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs()
											__a					:						Union[str, Any]      =					"bert"
											self.model_tester.create_and_check_model(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     int:




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase   )



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




											'''simple docstring'''



											__a					:						Tuple      =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_decoder_model_past_large_inputs(*__UpperCAmelCase   )



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




											'''simple docstring'''



											(
											    (
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			
											)					:						int      =					self.model_tester.prepare_config_and_inputs_for_decoder()

											__a					:						Dict      =					None

											self.model_tester.create_and_check_model_as_decoder(
											    __UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					)



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




											'''simple docstring'''



											__a					:						str      =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_for_causal_lm(*__UpperCAmelCase   )



						@slow
						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Optional[Any]      =					BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"   )
											self.assertIsNotNone(__UpperCAmelCase   )




@require_torch
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											__a					:						Tuple      =					BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"   )
											__a					:						List[Any]      =					torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]   )
											with torch.no_grad():
																__a					:						Any      =					model(__UpperCAmelCase   )[0]
											__a					:						List[str]      =					torch.Size([1, 8, 1024]   )
											self.assertEqual(output.shape ,					__UpperCAmelCase   )
											__a					:						str      =					torch.tensor(
											    [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]]   )
											self.assertTrue(torch.allclose(output[:, :3, :3] ,					__UpperCAmelCase ,					atol=1E-4   )   )




@require_torch
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											__a					:						int      =					BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"   )
											__a					:						Dict      =					torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]   )
											with torch.no_grad():
																__a					:						List[str]      =					model(__UpperCAmelCase   )[0]
											__a					:						Optional[int]      =					torch.Size([1, 8, 50358]   )
											self.assertEqual(output.shape ,					__UpperCAmelCase   )
											__a					:						List[Any]      =					torch.tensor(
											    [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]]   )
											self.assertTrue(torch.allclose(output[:, :3, :3] ,					__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





