






import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    DebertaVaForMaskedLM,
				    DebertaVaForMultipleChoice,
				    DebertaVaForQuestionAnswering,
				    DebertaVaForSequenceClassification,
				    DebertaVaForTokenClassification,
				    DebertaVaModel,
				)
				from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST


class 							__a							(  __A						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=13 ,    UpperCamelCase__=7 ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=99 ,    UpperCamelCase__=32 ,    UpperCamelCase__=5 ,    UpperCamelCase__=4 ,    UpperCamelCase__=37 ,    UpperCamelCase__="gelu" ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=512 ,    UpperCamelCase__=16 ,    UpperCamelCase__=2 ,    UpperCamelCase__=0.02 ,    UpperCamelCase__=False ,    UpperCamelCase__=True ,    UpperCamelCase__="None" ,    UpperCamelCase__=3 ,    UpperCamelCase__=4 ,    UpperCamelCase__=None ,    ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   parent
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   batch_size
						SCREAMING_SNAKE_CASE_   :	Any													=   seq_length
						SCREAMING_SNAKE_CASE_   :	str													=   is_training
						SCREAMING_SNAKE_CASE_   :	str													=   use_input_mask
						SCREAMING_SNAKE_CASE_   :	Tuple													=   use_token_type_ids
						SCREAMING_SNAKE_CASE_   :	str													=   use_labels
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   vocab_size
						SCREAMING_SNAKE_CASE_   :	Dict													=   hidden_size
						SCREAMING_SNAKE_CASE_   :	str													=   num_hidden_layers
						SCREAMING_SNAKE_CASE_   :	List[str]													=   num_attention_heads
						SCREAMING_SNAKE_CASE_   :	Tuple													=   intermediate_size
						SCREAMING_SNAKE_CASE_   :	Any													=   hidden_act
						SCREAMING_SNAKE_CASE_   :	Tuple													=   hidden_dropout_prob
						SCREAMING_SNAKE_CASE_   :	str													=   attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   max_position_embeddings
						SCREAMING_SNAKE_CASE_   :	Tuple													=   type_vocab_size
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   type_sequence_label_size
						SCREAMING_SNAKE_CASE_   :	Any													=   initializer_range
						SCREAMING_SNAKE_CASE_   :	List[str]													=   num_labels
						SCREAMING_SNAKE_CASE_   :	Any													=   num_choices
						SCREAMING_SNAKE_CASE_   :	Any													=   relative_attention
						SCREAMING_SNAKE_CASE_   :	Tuple													=   position_biased_input
						SCREAMING_SNAKE_CASE_   :	Tuple													=   pos_att_type
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   scope


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size       )

						SCREAMING_SNAKE_CASE_   :	List[str]													=   None
						if self.use_input_mask:
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   ids_tensor([self.batch_size, self.seq_length] ,    vocab_size=2       )

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

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

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

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


		def   __snake_case			(						self       ):
						return DebertaVaConfig(
						    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 ,    relative_attention=self.relative_attention ,    position_biased_input=self.position_biased_input ,    pos_att_type=self.pos_att_type ,    )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						self.parent.assertListEqual(list(result.loss.size()       ) ,    []       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	str													=   DebertaVaModel(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Tuple													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__       )[0]
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   model(UpperCamelCase__ ,    token_type_ids=UpperCamelCase__       )[0]
						SCREAMING_SNAKE_CASE_   :	Dict													=   model(UpperCamelCase__       )[0]

						self.parent.assertListEqual(list(sequence_output.size()       ) ,    [self.batch_size, self.seq_length, self.hidden_size]       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	List[str]													=   DebertaVaForMaskedLM(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	List[str]													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    labels=UpperCamelCase__       )

						self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.num_labels
						SCREAMING_SNAKE_CASE_   :	Any													=   DebertaVaForSequenceClassification(UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Any													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    labels=UpperCamelCase__       )
						self.parent.assertListEqual(list(result.logits.size()       ) ,    [self.batch_size, self.num_labels]       )
						self.check_loss_output(UpperCamelCase__       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.num_labels
						SCREAMING_SNAKE_CASE_   :	int													=   DebertaVaForTokenClassification(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    labels=UpperCamelCase__       )
						self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.num_labels)       )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   DebertaVaForQuestionAnswering(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   model(
						    UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    start_positions=UpperCamelCase__ ,    end_positions=UpperCamelCase__ ,    )
						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   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   DebertaVaForMultipleChoice(config=UpperCamelCase__       )
						model.to(UpperCamelCase__       )
						model.eval()
						SCREAMING_SNAKE_CASE_   :	Tuple													=   input_ids.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
						SCREAMING_SNAKE_CASE_   :	List[str]													=   token_type_ids.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   input_mask.unsqueeze(1       ).expand(-1 ,    self.num_choices ,    -1       ).contiguous()
						SCREAMING_SNAKE_CASE_   :	Tuple													=   model(
						    UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    token_type_ids=UpperCamelCase__ ,    labels=UpperCamelCase__ ,    )
						self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_choices)       )






		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       (
						    SCREAMING_SNAKE_CASE_
						)							,       
						)   :	Union[str, Any]													=   config_and_inputs
						SCREAMING_SNAKE_CASE_   :	List[str]													=   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict






