'''simple docstring'''




import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE       :     Any     =  logging.getLogger(__name__)

@dataclass(frozen=snake_case__    )
class 	lowerCamelCase_	:
      '''simple docstring'''

      __UpperCamelCase:  str
      __UpperCamelCase:  str
      __UpperCamelCase:  Optional[str]						=						None
      __UpperCamelCase:  Optional[str]						=						None
      __UpperCamelCase:  Optional[str]						=						None

@dataclass(frozen=snake_case__    )
class 	lowerCamelCase_	:
      '''simple docstring'''

      __UpperCamelCase:  List[int]
      __UpperCamelCase:  Optional[List[int]]						=						None
      __UpperCamelCase:  Optional[List[int]]						=						None
      __UpperCamelCase:  Optional[Union[int, float]]						=						None
      __UpperCamelCase:  Optional[int]						=						None


if is_torch_available():
  import torch
  from torch.utils.data import Dataset

  class 	lowerCamelCase_	(snake_case__    ):
        '''simple docstring'''

        __UpperCamelCase:  List[InputFeatures]




        def __init__(       self						:				Optional[Any]						,					A						:				str						,					A						:				PreTrainedTokenizer						,					A						:				str						,					A						:				Optional[int] = None						,					A						:				List[Any]=False						,					A						:				bool = False						,					):
               _UpperCAmelCase     :  Optional[int]							      =    hans_processors[task]()

               _UpperCAmelCase     :  int							      =    os.path.join(
                   A						,					"cached_{}_{}_{}_{}".format(
                       "dev" if evaluate else "train"						,					tokenizer.__class__.__name__						,					str(A       )						,					A						,					)						,					)
               _UpperCAmelCase     :  List[Any]							      =    processor.get_labels()
               if tokenizer.__class__ in (
                   RobertaTokenizer,
                   RobertaTokenizerFast,
                   XLMRobertaTokenizer,
                   BartTokenizer,
                   BartTokenizerFast,
               ):
                      # HACK(label indices are swapped in RoBERTa pretrained model)
                      _UpperCAmelCase      ,  _UpperCAmelCase     :  Tuple							      =    label_list[2], label_list[1]
               _UpperCAmelCase     :  Tuple							      =    label_list

               # Make sure only the first process in distributed training processes the dataset,
               # and the others will use the cache.
               _UpperCAmelCase     :  List[str]							      =    cached_features_file + ".lock"
               with FileLock(A       ):
                      if os.path.exists(A       ) and not overwrite_cache:
                             logger.info(F"""Loading features from cached file {cached_features_file}"""       )
                             _UpperCAmelCase     :  str							      =    torch.load(A       )
                      else:
                             logger.info(F"""Creating features from dataset file at {data_dir}"""       )

                             _UpperCAmelCase     :  Optional[int]							      =    (
                                 processor.get_dev_examples(A       ) if evaluate else processor.get_train_examples(A       )
                             )

                             logger.info("Training examples: %s"						,					len(A       )       )
                             _UpperCAmelCase     :  Optional[Any]							      =    hans_convert_examples_to_features(A						,					A						,					A						,					A       )
                             logger.info("Saving features into cached file %s"						,					A       )
                             torch.save(self.features						,					A       )




        def __len__(       self						:				Tuple       ):
               return len(self.features       )




        def __getitem__(       self						:				Optional[int]						,					A						:				str       ):
               return self.features[i]





        def      _A	(       self						:				Optional[int]       ):
               return self.label_list


