
import gc
import unittest

from transformers import CTRLConfig, 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, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
					    CTRLForSequenceClassification,
					    CTRLLMHeadModel,
					    CTRLModel,
					)


class 				lowercase					:



		def __init__( self				,      A_				,      A_=14				,      A_=7				,      A_=True				,      A_=True				,      A_=True				,      A_=True				,      A_=True				,      A_=99				,      A_=32				,      A_=5				,      A_=4				,      A_=37				,      A_="gelu"				,      A_=0.1				,      A_=0.1				,      A_=512				,      A_=16				,      A_=2				,      A_=0.02				,      A_=3				,      A_=4				,      A_=None				,      )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     parent
				UpperCamelCase		=     batch_size
				UpperCamelCase		=     seq_length
				UpperCamelCase		=     is_training
				UpperCamelCase		=     use_token_type_ids
				UpperCamelCase		=     use_input_mask
				UpperCamelCase		=     use_labels
				UpperCamelCase		=     use_mc_token_ids
				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 __UpperCamelCase ( self       )  ->					Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase		=     ids_tensor([self.batch_size, self.seq_length]				,      self.vocab_size       )

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

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

				UpperCamelCase		=     None
				if self.use_mc_token_ids:
						UpperCamelCase		=     ids_tensor([self.batch_size, self.num_choices]				,      self.seq_length       )

				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		=     self.get_config()

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

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



		def __UpperCamelCase ( self       )  ->					Dict:
				"""simple docstring"""
				return CTRLConfig(
				    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				,      )



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_				,      A_				,      A_				,      *A_       )  ->					Optional[int]:
				"""simple docstring"""
				UpperCamelCase		=     CTRLModel(config=A_       )
				model.to(A_       )
				model.eval()

				model(A_				,      token_type_ids=A_				,      head_mask=A_       )
				model(A_				,      token_type_ids=A_       )
				UpperCamelCase		=     model(A_       )
				self.parent.assertEqual(result.last_hidden_state.shape				,      (self.batch_size, self.seq_length, self.hidden_size)       )
				self.parent.assertEqual(len(result.past_key_values       )				,      config.n_layer       )



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_				,      A_				,      A_				,      *A_       )  ->					str:
				"""simple docstring"""
				UpperCamelCase		=     CTRLLMHeadModel(A_       )
				model.to(A_       )
				model.eval()

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



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase		=     self.prepare_config_and_inputs()

				(
				    UpperCamelCase							
				)		=     config_and_inputs

				UpperCamelCase		=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

				return config, inputs_dict



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_				,      A_				,      *A_       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase		=     self.num_labels
				UpperCamelCase		=     CTRLForSequenceClassification(A_       )
				model.to(A_       )
				model.eval()
				UpperCamelCase		=     ids_tensor([self.batch_size]				,      self.type_sequence_label_size       )
				UpperCamelCase		=     model(A_				,      token_type_ids=A_				,      labels=A_       )
				self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_labels)       )






@require_torch
class 				lowercase					(		__UpperCAmelCase  ,   __UpperCAmelCase  ,   __UpperCAmelCase  ,   unittest.TestCase   ):
		__lowercase       :       List[str]	     =				(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
		__lowercase       :       int	     =				(CTRLLMHeadModel,) if is_torch_available() else ()
		__lowercase       :       Dict	     =				(
		    {
		        '''feature-extraction''': CTRLModel,
		        '''text-classification''': CTRLForSequenceClassification,
		        '''text-generation''': CTRLLMHeadModel,
		        '''zero-shot''': CTRLForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		__lowercase       :       Union[str, Any]	     =				True
		__lowercase       :       Any	     =				False
		__lowercase       :       Optional[Any]	     =				False



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_				,      A_				,      A_       )  ->					Any:
				"""simple docstring"""

				if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
						# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
						# `CTRLConfig` 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 __UpperCamelCase ( self       )  ->					Optional[int]:
				"""simple docstring"""
				UpperCamelCase		=     CTRLModelTester(self       )
				UpperCamelCase		=     ConfigTester(self				,      config_class=A_				,      n_embd=37       )



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				super().tearDown()
				# clean-up as much as possible GPU memory occupied by PyTorch
				gc.collect()
				torch.cuda.empty_cache()



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase ( self       )  ->					Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase		=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_ctrl_model(*A_       )



		def __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				UpperCamelCase		=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(*A_       )



		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'       )
		def __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				pass



		@slow
		def __UpperCamelCase ( self       )  ->					Optional[int]:
				"""simple docstring"""

				for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase		=     CTRLModel.from_pretrained(A_       )
						self.assertIsNotNone(A_       )



		@unittest.skip('The model doesn\'t support left padding'       )  # and it's not used enough to be worth fixing :)
		def __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				pass






@require_torch
class 				lowercase					(		unittest.TestCase   ):



		def __UpperCamelCase ( self       )  ->					Union[str, Any]:
				"""simple docstring"""
				super().tearDown()
				# clean-up as much as possible GPU memory occupied by PyTorch
				gc.collect()
				torch.cuda.empty_cache()



		@slow
		def __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				UpperCamelCase		=     CTRLLMHeadModel.from_pretrained('ctrl'       )
				model.to(A_       )
				UpperCamelCase		=     torch.tensor(
				    [[11_859, 0, 1_611, 8]]				,      dtype=torch.long				,      device=A_       )  # Legal the president is
				UpperCamelCase		=     [
				    11_859,
				    0,
				    1_611,
				    8,
				    5,
				    150,
				    26_449,
				    2,
				    19,
				    348,
				    469,
				    3,
				    2_595,
				    48,
				    20_740,
				    246_533,
				    246_533,
				    19,
				    30,
				    5,
				]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

				UpperCamelCase		=     model.generate(A_				,      do_sample=A_       )
				self.assertListEqual(output_ids[0].tolist()				,      A_       )







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
lowercase      :       List[str]									=  """src/transformers"""
lowercase      :       Optional[int]									=  """docs/source/en/tasks"""



def A_	(		A__		,      A__		,      A__				)  ->		Tuple:
       with open(A__		,      'r'		,      encoding='utf-8'		,      newline='\n'				) as f:
              a__						:	Any =				f.readlines()
       # Find the start prompt.
       a__						:	str =				0
       while not lines[start_index].startswith(A__				):
              start_index += 1
       start_index += 1

       a__						:	int =				start_index
       while not lines[end_index].startswith(A__				):
              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.
lowercase      :       Tuple									=  direct_transformers_import(TRANSFORMERS_PATH)

lowercase      :       Optional[Any]									=  {
    """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`).
lowercase      :       Optional[Any]									=  {
    """summarization.md""": ("""nllb""",),
    """translation.md""": ("""nllb""",),
}



def A_	(		A__				)  ->		Optional[int]:
       a__						:	Optional[Any] =				TASK_GUIDE_TO_MODELS[task_guide]
       a__						:	int =				SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(A__		,      set()				)
       a__						:	Optional[Any] =				{
           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_	(		A__		,      A__=False				)  ->		Optional[int]:
       a__ ,							a__ ,							a__ ,							a__						:	Dict =				_find_text_in_file(
           filename=os.path.join(A__		,      A__				)		,      start_prompt='<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->'		,      end_prompt='<!--End of the generated tip-->'		,      )

       a__						:	List[Any] =				get_model_list_for_task(A__				)

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

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


