

'''simple docstring'''


import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    AlbertForMaskedLM,
						    AlbertForMultipleChoice,
						    AlbertForPreTraining,
						    AlbertForQuestionAnswering,
						    AlbertForSequenceClassification,
						    AlbertForTokenClassification,
						    AlbertModel,
						)
						from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class 					lowercase						:


							def __init__(			self    ,			_snake_case    ,			_snake_case=13    ,			_snake_case=7    ,			_snake_case=True    ,			_snake_case=True    ,			_snake_case=True    ,			_snake_case=True    ,			_snake_case=99    ,			_snake_case=16    ,			_snake_case=36    ,			_snake_case=6    ,			_snake_case=6    ,			_snake_case=6    ,			_snake_case=37    ,			_snake_case="gelu"    ,			_snake_case=0.1    ,			_snake_case=0.1    ,			_snake_case=512    ,			_snake_case=16    ,			_snake_case=2    ,			_snake_case=0.02    ,			_snake_case=3    ,			_snake_case=4    ,			_snake_case=None    ,			)							->				List[Any]:
								UpperCAmelCase_		:      Optional[int]       	=					parent
								UpperCAmelCase_		:      Any       	=					batch_size
								UpperCAmelCase_		:      Tuple       	=					seq_length
								UpperCAmelCase_		:      List[Any]       	=					is_training
								UpperCAmelCase_		:      Tuple       	=					use_input_mask
								UpperCAmelCase_		:      Any       	=					use_token_type_ids
								UpperCAmelCase_		:      Any       	=					use_labels
								UpperCAmelCase_		:      Dict       	=					vocab_size
								UpperCAmelCase_		:      Dict       	=					embedding_size
								UpperCAmelCase_		:      List[Any]       	=					hidden_size
								UpperCAmelCase_		:      Optional[Any]       	=					num_hidden_layers
								UpperCAmelCase_		:      Optional[int]       	=					num_hidden_groups
								UpperCAmelCase_		:      Tuple       	=					num_attention_heads
								UpperCAmelCase_		:      List[Any]       	=					intermediate_size
								UpperCAmelCase_		:      Optional[int]       	=					hidden_act
								UpperCAmelCase_		:      Optional[int]       	=					hidden_dropout_prob
								UpperCAmelCase_		:      Tuple       	=					attention_probs_dropout_prob
								UpperCAmelCase_		:      Tuple       	=					max_position_embeddings
								UpperCAmelCase_		:      str       	=					type_vocab_size
								UpperCAmelCase_		:      List[str]       	=					type_sequence_label_size
								UpperCAmelCase_		:      List[str]       	=					initializer_range
								UpperCAmelCase_		:      Tuple       	=					num_labels
								UpperCAmelCase_		:      int       	=					num_choices
								UpperCAmelCase_		:      List[Any]       	=					scope


							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Optional[int]       	=					ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

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

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

								UpperCAmelCase_		:      Optional[Any]       	=					None
								UpperCAmelCase_		:      Union[str, Any]       	=					None
								UpperCAmelCase_		:      Dict       	=					None
								if self.use_labels:
									UpperCAmelCase_		:      Any       	=					ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)
									UpperCAmelCase_		:      Dict       	=					ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels)
									UpperCAmelCase_		:      Optional[Any]       	=					ids_tensor([self.batch_size]    ,			self.num_choices)

								UpperCAmelCase_		:      Any       	=					self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


							def 						_snake_case							(			self)							->				List[str]:
								return 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    ,			initializer_range=self.initializer_range    ,			num_hidden_groups=self.num_hidden_groups    ,			)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				List[str]:
								UpperCAmelCase_		:      Tuple       	=					AlbertModel(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      Any       	=					model(lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_)
								UpperCAmelCase_		:      List[Any]       	=					model(lowerCamelCase_    ,			token_type_ids=lowerCamelCase_)
								UpperCAmelCase_		:      List[Any]       	=					model(lowerCamelCase_)
								self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))
								self.parent.assertEqual(result.pooler_output.shape    ,			(self.batch_size, self.hidden_size))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				List[Any]:
								UpperCAmelCase_		:      Optional[Any]       	=					AlbertForPreTraining(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      Dict       	=					model(
								    lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			labels=lowerCamelCase_    ,			sentence_order_label=lowerCamelCase_    ,			)
								self.parent.assertEqual(result.prediction_logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))
								self.parent.assertEqual(result.sop_logits.shape    ,			(self.batch_size, config.num_labels))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				List[str]:
								UpperCAmelCase_		:      Tuple       	=					AlbertForMaskedLM(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      List[Any]       	=					model(lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			labels=lowerCamelCase_)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Union[str, Any]:
								UpperCAmelCase_		:      List[Any]       	=					AlbertForQuestionAnswering(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      str       	=					model(
								    lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			start_positions=lowerCamelCase_    ,			end_positions=lowerCamelCase_    ,			)
								self.parent.assertEqual(result.start_logits.shape    ,			(self.batch_size, self.seq_length))
								self.parent.assertEqual(result.end_logits.shape    ,			(self.batch_size, self.seq_length))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Any:
								UpperCAmelCase_		:      Union[str, Any]       	=					self.num_labels
								UpperCAmelCase_		:      str       	=					AlbertForSequenceClassification(lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      Tuple       	=					model(lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			labels=lowerCamelCase_)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				str:
								UpperCAmelCase_		:      Optional[Any]       	=					self.num_labels
								UpperCAmelCase_		:      Tuple       	=					AlbertForTokenClassification(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      List[str]       	=					model(lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			labels=lowerCamelCase_)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.num_labels))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[int]:
								UpperCAmelCase_		:      List[str]       	=					self.num_choices
								UpperCAmelCase_		:      int       	=					AlbertForMultipleChoice(config=lowerCamelCase_)
								model.to(lowerCamelCase_)
								model.eval()
								UpperCAmelCase_		:      Dict       	=					input_ids.unsqueeze(1).expand(-1    ,			self.num_choices    ,			-1).contiguous()
								UpperCAmelCase_		:      Union[str, Any]       	=					token_type_ids.unsqueeze(1).expand(-1    ,			self.num_choices    ,			-1).contiguous()
								UpperCAmelCase_		:      Optional[int]       	=					input_mask.unsqueeze(1).expand(-1    ,			self.num_choices    ,			-1).contiguous()
								UpperCAmelCase_		:      List[Any]       	=					model(
								    lowerCamelCase_    ,			attention_mask=lowerCamelCase_    ,			token_type_ids=lowerCamelCase_    ,			labels=lowerCamelCase_    ,			)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_choices))





							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Tuple       	=					self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,    (
								    UpperCAmelCase_      
								)	,          
								)		:      int       	=					config_and_inputs
								UpperCAmelCase_		:      Union[str, Any]       	=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict



