



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


_lowerCamelCase	:       Tuple      	=	logging.getLogger(__name__)

@dataclass(frozen=_a		)
class  SCREAMING_SNAKE_CASE :






    """simple docstring"""





    _SCREAMING_SNAKE_CASE	  =  42
    _SCREAMING_SNAKE_CASE	  =  42
    _SCREAMING_SNAKE_CASE	  =  None
    _SCREAMING_SNAKE_CASE	  =  None
    _SCREAMING_SNAKE_CASE	  =  None

@dataclass(frozen=_a		)
class  SCREAMING_SNAKE_CASE :






    """simple docstring"""





    _SCREAMING_SNAKE_CASE	  =  42
    _SCREAMING_SNAKE_CASE	  =  None
    _SCREAMING_SNAKE_CASE	  =  None
    _SCREAMING_SNAKE_CASE	  =  None
    _SCREAMING_SNAKE_CASE	  =  None


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

   class  SCREAMING_SNAKE_CASE (  _a		):






       """simple docstring"""





       _SCREAMING_SNAKE_CASE	  =  42






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


          """simple docstring"""







          UpperCamelCase					=  hans_processors[task]()

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

          # Make sure only the first process in distributed training processes the dataset,
          # and the others will use the cache.
          UpperCamelCase					=  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}""" )
                UpperCamelCase					=  torch.load(UpperCamelCase__ )
             else:
                logger.info(f"""Creating features from dataset file at {data_dir}""" )

                UpperCamelCase					=  (
                    processor.get_dev_examples(UpperCamelCase__ ) if evaluate else processor.get_train_examples(UpperCamelCase__ )
                )

                logger.info('Training examples: %s'  ,						len(UpperCamelCase__ ) )
                UpperCamelCase					=  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      :					Any ):


          """simple docstring"""







          return len(self.features )






       def __getitem__(  self      :					Optional[Any]  ,						UpperCamelCase__      :					Union[str, Any] ):


          """simple docstring"""







          return self.features[i]






       def  A       (  self      :					Any ):


          """simple docstring"""







          return self.label_list


if is_tf_available():
   import tensorflow as tf

   class  SCREAMING_SNAKE_CASE :






       """simple docstring"""





       _SCREAMING_SNAKE_CASE	  =  42






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


          """simple docstring"""







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

          UpperCamelCase					=  processor.get_dev_examples(UpperCamelCase__ ) if evaluate else processor.get_train_examples(UpperCamelCase__ )
          UpperCamelCase					=  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,
                )

          UpperCamelCase					=  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      :					Dict ):


          """simple docstring"""







          return self.dataset






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


          """simple docstring"""







          return len(self.features )






       def __getitem__(  self      :					List[str]  ,						UpperCamelCase__      :					str ):


          """simple docstring"""







          return self.features[i]






       def  A       (  self      :					Tuple ):


          """simple docstring"""







          return self.label_list

class  SCREAMING_SNAKE_CASE (  _a		):






    """simple docstring"""





    def  A       (  self      :					List[Any]  ,						UpperCamelCase__      :					Optional[int] ):


       """simple docstring"""







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






    def  A       (  self      :					Optional[int]  ,						UpperCamelCase__      :					Union[str, Any] ):


       """simple docstring"""







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






    def  A       (  self      :					Optional[Any] ):


       """simple docstring"""







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






    def  A       (  self      :					Optional[Any]  ,						UpperCamelCase__      :					Union[str, Any]  ,						UpperCamelCase__      :					str ):


       """simple docstring"""







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







def 				__lowerCamelCase				(   A__    ,						A__    ,						A__    ,						A__    ,						)       ->	Union[str, Any]:






   """simple docstring"""
   UpperCamelCase					=  {label: i for i, label in enumerate(A__    )}

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

      UpperCamelCase					=  tokenizer(
          example.text_a    ,						example.text_b    ,						add_special_tokens=A__    ,						max_length=A__    ,						padding='max_length'    ,						truncation=A__    ,						return_overflowing_tokens=A__    ,						)

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

      UpperCamelCase					=  int(example.pairID    )

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

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

   return features


_lowerCamelCase	:       Any      	=	{
    "hans": 3,
}
_lowerCamelCase	:       Optional[int]      	=	{
    "hans": HansProcessor,
}






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_lowerCamelCase	:       List[Any]      	=	{
    "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"],
    "tokenization_m2m_100": ["M2M100Tokenizer"],
}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _lowerCamelCase	:       int      	=	[
       "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST",
       "M2M100ForConditionalGeneration",
       "M2M100Model",
       "M2M100PreTrainedModel",
   ]


if TYPE_CHECKING:
   from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
   from .tokenization_mam_aaa import MaMaaaTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_mam_aaa import (
          M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
          MaMaaaForConditionalGeneration,
          MaMaaaModel,
          MaMaaaPreTrainedModel,
      )


else:
   import sys

   _lowerCamelCase	:       Optional[int]      	=	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

