






import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


__A					=	logging.getLogger(__name__)







def       lowercase__	(					A_:						int  ,      A_:						Optional[Any]		)	->				int:







       """simple docstring"""






       return (preds == labels).mean()



@dataclass
class 				_A			:
    """simple docstring"""

    lowerCamelCase	:							str				= field(
        metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}				)
    lowerCamelCase	:							Optional[str]				= field(
        default=UpperCamelCase					,					metadata={'help': 'Pretrained config name or path if not the same as model_name'}				)
    lowerCamelCase	:							Optional[str]				= field(
        default=UpperCamelCase					,					metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}				)
    lowerCamelCase	:							Optional[str]				= field(
        default=UpperCamelCase					,					metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}					,					)




@dataclass
class 				_A			:
    """simple docstring"""

    lowerCamelCase	:							str				= field(metadata={'help': 'The name of the task to train on: ' + ', '.join(processors.keys()				)}				)
    lowerCamelCase	:							str				= field(metadata={'help': 'Should contain the data files for the task.'}				)
    lowerCamelCase	:							int				= field(
        default=128					,					metadata={
            'help': (
                'The maximum total input sequence length after tokenization. Sequences longer '
                'than this will be truncated, sequences shorter will be padded.'
            )
        }					,					)
    lowerCamelCase	:							bool				= field(
        default=UpperCamelCase					,					metadata={'help': 'Overwrite the cached training and evaluation sets'}				)







