






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


snake_case : List[str]     		=		logging.getLogger(__name__)



@dataclass(frozen=a_)
class        UpperCamelCase__ :







    """simple docstring"""
    __UpperCAmelCase           = 42
    __UpperCAmelCase           = 42
    __UpperCAmelCase           = None
    __UpperCAmelCase           = None
    __UpperCAmelCase           = None



@dataclass(frozen=a_)
class        UpperCamelCase__ :







    """simple docstring"""
    __UpperCAmelCase           = 42
    __UpperCAmelCase           = None
    __UpperCAmelCase           = None
    __UpperCAmelCase           = None
    __UpperCAmelCase           = None


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



 class        UpperCamelCase__ (				a_):







     """simple docstring"""
     __UpperCAmelCase           = 42







     def __init__(		self :	Any				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	PreTrainedTokenizer				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Optional[int] = None				,    UpperCamelCase_ :	List[str]=False				,    UpperCamelCase_ :	bool = False				,    ):





            '''simple docstring'''

            __magic_name__      				=    hans_processors[task]()

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

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

                          __magic_name__      				=    (
                              processor.get_dev_examples(UpperCamelCase_ ) if evaluate else processor.get_train_examples(UpperCamelCase_ )
                          )

                          logger.info('Training examples: %s'				,    len(UpperCamelCase_ ) )
                          __magic_name__      				=    hans_convert_examples_to_features(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_ )
                          logger.info('Saving features into cached file %s'				,    UpperCamelCase_ )
                          torch.save(self.features				,    UpperCamelCase_ )







     def __len__(		self :	Optional[Any] ):





            '''simple docstring'''

            return len(self.features )







     def __getitem__(		self :	Dict				,    UpperCamelCase_ :	Tuple ):





            '''simple docstring'''

            return self.features[i]







     def 				a__			(		self :	Tuple ):





            '''simple docstring'''

            return self.label_list


if is_tf_available():
 import tensorflow as tf



 class        UpperCamelCase__ :







     """simple docstring"""
     __UpperCAmelCase           = 42







     def __init__(		self :	Dict				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	PreTrainedTokenizer				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Optional[int] = 1_2_8				,    UpperCamelCase_ :	Dict=False				,    UpperCamelCase_ :	bool = False				,    ):





            '''simple docstring'''

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

            __magic_name__      				=    processor.get_dev_examples(UpperCamelCase_ ) if evaluate else processor.get_train_examples(UpperCamelCase_ )
            __magic_name__      				=    hans_convert_examples_to_features(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_ )

            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(UpperCamelCase_ )) )

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

            __magic_name__      				=    tf.data.Dataset.from_generator(
                UpperCamelCase_				,    (
                    {
                        '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 :	Tuple ):





            '''simple docstring'''

            return self.dataset







     def __len__(		self :	Dict ):





            '''simple docstring'''

            return len(self.features )







     def __getitem__(		self :	Dict				,    UpperCamelCase_ :	int ):





            '''simple docstring'''

            return self.features[i]







     def 				a__			(		self :	Union[str, Any] ):





            '''simple docstring'''

            return self.label_list



class        UpperCamelCase__ (				a_):







    """simple docstring"""







    def 				a__			(		self :	List[Any]				,    UpperCamelCase_ :	int ):





           '''simple docstring'''

           return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase_				,    'heuristics_train_set.txt' ) )				,    'train' )







    def 				a__			(		self :	int				,    UpperCamelCase_ :	Optional[int] ):





           '''simple docstring'''

           return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase_				,    'heuristics_evaluation_set.txt' ) )				,    'dev' )







    def 				a__			(		self :	Any ):





           '''simple docstring'''

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







    def 				a__			(		self :	Any				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	Dict ):





           '''simple docstring'''

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


def 							A						(				__snake_case: List[InputExample] ,     __snake_case: List[str] ,     __snake_case: int ,     __snake_case: PreTrainedTokenizer ,     )    ->		Any:

       """simple docstring"""





       __magic_name__      				=    {label: i for i, label in enumerate(__snake_case  )}

       __magic_name__      				=    []
       for ex_index, example in tqdm.tqdm(enumerate(__snake_case  ) ,     desc='convert examples to features'  ):
              if ex_index % 1_0_0_0_0 == 0:
                     logger.info('Writing example %d' % (ex_index)  )

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

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

              __magic_name__      				=    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


snake_case : str     		=		{
    """hans""": 3,
}






snake_case : List[str]     		=		{
    """hans""": HansProcessor,
}







"""simple docstring"""
from ..utils import DummyObject, requires_backends



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	List[Any]				,    *UpperCamelCase_ :	str				,    **UpperCamelCase_ :	Any ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Optional[int]				,    *UpperCamelCase_ :	Tuple				,    **UpperCamelCase_ :	List[Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Dict ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	List[Any]				,    *UpperCamelCase_ :	str				,    **UpperCamelCase_ :	str ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Optional[int]				,    *UpperCamelCase_ :	Optional[int]				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Tuple				,    *UpperCamelCase_ :	List[str]				,    **UpperCamelCase_ :	Optional[Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	Optional[int]				,    *UpperCamelCase_ :	List[Any]				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	List[str]				,    **UpperCamelCase_ :	Tuple ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	Tuple				,    *UpperCamelCase_ :	Any				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )



class        UpperCamelCase__ (				metaclass=a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""flax""", """transformers"""]







    def __init__(		self :	Optional[Any]				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Dict ):





           '''simple docstring'''

           requires_backends(self				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Dict				,    **UpperCamelCase_ :	List[str] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )







    @classmethod
    def 				a__			(		cls :	str				,    *UpperCamelCase_ :	Optional[Any]				,    **UpperCamelCase_ :	Union[str, Any] ):





           '''simple docstring'''

           requires_backends(cls				,    ['flax', 'transformers'] )