



import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
							    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    OpenAIGPTConfig,
							    OpenAIGPTDoubleHeadsModel,
							    OpenAIGPTForSequenceClassification,
							    OpenAIGPTLMHeadModel,
							    OpenAIGPTModel,
							)







class       __lowerCAmelCase   :



			def __init__(						self       :  Union[str, Any]			,		A       :  Union[str, Any]			,		A       :  int=13			,		A       :  str=7			,		A       :  Any=True			,		A       :  List[str]=True			,		A       :  Union[str, Any]=True			,		A       :  Tuple=99			,		A       :  Optional[int]=32			,		A       :  Optional[Any]=5			,		A       :  Union[str, Any]=4			,		A       :  Optional[int]=37			,		A       :  Dict="gelu"			,		A       :  Optional[int]=0.1			,		A       :  Tuple=0.1			,		A       :  Tuple=5_12			,		A       :  int=16			,		A       :  Optional[int]=2			,		A       :  Dict=0.0_2			,		A       :  Union[str, Any]=3			,		A       :  Dict=4			,		A       :  Tuple=None			,		)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       parent
						_UpperCAmelCase			=       batch_size
						_UpperCAmelCase			=       seq_length
						_UpperCAmelCase			=       is_training
						_UpperCAmelCase			=       use_token_type_ids
						_UpperCAmelCase			=       use_labels
						_UpperCAmelCase			=       vocab_size
						_UpperCAmelCase			=       hidden_size
						_UpperCAmelCase			=       num_hidden_layers
						_UpperCAmelCase			=       num_attention_heads
						_UpperCAmelCase			=       intermediate_size
						_UpperCAmelCase			=       hidden_act
						_UpperCAmelCase			=       hidden_dropout_prob
						_UpperCAmelCase			=       attention_probs_dropout_prob
						_UpperCAmelCase			=       max_position_embeddings
						_UpperCAmelCase			=       type_vocab_size
						_UpperCAmelCase			=       type_sequence_label_size
						_UpperCAmelCase			=       initializer_range
						_UpperCAmelCase			=       num_labels
						_UpperCAmelCase			=       num_choices
						_UpperCAmelCase			=       scope
						_UpperCAmelCase			=       self.vocab_size - 1



			def    _lowerCamelCase	(						self       :  str)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size)

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

						_UpperCAmelCase			=       None
						_UpperCAmelCase			=       None
						_UpperCAmelCase			=       None
						if self.use_labels:
									_UpperCAmelCase			=       ids_tensor([self.batch_size]			,		self.type_sequence_label_size)
									_UpperCAmelCase			=       ids_tensor([self.batch_size, self.seq_length]			,		self.num_labels)
									_UpperCAmelCase			=       ids_tensor([self.batch_size]			,		self.num_choices)

						_UpperCAmelCase			=       OpenAIGPTConfig(
						    vocab_size=self.vocab_size			,		n_embd=self.hidden_size			,		n_layer=self.num_hidden_layers			,		n_head=self.num_attention_heads			,		n_positions=self.max_position_embeddings			,		pad_token_id=self.pad_token_id			,		)

						_UpperCAmelCase			=       ids_tensor([self.num_hidden_layers, self.num_attention_heads]			,		2)

						return (
						    config,
						    input_ids,
						    head_mask,
						    token_type_ids,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						)



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  Optional[Any]			,		A       :  Tuple			,		A       :  Tuple			,		A       :  int			,		*A       :  int)       ->   int:



						"""simple docstring"""






						_UpperCAmelCase			=       OpenAIGPTModel(config=__lowerCAmelCase)
						model.to(__lowerCAmelCase)
						model.eval()

						_UpperCAmelCase			=       model(__lowerCAmelCase			,		token_type_ids=__lowerCAmelCase			,		head_mask=__lowerCAmelCase)
						_UpperCAmelCase			=       model(__lowerCAmelCase			,		token_type_ids=__lowerCAmelCase)
						_UpperCAmelCase			=       model(__lowerCAmelCase)

						self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size))



			def    _lowerCamelCase	(						self       :  int			,		A       :  Tuple			,		A       :  List[Any]			,		A       :  Optional[Any]			,		A       :  Optional[Any]			,		*A       :  Any)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       OpenAIGPTLMHeadModel(__lowerCAmelCase)
						model.to(__lowerCAmelCase)
						model.eval()

						_UpperCAmelCase			=       model(__lowerCAmelCase			,		token_type_ids=__lowerCAmelCase			,		labels=__lowerCAmelCase)
						self.parent.assertEqual(result.loss.shape			,		())
						self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size))



			def    _lowerCamelCase	(						self       :  Optional[Any]			,		A       :  List[Any]			,		A       :  int			,		A       :  Optional[Any]			,		A       :  List[str]			,		*A       :  Union[str, Any])       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       OpenAIGPTDoubleHeadsModel(__lowerCAmelCase)
						model.to(__lowerCAmelCase)
						model.eval()

						_UpperCAmelCase			=       model(__lowerCAmelCase			,		token_type_ids=__lowerCAmelCase			,		labels=__lowerCAmelCase)
						self.parent.assertEqual(result.loss.shape			,		())
						self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size))



			def    _lowerCamelCase	(						self       :  Optional[Any]			,		A       :  int			,		A       :  int			,		A       :  str			,		A       :  str			,		*A       :  Optional[Any])       ->   List[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.num_labels
						_UpperCAmelCase			=       OpenAIGPTForSequenceClassification(__lowerCAmelCase)
						model.to(__lowerCAmelCase)
						model.eval()

						_UpperCAmelCase			=       ids_tensor([self.batch_size]			,		self.type_sequence_label_size)
						_UpperCAmelCase			=       model(__lowerCAmelCase			,		token_type_ids=__lowerCAmelCase			,		labels=__lowerCAmelCase)
						self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels))



			def    _lowerCamelCase	(						self       :  List[Any])       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       self.prepare_config_and_inputs()
						(
						    (
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,					(
						    _UpperCAmelCase							
						)	,												
						)			=       config_and_inputs
						_UpperCAmelCase			=       {
						    'input_ids': input_ids,
						    'token_type_ids': token_type_ids,
						    'head_mask': head_mask,
						}

						return config, inputs_dict







@require_torch
class       __lowerCAmelCase   (      A    ,						A    ,						A    ,						unittest.TestCase						):
			UpperCamelCase        =					(
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase        =					(
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			UpperCamelCase        =					(
			    {
			        'feature-extraction': OpenAIGPTModel,
			        'text-classification': OpenAIGPTForSequenceClassification,
			        'text-generation': OpenAIGPTLMHeadModel,
			        'zero-shot': OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  List[str]			,		A       :  Union[str, Any]			,		A       :  Tuple			,		A       :  Tuple			,		A       :  int)       ->   str:



						"""simple docstring"""






						if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
									# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
									# `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
									# tiny config could not be created.
									return True

						return False



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  Optional[Any]			,		A       :  Optional[int]			,		A       :  Optional[Any]=False)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       super()._prepare_for_class(__lowerCAmelCase			,		__lowerCAmelCase			,		return_labels=__lowerCAmelCase)

						if return_labels:
									if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
												_UpperCAmelCase			=       torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)			,		dtype=torch.long			,		device=__lowerCAmelCase			,		)
												_UpperCAmelCase			=       inputs_dict['labels']
												_UpperCAmelCase			=       inputs_dict['labels']
												_UpperCAmelCase			=       torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.num_choices)			,		dtype=torch.long			,		device=__lowerCAmelCase			,		)
												_UpperCAmelCase			=       torch.zeros(
												    self.model_tester.batch_size			,		dtype=torch.long			,		device=__lowerCAmelCase)
						return inputs_dict



			def    _lowerCamelCase	(						self       :  str)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       OpenAIGPTModelTester(self)
						_UpperCAmelCase			=       ConfigTester(self			,		config_class=__lowerCAmelCase			,		n_embd=37)



			def    _lowerCamelCase	(						self       :  Dict)       ->   Any:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def    _lowerCamelCase	(						self       :  Any)       ->   Tuple:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*__lowerCAmelCase)



			def    _lowerCamelCase	(						self       :  str)       ->   Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*__lowerCAmelCase)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_double_lm_head_model(*__lowerCAmelCase)



			def    _lowerCamelCase	(						self       :  str)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCAmelCase)



			@slow
			def    _lowerCamelCase	(						self       :  Tuple)       ->   List[Any]:



						"""simple docstring"""






						for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_UpperCAmelCase			=       OpenAIGPTModel.from_pretrained(__lowerCAmelCase)
									self.assertIsNotNone(__lowerCAmelCase)







