






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=_snake_case      )
class   lowerCamelCase				:







		'''simple docstring'''



		_snake_case   :   str
		_snake_case   :   str
		_snake_case   :   Optional[str]							=  None
		_snake_case   :   Optional[str]							=  None
		_snake_case   :   Optional[str]							=  None





@dataclass(frozen=_snake_case      )
class   lowerCamelCase				:







		'''simple docstring'''



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


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



		class   lowerCamelCase				(_snake_case      ):







				'''simple docstring'''



				_snake_case   :   List[InputFeatures]



				def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase = None    ,		_UpperCamelCase=False    ,		_UpperCamelCase = False    ,		)     ->							Optional[int]:
									UpperCAmelCase_   :				Union[str, Any]						=						hans_processors[task]()

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

									# Make sure only the first process in distributed training processes the dataset,
									# and the others will use the cache.
									UpperCAmelCase_   :				List[Any]						=						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}"       )
																			UpperCAmelCase_   :				Optional[Any]						=						torch.load(_UpperCamelCase       )
														else:
																			logger.info(f"Creating features from dataset file at {data_dir}"       )

																			UpperCAmelCase_   :				Union[str, Any]						=						(
																			    processor.get_dev_examples(_UpperCamelCase       ) if evaluate else processor.get_train_examples(_UpperCamelCase       )
																			)

																			logger.info('Training examples: %s'    ,		len(_UpperCamelCase       )       )
																			UpperCAmelCase_   :				Any						=						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       )     ->							int:
									return len(self.features       )



				def __getitem__(							self    ,		_UpperCamelCase       )     ->							InputFeatures:
									return self.features[i]



				def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
									return self.label_list


if is_tf_available():
		import tensorflow as tf



		class   lowerCamelCase				:







				'''simple docstring'''



				_snake_case   :   List[InputFeatures]



				def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase = 1_2_8    ,		_UpperCamelCase=False    ,		_UpperCamelCase = False    ,		)     ->							int:
									UpperCAmelCase_   :				Tuple						=						hans_processors[task]()
									UpperCAmelCase_   :				Optional[Any]						=						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_   :				Optional[Any]						=						label_list

									UpperCAmelCase_   :				Any						=						processor.get_dev_examples(_UpperCamelCase       ) if evaluate else processor.get_train_examples(_UpperCamelCase       )
									UpperCAmelCase_   :				Any						=						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_0_0_0_0 == 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,
																			)

									UpperCAmelCase_   :				Any						=						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  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
									return self.dataset



				def __len__(							self       )     ->							int:
									return len(self.features       )



				def __getitem__(							self    ,		_UpperCamelCase       )     ->							InputFeatures:
									return self.features[i]



				def  __UpperCAmelCase      (							self       )     ->							List[str]:
									return self.label_list



class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							str:
							return self._create_examples(self._read_tsv(os.path.join(_UpperCamelCase    ,		'heuristics_train_set.txt'       )       )    ,		'train'       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							List[str]:
							return self._create_examples(self._read_tsv(os.path.join(_UpperCamelCase    ,		'heuristics_evaluation_set.txt'       )       )    ,		'dev'       )



		def  __UpperCAmelCase      (							self       )     ->							int:
							return ["contradiction", "entailment", "neutral"]



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





def 					lowercase__				(					__snake_case   :							List[InputExample]					,	__snake_case   :							List[str]					,	__snake_case   :							int					,	__snake_case   :							PreTrainedTokenizer					,	):


					'''simple docstring'''


					UpperCAmelCase_   :				Tuple						=						{label: i for i, label in enumerate(__snake_case  )}

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

										UpperCAmelCase_   :				str						=						tokenizer(
										    example.text_a					,	example.text_b					,	add_special_tokens=__snake_case					,	max_length=__snake_case					,	padding='max_length'					,	truncation=__snake_case					,	return_overflowing_tokens=__snake_case					,	)

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

										UpperCAmelCase_   :				Tuple						=						int(example.pairID  )

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

					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,
}














from typing import Mapping

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


__UpperCAmelCase     							=						logging.get_logger(__name__)

