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



							def 			UpperCamelCase			(		self							):
													_snake_case		:	Optional[Any]							=       ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size							)

													_snake_case		:	Tuple							=       None
													if self.use_input_mask:
																			_snake_case		:	Dict							=       random_attention_mask([self.batch_size, self.seq_length]							)

													_snake_case		:	int							=       None
													if self.use_token_type_ids:
																			_snake_case		:	Union[str, Any]							=       ids_tensor([self.batch_size, self.seq_length] ,							self.type_vocab_size							)

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

													_snake_case		:	Union[str, Any]							=       self.get_config()

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



							def 			UpperCamelCase			(		self							):
													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 ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	List[Any]							=       AlbertModel(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	Dict							=       model(_lowerCAmelCase ,							attention_mask=_lowerCAmelCase ,							token_type_ids=_lowerCAmelCase							)
													_snake_case		:	Union[str, Any]							=       model(_lowerCAmelCase ,							token_type_ids=_lowerCAmelCase							)
													_snake_case		:	Dict							=       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 ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	Tuple							=       AlbertForPreTraining(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	Union[str, Any]							=       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 ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	int							=       AlbertForMaskedLM(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	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.vocab_size)							)



							def 			UpperCamelCase			(		self ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	Dict							=       AlbertForQuestionAnswering(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	Dict							=       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 ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	Tuple							=       self.num_labels
													_snake_case		:	Any							=       AlbertForSequenceClassification(_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	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 			UpperCamelCase			(		self ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	Optional[Any]							=       self.num_labels
													_snake_case		:	Tuple							=       AlbertForTokenClassification(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	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 			UpperCamelCase			(		self ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_ ,							lowercase_							):
													_snake_case		:	int							=       self.num_choices
													_snake_case		:	Optional[Any]							=       AlbertForMultipleChoice(config=_lowerCAmelCase							)
													model.to(_lowerCAmelCase							)
													model.eval()
													_snake_case		:	List[str]							=       input_ids.unsqueeze(1							).expand(-1 ,							self.num_choices ,							-1							).contiguous()
													_snake_case		:	Any							=       token_type_ids.unsqueeze(1							).expand(-1 ,							self.num_choices ,							-1							).contiguous()
													_snake_case		:	Dict							=       input_mask.unsqueeze(1							).expand(-1 ,							self.num_choices ,							-1							).contiguous()
													_snake_case		:	Tuple							=       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							):
													_snake_case		:	Optional[Any]							=       self.prepare_config_and_inputs()
													(
													    _snake_case
													)		:	int							=       config_and_inputs
													_snake_case		:	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						):
							_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 ,							lowercase_ ,							lowercase_ ,							lowercase_=False							):
													_snake_case		:	List[str]							=       super()._prepare_for_class(_lowerCAmelCase ,							_lowerCAmelCase ,							return_labels=_lowerCAmelCase							)

													if return_labels:
																			if model_class in get_values(_lowerCAmelCase							):
																									_snake_case		:	List[str]							=       torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.seq_length) ,							dtype=torch.long ,							device=_lowerCAmelCase							)
																									_snake_case		:	Dict							=       torch.zeros(
																									    self.model_tester.batch_size ,							dtype=torch.long ,							device=_lowerCAmelCase							)
													return inputs_dict



							def 			UpperCamelCase			(		self							):
													_snake_case		:	Tuple							=       AlbertModelTester(self							)
													_snake_case		:	List[str]							=       ConfigTester(self ,							config_class=_lowerCAmelCase ,							hidden_size=37							)



							def 			UpperCamelCase			(		self							):
													self.config_tester.run_common_tests()



							def 			UpperCamelCase			(		self							):
													_snake_case		:	str							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	Tuple							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	int							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	Any							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	str							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	Any							=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase							)



							def 			UpperCamelCase			(		self							):
													_snake_case		:	List[Any]							=       self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			_snake_case		:	str							=       type
																			self.model_tester.create_and_check_model(*_lowerCAmelCase							)



							@slow
							def 			UpperCamelCase			(		self							):




													for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			_snake_case		:	Union[str, Any]							=       AlbertModel.from_pretrained(_lowerCAmelCase							)
																			self.assertIsNotNone(_lowerCAmelCase							)




@require_torch
class   lowercase_       (	unittest.TestCase						):



							@slow
							def 			UpperCamelCase			(		self							):
													_snake_case		:	Optional[Any]							=       AlbertModel.from_pretrained("albert-base-v2"							)
													_snake_case		:	Any							=       torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]							)
													_snake_case		:	Dict							=       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
													with torch.no_grad():
																			_snake_case		:	Dict							=       model(_lowerCAmelCase ,							attention_mask=_lowerCAmelCase							)[0]
													_snake_case		:	Optional[int]							=       torch.Size((1, 11, 768)							)
													self.assertEqual(output.shape ,							_lowerCAmelCase							)
													_snake_case		:	Tuple							=       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							)							)






def 		lowerCAmelCase  (	UpperCamelCase__   :   list       ,			UpperCamelCase__   :   list       ,			UpperCamelCase__   :   int					)      ->    list:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				Tuple           =   len(UpperCamelCase__					)
						__SCREAMING_SNAKE_CASE:				Optional[int]           =   [[0] * n for i in range(UpperCamelCase__					)]
						for i in range(UpperCamelCase__					):
												__SCREAMING_SNAKE_CASE:				str           =   y_points[i]

						for i in range(2       ,			UpperCamelCase__					):
												for j in range(UpperCamelCase__       ,			UpperCamelCase__					):
																		__SCREAMING_SNAKE_CASE:				Optional[int]           =   (
																		    (xa - x_points[j - i + 1]) * q[j][i - 1]
																		    - (xa - x_points[j]) * q[j - 1][i - 1]
																		) / (x_points[j] - x_points[j - i + 1])

						return [q[n - 1][n - 1], q]


if __name__ == "__main__":
							import doctest

							doctest.testmod()



