





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


A_		:  Any		 =						logging.getLogger(__name__)
@dataclass(frozen=__SCREAMING_SNAKE_CASE							)
class 					__snake_case  :






   '''simple docstring'''





   lowerCamelCase__    							=      42
   lowerCamelCase__    							=      42
   lowerCamelCase__    							=      None
   lowerCamelCase__    							=      None
   lowerCamelCase__    							=      None



@dataclass(frozen=__SCREAMING_SNAKE_CASE							)
class 					__snake_case  :






   '''simple docstring'''





   lowerCamelCase__    							=      42
   lowerCamelCase__    							=      None
   lowerCamelCase__    							=      None
   lowerCamelCase__    							=      None
   lowerCamelCase__    							=      None


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






       '''simple docstring'''





       lowerCamelCase__    							=      42

       def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE = None	,		__SCREAMING_SNAKE_CASE=False	,		__SCREAMING_SNAKE_CASE = False	,		):
        snake_case__     :					Dict    =  hans_processors[task]()

        snake_case__     :					Tuple    =  os.path.join(
            __SCREAMING_SNAKE_CASE	,		"""cached_{}_{}_{}_{}""".format(
                """dev""" if evaluate else """train"""	,		tokenizer.__class__.__name__	,		str(__SCREAMING_SNAKE_CASE       )	,		__SCREAMING_SNAKE_CASE	,		)	,		)
        snake_case__     :					Union[str, 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__     :					List[str]    =  label_list

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

          snake_case__     :					List[Any]    =  (
              processor.get_dev_examples(__SCREAMING_SNAKE_CASE       ) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE       )
          )

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

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

       def __getitem__(							self	,		__SCREAMING_SNAKE_CASE       ):
        return self.features[i]


       def 		__UpperCamelCase						(							self       ):
        return self.label_list


if is_tf_available():
    import tensorflow as tf
    class 					__snake_case  :






       '''simple docstring'''





       lowerCamelCase__    							=      42

       def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE = 1_2_8	,		__SCREAMING_SNAKE_CASE=False	,		__SCREAMING_SNAKE_CASE = False	,		):
        snake_case__     :					Tuple    =  hans_processors[task]()
        snake_case__     :					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[Any]    =  label_list[2], label_list[1]
        snake_case__     :					int    =  label_list

        snake_case__     :					Union[str, Any]    =  processor.get_dev_examples(__SCREAMING_SNAKE_CASE       ) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE       )
        snake_case__     :					Optional[Any]    =  hans_convert_examples_to_features(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

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

          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__     :					Optional[int]    =  tf.data.Dataset.from_generator(
            __SCREAMING_SNAKE_CASE	,		(
                {
                    """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       ):
        return self.dataset

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

       def __getitem__(							self	,		__SCREAMING_SNAKE_CASE       ):
        return self.features[i]


       def 		__UpperCamelCase						(							self       ):
        return self.label_list
class 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE       ):
    return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE	,		"""heuristics_train_set.txt"""       )       )	,		"""train"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE       ):
    return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE	,		"""heuristics_evaluation_set.txt"""       )       )	,		"""dev"""       )

   def 		__UpperCamelCase						(							self       ):
    return ["contradiction", "entailment", "neutral"]


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



def        UpperCamelCase__		(						__magic_name__  :   List[InputExample]		,       __magic_name__  :   List[str]		,       __magic_name__  :   int		,       __magic_name__  :   PreTrainedTokenizer		,       )				->			Optional[Any]:
 '''simple docstring'''






 snake_case__     :					Optional[int]    =  {label: i for i, label in enumerate(__magic_name__      )}

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

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

  snake_case__     :					Tuple    =  label_map[example.label] if example.label in label_map else 0

  snake_case__     :					Tuple    =  int(example.pairID      )

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

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

 return features


A_		:  int		 =						{
    "hans": 3,
}





A_		:  Tuple		 =						{
    "hans": HansProcessor,
}











'''simple docstring'''

# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__lowerCAmelCase				:      Union[str, Any]						      ={"configuration_mra": ["MRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "MraConfig"]}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __lowerCAmelCase				:      int						      =[
      "MRA_PRETRAINED_MODEL_ARCHIVE_LIST",
      "MraForMaskedLM",
      "MraForMultipleChoice",
      "MraForQuestionAnswering",
      "MraForSequenceClassification",
      "MraForTokenClassification",
      "MraLayer",
      "MraModel",
      "MraPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_mra import (
        MRA_PRETRAINED_MODEL_ARCHIVE_LIST,
        MraForMaskedLM,
        MraForMultipleChoice,
        MraForQuestionAnswering,
        MraForSequenceClassification,
        MraForTokenClassification,
        MraLayer,
        MraModel,
        MraPreTrainedModel,
    )
else:
  import sys

  __lowerCAmelCase				:      Dict						      =_LazyModule(__name__, globals()["__file__"], _import_structure)



