






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

import numpy as np
import torch
from utils_hans import HansDataset, InputFeatures, hans_processors, hans_tasks_num_labels

import transformers
from transformers import (
    AutoConfig,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    default_data_collator,
    set_seed,
)
from transformers.trainer_utils import is_main_process


__UpperCAmelCase      				=							logging.getLogger(__name__)
@dataclass
class 					a_:



     """simple docstring"""



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

@dataclass
class 					a_:



     """simple docstring"""



     __snake_case						:							str       			=field(
         metadata={'''help''': '''The name of the task to train selected in the list: ''' + ''', '''.join(hans_processors.keys()					)}					)
     __snake_case						:							str       			=field(
         metadata={'''help''': '''The input data dir. Should contain the .tsv files (or other data files) for the task.'''}					)
     __snake_case						:							int       			=field(
         default=1_28	,    metadata={
             '''help''': (
                 '''The maximum total input sequence length after tokenization. Sequences longer '''
                 '''than this will be truncated, sequences shorter will be padded.'''
             )
         }	,    )
     __snake_case						:							bool       			=field(
         default=lowercase__	,    metadata={'''help''': '''Overwrite the cached training and evaluation sets'''}					)





def 		A_			(		lowercase_	)							->Dict[str, torch.Tensor]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						default_data_collator(lowercase_	)
 SCREAMING_SNAKE_CASE							  =						batch.pop('pairID'					,       lowercase_	)
 return batch





def 		A_			(		)							->List[str]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)	)
 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'					,       lowercase_	)

 # Set seed
 set_seed(training_args.seed	)

 try:
  SCREAMING_SNAKE_CASE							  =						hans_tasks_num_labels[data_args.task_name]
 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.

 SCREAMING_SNAKE_CASE							  =						AutoConfig.from_pretrained(
     model_args.config_name if model_args.config_name else model_args.model_name_or_path					,       num_labels=lowercase_					,       finetuning_task=data_args.task_name					,       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					,       )
 SCREAMING_SNAKE_CASE							  =						AutoModelForSequenceClassification.from_pretrained(
     model_args.model_name_or_path					,       from_tf=bool('.ckpt' in model_args.model_name_or_path	)					,       config=lowercase_					,       cache_dir=model_args.cache_dir					,       )

 # Get datasets
 SCREAMING_SNAKE_CASE							  =						(
     HansDataset(
         data_dir=data_args.data_dir					,       tokenizer=lowercase_					,       task=data_args.task_name					,       max_seq_length=data_args.max_seq_length					,       overwrite_cache=data_args.overwrite_cache					,       )
     if training_args.do_train
     else None
 )
 SCREAMING_SNAKE_CASE							  =						(
     HansDataset(
         data_dir=data_args.data_dir					,       tokenizer=lowercase_					,       task=data_args.task_name					,       max_seq_length=data_args.max_seq_length					,       overwrite_cache=data_args.overwrite_cache					,       evaluate=lowercase_					,       )
     if training_args.do_eval
     else None
 )

 # Initialize our Trainer
 SCREAMING_SNAKE_CASE							  =						Trainer(
     model=lowercase_					,       args=lowercase_					,       train_dataset=lowercase_					,       eval_dataset=lowercase_					,       data_collator=lowercase_					,       )

 # 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
 if training_args.do_eval:
  logger.info('*** Evaluate ***'	)

  SCREAMING_SNAKE_CASE							  =						trainer.predict(lowercase_	)
  SCREAMING_SNAKE_CASE							  =						output.predictions
  SCREAMING_SNAKE_CASE							  =						np.argmax(lowercase_					,       axis=1	)

  SCREAMING_SNAKE_CASE							  =						[ex.pairID for ex in eval_dataset]
  SCREAMING_SNAKE_CASE							  =						os.path.join(training_args.output_dir					,       'hans_predictions.txt'	)
  SCREAMING_SNAKE_CASE							  =						eval_dataset.get_labels()
  if trainer.is_world_master():
   with open(lowercase_					,       'w'	) as writer:
    writer.write('pairID,gold_label\n'	)
    for pid, pred in zip(lowercase_					,       lowercase_	):
     writer.write('ex' + str(lowercase_	) + ',' + label_list[int(lowercase_	)] + '\n'	)

  trainer._log(output.metrics	)





def 		A_			(		lowercase_	)							->int:







 """simple docstring"""

 main()


if __name__ == "__main__":
      main()












import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm





def 		A_			(		lowercase_					,       lowercase_ = False	)							->Union[str, Any]:







 """simple docstring"""

 if device_specific:
  seed += AcceleratorState().process_index
 random.seed(lowercase_	)
 np.random.seed(lowercase_	)
 torch.manual_seed(lowercase_	)
 if is_xpu_available():
  torch.xpu.manual_seed_all(lowercase_	)
 elif is_npu_available():
  torch.npu.manual_seed_all(lowercase_	)
 else:
  torch.cuda.manual_seed_all(lowercase_	)
 # ^^ safe to call this function even if cuda is not available
 if is_tpu_available():
  xm.set_rng_state(lowercase_	)





def 		A_			(		lowercase_ = None					,       lowercase_ = None	)							->Optional[Any]:







 """simple docstring"""

 if rng_type == RNGType.TORCH:
  SCREAMING_SNAKE_CASE							  =						torch.get_rng_state()
 elif rng_type == RNGType.CUDA:
  SCREAMING_SNAKE_CASE							  =						torch.cuda.get_rng_state()
 elif rng_type == RNGType.XLA:
  assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
  SCREAMING_SNAKE_CASE							  =						torch.tensor(xm.get_rng_state()	)
 elif rng_type == RNGType.NPU:
  assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
  SCREAMING_SNAKE_CASE							  =						torch.npu.get_rng_state()
 elif rng_type == RNGType.XPU:
  assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
  SCREAMING_SNAKE_CASE							  =						torch.xpu.get_rng_state()
 elif rng_type == RNGType.GENERATOR:
  assert generator is not None, "Need a generator to synchronize its seed."
  SCREAMING_SNAKE_CASE							  =						generator.get_state()

 # Broadcast the rng state from device 0 to other devices
 SCREAMING_SNAKE_CASE							  =						AcceleratorState()
 if state.distributed_type == DistributedType.TPU:
  SCREAMING_SNAKE_CASE							  =						rng_state.to(xm.xla_device()	)
  xm.collective_broadcast([rng_state]	)
  xm.mark_step()
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif (
     state.distributed_type in CUDA_DISTRIBUTED_TYPES
     or state.distributed_type == DistributedType.MULTI_NPU
     or state.distributed_type == DistributedType.MULTI_XPU
 ):
  SCREAMING_SNAKE_CASE							  =						rng_state.to(state.device	)
  torch.distributed.broadcast(lowercase_					,       0	)
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif state.distributed_type == DistributedType.MULTI_CPU:
  torch.distributed.broadcast(lowercase_					,       0	)

 # Set the broadcast rng state
 if rng_type == RNGType.TORCH:
  torch.set_rng_state(lowercase_	)
 elif rng_type == RNGType.CUDA:
  torch.cuda.set_rng_state(lowercase_	)
 elif rng_type == RNGType.NPU:
  torch.npu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XPU:
  torch.xpu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XLA:
  xm.set_rng_state(rng_state.item()	)
 elif rng_type == RNGType.GENERATOR:
  generator.set_state(lowercase_	)





def 		A_			(		lowercase_					,       lowercase_ = None	)							->str:







 """simple docstring"""

 for rng_type in rng_types:
  synchronize_rng_state(RNGType(lowercase_	)					,       generator=lowercase_	)