if is_tf_available():
  import tensorflow as tf

  class 	lowerCamelCase_	:
        '''simple docstring'''

        __UpperCamelCase:  List[InputFeatures]




        def __init__(       self						:				Any						,					A						:				str						,					A						:				PreTrainedTokenizer						,					A						:				str						,					A						:				Optional[int] = 128						,					A						:				str=False						,					A						:				bool = False						,					):
               _UpperCAmelCase     :  List[str]							      =    hans_processors[task]()
               _UpperCAmelCase     :  Optional[Any]							      =    processor.get_labels()
               if tokenizer.__class__ in (
                   RobertaTokenizer,
                   RobertaTokenizerFast,
                   XLMRobertaTokenizer,
                   BartTokenizer,
                   BartTokenizerFast,
               ):
                      # HACK(label indices are swapped in RoBERTa pretrained model)
                      _UpperCAmelCase      ,  _UpperCAmelCase     :  int							      =    label_list[2], label_list[1]
               _UpperCAmelCase     :  Optional[Any]							      =    label_list

               _UpperCAmelCase     :  Tuple							      =    processor.get_dev_examples(A       ) if evaluate else processor.get_train_examples(A       )
               _UpperCAmelCase     :  List[str]							      =    hans_convert_examples_to_features(A						,					A						,					A						,					A       )

               def gen():
                      for ex_index, ex in tqdm.tqdm(enumerate(self.features       )						,					desc="convert examples to features"       ):
                             if ex_index % 10000 == 0:
                                    logger.info("Writing example %d of %d" % (ex_index, len(A       ))       )

                             yield (
                                 {
                                     "example_id": 0,
                                     "input_ids": ex.input_ids,
                                     "attention_mask": ex.attention_mask,
                                     "token_type_ids": ex.token_type_ids,
                                 },
                                 ex.label,
                             )

               _UpperCAmelCase     :  List[str]							      =    tf.data.Dataset.from_generator(
                   A						,					(
                       {
                           "example_id": tf.intaa,
                           "input_ids": tf.intaa,
                           "attention_mask": tf.intaa,
                           "token_type_ids": tf.intaa,
                       },
                       tf.intaa,
                   )						,					(
                       {
                           "example_id": tf.TensorShape([]       ),
                           "input_ids": tf.TensorShape([None, None]       ),
                           "attention_mask": tf.TensorShape([None, None]       ),
                           "token_type_ids": tf.TensorShape([None, None]       ),
                       },
                       tf.TensorShape([]       ),
                   )						,					)




        def      _A	(       self						:				Optional[int]       ):
               return self.dataset




        def __len__(       self						:				Optional[Any]       ):
               return len(self.features       )




        def __getitem__(       self						:				int						,					A						:				List[Any]       ):
               return self.features[i]





        def      _A	(       self						:				Optional[int]       ):
               return self.label_list

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      def      _A	(       self						:				Optional[Any]						,					A						:				Union[str, Any]       ):
             return self._create_examples(self._read_tsv(os.path.join(A						,					"heuristics_train_set.txt"       )       )						,					"train"       )




      def      _A	(       self						:				Optional[int]						,					A						:				List[str]       ):
             return self._create_examples(self._read_tsv(os.path.join(A						,					"heuristics_evaluation_set.txt"       )       )						,					"dev"       )




      def      _A	(       self						:				Optional[int]       ):
             return ["contradiction", "entailment", "neutral"]





      def      _A	(       self						:				Tuple						,					A						:				Optional[int]						,					A						:				Tuple       ):
             _UpperCAmelCase     :  List[Any]							      =    []
             for i, line in enumerate(A       ):
                    if i == 0:
                           continue
                    _UpperCAmelCase     :  int							      =    "%s-%s" % (set_type, line[0])
                    _UpperCAmelCase     :  int							      =    line[5]
                    _UpperCAmelCase     :  Tuple							      =    line[6]
                    _UpperCAmelCase     :  Optional[Any]							      =    line[7][2:] if line[7].startswith("ex"       ) else line[7]
                    _UpperCAmelCase     :  int							      =    line[0]
                    examples.append(InputExample(guid=A						,					text_a=A						,					text_b=A						,					label=A						,					pairID=A       )       )
             return examples

def 	UpperCamelCase_							(					_UpperCAmelCase  :      List[InputExample]			,	_UpperCAmelCase  :      List[str]			,	_UpperCAmelCase  :      int			,	_UpperCAmelCase  :      PreTrainedTokenizer			,	)    ->    str:


       """simple docstring"""







       _UpperCAmelCase     :  Optional[Any]							      =    {label: i for i, label in enumerate(_UpperCAmelCase    )}

       _UpperCAmelCase     :  str							      =    []
       for ex_index, example in tqdm.tqdm(enumerate(_UpperCAmelCase    )			,	desc="convert examples to features"    ):
              if ex_index % 10_000 == 0:
                     logger.info("Writing example %d" % (ex_index)    )

              _UpperCAmelCase     :  str							      =    tokenizer(
                  example.text_a			,	example.text_b			,	add_special_tokens=_UpperCAmelCase			,	max_length=_UpperCAmelCase			,	padding="max_length"			,	truncation=_UpperCAmelCase			,	return_overflowing_tokens=_UpperCAmelCase			,	)

              _UpperCAmelCase     :  Dict							      =    label_map[example.label] if example.label in label_map else 0

              _UpperCAmelCase     :  Dict							      =    int(example.pairID    )

              features.append(InputFeatures(**_UpperCAmelCase			,	label=_UpperCAmelCase			,	pairID=_UpperCAmelCase    )    )

       for i, example in enumerate(examples[:5]    ):
              logger.info("*** Example ***"    )
              logger.info(F"""guid: {example}"""    )
              logger.info(F"""features: {features[i]}"""    )

       return features


__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """hans""": 3,
}


