
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__    :		Dict          =   logging.getLogger(__name__)



@dataclass(frozen=UpperCAmelCase_							)
class       _snake_case   :
 __lowerCAmelCase  :     str
 __lowerCAmelCase  :     str
 __lowerCAmelCase  :     Optional[str]        =			None
 __lowerCAmelCase  :     Optional[str]        =			None
 __lowerCAmelCase  :     Optional[str]        =			None



@dataclass(frozen=UpperCAmelCase_							)
class       _snake_case   :
 __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       _snake_case   (	UpperCAmelCase_							):
      __lowerCAmelCase  :     List[InputFeatures]


      def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_ = None					,  SCREAMING_SNAKE_CASE_=False					,  SCREAMING_SNAKE_CASE_ = False					,  ):


        '''simple docstring'''




        lowercase__  :   Any    		=      hans_processors[task]()

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

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

            lowercase__  :   str    		=      (
                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_))
            lowercase__  :   str    		=      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):


        '''simple docstring'''




        return len(self.features)


      def __getitem__(					self					,  SCREAMING_SNAKE_CASE_):


        '''simple docstring'''




        return self.features[i]



      def      lowercase__     (					self):


        '''simple docstring'''




        return self.label_list


if is_tf_available():
     import tensorflow as tf



     class       _snake_case   :
      __lowerCAmelCase  :     List[InputFeatures]


      def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_ = 1_28					,  SCREAMING_SNAKE_CASE_=False					,  SCREAMING_SNAKE_CASE_ = False					,  ):


        '''simple docstring'''




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

        lowercase__  :   List[str]    		=      processor.get_dev_examples(SCREAMING_SNAKE_CASE_) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_)
        lowercase__  :   Optional[int]    		=      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_00_00 == 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,
            )

        lowercase__  :   List[Any]    		=      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      lowercase__     (					self):


        '''simple docstring'''




        return self.dataset


      def __len__(					self):


        '''simple docstring'''




        return len(self.features)


      def __getitem__(					self					,  SCREAMING_SNAKE_CASE_):


        '''simple docstring'''




        return self.features[i]



      def      lowercase__     (					self):


        '''simple docstring'''




        return self.label_list



class       _snake_case   (	UpperCAmelCase_							):


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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


 def      lowercase__     (					self):


   '''simple docstring'''




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



 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Dict    		=      []
   for i, line in enumerate(SCREAMING_SNAKE_CASE_):
     if i == 0:
       continue
     lowercase__  :   Dict    		=      """%s-%s""" % (set_type, line[0])
     lowercase__  :   Union[str, Any]    		=      line[5]
     lowercase__  :   Tuple    		=      line[6]
     lowercase__  :   Dict    		=      line[7][2:] if line[7].startswith("""ex""") else line[7]
     lowercase__  :   Dict    		=      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	(      lowercase_ , lowercase_ , lowercase_ , lowercase_ , )		->	List[Any]:


  '''simple docstring'''






  lowercase__  :   int    		=      {label: i for i, label in enumerate(lowercase_   )}

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

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

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

    lowercase__  :   Tuple    		=      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


lowerCamelCase__    :		Any          =   {
    """hans""": 3,
}



lowerCamelCase__    :		Any          =   {
    """hans""": HansProcessor,
}








import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..bit import BitConfig


lowerCamelCase__    :		List[str]          =   logging.get_logger(__name__)

lowerCamelCase__    :		str          =   {
    """Intel/dpt-large""": """https://huggingface.co/Intel/dpt-large/resolve/main/config.json""",
    # See all DPT models at https://huggingface.co/models?filter=dpt
}



