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						:


				'''simple docstring'''






				def __init__(						self		,     lowercase		,     lowercase=1_3		,     lowercase=7		,     lowercase=True		,     lowercase=True		,     lowercase=True		,     lowercase=True		,     lowercase=9_9		,     lowercase=1_6		,     lowercase=3_6		,     lowercase=6		,     lowercase=6		,     lowercase=6		,     lowercase=3_7		,     lowercase="gelu"		,     lowercase=0.1		,     lowercase=0.1		,     lowercase=5_1_2		,     lowercase=1_6		,     lowercase=2		,     lowercase=0.02		,     lowercase=3		,     lowercase=4		,     lowercase=None		,     ):



						"""simple docstring"""





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



						"""simple docstring"""





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

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

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

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

						A_		:    Tuple         =				self.get_config()

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



						"""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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    Optional[int]         =				AlbertModel(config=lowerCAmelCase__ )
						model.to(lowerCAmelCase__ )
						model.eval()
						A_		:    int         =				model(lowerCAmelCase__		,     attention_mask=lowerCAmelCase__		,     token_type_ids=lowerCAmelCase__ )
						A_		:    Dict         =				model(lowerCAmelCase__		,     token_type_ids=lowerCAmelCase__ )
						A_		:    Optional[Any]         =				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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[Any]         =				AlbertForPreTraining(config=lowerCAmelCase__ )
						model.to(lowerCAmelCase__ )
						model.eval()
						A_		:    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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    str         =				AlbertForMaskedLM(config=lowerCAmelCase__ )
						model.to(lowerCAmelCase__ )
						model.eval()
						A_		:    Optional[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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[Any]         =				AlbertForQuestionAnswering(config=lowerCAmelCase__ )
						model.to(lowerCAmelCase__ )
						model.eval()
						A_		:    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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





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



						"""simple docstring"""





						A_		:    str         =				self.num_labels
						A_		:    Dict         =				AlbertForTokenClassification(config=lowerCAmelCase__ )
						model.to(lowerCAmelCase__ )
						model.eval()
						A_		:    Dict         =				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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





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







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				self.prepare_config_and_inputs()
						(
						    (
						    A_    
						)       ,      (
						    A_    
						)       ,      (
						    A_    
						)       ,      (
						    A_    
						)       ,      (
						    A_    
						)       ,      (
						    A_    
						)       ,      (
						    A_    
						)       ,          
						)		:    List[str]         =				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 UpperCAmelCase						( lowerCamelCase_ ,	lowerCamelCase_ ,	unittest.TestCase  ):


				'''simple docstring'''






				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   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase=False ):



						"""simple docstring"""





						A_		:    Any         =				super()._prepare_for_class(lowerCAmelCase__		,     lowerCAmelCase__		,     return_labels=lowerCAmelCase__ )

						if return_labels:
								if model_class in get_values(lowerCAmelCase__ ):
										A_		:    int         =				torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)		,     dtype=torch.long		,     device=lowerCAmelCase__ )
										A_		:    Optional[Any]         =				torch.zeros(
										    self.model_tester.batch_size		,     dtype=torch.long		,     device=lowerCAmelCase__ )
						return inputs_dict
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				AlbertModelTester(self )
						A_		:    str         =				ConfigTester(self		,     config_class=lowerCAmelCase__		,     hidden_size=3_7 )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





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



						"""simple docstring"""





						A_		:    Any         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Any         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__ )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





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







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





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





@require_torch
class UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[str]         =				AlbertModel.from_pretrained('albert-base-v2' )
						A_		:    Union[str, Any]         =				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]] )
						A_		:    Optional[int]         =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
						with torch.no_grad():
								A_		:    Union[str, Any]         =				model(lowerCAmelCase__		,     attention_mask=lowerCAmelCase__ )[0]
						A_		:    str         =				torch.Size((1, 1_1, 7_6_8) )
						self.assertEqual(output.shape		,     lowerCAmelCase__ )
						A_		:    int         =				torch.tensor(
						    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] )

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







import warnings

from ...utils import logging
from .image_processing_donut import DonutImageProcessor


_UpperCAmelCase            =		logging.get_logger(__name__)





class UpperCAmelCase						( __A  ):


				'''simple docstring'''






				def __init__(						self		,     *lowercase		,     **lowercase ):



						"""simple docstring"""





						warnings.warn(
						    'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
						    ' use DonutImageProcessor instead.'		,     lowercase		,     )
						super().__init__(*lowercase		,     **lowercase )