__UpperCAmelCase     							=						{
    'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}



class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		_snake_case   :   Union[str, Any]							=  '''umt5'''
		_snake_case   :   Union[str, Any]							=  ['''past_key_values''']



		def __init__(							self    ,		_UpperCamelCase=2_5_0_1_1_2    ,		_UpperCamelCase=5_1_2    ,		_UpperCamelCase=6_4    ,		_UpperCamelCase=1_0_2_4    ,		_UpperCamelCase=8    ,		_UpperCamelCase=None    ,		_UpperCamelCase=6    ,		_UpperCamelCase=3_2    ,		_UpperCamelCase=1_2_8    ,		_UpperCamelCase=0.1    ,		_UpperCamelCase=1E-6    ,		_UpperCamelCase=1.0    ,		_UpperCamelCase="gated-gelu"    ,		_UpperCamelCase=True    ,		_UpperCamelCase=True    ,		_UpperCamelCase="T5Tokenizer"    ,		_UpperCamelCase=True    ,		_UpperCamelCase=0    ,		_UpperCamelCase=1    ,		_UpperCamelCase=0    ,		**_UpperCamelCase    ,		)     ->							List[Any]:
							super().__init__(
							    is_encoder_decoder=_UpperCamelCase    ,		tokenizer_class=_UpperCamelCase    ,		tie_word_embeddings=_UpperCamelCase    ,		pad_token_id=_UpperCamelCase    ,		eos_token_id=_UpperCamelCase    ,		decoder_start_token_id=_UpperCamelCase    ,		**_UpperCamelCase    ,		)
							UpperCAmelCase_   :				Union[str, Any]						=						vocab_size
							UpperCAmelCase_   :				List[str]						=						d_model
							UpperCAmelCase_   :				Any						=						d_kv
							UpperCAmelCase_   :				Optional[int]						=						d_ff
							UpperCAmelCase_   :				List[Any]						=						num_layers
							UpperCAmelCase_   :				Optional[Any]						=						(
							    num_decoder_layers if num_decoder_layers is not None else self.num_layers
							)  # default = symmetry
							UpperCAmelCase_   :				Optional[int]						=						num_heads
							UpperCAmelCase_   :				Optional[int]						=						relative_attention_num_buckets
							UpperCAmelCase_   :				Dict						=						relative_attention_max_distance
							UpperCAmelCase_   :				Tuple						=						dropout_rate
							UpperCAmelCase_   :				Union[str, Any]						=						layer_norm_epsilon
							UpperCAmelCase_   :				Optional[int]						=						initializer_factor
							UpperCAmelCase_   :				List[str]						=						feed_forward_proj
							UpperCAmelCase_   :				Any						=						use_cache

							UpperCAmelCase_   :				List[Any]						=						self.feed_forward_proj.split('-'       )
							UpperCAmelCase_   :				List[Any]						=						act_info[-1]
							UpperCAmelCase_   :				Union[str, Any]						=						act_info[0] == 'gated'

							if len(_UpperCamelCase       ) > 1 and act_info[0] != "gated" or len(_UpperCamelCase       ) > 2:
												raise ValueError(
												    f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."
												    'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
												    '\'gated-gelu\' or \'relu\''       )

							if feed_forward_proj == "gated-gelu":
												UpperCAmelCase_   :				Optional[int]						=						'gelu_new'



		@property
		def  __UpperCAmelCase      (							self       )     ->							int:
							return self.d_model



		@property
		def  __UpperCAmelCase      (							self       )     ->							Any:
							return self.num_heads



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							return self.num_layers





class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		@property
		# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
		def  __UpperCAmelCase      (							self       )     ->							Mapping[str, Mapping[int, str]]:
							UpperCAmelCase_   :				str						=						{
							    'input_ids': {0: 'batch', 1: 'encoder_sequence'},
							    'attention_mask': {0: 'batch', 1: 'encoder_sequence'},
							}
							if self.use_past:
												UpperCAmelCase_   :				Optional[int]						=						'past_encoder_sequence + sequence'
												UpperCAmelCase_   :				str						=						{0: 'batch'}
												UpperCAmelCase_   :				Optional[int]						=						{0: 'batch', 1: 'past_decoder_sequence + sequence'}
							else:
												UpperCAmelCase_   :				Optional[int]						=						{0: 'batch', 1: 'decoder_sequence'}
												UpperCAmelCase_   :				Union[str, Any]						=						{0: 'batch', 1: 'decoder_sequence'}

							if self.use_past:
												self.fill_with_past_key_values_(_UpperCamelCase    ,		direction='inputs'       )

							return common_inputs



		@property
		# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
		def  __UpperCAmelCase      (							self       )     ->							int:
							return 1_3



		@property
		def  __UpperCAmelCase      (							self       )     ->							float:
							return 5E-4






