

'''simple docstring'''
from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.ctrl.modeling_tf_ctrl import (
         TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFCTRLForSequenceClassification,
         TFCTRLLMHeadModel,
         TFCTRLModel,
     )







class 							A__					( A__						):





  def __init__(	self   :			List[str]							,      _a   :			List[Any]							,      )			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=parent
       _SCREAMING_SNAKE_CASE												=13
       _SCREAMING_SNAKE_CASE												=7
       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=True
       _SCREAMING_SNAKE_CASE												=99
       _SCREAMING_SNAKE_CASE												=32
       _SCREAMING_SNAKE_CASE												=2
       _SCREAMING_SNAKE_CASE												=4
       _SCREAMING_SNAKE_CASE												=37
       _SCREAMING_SNAKE_CASE												='gelu'
       _SCREAMING_SNAKE_CASE												=0.1
       _SCREAMING_SNAKE_CASE												=0.1
       _SCREAMING_SNAKE_CASE												=512
       _SCREAMING_SNAKE_CASE												=16
       _SCREAMING_SNAKE_CASE												=2
       _SCREAMING_SNAKE_CASE												=0.02
       _SCREAMING_SNAKE_CASE												=3
       _SCREAMING_SNAKE_CASE												=4
       _SCREAMING_SNAKE_CASE												=None
       _SCREAMING_SNAKE_CASE												=self.vocab_size - 1





  def 		A						(	self   :			List[Any]				)			->					str:





       '''simple docstring'''






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

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

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

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

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

       _SCREAMING_SNAKE_CASE												=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							,      )

       _SCREAMING_SNAKE_CASE												=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 		A						(	self   :			int							,      _a   :			Optional[Any]							,      _a   :			Optional[Any]							,      _a   :			List[Any]							,      _a   :			Optional[int]							,      _a   :			Any							,      *_a   :			Any				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=TFCTRLModel(config=_a				)
       _SCREAMING_SNAKE_CASE												={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       _SCREAMING_SNAKE_CASE												=model(_a				)

       _SCREAMING_SNAKE_CASE												=[input_ids, None, input_mask]  # None is the input for 'past'
       _SCREAMING_SNAKE_CASE												=model(_a				)

       _SCREAMING_SNAKE_CASE												=model(_a				)

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





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=TFCTRLLMHeadModel(config=_a				)
       _SCREAMING_SNAKE_CASE												={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       _SCREAMING_SNAKE_CASE												=model(_a				)
       self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.vocab_size)				)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.num_labels
       _SCREAMING_SNAKE_CASE												=ids_tensor([self.batch_size]							,      self.type_sequence_label_size				)
       _SCREAMING_SNAKE_CASE												={
           'input_ids': input_ids,
           'token_type_ids': token_type_ids,
           'labels': sequence_labels,
       }
       _SCREAMING_SNAKE_CASE												=TFCTRLForSequenceClassification(_a				)
       _SCREAMING_SNAKE_CASE												=model(_a				)
       self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_labels)				)





  def 		A						(	self   :			str				)			->					Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=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
       )					,		(
           _SCREAMING_SNAKE_CASE
       )					,		(
           _SCREAMING_SNAKE_CASE
       )					,		
       )												=config_and_inputs

       _SCREAMING_SNAKE_CASE												={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_tf
class 							A__					( A__  ,     A__  ,     unittest.TestCase						):
  A__					=       (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
  A__					=       (TFCTRLLMHeadModel,) if is_tf_available() else ()
  A__					=       (
      {
          'feature-extraction': TFCTRLModel,
          'text-classification': TFCTRLForSequenceClassification,
          'text-generation': TFCTRLLMHeadModel,
          'zero-shot': TFCTRLForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  A__					=       False
  A__					=       False





  def 		A						(	self   :			Dict							,      _a   :			Optional[int]							,      _a   :			int							,      _a   :			Any							,      _a   :			int							,      _a   :			Union[str, Any]				)			->					List[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 		A						(	self   :			Optional[int]				)			->					Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=TFCTRLModelTester(self				)
       _SCREAMING_SNAKE_CASE												=ConfigTester(self							,      config_class=_a							,      n_embd=37				)





  def 		A						(	self   :			List[str]				)			->					int:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 		A						(	self   :			Any				)			->					str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*_a				)





  def 		A						(	self   :			Any				)			->					Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_lm_head(*_a				)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_for_sequence_classification(*_a				)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=self.model_tester.prepare_config_and_inputs_for_common()
       _SCREAMING_SNAKE_CASE												=[TFCTRLLMHeadModel]
       _SCREAMING_SNAKE_CASE												=[TFCTRLForSequenceClassification]

       for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE												=model_class(_a				)
            model.build()  # may be needed for the get_bias() call below
            assert isinstance(model.get_input_embeddings()							,      tf.keras.layers.Layer				)

            if model_class in list_lm_models:
                 _SCREAMING_SNAKE_CASE												=model.get_output_embeddings()
                 assert isinstance(_a							,      tf.keras.layers.Layer				)
                 _SCREAMING_SNAKE_CASE												=model.get_bias()
                 assert isinstance(_a							,      _a				)
                 for k, v in name.items():
                      assert isinstance(_a							,      tf.Variable				)
            elif model_class in list_other_models_with_output_ebd:
                 _SCREAMING_SNAKE_CASE												=model.get_output_embeddings()
                 assert isinstance(_a							,      tf.keras.layers.Layer				)
                 _SCREAMING_SNAKE_CASE												=model.get_bias()
                 assert name is None
            else:
                 _SCREAMING_SNAKE_CASE												=model.get_output_embeddings()
                 assert x is None
                 _SCREAMING_SNAKE_CASE												=model.get_bias()
                 assert name is None





  @slow
  def 		A						(	self   :			Dict				)			->					str:





       '''simple docstring'''






       for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE												=TFCTRLModel.from_pretrained(_a				)
            self.assertIsNotNone(_a				)







@require_tf
class 							A__					( unittest.TestCase						):





  @slow
  def 		A						(	self   :			int				)			->					List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=TFCTRLLMHeadModel.from_pretrained('ctrl'				)
       _SCREAMING_SNAKE_CASE												=tf.convert_to_tensor([[1_1859, 0, 1611, 8]]							,      dtype=tf.intaa				)  # Legal the president is
       _SCREAMING_SNAKE_CASE												=[
           1_1859,
           0,
           1611,
           8,
           5,
           150,
           2_6449,
           2,
           19,
           348,
           469,
           3,
           2595,
           48,
           2_0740,
           24_6533,
           24_6533,
           19,
           30,
           5,
       ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

       _SCREAMING_SNAKE_CASE												=model.generate(_a							,      do_sample=_a				)
       self.assertListEqual(output_ids[0].numpy().tolist()							,      _a				)



'''simple docstring'''
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


lowerCamelCase				:   int      				=  logging.getLogger(__name__)







@dataclass
class 							A__					:
  A__					=       field(
      metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}						)
  A__					=       field(
      default=A__  ,     metadata={'help': 'Pretrained config name or path if not the same as model_name'}						)
  A__					=       field(
      default=A__  ,     metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}						)
  A__					=       field(default=A__  ,     metadata={'help': 'Set this flag to use fast tokenization.'}						)
  # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
  # or just modify its tokenizer_config.json.
  A__					=       field(
      default=A__  ,     metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}  ,     )


def  _lowerCAmelCase      (		)   ->    Union[str, Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE												=HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)       )

     if len(sys.argv       ) == 2 and sys.argv[1].endswith('.json'       ):
          # If we pass only one argument to the script and it's the path to a json file,
          # let's parse it to get our arguments.
          _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]       )       )
     else:
          _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=parser.parse_args_into_dataclasses()

     if (
         os.path.exists(training_args.output_dir       )
         and os.listdir(training_args.output_dir       )
         and training_args.do_train
         and not training_args.overwrite_output_dir
     ):
          raise ValueError(
              f"Output directory ({training_args.output_dir}) already exists and is not empty. Use"
              ' --overwrite_output_dir to overcome.'       )

     # Setup logging
     logging.basicConfig(
         format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'							,				datefmt='%m/%d/%Y %H:%M:%S'							,				level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN							,				)
     logger.warning(
         'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s'							,				training_args.local_rank							,				training_args.device							,				training_args.n_gpu							,				bool(training_args.local_rank != -1       )							,				training_args.fpaa							,				)
     # Set the verbosity to info of the Transformers logger (on main process only):
     if is_main_process(training_args.local_rank       ):
          transformers.utils.logging.set_verbosity_info()
          transformers.utils.logging.enable_default_handler()
          transformers.utils.logging.enable_explicit_format()
     logger.info('Training/evaluation parameters %s'							,				_UpperCamelCase       )

     # Prepare Question-Answering task
     # Load pretrained model and tokenizer
     #
     # Distributed training:
     # The .from_pretrained methods guarantee that only one local process can concurrently
     # download model & vocab.

     _SCREAMING_SNAKE_CASE												=AutoConfig.from_pretrained(
         model_args.config_name if model_args.config_name else model_args.model_name_or_path							,				cache_dir=model_args.cache_dir							,				)
     _SCREAMING_SNAKE_CASE												=AutoTokenizer.from_pretrained(
         model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path							,				cache_dir=model_args.cache_dir							,				use_fast=_UpperCamelCase							,				)
     _SCREAMING_SNAKE_CASE												=AutoModelForQuestionAnswering.from_pretrained(
         model_args.model_name_or_path							,				from_tf=bool('.ckpt' in model_args.model_name_or_path       )							,				config=_UpperCamelCase							,				cache_dir=model_args.cache_dir							,				)

     # Get datasets
     _SCREAMING_SNAKE_CASE												=hasattr(model.config							,				'lang2id'       )
     _SCREAMING_SNAKE_CASE												=(
         SquadDataset(
             _UpperCamelCase							,				tokenizer=_UpperCamelCase							,				is_language_sensitive=_UpperCamelCase							,				cache_dir=model_args.cache_dir       )
         if training_args.do_train
         else None
     )
     _SCREAMING_SNAKE_CASE												=(
         SquadDataset(
             _UpperCamelCase							,				tokenizer=_UpperCamelCase							,				mode='dev'							,				is_language_sensitive=_UpperCamelCase							,				cache_dir=model_args.cache_dir							,				)
         if training_args.do_eval
         else None
     )

     # Data collator
     _SCREAMING_SNAKE_CASE												=DataCollatorWithPadding(_UpperCamelCase							,				pad_to_multiple_of=8       ) if training_args.fpaa else None

     # Initialize our Trainer
     _SCREAMING_SNAKE_CASE												=Trainer(
         model=_UpperCamelCase							,				args=_UpperCamelCase							,				train_dataset=_UpperCamelCase							,				eval_dataset=_UpperCamelCase							,				data_collator=_UpperCamelCase							,				)

     # Training
     if training_args.do_train:
          trainer.train(
              model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path       ) else None       )
          trainer.save_model()
          # For convenience, we also re-save the tokenizer to the same directory,
          # so that you can share your model easily on huggingface.co/models =)
          if trainer.is_world_master():
               tokenizer.save_pretrained(training_args.output_dir       )


def  _lowerCAmelCase      (		_UpperCamelCase		:  Tuple       )   ->    List[str]:
     """simple docstring"""




     main()


if __name__ == "__main__":
     main()