__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """hans""": HansProcessor,
}









import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
							import torch_xla.core.xla_model as xm
							import torch_xla.debug.metrics as met
class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				def __init__(     self						:    Dict,  *lowerCamelCase						:    Union[str, Any],  lowerCamelCase						:    Union[str, Any]=None,  lowerCamelCase						:    Any=None,  **lowerCamelCase						:    str  ):

								'''simple docstring'''
								super().__init__(*lowerCamelCase,  **lowerCamelCase  )
								lowercase__    =			eval_examples
								lowercase__    =			post_process_function




				def 				lowercase__   (     self						:    int,  lowerCamelCase						:    str=None,  lowerCamelCase						:    Optional[Any]=None,  lowerCamelCase						:    Union[str, Any]=None,  lowerCamelCase						:    str = "eval"  ):

								'''simple docstring'''
								lowercase__    =			self.eval_dataset if eval_dataset is None else eval_dataset
								lowercase__    =			self.get_eval_dataloader(lowerCamelCase  )
								lowercase__    =			self.eval_examples if eval_examples is None else eval_examples

								# Temporarily disable metric computation, we will do it in the loop here.
								lowercase__    =			self.compute_metrics
								lowercase__    =			None
								lowercase__    =			self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								lowercase__    =			time.time()
								try:
												lowercase__    =			eval_loop(
												    lowerCamelCase,  description='''Evaluation''',  prediction_loss_only=True if compute_metrics is None else None,  ignore_keys=lowerCamelCase,  metric_key_prefix=lowerCamelCase,  )
								finally:
												lowercase__    =			compute_metrics
								lowercase__    =			self.args.eval_batch_size * self.args.world_size
								if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
												start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
								output.metrics.update(
								    speed_metrics(
								        lowerCamelCase,  lowerCamelCase,  num_samples=output.num_samples,  num_steps=math.ceil(output.num_samples / total_batch_size  ),  )  )
								if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
												# Only the main node write the results by default
												lowercase__    =			self.post_process_function(lowerCamelCase,  lowerCamelCase,  output.predictions  )
												lowercase__    =			self.compute_metrics(lowerCamelCase  )

												# Prefix all keys with metric_key_prefix + '_'
												for key in list(metrics.keys()  ):
																if not key.startswith(F"""{metric_key_prefix}_"""  ):
																				lowercase__    =			metrics.pop(lowerCamelCase  )
												metrics.update(output.metrics  )
								else:
												lowercase__    =			output.metrics

								if self.args.should_log:
												# Only the main node log the results by default
												self.log(lowerCamelCase  )

								if self.args.tpu_metrics_debug or self.args.debug:
												# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
												xm.master_print(met.metrics_report()  )

								lowercase__    =			self.callback_handler.on_evaluate(self.args,  self.state,  self.control,  lowerCamelCase  )
								return metrics




				def 				lowercase__   (     self						:    List[Any],  lowerCamelCase						:    Any,  lowerCamelCase						:    Dict,  lowerCamelCase						:    int=None,  lowerCamelCase						:    str = "test"  ):

								'''simple docstring'''
								lowercase__    =			self.get_test_dataloader(lowerCamelCase  )

								# Temporarily disable metric computation, we will do it in the loop here.
								lowercase__    =			self.compute_metrics
								lowercase__    =			None
								lowercase__    =			self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								lowercase__    =			time.time()
								try:
												lowercase__    =			eval_loop(
												    lowerCamelCase,  description='''Prediction''',  prediction_loss_only=True if compute_metrics is None else None,  ignore_keys=lowerCamelCase,  metric_key_prefix=lowerCamelCase,  )
								finally:
												lowercase__    =			compute_metrics
								lowercase__    =			self.args.eval_batch_size * self.args.world_size
								if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
												start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
								output.metrics.update(
								    speed_metrics(
								        lowerCamelCase,  lowerCamelCase,  num_samples=output.num_samples,  num_steps=math.ceil(output.num_samples / total_batch_size  ),  )  )

								if self.post_process_function is None or self.compute_metrics is None:
												return output

								lowercase__    =			self.post_process_function(lowerCamelCase,  lowerCamelCase,  output.predictions,  '''predict'''  )
								lowercase__    =			self.compute_metrics(lowerCamelCase  )

								# Prefix all keys with metric_key_prefix + '_'
								for key in list(metrics.keys()  ):
												if not key.startswith(F"""{metric_key_prefix}_"""  ):
																lowercase__    =			metrics.pop(lowerCamelCase  )
								metrics.update(output.metrics  )
								return PredictionOutput(predictions=predictions.predictions,  label_ids=predictions.label_ids,  metrics=lowerCamelCase  )