def       lowercase__	(					)	->				str:







       """simple docstring"""






       __UpperCAmelCase     				=HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)		)
       __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase     				=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"""  ,      A_		)

       # Set seed
       set_seed(training_args.seed		)

       try:
              __UpperCAmelCase     				=processors[data_args.task_name]()
              __UpperCAmelCase     				=processor.get_labels()
              __UpperCAmelCase     				=len(A_		)
       except KeyError:
              raise ValueError("""Task not found: %s""" % (data_args.task_name)		)

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

       __UpperCAmelCase     				=AutoConfig.from_pretrained(
           model_args.config_name if model_args.config_name else model_args.model_name_or_path  ,      num_labels=A_  ,      finetuning_task=data_args.task_name  ,      cache_dir=model_args.cache_dir  ,      )
       __UpperCAmelCase     				=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  ,      )
       __UpperCAmelCase     				=AutoModelForMultipleChoice.from_pretrained(
           model_args.model_name_or_path  ,      from_tf=bool(""".ckpt""" in model_args.model_name_or_path		)  ,      config=A_  ,      cache_dir=model_args.cache_dir  ,      )

       # Get datasets
       __UpperCAmelCase     				=(
           MultipleChoiceDataset(
               data_dir=data_args.data_dir  ,      tokenizer=A_  ,      task=data_args.task_name  ,      max_seq_length=data_args.max_seq_length  ,      overwrite_cache=data_args.overwrite_cache  ,      mode=Split.train  ,      )
           if training_args.do_train
           else None
       )
       __UpperCAmelCase     				=(
           MultipleChoiceDataset(
               data_dir=data_args.data_dir  ,      tokenizer=A_  ,      task=data_args.task_name  ,      max_seq_length=data_args.max_seq_length  ,      overwrite_cache=data_args.overwrite_cache  ,      mode=Split.dev  ,      )
           if training_args.do_eval
           else None
       )

       def compute_metrics(A_:						EvalPrediction		) -> Dict:
              __UpperCAmelCase     				=np.argmax(p.predictions  ,      axis=1		)
              return {"acc": simple_accuracy(A_  ,      p.label_ids		)}

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

       # Initialize our Trainer
       __UpperCAmelCase     				=Trainer(
           model=A_  ,      args=A_  ,      train_dataset=A_  ,      eval_dataset=A_  ,      compute_metrics=A_  ,      data_collator=A_  ,      )

       # 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		)

    # Evaluation
       __UpperCAmelCase     				={}
       if training_args.do_eval:
              logger.info("""*** Evaluate ***"""		)

              __UpperCAmelCase     				=trainer.evaluate()

              __UpperCAmelCase     				=os.path.join(training_args.output_dir  ,      """eval_results.txt"""		)
              if trainer.is_world_master():
                     with open(A_  ,      """w"""		) as writer:
                            logger.info("""***** Eval results *****"""		)
                            for key, value in result.items():
                                   logger.info("""  %s = %s"""  ,      A_  ,      A_		)
                                   writer.write("""%s = %s\n""" % (key, value)		)

                            results.update(A_		)

       return results







def       lowercase__	(					A_:						Union[str, Any]		)	->				Any:







       """simple docstring"""






       main()


if __name__ == "__main__":
   main()






"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

__SCREAMING_SNAKE_CASE							=     {
    'vocab_file': {
        'junnyu/roformer_chinese_small': 'https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_base': 'https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_char_small': (
            'https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_chinese_char_base': (
            'https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_discriminator': (
            'https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_generator': (
            'https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt'
        ),
    }
}

__SCREAMING_SNAKE_CASE							=     {
    'junnyu/roformer_chinese_small': 1_536,
    'junnyu/roformer_chinese_base': 1_536,
    'junnyu/roformer_chinese_char_small': 512,
    'junnyu/roformer_chinese_char_base': 512,
    'junnyu/roformer_small_discriminator': 128,
    'junnyu/roformer_small_generator': 128,
}


__SCREAMING_SNAKE_CASE							=     {
    'junnyu/roformer_chinese_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_base': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_base': {'do_lower_case': True},
    'junnyu/roformer_small_discriminator': {'do_lower_case': True},
    'junnyu/roformer_small_generator': {'do_lower_case': True},
}





class 				a__					(						A__       ):
     UpperCAmelCase__         =      VOCAB_FILES_NAMES
     UpperCAmelCase__         =      PRETRAINED_VOCAB_FILES_MAP
     UpperCAmelCase__         =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     UpperCAmelCase__         =      PRETRAINED_INIT_CONFIGURATION
     UpperCAmelCase__         =      RoFormerTokenizer



     def __init__(							self :Tuple      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :List[Any]=True      ,  _lowerCamelCase :Dict="[UNK]"      ,  _lowerCamelCase :List[str]="[SEP]"      ,  _lowerCamelCase :str="[PAD]"      ,  _lowerCamelCase :Optional[Any]="[CLS]"      ,  _lowerCamelCase :Optional[int]="[MASK]"      ,  _lowerCamelCase :str=True      ,  _lowerCamelCase :Tuple=None      ,  **_lowerCamelCase :Any      ,  ):







      '''simple docstring'''



      super().__init__(
          _lowerCamelCase      ,  tokenizer_file=_lowerCamelCase      ,  do_lower_case=_lowerCamelCase      ,  unk_token=_lowerCamelCase      ,  sep_token=_lowerCamelCase      ,  pad_token=_lowerCamelCase      ,  cls_token=_lowerCamelCase      ,  mask_token=_lowerCamelCase      ,  tokenize_chinese_chars=_lowerCamelCase      ,  strip_accents=_lowerCamelCase      ,  **_lowerCamelCase      ,  )

      UpperCamelCase_     :					Any   	=json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
      if (
          pre_tok_state.get('lowercase'      ,  _lowerCamelCase   ) != do_lower_case
          or pre_tok_state.get('strip_accents'      ,  _lowerCamelCase   ) != strip_accents
      ):
       UpperCamelCase_     :					Optional[Any]   	=getattr(_lowerCamelCase      ,  pre_tok_state.pop('type'   )   )
       UpperCamelCase_     :					Tuple   	=do_lower_case
       UpperCamelCase_     :					Union[str, Any]   	=strip_accents
       UpperCamelCase_     :					Union[str, Any]   	=pre_tok_class(**_lowerCamelCase   )

      UpperCamelCase_     :					List[str]   	=do_lower_case



     def __getstate__(							self :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=self.__dict__.copy()
      UpperCamelCase_     :					Union[str, Any]   	=BertPreTokenizer()
      return state



     def __setstate__(							self :str      ,  _lowerCamelCase :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=d
      UpperCamelCase_     :					Optional[int]   	=self.__dict__['_tokenizer'].get_vocab()
      UpperCamelCase_     :					Any   	=PreTokenizer.custom(JiebaPreTokenizer(_lowerCamelCase   )   )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :Optional[Any]      ,  _lowerCamelCase :Any=None   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=[self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
       output += token_ids_a + [self.sep_token_id]

      return output



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :List[int]      ,  _lowerCamelCase :Optional[List[int]] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[Any]   	=[self.sep_token_id]
      UpperCamelCase_     :					List[Any]   	=[self.cls_token_id]
      if token_ids_a is None:
       return len(cls + token_ids_a + sep   ) * [0]
      return len(cls + token_ids_a + sep   ) * [0] + len(token_ids_a + sep   ) * [1]



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[str] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[Any]   	=self._tokenizer.model.save(_lowerCamelCase      ,  name=_lowerCamelCase   )
      return tuple(_lowerCamelCase   )




     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :List[Any]=None      ,  _lowerCamelCase :Optional[Any]=None      ,  _lowerCamelCase :int=False      ,  **_lowerCamelCase :Optional[int]      ,  ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=BertPreTokenizer()
      return super().save_pretrained(_lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase      ,  **_lowerCamelCase   )
