


"""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   			=    logging.getLogger(__name__)







@dataclass(frozen=snake_case_	)
class _lowerCAmelCase				:
   __UpperCAmelCase :       str
   __UpperCAmelCase :       str
   __UpperCAmelCase :       Optional[str]							  =  None
   __UpperCAmelCase :       Optional[str]							  =  None
   __UpperCAmelCase :       Optional[str]							  =  None







@dataclass(frozen=snake_case_	)
class _lowerCAmelCase				:
   __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_	):
     __UpperCAmelCase :       List[InputFeatures]







     def __init__(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__ = None						,	UpperCamelCase__=False						,	UpperCamelCase__ = False						,	)	->		Optional[Any]:

         '''simple docstring'''




         snake_case    :      Dict      					=    hans_processors[task]()

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

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

                 snake_case    :      Dict      					=    (
                     processor.get_dev_examples(UpperCamelCase__						) if evaluate else processor.get_train_examples(UpperCamelCase__						)
                 )

                 logger.info("Training examples: %s"						,	len(UpperCamelCase__						)						)
                 snake_case    :      List[str]      					=    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						)	->		List[str]:

         '''simple docstring'''




         return len(self.features						)







     def __getitem__(	self						,	UpperCamelCase__						)	->		InputFeatures:

         '''simple docstring'''




         return self.features[i]







     def  lowerCamelCase		(	self						)	->		Tuple:

         '''simple docstring'''




         return self.label_list


if is_tf_available():
  import tensorflow as tf







  class _lowerCAmelCase				:
     __UpperCAmelCase :       List[InputFeatures]







     def __init__(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__ = 128						,	UpperCamelCase__=False						,	UpperCamelCase__ = False						,	)	->		Tuple:

         '''simple docstring'''




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

         snake_case    :      Optional[int]      					=    processor.get_dev_examples(UpperCamelCase__						) if evaluate else processor.get_train_examples(UpperCamelCase__						)
         snake_case    :      Dict      					=    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_0000 == 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,
                 )

         snake_case    :      str      					=    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  lowerCamelCase		(	self						)	->		Optional[Any]:

         '''simple docstring'''




         return self.dataset







     def __len__(	self						)	->		Dict:

         '''simple docstring'''




         return len(self.features						)







     def __getitem__(	self						,	UpperCamelCase__						)	->		InputFeatures:

         '''simple docstring'''




         return self.features[i]







     def  lowerCamelCase		(	self						)	->		List[Any]:

         '''simple docstring'''




         return self.label_list







class _lowerCAmelCase				(      snake_case_	):







   def  lowerCamelCase		(	self						,	UpperCamelCase__						)	->		Dict:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						)	->		Union[str, Any]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						)	->		List[Any]:

       '''simple docstring'''




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







def   __lowerCAmelCase						(						lowercase  :  List[InputExample]						,							lowercase  :  List[str]						,							lowercase  :  int						,							lowercase  :  PreTrainedTokenizer						,							)	->						Optional[Any]:


    """simple docstring"""





    snake_case    :      str      					=    {label: i for i, label in enumerate(lowercase )}

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

        snake_case    :      Dict      					=    tokenizer(
            example.text_a						,							example.text_b						,							add_special_tokens=lowercase						,							max_length=lowercase						,							padding="max_length"						,							truncation=lowercase						,							return_overflowing_tokens=lowercase						,							)

        snake_case    :      str      					=    label_map[example.label] if example.label in label_map else 0

        snake_case    :      str      					=    int(example.pairID )

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

    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   			=    {
    """hans""": 3,
}


__snake_case   			=    {
    """hans""": HansProcessor,
}









"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__snake_case   			=    logging.get_logger(__name__)

__snake_case   			=    {
    """facebook/xmod-base""": """https://huggingface.co/facebook/xmod-base/resolve/main/config.json""",
    """facebook/xmod-large-prenorm""": """https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json""",
    """facebook/xmod-base-13-125k""": """https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-125k""": """https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-195k""": """https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json""",
    """facebook/xmod-base-60-125k""": """https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json""",
    """facebook/xmod-base-60-265k""": """https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json""",
    """facebook/xmod-base-75-125k""": """https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json""",
    """facebook/xmod-base-75-269k""": """https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json""",
}







class _lowerCAmelCase				(      snake_case_	):
   __UpperCAmelCase :       Optional[Any]							  =  '''xmod'''







   def __init__(	self						,	UpperCamelCase__=3_0522						,	UpperCamelCase__=768						,	UpperCamelCase__=12						,	UpperCamelCase__=12						,	UpperCamelCase__=3072						,	UpperCamelCase__="gelu"						,	UpperCamelCase__=0.1						,	UpperCamelCase__=0.1						,	UpperCamelCase__=512						,	UpperCamelCase__=2						,	UpperCamelCase__=0.02						,	UpperCamelCase__=1e-12						,	UpperCamelCase__=1						,	UpperCamelCase__=0						,	UpperCamelCase__=2						,	UpperCamelCase__="absolute"						,	UpperCamelCase__=True						,	UpperCamelCase__=None						,	UpperCamelCase__=False						,	UpperCamelCase__=2						,	UpperCamelCase__=False						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=("en_XX",)						,	UpperCamelCase__=None						,	**UpperCamelCase__						,	)	->		int:

       '''simple docstring'''




       super().__init__(pad_token_id=UpperCamelCase__						,	bos_token_id=UpperCamelCase__						,	eos_token_id=UpperCamelCase__						,	**UpperCamelCase__						)

       snake_case    :      List[Any]      					=    vocab_size
       snake_case    :      List[Any]      					=    hidden_size
       snake_case    :      Union[str, Any]      					=    num_hidden_layers
       snake_case    :      Any      					=    num_attention_heads
       snake_case    :      List[str]      					=    hidden_act
       snake_case    :      Union[str, Any]      					=    intermediate_size
       snake_case    :      int      					=    hidden_dropout_prob
       snake_case    :      Any      					=    attention_probs_dropout_prob
       snake_case    :      Optional[int]      					=    max_position_embeddings
       snake_case    :      Tuple      					=    type_vocab_size
       snake_case    :      List[str]      					=    initializer_range
       snake_case    :      int      					=    layer_norm_eps
       snake_case    :      Optional[Any]      					=    position_embedding_type
       snake_case    :      int      					=    use_cache
       snake_case    :      Dict      					=    classifier_dropout
       snake_case    :      Dict      					=    pre_norm
       snake_case    :      Union[str, Any]      					=    adapter_reduction_factor
       snake_case    :      Any      					=    adapter_layer_norm
       snake_case    :      Optional[int]      					=    adapter_reuse_layer_norm
       snake_case    :      List[Any]      					=    ln_before_adapter
       snake_case    :      str      					=    list(UpperCamelCase__						)
       snake_case    :      int      					=    default_language







class _lowerCAmelCase				(      snake_case_	):







   @property
   def  lowerCamelCase		(	self						)	->		Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task == "multiple-choice":
           snake_case    :      Optional[int]      					=    {0: "batch", 1: "choice", 2: "sequence"}
       else:
           snake_case    :      Optional[Any]      					=    {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
           ]						)