@require_torch
class 					lowercase						(  a__,					a__,					unittest.TestCase ):
							_lowerCamelCase		:      Dict=   (
							    (
							        AlbertModel,
							        AlbertForPreTraining,
							        AlbertForMaskedLM,
							        AlbertForMultipleChoice,
							        AlbertForSequenceClassification,
							        AlbertForTokenClassification,
							        AlbertForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase		:      str=   (
							    {
							        "feature-extraction": AlbertModel,
							        "fill-mask": AlbertForMaskedLM,
							        "question-answering": AlbertForQuestionAnswering,
							        "text-classification": AlbertForSequenceClassification,
							        "token-classification": AlbertForTokenClassification,
							        "zero-shot": AlbertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_lowerCamelCase		:      Optional[int]=   True


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case=False)							->				int:
								UpperCAmelCase_		:      Union[str, Any]       	=					super()._prepare_for_class(lowerCamelCase_    ,			lowerCamelCase_    ,			return_labels=lowerCamelCase_)

								if return_labels:
									if model_class in get_values(lowerCamelCase_):
										UpperCAmelCase_		:      Tuple       	=					torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)    ,			dtype=torch.long    ,			device=lowerCamelCase_)
										UpperCAmelCase_		:      str       	=					torch.zeros(
										    self.model_tester.batch_size    ,			dtype=torch.long    ,			device=lowerCamelCase_)
								return inputs_dict


							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      List[str]       	=					AlbertModelTester(self)
								UpperCAmelCase_		:      List[str]       	=					ConfigTester(self    ,			config_class=lowerCamelCase_    ,			hidden_size=37)


							def 						_snake_case							(			self)							->				Any:
								self.config_tester.run_common_tests()


							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      List[str]       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      str       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				List[str]:
								UpperCAmelCase_		:      Any       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Union[str, Any]       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				Tuple:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				List[str]:
								UpperCAmelCase_		:      str       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_)


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
									UpperCAmelCase_		:      str       	=					type
									self.model_tester.create_and_check_model(*lowerCamelCase_)





							@slow
							def 						_snake_case							(			self)							->				Tuple:
								for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_		:      Dict       	=					AlbertModel.from_pretrained(lowerCamelCase_)
									self.assertIsNotNone(lowerCamelCase_)



