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




@dataclass(frozen=__a    )
class  __magic_name__  :





   """simple docstring"""




   lowerCAmelCase      :    str
   lowerCAmelCase      :    str
   lowerCAmelCase      :    Optional[str]  			=   None
   lowerCAmelCase      :    Optional[str]  			=   None
   lowerCAmelCase      :    Optional[str]  			=   None




@dataclass(frozen=__a    )
class  __magic_name__  :





   """simple docstring"""




   lowerCAmelCase      :    List[int]
   lowerCAmelCase      :    Optional[List[int]]  			=   None
   lowerCAmelCase      :    Optional[List[int]]  			=   None
   lowerCAmelCase      :    Optional[Union[int, float]]  			=   None
   lowerCAmelCase      :    Optional[int]  			=   None


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




  class  __magic_name__  (		__a    ):





     """simple docstring"""




     lowerCAmelCase      :    List[InputFeatures]



     def __init__(	self					:  Dict		,  _lowercase					:  str		,  _lowercase					:  PreTrainedTokenizer		,  _lowercase					:  str		,  _lowercase					:  Optional[int] = None		,  _lowercase					:  Tuple=False		,  _lowercase					:  bool = False		,  ):



      """simple docstring"""



      _UpperCamelCase:     Union[str, Any]							= hans_processors[task]()

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

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

        _UpperCamelCase:     List[str]							= (
            processor.get_dev_examples(_lowercase							) if evaluate else processor.get_train_examples(_lowercase							)
        )

        logger.info('''Training examples: %s'''		,  len(_lowercase							)							)
        _UpperCamelCase:     Optional[int]							= hans_convert_examples_to_features(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase							)
        logger.info('''Saving features into cached file %s'''		,  _lowercase							)
        torch.save(self.features		,  _lowercase							)



     def __len__(	self					:  Optional[Any]							):



      """simple docstring"""



      return len(self.features							)



     def __getitem__(	self					:  Optional[int]		,  _lowercase					:  str							):



      """simple docstring"""



      return self.features[i]



     def   lowerCAmelCase						(	self					:  Any							):



      """simple docstring"""



      return self.label_list