@require_torch
class       __lowerCAmelCase   (      unittest.TestCase						):



			@slow
			def    _lowerCamelCase	(						self       :  Optional[int])       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       OpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
						model.to(__lowerCAmelCase)
						_UpperCAmelCase			=       torch.tensor([[4_81, 47_35, 5_44]]			,		dtype=torch.long			,		device=__lowerCAmelCase)  # the president is
						_UpperCAmelCase			=       [
						    4_81,
						    47_35,
						    5_44,
						    2_46,
						    9_63,
						    8_70,
						    7_62,
						    2_39,
						    2_44,
						    4_04_77,
						    2_44,
						    2_49,
						    7_19,
						    8_81,
						    4_87,
						    5_44,
						    2_40,
						    2_44,
						    6_03,
						    4_81,
						]  # the president is a very good man. " \n " i\'m sure he is, " said the

						_UpperCAmelCase			=       model.generate(__lowerCAmelCase			,		do_sample=__lowerCAmelCase)
						self.assertListEqual(output_ids[0].tolist()			,		__lowerCAmelCase)





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
UpperCAmelCase__       =	"src/transformers"
UpperCAmelCase__       =	"docs/source/en/tasks"





def 							A     (					_UpperCAmelCase    :       List[Any]							,       _UpperCAmelCase    :       Optional[int]							,       _UpperCAmelCase    :       Any   )    ->			int:







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

			_UpperCAmelCase			=       start_index
			while not lines[end_index].startswith(_UpperCAmelCase   ):
						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.
UpperCAmelCase__       =	direct_transformers_import(TRANSFORMERS_PATH)

UpperCAmelCase__       =	{
    "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`).
UpperCAmelCase__       =	{
    "summarization.md": ("nllb",),
    "translation.md": ("nllb",),
}





def 							A     (					_UpperCAmelCase    :       List[Any]   )    ->			int:







			'''simple docstring'''
			_UpperCAmelCase			=       TASK_GUIDE_TO_MODELS[task_guide]
			_UpperCAmelCase			=       SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(_UpperCAmelCase							,       set()   )
			_UpperCAmelCase			=       {
			    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 							A     (					_UpperCAmelCase    :       Tuple							,       _UpperCAmelCase    :       Dict=False   )    ->			List[Any]:







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

			_UpperCAmelCase			=       get_model_list_for_task(_UpperCAmelCase   )

			if current_list != new_list:
						if overwrite:
									with open(os.path.join(_UpperCAmelCase							,       _UpperCAmelCase   )							,       '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__":
							UpperCAmelCase__       =	argparse.ArgumentParser()
							parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
							UpperCAmelCase__       =	parser.parse_args()

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