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


__SCREAMING_SNAKE_CASE							=     logging.getLogger(__name__)





@dataclass(frozen=__snake_case       )
class 				a__					:
     UpperCAmelCase__         =      42
     UpperCAmelCase__         =      42
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      None





@dataclass(frozen=__snake_case       )
class 				a__					:
     UpperCAmelCase__         =      42
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      None
     UpperCAmelCase__         =      None


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





 class 				a__					(						__snake_case       ):
      UpperCAmelCase__         =      42



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







       '''simple docstring'''



       UpperCamelCase_     :					Tuple   	=hans_processors[task]()

       UpperCamelCase_     :					List[Any]   	=os.path.join(
           __lowerCamelCase      ,  'cached_{}_{}_{}_{}'.format(
               'dev' if evaluate else 'train'      ,  tokenizer.__class__.__name__      ,  str(__lowerCamelCase   )      ,  __lowerCamelCase      ,  )      ,  )
       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_     :					Optional[int]   	=label_list[2], label_list[1]
       UpperCamelCase_     :					Any   	=label_list

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

         UpperCamelCase_     :					Optional[Any]   	=(
             processor.get_dev_examples(__lowerCamelCase   ) if evaluate else processor.get_train_examples(__lowerCamelCase   )
         )

         logger.info('Training examples: %s'      ,  len(__lowerCamelCase   )   )
         UpperCamelCase_     :					Tuple   	=hans_convert_examples_to_features(__lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase   )
         logger.info('Saving features into cached file %s'      ,  __lowerCamelCase   )
         torch.save(self.features      ,  __lowerCamelCase   )



      def __len__(							self :List[str]   ):







       '''simple docstring'''



       return len(self.features   )



      def __getitem__(							self :List[str]      ,  _lowerCamelCase :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 				a__					:
      UpperCAmelCase__         =      42



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







       '''simple docstring'''



       UpperCamelCase_     :					List[str]   	=hans_processors[task]()
       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_     :					Tuple   	=label_list[2], label_list[1]
       UpperCamelCase_     :					Optional[Any]   	=label_list

       UpperCamelCase_     :					Optional[int]   	=processor.get_dev_examples(__lowerCamelCase   ) if evaluate else processor.get_train_examples(__lowerCamelCase   )
       UpperCamelCase_     :					Tuple   	=hans_convert_examples_to_features(__lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase   )

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

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







       '''simple docstring'''



       return self.dataset



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







       '''simple docstring'''



       return len(self.features   )



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







       '''simple docstring'''



       return self.features[i]




      def  lowerCamelCase_						(							self :Any   ):







       '''simple docstring'''



       return self.label_list





class 				a__					(						__snake_case       ):



     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :str   ):







      '''simple docstring'''



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



     def  lowerCamelCase_						(							self :List[Any]      ,  _lowerCamelCase :Any   ):







      '''simple docstring'''



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



     def  lowerCamelCase_						(							self :List[Any]   ):







      '''simple docstring'''



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




     def  lowerCamelCase_						(							self :int      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :Union[str, Any]   ):







      '''simple docstring'''



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




def       A_  (				__lowercase						,	__lowercase						,	__lowercase						,	__lowercase						,	):
 UpperCamelCase_     :					str   	={label: i for i, label in enumerate(lowerCAmelCase_	)}

 UpperCamelCase_     :					Optional[Any]   	=[]
 for ex_index, example in tqdm.tqdm(enumerate(lowerCAmelCase_	)						,	desc='convert examples to features'	):
  if ex_index % 1_00_00 == 0:
   logger.info('Writing example %d' % (ex_index)	)

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

  UpperCamelCase_     :					Optional[int]   	=label_map[example.label] if example.label in label_map else 0

  UpperCamelCase_     :					Tuple   	=int(example.pairID	)

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

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

 return features


__SCREAMING_SNAKE_CASE							=     {
    'hans': 3,
}





__SCREAMING_SNAKE_CASE							=     {
    'hans': HansProcessor,
}




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a	 =      logging.get_logger(__name__)