@require_torch
class 					lowercase						(  unittest.TestCase ):


							@slow
							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      Union[str, Any]       	=					AlbertModel.from_pretrained('albert-base-v2')
								UpperCAmelCase_		:      Union[str, Any]       	=					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]])
								UpperCAmelCase_		:      Optional[Any]       	=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
								with torch.no_grad():
									UpperCAmelCase_		:      Optional[Any]       	=					model(lowerCamelCase_    ,			attention_mask=lowerCamelCase_)[0]
								UpperCAmelCase_		:      List[Any]       	=					torch.Size((1, 11, 768))
								self.assertEqual(output.shape    ,			lowerCamelCase_)
								UpperCAmelCase_		:      List[str]       	=					torch.tensor(
								    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]])

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









'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCAmelCase__										= logging.get_logger(__name__)

lowerCAmelCase__										= {
    "microsoft/swin-tiny-patch4-window7-224": (
        "https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json"
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}



class 					lowercase						(  a_,					a_ ):
							_lowerCamelCase		:      Optional[int]=   "swin"

							_lowerCamelCase		:      Dict=   {
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}


							def __init__(			self    ,			_snake_case=224    ,			_snake_case=4    ,			_snake_case=3    ,			_snake_case=96    ,			_snake_case=[2, 2, 6, 2]    ,			_snake_case=[3, 6, 12, 24]    ,			_snake_case=7    ,			_snake_case=4.0    ,			_snake_case=True    ,			_snake_case=0.0    ,			_snake_case=0.0    ,			_snake_case=0.1    ,			_snake_case="gelu"    ,			_snake_case=False    ,			_snake_case=0.02    ,			_snake_case=1e-5    ,			_snake_case=32    ,			_snake_case=None    ,			_snake_case=None    ,			**_snake_case    ,			)							->				Tuple:
								super().__init__(**_snake_case)

								UpperCAmelCase_		:      Any       	=					image_size
								UpperCAmelCase_		:      Dict       	=					patch_size
								UpperCAmelCase_		:      int       	=					num_channels
								UpperCAmelCase_		:      Tuple       	=					embed_dim
								UpperCAmelCase_		:      Dict       	=					depths
								UpperCAmelCase_		:      Dict       	=					len(_snake_case)
								UpperCAmelCase_		:      List[Any]       	=					num_heads
								UpperCAmelCase_		:      Optional[int]       	=					window_size
								UpperCAmelCase_		:      Any       	=					mlp_ratio
								UpperCAmelCase_		:      Any       	=					qkv_bias
								UpperCAmelCase_		:      str       	=					hidden_dropout_prob
								UpperCAmelCase_		:      Optional[int]       	=					attention_probs_dropout_prob
								UpperCAmelCase_		:      Dict       	=					drop_path_rate
								UpperCAmelCase_		:      int       	=					hidden_act
								UpperCAmelCase_		:      Tuple       	=					use_absolute_embeddings
								UpperCAmelCase_		:      Optional[int]       	=					layer_norm_eps
								UpperCAmelCase_		:      Optional[int]       	=					initializer_range
								UpperCAmelCase_		:      Optional[Any]       	=					encoder_stride
								# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								UpperCAmelCase_		:      Union[str, Any]       	=					int(embed_dim * 2 ** (len(_snake_case) - 1))
								UpperCAmelCase_		:      Tuple       	=					['stem'] + [F"""stage{idx}""" for idx in range(1    ,			len(_snake_case) + 1)]
								UpperCAmelCase_	,    UpperCAmelCase_		:      Dict       	=					get_aligned_output_features_output_indices(
								    out_features=_snake_case    ,			out_indices=_snake_case    ,			stage_names=self.stage_names)



class 					lowercase						(  a_ ):
							_lowerCamelCase		:      Tuple=   version.parse("1.11" )


							@property
							def 						_snake_case							(			self)							->				Mapping[str, Mapping[int, str]]:
								return OrderedDict(
								    [
								        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
								    ])





							@property
							def 						_snake_case							(			self)							->				float:
								return 1e-4






