


"""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    lowerCamelCase__						:
      def __init__(		self	:	Union[str, Any]      ,       A_	:	Optional[int]      ,       A_	:	str=1_3      ,       A_	:	str=7      ,       A_	:	Optional[int]=True      ,       A_	:	List[Any]=True      ,       A_	:	Any=True      ,       A_	:	int=True      ,       A_	:	Tuple=9_9      ,       A_	:	Optional[Any]=1_6      ,       A_	:	Dict=3_6      ,       A_	:	List[Any]=6      ,       A_	:	Optional[Any]=6      ,       A_	:	Any=6      ,       A_	:	Optional[int]=3_7      ,       A_	:	Tuple="gelu"      ,       A_	:	str=0.1      ,       A_	:	int=0.1      ,       A_	:	int=5_1_2      ,       A_	:	List[Any]=1_6      ,       A_	:	List[Any]=2      ,       A_	:	int=0.02      ,       A_	:	Any=3      ,       A_	:	Dict=4      ,       A_	:	Optional[Any]=None      ,       ):






       '''simple docstring'''


       __lowercase									=	parent
       __lowercase									=	batch_size
       __lowercase									=	seq_length
       __lowercase									=	is_training
       __lowercase									=	use_input_mask
       __lowercase									=	use_token_type_ids
       __lowercase									=	use_labels
       __lowercase									=	vocab_size
       __lowercase									=	embedding_size
       __lowercase									=	hidden_size
       __lowercase									=	num_hidden_layers
       __lowercase									=	num_hidden_groups
       __lowercase									=	num_attention_heads
       __lowercase									=	intermediate_size
       __lowercase									=	hidden_act
       __lowercase									=	hidden_dropout_prob
       __lowercase									=	attention_probs_dropout_prob
       __lowercase									=	max_position_embeddings
       __lowercase									=	type_vocab_size
       __lowercase									=	type_sequence_label_size
       __lowercase									=	initializer_range
       __lowercase									=	num_labels
       __lowercase									=	num_choices
       __lowercase									=	scope
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


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

       __lowercase									=	None
       if self.use_input_mask:
        __lowercase									=	random_attention_mask([self.batch_size, self.seq_length]							)

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

       __lowercase									=	None
       __lowercase									=	None
       __lowercase									=	None
       if self.use_labels:
        __lowercase									=	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size							)
        __lowercase									=	ids_tensor([self.batch_size, self.seq_length]      ,       self.num_labels							)
        __lowercase									=	ids_tensor([self.batch_size]      ,       self.num_choices							)

       __lowercase									=	self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict							):






       '''simple docstring'''


       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 SCREAMING_SNAKE_CASE_	(		self	:	Tuple      ,       A_	:	Tuple      ,       A_	:	List[Any]      ,       A_	:	int      ,       A_	:	Dict      ,       A_	:	Dict      ,       A_	:	Union[str, Any]      ,       A_	:	List[Any]							):






       '''simple docstring'''


       __lowercase									=	AlbertModel(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(A_      ,       attention_mask=A_      ,       token_type_ids=A_							)
       __lowercase									=	model(A_      ,       token_type_ids=A_							)
       __lowercase									=	model(A_							)
       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 SCREAMING_SNAKE_CASE_	(		self	:	str      ,       A_	:	Union[str, Any]      ,       A_	:	Optional[int]      ,       A_	:	int      ,       A_	:	Optional[int]      ,       A_	:	Any      ,       A_	:	Optional[Any]      ,       A_	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	AlbertForPreTraining(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(
           A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_      ,       sentence_order_label=A_      ,       )
       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 SCREAMING_SNAKE_CASE_	(		self	:	int      ,       A_	:	List[str]      ,       A_	:	Union[str, Any]      ,       A_	:	int      ,       A_	:	Optional[Any]      ,       A_	:	List[str]      ,       A_	:	int      ,       A_	:	Dict							):






       '''simple docstring'''


       __lowercase									=	AlbertForMaskedLM(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_							)
       self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Any      ,       A_	:	List[Any]      ,       A_	:	Dict      ,       A_	:	str      ,       A_	:	Any      ,       A_	:	Tuple      ,       A_	:	Optional[Any]      ,       A_	:	int							):






       '''simple docstring'''


       __lowercase									=	AlbertForQuestionAnswering(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(
           A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       start_positions=A_      ,       end_positions=A_      ,       )
       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 SCREAMING_SNAKE_CASE_	(		self	:	int      ,       A_	:	Tuple      ,       A_	:	int      ,       A_	:	Any      ,       A_	:	Optional[Any]      ,       A_	:	List[str]      ,       A_	:	List[str]      ,       A_	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	self.num_labels
       __lowercase									=	AlbertForSequenceClassification(A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_							)
       self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]      ,       A_	:	List[Any]      ,       A_	:	str      ,       A_	:	List[Any]      ,       A_	:	Dict      ,       A_	:	Optional[Any]      ,       A_	:	Optional[Any]      ,       A_	:	str							):






       '''simple docstring'''


       __lowercase									=	self.num_labels
       __lowercase									=	AlbertForTokenClassification(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_							)
       self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.num_labels)							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple      ,       A_	:	Optional[int]      ,       A_	:	Optional[int]      ,       A_	:	Any      ,       A_	:	Optional[int]      ,       A_	:	int      ,       A_	:	Union[str, Any]      ,       A_	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	self.num_choices
       __lowercase									=	AlbertForMultipleChoice(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	input_ids.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	token_type_ids.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	input_mask.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	model(
           A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_      ,       )
       self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices)							)






      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	self.prepare_config_and_inputs()
       (
           (
           __lowercase		
       )							,		(
           __lowercase		
       )							,		(
           __lowercase		
       )							,		(
           __lowercase		
       )							,		(
           __lowercase		
       )							,		(
           __lowercase		
       )							,		(
           __lowercase		
       )							,				
       )									=	config_and_inputs
       __lowercase									=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
       return config, inputs_dict






@require_torch
class    lowerCamelCase__						(		_a			,   _a			,   unittest.TestCase    ):
      a :     Optional[int]											=		(
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      a :     List[Any]											=		(
          {
              """feature-extraction""": AlbertModel,
              """fill-mask""": AlbertForMaskedLM,
              """question-answering""": AlbertForQuestionAnswering,
              """text-classification""": AlbertForSequenceClassification,
              """token-classification""": AlbertForTokenClassification,
              """zero-shot""": AlbertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      a :     int											=		True
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict      ,       A_	:	Dict      ,       A_	:	str      ,       A_	:	List[str]=False							):






       '''simple docstring'''


       __lowercase									=	super()._prepare_for_class(A_      ,       A_      ,       return_labels=A_							)

       if return_labels:
        if model_class in get_values(A_							):
         __lowercase									=	torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)      ,       dtype=torch.long      ,       device=A_							)
         __lowercase									=	torch.zeros(
             self.model_tester.batch_size      ,       dtype=torch.long      ,       device=A_							)
       return inputs_dict
      def SCREAMING_SNAKE_CASE_	(		self	:	List[Any]							):






       '''simple docstring'''


       __lowercase									=	AlbertModelTester(self							)
       __lowercase									=	ConfigTester(self      ,       config_class=A_      ,       hidden_size=3_7							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       self.config_tester.run_common_tests()
      def SCREAMING_SNAKE_CASE_	(		self	:	List[str]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	List[Any]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
        __lowercase									=	type
        self.model_tester.create_and_check_model(*A_							)






      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowercase									=	AlbertModel.from_pretrained(A_							)
        self.assertIsNotNone(A_							)






@require_torch
class    lowerCamelCase__						(		unittest.TestCase    ):
      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	AlbertModel.from_pretrained("""albert-base-v2"""							)
       __lowercase									=	torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]							)
       __lowercase									=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
       with torch.no_grad():
        __lowercase									=	model(A_      ,       attention_mask=A_							)[0]
       __lowercase									=	torch.Size((1, 1_1, 7_6_8)							)
       self.assertEqual(output.shape      ,       A_							)
       __lowercase									=	torch.tensor(
           [[[-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(torch.allclose(output[:, 1:4, 1:4]      ,       A_      ,       atol=1e-4							)							)




"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__  							=logging.get_logger(__name__)

UpperCAmelCase__  							={
    "microsoft/swinv2-tiny-patch4-window8-256": (
        "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json"
    ),
}





class    lowerCamelCase__						(		_a    ):
      a :     List[Any]											=		"""swinv2"""

      a :     Any											=		{
          """num_attention_heads""": """num_heads""",
          """num_hidden_layers""": """num_layers""",
      }
      def __init__(		self	:	List[str]      ,       A_	:	Any=2_2_4      ,       A_	:	int=4      ,       A_	:	Optional[int]=3      ,       A_	:	List[Any]=9_6      ,       A_	:	List[Any]=[2, 2, 6, 2]      ,       A_	:	List[str]=[3, 6, 1_2, 2_4]      ,       A_	:	Union[str, Any]=7      ,       A_	:	int=4.0      ,       A_	:	List[str]=True      ,       A_	:	str=0.0      ,       A_	:	Any=0.0      ,       A_	:	Union[str, Any]=0.1      ,       A_	:	Optional[Any]="gelu"      ,       A_	:	int=False      ,       A_	:	List[Any]=0.02      ,       A_	:	Tuple=1e-5      ,       A_	:	Tuple=3_2      ,       **A_	:	int      ,       ):






       '''simple docstring'''


       super().__init__(**A_							)

       __lowercase									=	image_size
       __lowercase									=	patch_size
       __lowercase									=	num_channels
       __lowercase									=	embed_dim
       __lowercase									=	depths
       __lowercase									=	len(A_							)
       __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									=	layer_norm_eps
       __lowercase									=	initializer_range
       __lowercase									=	encoder_stride
       # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       __lowercase									=	int(embed_dim * 2 ** (len(A_							) - 1)							)
       __lowercase									=	(0, 0, 0, 0)
