



"""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,
)


UpperCAmelCase						:		Any			= logging.getLogger(__name__)


@dataclass(frozen=__lowerCAmelCase   )
class    SCREAMING_SNAKE_CASE__ :
    lowercase__      =   42
    lowercase__      =   42
    lowercase__      =   None
    lowercase__      =   None
    lowercase__      =   None







@dataclass(frozen=__lowerCAmelCase   )
class    SCREAMING_SNAKE_CASE__ :
    lowercase__      =   42
    lowercase__      =   None
    lowercase__      =   None
    lowercase__      =   None
    lowercase__      =   None


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


    class    SCREAMING_SNAKE_CASE__ (							__lowerCAmelCase   ):
        lowercase__      =   42

        def __init__(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Tuple = None ,  lowerCAmelCase_			:				Optional[Any]=False ,  lowerCAmelCase_			:				List[str] = False ,  ):







          """simple docstring"""







          lowercase_								= hans_processors[task]()

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

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

              lowercase_								= (
                  processor.get_dev_examples(lowerCAmelCase_) if evaluate else processor.get_train_examples(lowerCAmelCase_)
              )

              logger.info("""Training examples: %s""" ,  len(lowerCAmelCase_))
              lowercase_								= hans_convert_examples_to_features(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_)
              logger.info("""Saving features into cached file %s""" ,  lowerCAmelCase_)
              torch.save(self.features ,  lowerCAmelCase_)

        def __len__(				self			:				Optional[Any]):







          """simple docstring"""







          return len(self.features)

        def __getitem__(				self			:				List[str] ,  lowerCAmelCase_			:				Any):







          """simple docstring"""







          return self.features[i]




        def 					_UpperCAmelCase	(				self			:				Dict):







          """simple docstring"""







          return self.label_list


if is_tf_available():
    import tensorflow as tf


    class    SCREAMING_SNAKE_CASE__ :
        lowercase__      =   42

        def __init__(				self			:				int ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				int = 1_2_8 ,  lowerCAmelCase_			:				Tuple=False ,  lowerCAmelCase_			:				List[Any] = False ,  ):







          """simple docstring"""







          lowercase_								= hans_processors[task]()
          lowercase_								= processor.get_labels()
          if tokenizer.__class__ in (
              RobertaTokenizer,
              RobertaTokenizerFast,
              XLMRobertaTokenizer,
              BartTokenizer,
              BartTokenizerFast,
          ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            lowercase_					,    lowercase_								= label_list[2], label_list[1]
          lowercase_								= label_list

          lowercase_								= processor.get_dev_examples(lowerCAmelCase_) if evaluate else processor.get_train_examples(lowerCAmelCase_)
          lowercase_								= hans_convert_examples_to_features(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_)

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

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

          lowercase_								= tf.data.Dataset.from_generator(
              lowerCAmelCase_ ,  (
                  {
                      """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 					_UpperCAmelCase	(				self			:				Union[str, Any]):







          """simple docstring"""







          return self.dataset

        def __len__(				self			:				str):







          """simple docstring"""







          return len(self.features)

        def __getitem__(				self			:				int ,  lowerCAmelCase_			:				Union[str, Any]):







          """simple docstring"""







          return self.features[i]




        def 					_UpperCAmelCase	(				self			:				Optional[int]):







          """simple docstring"""







          return self.label_list


class    SCREAMING_SNAKE_CASE__ (							__lowerCAmelCase   ):

    def 					_UpperCAmelCase	(				self			:				str ,  lowerCAmelCase_			:				int):







      """simple docstring"""







      return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase_ ,  """heuristics_train_set.txt""")) ,  """train""")

    def 					_UpperCAmelCase	(				self			:				Optional[Any] ,  lowerCAmelCase_			:				Dict):







      """simple docstring"""







      return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase_ ,  """heuristics_evaluation_set.txt""")) ,  """dev""")

    def 					_UpperCAmelCase	(				self			:				Tuple):







      """simple docstring"""







      return ["contradiction", "entailment", "neutral"]




    def 					_UpperCAmelCase	(				self			:				int ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Union[str, Any]):







      """simple docstring"""







      lowercase_								= []
      for i, line in enumerate(lowerCAmelCase_):
        if i == 0:
          continue
        lowercase_								= """%s-%s""" % (set_type, line[0])
        lowercase_								= line[5]
        lowercase_								= line[6]
        lowercase_								= line[7][2:] if line[7].startswith("""ex""") else line[7]
        lowercase_								= line[0]
        examples.append(InputExample(guid=lowerCAmelCase_ ,  text_a=lowerCAmelCase_ ,  text_b=lowerCAmelCase_ ,  label=lowerCAmelCase_ ,  pairID=lowerCAmelCase_))
      return examples







def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase ,  __lowerCAmelCase ,  __lowerCAmelCase ,  __lowerCAmelCase ,  ) ->						Optional[int]:







  '''simple docstring'''


  lowercase_								= {label: i for i, label in enumerate(snake_case__							)}

  lowercase_								= []
  for ex_index, example in tqdm.tqdm(enumerate(snake_case__							) ,  desc="""convert examples to features"""							):
    if ex_index % 1_00_00 == 0:
      logger.info("""Writing example %d""" % (ex_index)							)

    lowercase_								= tokenizer(
        example.text_a ,  example.text_b ,  add_special_tokens=snake_case__ ,  max_length=snake_case__ ,  padding="""max_length""" ,  truncation=snake_case__ ,  return_overflowing_tokens=snake_case__ ,  )

    lowercase_								= label_map[example.label] if example.label in label_map else 0

    lowercase_								= int(example.pairID							)

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

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

  return features


UpperCAmelCase						:		List[str]			= {
    "hans": 3,
}


UpperCAmelCase						:		str			= {
    "hans": HansProcessor,
}






"""simple docstring"""
def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase ,  __lowerCAmelCase							) ->						int:







  '''simple docstring'''


  while a != 0:
    lowercase_					,    lowercase_								= b % a, a
  return b


def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase ,  __lowerCAmelCase							) ->						int:







  '''simple docstring'''


  if gcd(__lowerCAmelCase ,  __lowerCAmelCase							) != 1:
    lowercase_								= F'''mod inverse of {a!r} and {m!r} does not exist'''
    raise ValueError(__lowerCAmelCase							)
  lowercase_					,    lowercase_					,    lowercase_								= 1, 0, a
  lowercase_					,    lowercase_					,    lowercase_								= 0, 1, m
  while va != 0:
    lowercase_								= ua // va
    lowercase_					,    lowercase_					,    lowercase_					,    lowercase_					,    lowercase_					,    lowercase_								= (ua - q * va), (ua - q * va), (ua - q * va), va, va, va
  return ua % m

