



'''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					:				int ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Union[str, Any]=13 ,lowerCamelCase__					:				str=7 ,lowerCamelCase__					:				str=True ,lowerCamelCase__					:				Optional[Any]=True ,lowerCamelCase__					:				str=True ,lowerCamelCase__					:				Union[str, Any]=True ,lowerCamelCase__					:				Any=99 ,lowerCamelCase__					:				Optional[Any]=16 ,lowerCamelCase__					:				Optional[Any]=36 ,lowerCamelCase__					:				Tuple=6 ,lowerCamelCase__					:				List[str]=6 ,lowerCamelCase__					:				Optional[Any]=6 ,lowerCamelCase__					:				Union[str, Any]=37 ,lowerCamelCase__					:				Union[str, Any]="gelu" ,lowerCamelCase__					:				List[str]=0.1 ,lowerCamelCase__					:				Tuple=0.1 ,lowerCamelCase__					:				Tuple=512 ,lowerCamelCase__					:				Dict=16 ,lowerCamelCase__					:				Dict=2 ,lowerCamelCase__					:				Dict=0.0_2 ,lowerCamelCase__					:				str=3 ,lowerCamelCase__					:				Tuple=4 ,lowerCamelCase__					:				List[Any]=None ,):







							'''simple docstring'''
							_UpperCamelCase					:		Dict 				=   parent
							_UpperCamelCase					:		Dict 				=   batch_size
							_UpperCamelCase					:		Optional[Any] 				=   seq_length
							_UpperCamelCase					:		Tuple 				=   is_training
							_UpperCamelCase					:		List[str] 				=   use_input_mask
							_UpperCamelCase					:		List[str] 				=   use_token_type_ids
							_UpperCamelCase					:		Optional[int] 				=   use_labels
							_UpperCamelCase					:		int 				=   vocab_size
							_UpperCamelCase					:		int 				=   embedding_size
							_UpperCamelCase					:		List[str] 				=   hidden_size
							_UpperCamelCase					:		Tuple 				=   num_hidden_layers
							_UpperCamelCase					:		Any 				=   num_hidden_groups
							_UpperCamelCase					:		List[Any] 				=   num_attention_heads
							_UpperCamelCase					:		Dict 				=   intermediate_size
							_UpperCamelCase					:		Any 				=   hidden_act
							_UpperCamelCase					:		List[Any] 				=   hidden_dropout_prob
							_UpperCamelCase					:		Tuple 				=   attention_probs_dropout_prob
							_UpperCamelCase					:		int 				=   max_position_embeddings
							_UpperCamelCase					:		int 				=   type_vocab_size
							_UpperCamelCase					:		str 				=   type_sequence_label_size
							_UpperCamelCase					:		str 				=   initializer_range
							_UpperCamelCase					:		Any 				=   num_labels
							_UpperCamelCase					:		str 				=   num_choices
							_UpperCamelCase					:		str 				=   scope





	def 	UpperCamelCase_  ( self					:				List[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Optional[int] 				=   ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size )

							_UpperCamelCase					:		Optional[int] 				=   None
							if self.use_input_mask:
													_UpperCamelCase					:		Dict 				=   random_attention_mask([self.batch_size, self.seq_length] )

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

							_UpperCamelCase					:		Any 				=   None
							_UpperCamelCase					:		Optional[Any] 				=   None
							_UpperCamelCase					:		Optional[int] 				=   None
							if self.use_labels:
													_UpperCamelCase					:		str 				=   ids_tensor([self.batch_size] ,self.type_sequence_label_size )
													_UpperCamelCase					:		Any 				=   ids_tensor([self.batch_size, self.seq_length] ,self.num_labels )
													_UpperCamelCase					:		List[str] 				=   ids_tensor([self.batch_size] ,self.num_choices )

							_UpperCamelCase					:		Tuple 				=   self.get_config()

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





	def 	UpperCamelCase_  ( self					:				Tuple ):







							'''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 	UpperCamelCase_  ( self					:				Dict ,lowerCamelCase__					:				Optional[Any] ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				int ,lowerCamelCase__					:				int ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				Optional[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Dict 				=   AlbertModel(config=lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		str 				=   model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,token_type_ids=lowerCamelCase__ )
							_UpperCamelCase					:		Optional[Any] 				=   model(lowerCamelCase__ ,token_type_ids=lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   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 	UpperCamelCase_  ( self					:				Tuple ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				Any ,lowerCamelCase__					:				Dict ,lowerCamelCase__					:				List[Any] ,lowerCamelCase__					:				str ,lowerCamelCase__					:				int ,lowerCamelCase__					:				List[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		List[str] 				=   AlbertForPreTraining(config=lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		Tuple 				=   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 	UpperCamelCase_  ( self					:				List[str] ,lowerCamelCase__					:				List[Any] ,lowerCamelCase__					:				List[str] ,lowerCamelCase__					:				Dict ,lowerCamelCase__					:				Dict ,lowerCamelCase__					:				str ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Optional[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		int 				=   AlbertForMaskedLM(config=lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		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 	UpperCamelCase_  ( self					:				List[str] ,lowerCamelCase__					:				List[str] ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				int ,lowerCamelCase__					:				List[Any] ,lowerCamelCase__					:				Tuple ,lowerCamelCase__					:				List[Any] ,lowerCamelCase__					:				str ):







							'''simple docstring'''
							_UpperCamelCase					:		str 				=   AlbertForQuestionAnswering(config=lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		List[Any] 				=   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 	UpperCamelCase_  ( self					:				str ,lowerCamelCase__					:				Optional[int] ,lowerCamelCase__					:				Optional[Any] ,lowerCamelCase__					:				Optional[Any] ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Optional[int] ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Dict ):







							'''simple docstring'''
							_UpperCamelCase					:		List[str] 				=   self.num_labels
							_UpperCamelCase					:		Optional[Any] 				=   AlbertForSequenceClassification(lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		Dict 				=   model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,token_type_ids=lowerCamelCase__ ,labels=lowerCamelCase__ )
							self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) )





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







							'''simple docstring'''
							_UpperCamelCase					:		List[Any] 				=   self.num_labels
							_UpperCamelCase					:		Optional[int] 				=   AlbertForTokenClassification(config=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.seq_length, self.num_labels) )





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







							'''simple docstring'''
							_UpperCamelCase					:		str 				=   self.num_choices
							_UpperCamelCase					:		List[str] 				=   AlbertForMultipleChoice(config=lowerCamelCase__ )
							model.to(lowerCamelCase__ )
							model.eval()
							_UpperCamelCase					:		List[str] 				=   input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous()
							_UpperCamelCase					:		Optional[int] 				=   token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous()
							_UpperCamelCase					:		Dict 				=   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 	UpperCamelCase_  ( self					:				Optional[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Dict 				=   self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			(
							    _UpperCamelCase
							)				,			
							)					:		Dict 				=   config_and_inputs
							_UpperCamelCase					:		Optional[int] 				=   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict






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





	def 	UpperCamelCase_  ( self					:				str ,lowerCamelCase__					:				Optional[int] ,lowerCamelCase__					:				str ,lowerCamelCase__					:				Any=False ):







							'''simple docstring'''
							_UpperCamelCase					:		Optional[int] 				=   super()._prepare_for_class(lowerCamelCase__ ,lowerCamelCase__ ,return_labels=lowerCamelCase__ )

							if return_labels:
													if model_class in get_values(lowerCamelCase__ ):
																			_UpperCamelCase					:		int 				=   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 	UpperCamelCase_  ( self					:				Dict ):







							'''simple docstring'''
							_UpperCamelCase					:		Tuple 				=   AlbertModelTester(self )
							_UpperCamelCase					:		Any 				=   ConfigTester(self ,config_class=lowerCamelCase__ ,hidden_size=37 )





	def 	UpperCamelCase_  ( self					:				Optional[int] ):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	def 	UpperCamelCase_  ( self					:				Tuple ):







							'''simple docstring'''
							_UpperCamelCase					:		List[Any] 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				int ):







							'''simple docstring'''
							_UpperCamelCase					:		List[Any] 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Dict ):







							'''simple docstring'''
							_UpperCamelCase					:		int 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Any ):







							'''simple docstring'''
							_UpperCamelCase					:		List[str] 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Optional[int] ):







							'''simple docstring'''
							_UpperCamelCase					:		Union[str, Any] 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				str ):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Tuple ):







							'''simple docstring'''
							_UpperCamelCase					:		int 				=   self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													_UpperCamelCase					:		List[str] 				=   type
													self.model_tester.create_and_check_model(*lowerCamelCase__ )





	@slow
	def 	UpperCamelCase_  ( self					:				Dict ):







							'''simple docstring'''
							for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase					:		Union[str, Any] 				=   AlbertModel.from_pretrained(lowerCamelCase__ )
													self.assertIsNotNone(lowerCamelCase__ )






@require_torch
class 							lowercase__	(							unittest.TestCase   ):





	@slow
	def 	UpperCamelCase_  ( self					:				int ):







							'''simple docstring'''
							_UpperCamelCase					:		Tuple 				=   AlbertModel.from_pretrained('albert-base-v2' )
							_UpperCamelCase					:		Tuple 				=   torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] )
							_UpperCamelCase					:		List[str] 				=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
							with torch.no_grad():
													_UpperCamelCase					:		Optional[int] 				=   model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ )[0]
							_UpperCamelCase					:		Tuple 				=   torch.Size((1, 11, 768) )
							self.assertEqual(output.shape ,lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   torch.tensor(
							    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] )

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