if is_tf_available():
  import tensorflow as tf




  class  __magic_name__  :





     """simple docstring"""




     lowerCAmelCase      :    List[InputFeatures]



     def __init__(	self					:  Optional[int]		,  _lowercase					:  str		,  _lowercase					:  PreTrainedTokenizer		,  _lowercase					:  str		,  _lowercase					:  Optional[int] = 128		,  _lowercase					:  List[str]=False		,  _lowercase					:  bool = False		,  ):



      """simple docstring"""



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

      _UpperCamelCase:     Any							= processor.get_dev_examples(_lowercase							) if evaluate else processor.get_train_examples(_lowercase							)
      _UpperCamelCase:     int							= hans_convert_examples_to_features(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase							)

      def gen():
       for ex_index, ex in tqdm.tqdm(enumerate(self.features							)		,  desc='''convert examples to features'''							):
        if ex_index % 10_000 == 0:
         logger.info('''Writing example %d of %d''' % (ex_index, len(_lowercase							))							)

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

      _UpperCamelCase:     Optional[int]							= tf.data.Dataset.from_generator(
          _lowercase		,  (
              {
                  '''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[int]							):



      """simple docstring"""



      return self.dataset



     def __len__(	self					:  int							):



      """simple docstring"""



      return len(self.features							)



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



      """simple docstring"""



      return self.features[i]



     def   lowerCAmelCase						(	self					:  Dict							):



      """simple docstring"""



      return self.label_list




class  __magic_name__  (		__a    ):





   """simple docstring"""




   def   lowerCAmelCase						(	self					:  Tuple		,  _lowercase					:  Any							):



    """simple docstring"""



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



   def   lowerCAmelCase						(	self					:  Union[str, Any]		,  _lowercase					:  List[Any]							):



    """simple docstring"""



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



   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



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



   def   lowerCAmelCase						(	self					:  Tuple		,  _lowercase					:  List[str]		,  _lowercase					:  List[str]							):



    """simple docstring"""



    _UpperCamelCase:     int							= []
    for i, line in enumerate(_lowercase							):
     if i == 0:
      continue
     _UpperCamelCase:     List[str]							= '''%s-%s''' % (set_type, line[0])
     _UpperCamelCase:     Optional[Any]							= line[5]
     _UpperCamelCase:     List[str]							= line[6]
     _UpperCamelCase:     Dict							= line[7][2:] if line[7].startswith('''ex'''							) else line[7]
     _UpperCamelCase:     Tuple							= line[0]
     examples.append(InputExample(guid=_lowercase		,  text_a=_lowercase		,  text_b=_lowercase		,  label=_lowercase		,  pairID=_lowercase							)							)
    return examples




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







 '''simple docstring'''
 _UpperCamelCase:     List[Any]							= {label: i for i, label in enumerate(lowercase		)}

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

  _UpperCamelCase:     Union[str, Any]							= tokenizer(
      example.text_a  ,					example.text_b  ,					add_special_tokens=lowercase  ,					max_length=lowercase  ,					padding='''max_length'''  ,					truncation=lowercase  ,					return_overflowing_tokens=lowercase  ,					)

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

  _UpperCamelCase:     Union[str, Any]							= 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


UpperCAmelCase_			    =							{
    '''hans''': 3,
}





UpperCAmelCase_			    =							{
    '''hans''': HansProcessor,
}
# Copyright 2021 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.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


UpperCAmelCase_			    =							[
    '''EAGER''',
    '''AOT_EAGER''',
    '''INDUCTOR''',
    '''NVFUSER''',
    '''AOT_NVFUSER''',
    '''AOT_CUDAGRAPHS''',
    '''OFI''',
    '''FX2TRT''',
    '''ONNXRT''',
    '''IPEX''',
]




def       lowerCAmelCase_     (		lowercase:							str  ,					lowercase:							Optional[Any]=None  ,					lowercase:							List[str]=None  ,					lowercase:							List[str]=None		)    ->				Tuple:







 '''simple docstring'''
 _UpperCamelCase:     int							= True
 while ask_again:
  _UpperCamelCase:     Any							= input(lowercase		)
  try:
   if default is not None and len(lowercase		) == 0:
    return default
   return convert_value(lowercase		) if convert_value is not None else result
  except Exception:
   if error_message is not None:
    print(lowercase		)




def       lowerCAmelCase_     (		lowercase:							List[Any]  ,					lowercase:							Union[str, Any]=[]  ,					lowercase:							Union[str, Any]=None  ,					lowercase:							Union[str, Any]=0		)    ->				List[Any]:







 '''simple docstring'''
 _UpperCamelCase:     List[str]							= BulletMenu(lowercase  ,					lowercase		)
 _UpperCamelCase:     List[Any]							= menu.run(default_choice=lowercase		)
 return convert_value(lowercase		) if convert_value is not None else result




def       lowerCAmelCase_     (		lowercase:							Dict		)    ->				Optional[int]:







 '''simple docstring'''
 _UpperCamelCase:     int							= int(lowercase		)
 return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value]		)




def       lowerCAmelCase_     (		lowercase:							Tuple		)    ->				Any:







 '''simple docstring'''
 _UpperCamelCase:     str							= int(lowercase		)
 return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value]		)




def       lowerCAmelCase_     (		lowercase:							Union[str, Any]		)    ->				Optional[int]:







 '''simple docstring'''
 _UpperCamelCase:     Tuple							= int(lowercase		)
 return DynamoBackend(DYNAMO_BACKENDS[value]		).value




def       lowerCAmelCase_     (		lowercase:							int		)    ->				Any:







 '''simple docstring'''
 _UpperCamelCase:     int							= int(lowercase		)
 return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value]		)




def       lowerCAmelCase_     (		lowercase:							List[str]		)    ->				List[str]:







 '''simple docstring'''
 _UpperCamelCase:     Dict							= int(lowercase		)
 return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value]		)




def       lowerCAmelCase_     (		lowercase:							Any		)    ->				List[str]:







 '''simple docstring'''
 return {"yes": True, "no": False}[value.lower()]





class  __magic_name__  (		argparse.RawDescriptionHelpFormatter    ):





   """simple docstring"""




   def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  Optional[int]		,  _lowercase					:  List[Any]		,  _lowercase					:  Dict		,  _lowercase					:  Any							):



    """simple docstring"""



    _UpperCamelCase:     Optional[Any]							= super()._format_usage(_lowercase		,  _lowercase		,  _lowercase		,  _lowercase							)
    _UpperCamelCase:     int							= usage.replace('''<command> [<args>] '''		,  ''''''							)
    return usage