





'''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 					UpperCAmelCase	:




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




      def 							lowercase__       (     self     :					Optional[Any]    )							-> Tuple:
            _lowerCAmelCase					=				ids_tensor([self.batch_size, self.seq_length]     ,     self.vocab_size    )

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

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

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

            _lowerCAmelCase					=				self.get_config()

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




      def 							lowercase__       (     self     :					str    )							-> Tuple:
            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 							lowercase__       (     self     :					Optional[int]     ,     __snake_case     :					List[str]     ,     __snake_case     :					str     ,     __snake_case     :					Dict     ,     __snake_case     :					Any     ,     __snake_case     :					str     ,     __snake_case     :					Any     ,     __snake_case     :					Optional[int]    )							-> str:
            _lowerCAmelCase					=				AlbertModel(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				model(lowerCamelCase__     ,     attention_mask=lowerCamelCase__     ,     token_type_ids=lowerCamelCase__    )
            _lowerCAmelCase					=				model(lowerCamelCase__     ,     token_type_ids=lowerCamelCase__    )
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					Optional[Any]     ,     __snake_case     :					List[str]     ,     __snake_case     :					Dict     ,     __snake_case     :					List[Any]     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Any     ,     __snake_case     :					Tuple     ,     __snake_case     :					List[Any]    )							-> List[Any]:
            _lowerCAmelCase					=				AlbertForPreTraining(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					List[str]     ,     __snake_case     :					Tuple     ,     __snake_case     :					Tuple     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Tuple     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					List[str]     ,     __snake_case     :					Union[str, Any]    )							-> Union[str, Any]:
            _lowerCAmelCase					=				AlbertForMaskedLM(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					List[str]     ,     __snake_case     :					Tuple     ,     __snake_case     :					Any     ,     __snake_case     :					Tuple     ,     __snake_case     :					Any     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					str     ,     __snake_case     :					Dict    )							-> Tuple:
            _lowerCAmelCase					=				AlbertForQuestionAnswering(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					int     ,     __snake_case     :					List[Any]     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					Any     ,     __snake_case     :					Tuple     ,     __snake_case     :					List[str]     ,     __snake_case     :					str    )							-> str:
            _lowerCAmelCase					=				self.num_labels
            _lowerCAmelCase					=				AlbertForSequenceClassification(lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				model(lowerCamelCase__     ,     attention_mask=lowerCamelCase__     ,     token_type_ids=lowerCamelCase__     ,     labels=lowerCamelCase__    )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_labels)    )




      def 							lowercase__       (     self     :					Optional[Any]     ,     __snake_case     :					Any     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					List[Any]    )							-> Any:
            _lowerCAmelCase					=				self.num_labels
            _lowerCAmelCase					=				AlbertForTokenClassification(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					Dict     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					Tuple     ,     __snake_case     :					int     ,     __snake_case     :					List[Any]     ,     __snake_case     :					int     ,     __snake_case     :					Optional[int]    )							-> Any:
            _lowerCAmelCase					=				self.num_choices
            _lowerCAmelCase					=				AlbertForMultipleChoice(config=lowerCamelCase__    )
            model.to(lowerCamelCase__    )
            model.eval()
            _lowerCAmelCase					=				input_ids.unsqueeze(1    ).expand(-1     ,     self.num_choices     ,     -1    ).contiguous()
            _lowerCAmelCase					=				token_type_ids.unsqueeze(1    ).expand(-1     ,     self.num_choices     ,     -1    ).contiguous()
            _lowerCAmelCase					=				input_mask.unsqueeze(1    ).expand(-1     ,     self.num_choices     ,     -1    ).contiguous()
            _lowerCAmelCase					=				model(
                lowerCamelCase__     ,     attention_mask=lowerCamelCase__     ,     token_type_ids=lowerCamelCase__     ,     labels=lowerCamelCase__     ,     )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_choices)    )




      def 							lowercase__       (     self     :					int    )							-> str:
            _lowerCAmelCase					=				self.prepare_config_and_inputs()
            (
                _lowerCAmelCase     
            )					=				config_and_inputs
            _lowerCAmelCase					=				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
            return config, inputs_dict




@require_torch
class 					UpperCAmelCase	(     snake_case_				,						snake_case_				,						unittest.TestCase  ):
      _lowercase:     Any			=  (
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:     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 {}
      )
      _lowercase:     List[Any]			=  True




      def 							lowercase__       (     self     :					Union[str, Any]     ,     __snake_case     :					str     ,     __snake_case     :					int     ,     __snake_case     :					Tuple=False    )							-> str:
            _lowerCAmelCase					=				super()._prepare_for_class(lowerCamelCase__     ,     lowerCamelCase__     ,     return_labels=lowerCamelCase__    )

            if return_labels:
                  if model_class in get_values(lowerCamelCase__    ):
                        _lowerCAmelCase					=				torch.zeros(
                            (self.model_tester.batch_size, self.model_tester.seq_length)     ,     dtype=torch.long     ,     device=lowerCamelCase__    )
                        _lowerCAmelCase					=				torch.zeros(
                            self.model_tester.batch_size     ,     dtype=torch.long     ,     device=lowerCamelCase__    )
            return inputs_dict




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> Optional[Any]:
            _lowerCAmelCase					=				AlbertModelTester(self    )
            _lowerCAmelCase					=				ConfigTester(self     ,     config_class=lowerCamelCase__     ,     hidden_size=37    )




      def 							lowercase__       (     self     :					Optional[int]    )							-> Any:
            self.config_tester.run_common_tests()




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> Union[str, Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					str    )							-> Optional[Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					List[str]    )							-> List[Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					Optional[Any]    )							-> Optional[Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					Tuple    )							-> Optional[int]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					Tuple    )							-> Any:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__    )




      def 							lowercase__       (     self     :					Any    )							-> Optional[int]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                  _lowerCAmelCase					=				type
                  self.model_tester.create_and_check_model(*lowerCamelCase__    )




      @slow
      def 							lowercase__       (     self     :					List[str]    )							-> Union[str, Any]:
            for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  _lowerCAmelCase					=				AlbertModel.from_pretrained(lowerCamelCase__    )
                  self.assertIsNotNone(lowerCamelCase__    )




@require_torch
class 					UpperCAmelCase	(     unittest.TestCase  ):




      @slow
      def 							lowercase__       (     self     :					Optional[int]    )							-> Optional[int]:
            _lowerCAmelCase					=				AlbertModel.from_pretrained("""albert-base-v2"""    )
            _lowerCAmelCase					=				torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]    )
            _lowerCAmelCase					=				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
            with torch.no_grad():
                  _lowerCAmelCase					=				model(lowerCamelCase__     ,     attention_mask=lowerCamelCase__    )[0]
            _lowerCAmelCase					=				torch.Size((1, 11, 7_68)    )
            self.assertEqual(output.shape     ,     lowerCamelCase__    )
            _lowerCAmelCase					=				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]     ,     lowerCamelCase__     ,     atol=1E-4    )    )











'''simple docstring'''
from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

snake_case_					:      Optional[Any]    			=    TypeVar('KEY')
snake_case_					:      Dict    			=    TypeVar('VAL')






@dataclass(frozen=lowercase		,   slots=lowercase  )
class 				lowercase__      (				Generic[KEY, VAL]  ):
	lowercase__    							=     42
	lowercase__    							=     42






class 				lowercase__      (				_Item  ):





	def __init__(					self   :							Union[str, Any]					):







							'''simple docstring'''
							super().__init__(lowerCamelCase__					,lowerCamelCase__					)





	def __bool__(					self   :							Tuple					):







							'''simple docstring'''
							return False
snake_case_					:      int    			=    _DeletedItem()






class 				lowercase__      (				MutableMapping[KEY, VAL]  ):





	def __init__(					self   :							Any					,lowerCamelCase__   :							int = 8					,lowerCamelCase__   :							float = 0.7_5					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  initial_block_size
							_UpperCamelCase   :						list[_Item | None]												=  [None] * initial_block_size
							assert 0.0 < capacity_factor < 1.0
							_UpperCamelCase   :						Optional[int]												=  capacity_factor
							_UpperCamelCase   :						List[Any]												=  0





	def       UpperCamelCase_    (					self   :							Optional[int]					,lowerCamelCase__   :							KEY					):







							'''simple docstring'''
							return hash(lowerCamelCase__					) % len(self._buckets					)





	def       UpperCamelCase_    (					self   :							List[Any]					,lowerCamelCase__   :							int					):







							'''simple docstring'''
							return (ind + 1) % len(self._buckets					)





	def       UpperCamelCase_    (					self   :							Union[str, Any]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							KEY					,lowerCamelCase__   :							VAL					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  self._buckets[ind]
							if not stored:
													_UpperCamelCase   :						str												=  _Item(lowerCamelCase__					,lowerCamelCase__					)
													self._len += 1
													return True
							elif stored.key == key:
													_UpperCamelCase   :						Union[str, Any]												=  _Item(lowerCamelCase__					,lowerCamelCase__					)
													return True
							else:
													return False





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  len(self._buckets					) * self._capacity_factor
							return len(self					) >= int(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							if len(self._buckets					) <= self._initial_block_size:
													return False
							_UpperCamelCase   :						str												=  len(self._buckets					) * self._capacity_factor / 2
							return len(self					) < limit





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  self._buckets
							_UpperCamelCase   :						Dict												=  [None] * new_size
							_UpperCamelCase   :						str												=  0
							for item in old_buckets:
													if item:
																			self._add_item(item.key					,item.val					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							self._resize(len(self._buckets					) * 2					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							self._resize(len(self._buckets					) // 2					)





	def       UpperCamelCase_    (					self   :							Union[str, Any]					,lowerCamelCase__   :							KEY					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self._get_bucket_index(lowerCamelCase__					)
							for _ in range(len(self._buckets					)					):
													yield ind
													_UpperCamelCase   :						Optional[int]												=  self._get_next_ind(lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							KEY					,lowerCamelCase__   :							VAL					):







							'''simple docstring'''
							for ind in self._iterate_buckets(lowerCamelCase__					):
													if self._try_set(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					):
																			break





	def __setitem__(					self   :							Dict					,lowerCamelCase__   :							KEY					,lowerCamelCase__   :							VAL					):







							'''simple docstring'''
							if self._is_full():
													self._size_up()

							self._add_item(lowerCamelCase__					,lowerCamelCase__					)





	def __delitem__(					self   :							Optional[Any]					,lowerCamelCase__   :							KEY					):







							'''simple docstring'''
							for ind in self._iterate_buckets(lowerCamelCase__					):
													_UpperCamelCase   :						Any												=  self._buckets[ind]
													if item is None:
																			raise KeyError(lowerCamelCase__					)
													if item is _deleted:
																			continue
													if item.key == key:
																			_UpperCamelCase   :						Optional[Any]												=  _deleted
																			self._len -= 1
																			break
							if self._is_sparse():
													self._size_down()





	def __getitem__(					self   :							Dict					,lowerCamelCase__   :							KEY					):







							'''simple docstring'''
							for ind in self._iterate_buckets(lowerCamelCase__					):
													_UpperCamelCase   :						Any												=  self._buckets[ind]
													if item is None:
																			break
													if item is _deleted:
																			continue
													if item.key == key:
																			return item.val
							raise KeyError(lowerCamelCase__					)





	def __len__(					self   :							Dict					):







							'''simple docstring'''
							return self._len





	def __iter__(					self   :							List[str]					):







							'''simple docstring'''
							yield from (item.key for item in self._buckets if item)





	def __repr__(					self   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  ' ,'.join(
							    F'{item.key}: {item.val}' for item in self._buckets if item					)
							return F'HashMap({val_string})'