"""simple docstring"""



from __future__ import annotations

from collections.abc import Iterator
from typing import Generic, TypeVar

_UpperCamelCase		:  Any						=		TypeVar('T')

class        a     (   Generic[T]	):







    def __init__(			self							,		_lowerCamelCase       ):
      lowercase							    =       data
      lowercase							    =       None







    def __str__(			self       ):
      return F'{self.data}'


class        a     (   Generic[T]	):







    def __init__(			self       ):
      lowercase							    =       None







    def __iter__(			self       ):
      lowercase							    =       self.top
      while node:
        yield node.data
        lowercase							    =       node.next







    def __str__(			self       ):
      return "->".join([str(_lowerCamelCase       ) for item in self]       )







    def __len__(			self       ):
      return len(tuple(iter(self       )       )       )







    def 			UpperCamelCase_      (			self       ):
      return self.top is None







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase       ):
      lowercase							    =       Node(_lowerCamelCase       )
      if not self.is_empty():
        lowercase							    =       self.top
      lowercase							    =       node







    def 			UpperCamelCase_      (			self       ):
      if self.is_empty():
        raise IndexError('pop from empty stack'       )
      assert isinstance(self.top							,		_lowerCamelCase       )
      lowercase							    =       self.top
      lowercase							    =       self.top.next
      return pop_node.data







    def 			UpperCamelCase_      (			self       ):
      if self.is_empty():
        raise IndexError('peek from empty stack'       )

      assert self.top is not None
      return self.top.data







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       None


if __name__ == "__main__":
    from doctest import testmod

    testmod()


