
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      _SCREAMING_SNAKE_CASE				:





				def __init__(		self			:		Dict       ,   snake_case_			:		Optional[int]       ,   snake_case_			:		List[Any]=13       ,   snake_case_			:		List[Any]=7       ,   snake_case_			:		List[Any]=True       ,   snake_case_			:		Any=True       ,   snake_case_			:		int=True       ,   snake_case_			:		Optional[Any]=True       ,   snake_case_			:		Tuple=99       ,   snake_case_			:		int=16       ,   snake_case_			:		Optional[Any]=36       ,   snake_case_			:		List[str]=6       ,   snake_case_			:		Optional[Any]=6       ,   snake_case_			:		str=6       ,   snake_case_			:		int=37       ,   snake_case_			:		Dict="gelu"       ,   snake_case_			:		Union[str, Any]=0.1       ,   snake_case_			:		List[str]=0.1       ,   snake_case_			:		Dict=512       ,   snake_case_			:		Optional[int]=16       ,   snake_case_			:		int=2       ,   snake_case_			:		Any=0.02       ,   snake_case_			:		Dict=3       ,   snake_case_			:		List[Any]=4       ,   snake_case_			:		Any=None       ,   ):


								"""simple docstring"""

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





				def 							_UpperCAmelCase    (		self			:		Optional[Any]				):


								"""simple docstring"""

								A  :	Any				=					ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size				)

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

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

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

								A  :	List[Any]				=					self.get_config()

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





				def 							_UpperCAmelCase    (		self			:		List[str]				):


								"""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			:		Tuple       ,   snake_case_			:		Optional[int]       ,   snake_case_			:		Dict       ,   snake_case_			:		Tuple       ,   snake_case_			:		List[str]       ,   snake_case_			:		Tuple       ,   snake_case_			:		Tuple       ,   snake_case_			:		List[Any]				):


								"""simple docstring"""

								A  :	Union[str, Any]				=					AlbertModel(config=lowercase_				)
								model.to(lowercase_				)
								model.eval()
								A  :	Dict				=					model(lowercase_       ,   attention_mask=lowercase_       ,   token_type_ids=lowercase_				)
								A  :	List[Any]				=					model(lowercase_       ,   token_type_ids=lowercase_				)
								A  :	Any				=					model(lowercase_				)
								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			:		int       ,   snake_case_			:		str       ,   snake_case_			:		int       ,   snake_case_			:		List[str]       ,   snake_case_			:		int       ,   snake_case_			:		Tuple       ,   snake_case_			:		Optional[int]       ,   snake_case_			:		Any				):


								"""simple docstring"""

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


								"""simple docstring"""

								A  :	Optional[int]				=					AlbertForMaskedLM(config=lowercase_				)
								model.to(lowercase_				)
								model.eval()
								A  :	Any				=					model(lowercase_       ,   attention_mask=lowercase_       ,   token_type_ids=lowercase_       ,   labels=lowercase_				)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)				)





				def 							_UpperCAmelCase    (		self			:		List[Any]       ,   snake_case_			:		Optional[Any]       ,   snake_case_			:		Dict       ,   snake_case_			:		Optional[Any]       ,   snake_case_			:		Optional[Any]       ,   snake_case_			:		Union[str, Any]       ,   snake_case_			:		int       ,   snake_case_			:		List[Any]				):


								"""simple docstring"""

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


								"""simple docstring"""

								A  :	List[Any]				=					self.num_labels
								A  :	Optional[Any]				=					AlbertForSequenceClassification(lowercase_				)
								model.to(lowercase_				)
								model.eval()
								A  :	Optional[Any]				=					model(lowercase_       ,   attention_mask=lowercase_       ,   token_type_ids=lowercase_       ,   labels=lowercase_				)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)				)





				def 							_UpperCAmelCase    (		self			:		List[Any]       ,   snake_case_			:		str       ,   snake_case_			:		int       ,   snake_case_			:		Optional[Any]       ,   snake_case_			:		List[str]       ,   snake_case_			:		Optional[int]       ,   snake_case_			:		Optional[Any]       ,   snake_case_			:		List[str]				):


								"""simple docstring"""

								A  :	Optional[Any]				=					self.num_labels
								A  :	Any				=					AlbertForTokenClassification(config=lowercase_				)
								model.to(lowercase_				)
								model.eval()
								A  :	Dict				=					model(lowercase_       ,   attention_mask=lowercase_       ,   token_type_ids=lowercase_       ,   labels=lowercase_				)
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.num_labels)				)





				def 							_UpperCAmelCase    (		self			:		Union[str, Any]       ,   snake_case_			:		List[Any]       ,   snake_case_			:		Dict       ,   snake_case_			:		Tuple       ,   snake_case_			:		Tuple       ,   snake_case_			:		List[Any]       ,   snake_case_			:		List[Any]       ,   snake_case_			:		Union[str, Any]				):


								"""simple docstring"""

								A  :	List[Any]				=					self.num_choices
								A  :	Tuple				=					AlbertForMultipleChoice(config=lowercase_				)
								model.to(lowercase_				)
								model.eval()
								A  :	List[Any]				=					input_ids.unsqueeze(1				).expand(-1       ,   self.num_choices       ,   -1				).contiguous()
								A  :	Dict				=					token_type_ids.unsqueeze(1				).expand(-1       ,   self.num_choices       ,   -1				).contiguous()
								A  :	Optional[Any]				=					input_mask.unsqueeze(1				).expand(-1       ,   self.num_choices       ,   -1				).contiguous()
								A  :	int				=					model(
								    lowercase_       ,   attention_mask=lowercase_       ,   token_type_ids=lowercase_       ,   labels=lowercase_       ,   )
								self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)				)






				def 							_UpperCAmelCase    (		self			:		Tuple				):


								"""simple docstring"""

								A  :	List[str]				=					self.prepare_config_and_inputs()
								(
								    (
								    A      
								)			,							(
								    A      
								)			,							(
								    A      
								)			,							(
								    A      
								)			,							(
								    A      
								)			,							(
								    A      
								)			,							(
								    A      
								)			,							      
								)  :	Tuple				=					config_and_inputs
								A  :	Tuple				=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



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





				def 							_UpperCAmelCase    (		self			:		int       ,   snake_case_			:		Optional[int]       ,   snake_case_			:		Any       ,   snake_case_			:		Optional[Any]=False				):


								"""simple docstring"""

								A  :	Optional[Any]				=					super()._prepare_for_class(lowercase_       ,   lowercase_       ,   return_labels=lowercase_				)

								if return_labels:
												if model_class in get_values(lowercase_				):
																A  :	Dict				=					torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.seq_length)       ,   dtype=torch.long       ,   device=lowercase_				)
																A  :	Optional[Any]				=					torch.zeros(
																    self.model_tester.batch_size       ,   dtype=torch.long       ,   device=lowercase_				)
								return inputs_dict





				def 							_UpperCAmelCase    (		self			:		Optional[int]				):


								"""simple docstring"""

								A  :	Tuple				=					AlbertModelTester(self				)
								A  :	Any				=					ConfigTester(self       ,   config_class=lowercase_       ,   hidden_size=37				)





				def 							_UpperCAmelCase    (		self			:		List[str]				):


								"""simple docstring"""

								self.config_tester.run_common_tests()





				def 							_UpperCAmelCase    (		self			:		Dict				):


								"""simple docstring"""

								A  :	Optional[Any]				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		Optional[Any]				):


								"""simple docstring"""

								A  :	str				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		str				):


								"""simple docstring"""

								A  :	Optional[Any]				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		List[str]				):


								"""simple docstring"""

								A  :	int				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		str				):


								"""simple docstring"""

								A  :	Dict				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		Optional[Any]				):


								"""simple docstring"""

								A  :	Optional[int]				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*lowercase_				)





				def 							_UpperCAmelCase    (		self			:		List[Any]				):


								"""simple docstring"""

								A  :	Optional[Any]				=					self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
												A  :	Dict				=					type
												self.model_tester.create_and_check_model(*lowercase_				)






				@slow
				def 							_UpperCAmelCase    (		self			:		Dict				):


								"""simple docstring"""

								for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A  :	str				=					AlbertModel.from_pretrained(lowercase_				)
												self.assertIsNotNone(lowercase_				)



@require_torch
class      _SCREAMING_SNAKE_CASE				(		unittest.TestCase				):





				@slow
				def 							_UpperCAmelCase    (		self			:		Optional[int]				):


								"""simple docstring"""

								A  :	int				=					AlbertModel.from_pretrained('''albert-base-v2'''				)
								A  :	int				=					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]				)
								A  :	Union[str, Any]				=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
								with torch.no_grad():
												A  :	List[Any]				=					model(lowercase_       ,   attention_mask=lowercase_				)[0]
								A  :	List[Any]				=					torch.Size((1, 11, 768)				)
								self.assertEqual(output.shape       ,   lowercase_				)
								A  :	Dict				=					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]       ,   lowercase_       ,   atol=1E-4				)				)






'''simple docstring'''

import string
from math import logaa





def 		UpperCamelCase							(				_lowerCamelCase   :   str		,			_lowerCamelCase   :   str			):
    A__							=     document.translate(
        str.maketrans(""		,			""		,			string.punctuation			)			).replace("\n"		,			""			)
    A__							=     document_without_punctuation.split(" "			)  # word tokenization
    return len([word for word in tokenize_document if word.lower() == term.lower()]			)





def 		UpperCamelCase							(				_lowerCamelCase   :   str		,			_lowerCamelCase   :   str			):
    A__							=     corpus.lower().translate(
        str.maketrans(""		,			""		,			string.punctuation			)			)  # strip all punctuation and replace it with ''
    A__							=     corpus_without_punctuation.split("\n"			)
    A__							=     term.lower()
    return (len([doc for doc in docs if term in doc]			), len(_lowerCamelCase			))





def 		UpperCamelCase							(				_lowerCamelCase   :   int		,			_lowerCamelCase   :   int		,			_lowerCamelCase   :   Tuple=False			):
    if smoothing:
        if n == 0:
            raise ValueError("log10(0) is undefined."			)
        return round(1 + logaa(n / (1 + df)			)		,			3			)

    if df == 0:
        raise ZeroDivisionError("df must be > 0"			)
    elif n == 0:
        raise ValueError("log10(0) is undefined."			)
    return round(logaa(n / df			)		,			3			)





def 		UpperCamelCase							(				_lowerCamelCase   :   int		,			_lowerCamelCase   :   int			):
    return round(tf * idf		,			3			)