class       _snake_case   (	UpperCAmelCase_							):
 __lowerCAmelCase  :     Optional[int]        =			'dpt'


 def __init__(					self					,  SCREAMING_SNAKE_CASE_=7_68					,  SCREAMING_SNAKE_CASE_=12					,  SCREAMING_SNAKE_CASE_=12					,  SCREAMING_SNAKE_CASE_=30_72					,  SCREAMING_SNAKE_CASE_="gelu"					,  SCREAMING_SNAKE_CASE_=0.0					,  SCREAMING_SNAKE_CASE_=0.0					,  SCREAMING_SNAKE_CASE_=0.0_2					,  SCREAMING_SNAKE_CASE_=1E-12					,  SCREAMING_SNAKE_CASE_=3_84					,  SCREAMING_SNAKE_CASE_=16					,  SCREAMING_SNAKE_CASE_=3					,  SCREAMING_SNAKE_CASE_=False					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=[2, 5, 8, 11]					,  SCREAMING_SNAKE_CASE_="project"					,  SCREAMING_SNAKE_CASE_=[4, 2, 1, 0.5]					,  SCREAMING_SNAKE_CASE_=[96, 1_92, 3_84, 7_68]					,  SCREAMING_SNAKE_CASE_=2_56					,  SCREAMING_SNAKE_CASE_=-1					,  SCREAMING_SNAKE_CASE_=False					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=0.4					,  SCREAMING_SNAKE_CASE_=2_55					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=[1, 10_24, 24, 24]					,  SCREAMING_SNAKE_CASE_=[0, 1]					,  SCREAMING_SNAKE_CASE_=None					,  **SCREAMING_SNAKE_CASE_					,  ):


   '''simple docstring'''




   super().__init__(**SCREAMING_SNAKE_CASE_)

   lowercase__  :   List[str]    		=      hidden_size
   lowercase__  :   str    		=      is_hybrid

   if self.is_hybrid:
     if backbone_config is None:
       logger.info("""Initializing the config with a `BiT` backbone.""")
       lowercase__  :   Any    		=      {
           """global_padding""": """same""",
           """layer_type""": """bottleneck""",
           """depths""": [3, 4, 9],
           """out_features""": ["""stage1""", """stage2""", """stage3"""],
           """embedding_dynamic_padding""": True,
       }
       lowercase__  :   Optional[Any]    		=      BitConfig(**SCREAMING_SNAKE_CASE_)
     elif isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
       logger.info("""Initializing the config with a `BiT` backbone.""")
       lowercase__  :   List[Any]    		=      BitConfig(**SCREAMING_SNAKE_CASE_)
     elif isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):
       lowercase__  :   int    		=      backbone_config
     else:
       raise ValueError(
           f'backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.')

     lowercase__  :   Optional[Any]    		=      backbone_featmap_shape
     lowercase__  :   List[str]    		=      neck_ignore_stages

     if readout_type != "project":
       raise ValueError("""Readout type must be 'project' when using `DPT-hybrid` mode.""")
   else:
     lowercase__  :   Optional[int]    		=      None
     lowercase__  :   Union[str, Any]    		=      None
     lowercase__  :   Tuple    		=      []

   lowercase__  :   List[str]    		=      num_hidden_layers
   lowercase__  :   Union[str, Any]    		=      num_attention_heads
   lowercase__  :   List[Any]    		=      intermediate_size
   lowercase__  :   Optional[Any]    		=      hidden_act
   lowercase__  :   int    		=      hidden_dropout_prob
   lowercase__  :   str    		=      attention_probs_dropout_prob
   lowercase__  :   Dict    		=      initializer_range
   lowercase__  :   Tuple    		=      layer_norm_eps
   lowercase__  :   List[str]    		=      image_size
   lowercase__  :   Tuple    		=      patch_size
   lowercase__  :   Tuple    		=      num_channels
   lowercase__  :   Dict    		=      qkv_bias
   lowercase__  :   List[Any]    		=      backbone_out_indices
   if readout_type not in ["ignore", "add", "project"]:
     raise ValueError("""Readout_type must be one of ['ignore', 'add', 'project']""")
   lowercase__  :   Dict    		=      readout_type
   lowercase__  :   Tuple    		=      reassemble_factors
   lowercase__  :   Any    		=      neck_hidden_sizes
   lowercase__  :   Dict    		=      fusion_hidden_size
   lowercase__  :   Tuple    		=      head_in_index
   lowercase__  :   Tuple    		=      use_batch_norm_in_fusion_residual
   # auxiliary head attributes (semantic segmentation)
   lowercase__  :   Tuple    		=      use_auxiliary_head
   lowercase__  :   int    		=      auxiliary_loss_weight
   lowercase__  :   Union[str, Any]    		=      semantic_loss_ignore_index
   lowercase__  :   str    		=      semantic_classifier_dropout



 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      copy.deepcopy(self.__dict__)

   if output["backbone_config"] is not None:
     lowercase__  :   Optional[Any]    		=      self.backbone_config.to_dict()

   lowercase__  :   Dict    		=      self.__class__.model_type
   return output