__a	 =      {
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_text_model'''
      UpperCamelCase										=							['''past_key_values''']
      UpperCamelCase										=							{
          '''hidden_size''': '''hidden_size''',
          '''num_attention_heads''': '''num_heads''',
          '''num_hidden_layers''': '''num_layers''',
      }





      def __init__(		self    :       Optional[Any]       ,							__lowerCamelCase    :       Optional[int]=5_0_2_4_4       ,							__lowerCamelCase    :       int=7_6_8       ,							__lowerCamelCase    :       Union[str, Any]=6_4       ,							__lowerCamelCase    :       Dict=2_0_4_8       ,							__lowerCamelCase    :       int=1_2       ,							__lowerCamelCase    :       Any=1_2       ,							__lowerCamelCase    :       Union[str, Any]=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							__lowerCamelCase    :       Union[str, Any]=0.1       ,							__lowerCamelCase    :       str=1e-6       ,							__lowerCamelCase    :       int=1.0       ,							__lowerCamelCase    :       Optional[int]="gelu_new"       ,							__lowerCamelCase    :       int=0       ,							__lowerCamelCase    :       int=False       ,							__lowerCamelCase    :       Optional[Any]=0       ,							__lowerCamelCase    :       Dict=1       ,							__lowerCamelCase    :       List[Any]=False       ,							__lowerCamelCase    :       Optional[Any]=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> int:






         """simple docstring"""







         UpperCAmelCase      					=  vocab_size
         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  d_kv
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  num_layers
         UpperCAmelCase      					=  num_heads
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  layer_norm_epsilon
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  use_cache

         UpperCAmelCase      					=  eos_token_id
         UpperCAmelCase      					=  decoder_start_token_id

         # for backwards compatibility
         UpperCAmelCase      					=  dense_act_fn

         super().__init__(
             pad_token_id=__lowerCamelCase       ,							eos_token_id=__lowerCamelCase       ,							decoder_start_token_id=__lowerCamelCase       ,							tie_word_embeddings=__lowerCamelCase       ,							is_decoder=__lowerCamelCase       ,							**__lowerCamelCase       ,							)





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       Any							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the text config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""text_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_vision_model'''





      def __init__(		self    :       str       ,							__lowerCamelCase    :       Any=7_6_8       ,							__lowerCamelCase    :       Dict=7_6_8       ,							__lowerCamelCase    :       List[Any]=2_0_4_8       ,							__lowerCamelCase    :       List[Any]=6_4       ,							__lowerCamelCase    :       Union[str, Any]=1_2       ,							__lowerCamelCase    :       Optional[int]=1_2       ,							__lowerCamelCase    :       Tuple="gelu_new"       ,							__lowerCamelCase    :       Tuple=1e-6       ,							__lowerCamelCase    :       Any=0.0       ,							__lowerCamelCase    :       List[Any]=0.0       ,							__lowerCamelCase    :       int=1e-1_0       ,							__lowerCamelCase    :       Union[str, Any]=1.0       ,							__lowerCamelCase    :       Any=4_0_9_6       ,							__lowerCamelCase    :       Dict=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							**__lowerCamelCase    :       Optional[int]       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(**__lowerCamelCase							)

         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  patch_embed_hidden_size
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  num_hidden_layers
         UpperCAmelCase      					=  num_attention_heads
         UpperCAmelCase      					=  initializer_range
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  attention_dropout
         UpperCAmelCase      					=  layer_norm_eps
         UpperCAmelCase      					=  dense_act_fn
         UpperCAmelCase      					=  seq_len
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  d_kv





      @classmethod
      def 							_lowercase					(		cls    :       Any       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       int							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the vision config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""vision_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct'''
      UpperCamelCase										=							True





      def __init__(		self    :       str       ,							__lowerCamelCase    :       str=None       ,							__lowerCamelCase    :       Dict=None       ,							__lowerCamelCase    :       Optional[Any]=1.0       ,							__lowerCamelCase    :       str=0.02       ,							__lowerCamelCase    :       Any=False       ,							__lowerCamelCase    :       Tuple=False       ,							__lowerCamelCase    :       str=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(tie_word_embeddings=__lowerCamelCase       ,							is_encoder_decoder=__lowerCamelCase       ,							**__lowerCamelCase							)

         if text_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values."""							)

         if vision_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values."""							)

         UpperCAmelCase      					=  PixaStructTextConfig(**__lowerCamelCase							)
         UpperCAmelCase      					=  PixaStructVisionConfig(**__lowerCamelCase							)

         UpperCAmelCase      					=  self.text_config.decoder_start_token_id
         UpperCAmelCase      					=  self.text_config.pad_token_id
         UpperCAmelCase      					=  self.text_config.eos_token_id

         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  initializer_range

         UpperCAmelCase      					=  self.initializer_range
         UpperCAmelCase      					=  self.initializer_range

         UpperCAmelCase      					=  is_vqa





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       PixaStructTextConfig       ,							__lowerCamelCase    :       PixaStructVisionConfig       ,							**__lowerCamelCase    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         return cls(text_config=text_config.to_dict()       ,							vision_config=vision_config.to_dict()       ,							**__lowerCamelCase							)





      def 							_lowercase					(		self    :       List[str]							)     -> Optional[int]:






         """simple docstring"""







         UpperCAmelCase      					=  copy.deepcopy(self.__dict__							)
         UpperCAmelCase      					=  self.text_config.to_dict()
         UpperCAmelCase      					=  self.vision_config.to_dict()
         UpperCAmelCase      					=  self.__class__.model_type
         return output