@require_torch
class 							__a							(  __A ,	__A ,	unittest.TestCase						):





		'''simple docstring'''



		UpperCAmelCase__      :				Union[str, Any]				    =						(
		    (
		        DebertaVaModel,
		        DebertaVaForMaskedLM,
		        DebertaVaForSequenceClassification,
		        DebertaVaForTokenClassification,
		        DebertaVaForQuestionAnswering,
		        DebertaVaForMultipleChoice,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCAmelCase__      :				Tuple				    =						(
		    {
		        """feature-extraction""": DebertaVaModel,
		        """fill-mask""": DebertaVaForMaskedLM,
		        """question-answering""": DebertaVaForQuestionAnswering,
		        """text-classification""": DebertaVaForSequenceClassification,
		        """token-classification""": DebertaVaForTokenClassification,
		        """zero-shot""": DebertaVaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		UpperCAmelCase__      :				Optional[int]				    =						True
		UpperCAmelCase__      :				int				    =						False
		UpperCAmelCase__      :				Union[str, Any]				    =						False
		UpperCAmelCase__      :				List[str]				    =						False
		UpperCAmelCase__      :				Optional[int]				    =						False


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Any													=   DebertaVaModelTester(self       )
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   ConfigTester(self ,    config_class=UpperCamelCase__ ,    hidden_size=37       )


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


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	int													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase__       )


		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	Dict													=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCamelCase__       )






		@slow
		def   __snake_case			(						self       ):
						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   DebertaVaModel.from_pretrained(UpperCamelCase__       )
										self.assertIsNotNone(UpperCamelCase__       )






@require_torch
@require_sentencepiece
@require_tokenizers
class 							__a							(  unittest.TestCase						):





		'''simple docstring'''



		@unittest.skip(reason='Model not available yet'       )
		def   __snake_case			(						self       ):
						pass






		@slow
		def   __snake_case			(						self       ):
						SCREAMING_SNAKE_CASE_   :	List[str]													=   DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'       )

						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]       )
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
						with torch.no_grad():
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__       )[0]
						# compare the actual values for a slice.
						SCREAMING_SNAKE_CASE_   :	Dict													=   torch.tensor(
						    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]       )
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,    UpperCamelCase__ ,    atol=1E-4       ) ,    F'''{output[:, 1:4, 1:4]}'''       )







import argparse
import os

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_task_guides.py
A        =			'src/transformers'
A        =			'docs/source/en/tasks'



def 					_lowerCamelCase(			lowerCAmelCase__				:		Tuple    ,    lowerCAmelCase__				:		Optional[Any]    ,    lowerCAmelCase__				:		Union[str, Any]      ):





				'''simple docstring'''







				with open(lowerCAmelCase__    ,    'r'    ,    encoding='utf-8'    ,    newline='\n'      ) as f:
								SCREAMING_SNAKE_CASE_   :	Optional[int]													=   f.readlines()
				# Find the start prompt.
				SCREAMING_SNAKE_CASE_   :	Optional[int]													=   0
				while not lines[start_index].startswith(lowerCAmelCase__      ):
								start_index += 1
				start_index += 1

				SCREAMING_SNAKE_CASE_   :	int													=   start_index
				while not lines[end_index].startswith(lowerCAmelCase__      ):
								end_index += 1
				end_index -= 1

				while len(lines[start_index]      ) <= 1:
								start_index += 1
				while len(lines[end_index]      ) <= 1:
								end_index -= 1
				end_index += 1
				return "".join(lines[start_index:end_index]      ), start_index, end_index, lines


# This is to make sure the transformers module imported is the one in the repo.
A        =			direct_transformers_import(TRANSFORMERS_PATH)

A        =			{
    'asr.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES,
    'audio_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
    'language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
    'image_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
    'masked_language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES,
    'multiple_choice.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES,
    'object_detection.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,
    'question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,
    'semantic_segmentation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,
    'sequence_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,
    'summarization.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'token_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,
    'translation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'video_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES,
    'document_question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,
    'monocular_depth_estimation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES,
}

# This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any
# `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`).
A        =			{
    'summarization.md': ('nllb',),
    'translation.md': ('nllb',),
}



def 					_lowerCamelCase(			lowerCAmelCase__				:		Dict      ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_   :	List[str]													=   TASK_GUIDE_TO_MODELS[task_guide]
				SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(lowerCAmelCase__    ,    set()      )
				SCREAMING_SNAKE_CASE_   :	Optional[int]													=   {
				    code: name
				    for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
				    if (code in model_maping_names or code in special_model_types)
				}
				return ", ".join([F'''[{name}](../model_doc/{code})''' for code, name in model_names.items()]      ) + "\n"



def 					_lowerCamelCase(			lowerCAmelCase__				:		List[Any]    ,    lowerCAmelCase__				:		List[str]=False      ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_   :	Dict													=   _find_text_in_file(
				    filename=os.path.join(lowerCAmelCase__    ,    lowerCAmelCase__      )    ,    start_prompt='<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->'    ,    end_prompt='<!--End of the generated tip-->'    ,    )

				SCREAMING_SNAKE_CASE_   :	Dict													=   get_model_list_for_task(lowerCAmelCase__      )

				if current_list != new_list:
								if overwrite:
												with open(os.path.join(lowerCAmelCase__    ,    lowerCAmelCase__      )    ,    'w'    ,    encoding='utf-8'    ,    newline='\n'      ) as f:
																f.writelines(lines[:start_index] + [new_list] + lines[end_index:]      )
								else:
												raise ValueError(
												    F'''The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`'''
												    ' to fix this.'      )


if __name__ == "__main__":
				A        =			argparse.ArgumentParser()
				parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
				A        =			parser.parse_args()

				for task_guide in TASK_GUIDE_TO_MODELS.keys():
								check_model_list_for_task(task_guide, args.fix_and_overwrite)