






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=_UpperCAmelCase   )
class 				__UpperCAmelCase		:
			__snake_case    :				Optional[Any]									=							42
			__snake_case    :				List[str]									=							42
			__snake_case    :				Tuple									=							None
			__snake_case    :				str									=							None
			__snake_case    :				Optional[Any]									=							None



@dataclass(frozen=_UpperCAmelCase   )
class 				__UpperCAmelCase		:
			__snake_case    :				List[Any]									=							42
			__snake_case    :				Optional[int]									=							None
			__snake_case    :				Tuple									=							None
			__snake_case    :				Union[str, Any]									=							None
			__snake_case    :				List[str]									=							None


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



				class 				__UpperCAmelCase		(_UpperCAmelCase   ):
							__snake_case    :				Dict									=							42







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







													'''simple docstring'''




													_SCREAMING_SNAKE_CASE     				=  hans_processors[task]()

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

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

																									_SCREAMING_SNAKE_CASE     				=  (
																									    processor.get_dev_examples(__A					) if evaluate else processor.get_train_examples(__A					)
																									)

																									logger.info("""Training examples: %s"""				,     len(__A					)					)
																									_SCREAMING_SNAKE_CASE     				=  hans_convert_examples_to_features(__A				,     __A				,     __A				,     __A					)
																									logger.info("""Saving features into cached file %s"""				,     __A					)
																									torch.save(self.features				,     __A					)







							def __len__(     self:  Tuple					):







													'''simple docstring'''




													return len(self.features					)







							def __getitem__(     self:  str				,     UpperCAmelCase_:  int					):







													'''simple docstring'''




													return self.features[i]







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







													'''simple docstring'''




													return self.label_list


if is_tf_available():
				import tensorflow as tf



				class 				__UpperCAmelCase		:
							__snake_case    :				Optional[int]									=							42







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







													'''simple docstring'''




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

													_SCREAMING_SNAKE_CASE     				=  processor.get_dev_examples(__A					) if evaluate else processor.get_train_examples(__A					)
													_SCREAMING_SNAKE_CASE     				=  hans_convert_examples_to_features(__A				,     __A				,     __A				,     __A					)

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

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

													_SCREAMING_SNAKE_CASE     				=  tf.data.Dataset.from_generator(
													    __A				,     (
													        {
													            """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:  Dict					):







													'''simple docstring'''




													return self.dataset







							def __len__(     self:  Dict					):







													'''simple docstring'''




													return len(self.features					)







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







													'''simple docstring'''




													return self.features[i]







							def 							UpperCamelCase				(     self:  Tuple					):







													'''simple docstring'''




													return self.label_list



class 				__UpperCAmelCase		(_UpperCAmelCase   ):







			def 							UpperCamelCase				(     self:  Dict				,     UpperCAmelCase_:  Tuple					):







									'''simple docstring'''




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







			def 							UpperCamelCase				(     self:  Dict				,     UpperCAmelCase_:  Any					):







									'''simple docstring'''




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







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




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







			def 							UpperCamelCase				(     self:  Any				,     UpperCAmelCase_:  Any				,     UpperCAmelCase_:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  []
									for i, line in enumerate(__A					):
															if i == 0:
																					continue
															_SCREAMING_SNAKE_CASE     				=  """%s-%s""" % (set_type, line[0])
															_SCREAMING_SNAKE_CASE     				=  line[5]
															_SCREAMING_SNAKE_CASE     				=  line[6]
															_SCREAMING_SNAKE_CASE     				=  line[7][2:] if line[7].startswith("""ex"""					) else line[7]
															_SCREAMING_SNAKE_CASE     				=  line[0]
															examples.append(InputExample(guid=__A				,     text_a=__A				,     text_b=__A				,     label=__A				,     pairID=__A					)					)
									return examples

def 	__lowerCamelCase      (		snake_case__	,snake_case__	,snake_case__	,snake_case__	,)							->  Optional[Any]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  {label: i for i, label in enumerate(_lowercase		)}

						_SCREAMING_SNAKE_CASE     				=  []
						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)		)

												_SCREAMING_SNAKE_CASE     				=  tokenizer(
												    example.text_a	,example.text_b	,add_special_tokens=_lowercase	,max_length=_lowercase	,padding="""max_length"""	,truncation=_lowercase	,return_overflowing_tokens=_lowercase	,)

												_SCREAMING_SNAKE_CASE     				=  label_map[example.label] if example.label in label_map else 0

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









from collections import OrderedDict
from typing import Mapping

from packaging import version

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


UpperCamelCase			    =     logging.get_logger(__name__)

UpperCamelCase			    =     {
    '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''',
    '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}



class 				__UpperCAmelCase		(_UpperCAmelCase   ):
			__snake_case    :				Any									=							"mobilenet_v1"







			def __init__(     self:  List[str]				,     UpperCAmelCase_:  List[Any]=3				,     UpperCAmelCase_:  List[str]=224				,     UpperCAmelCase_:  Optional[Any]=1.0				,     UpperCAmelCase_:  Dict=8				,     UpperCAmelCase_:  List[str]="relu6"				,     UpperCAmelCase_:  List[Any]=True				,     UpperCAmelCase_:  Dict=0.9_99				,     UpperCAmelCase_:  Union[str, Any]=0.02				,     UpperCAmelCase_:  Tuple=0.0_01				,     **UpperCAmelCase_:  List[str]				,     ):







									'''simple docstring'''




									super().__init__(**UpperCAmelCase_					)

									if depth_multiplier <= 0:
															raise ValueError("""depth_multiplier must be greater than zero."""					)

									_SCREAMING_SNAKE_CASE     				=  num_channels
									_SCREAMING_SNAKE_CASE     				=  image_size
									_SCREAMING_SNAKE_CASE     				=  depth_multiplier
									_SCREAMING_SNAKE_CASE     				=  min_depth
									_SCREAMING_SNAKE_CASE     				=  hidden_act
									_SCREAMING_SNAKE_CASE     				=  tf_padding
									_SCREAMING_SNAKE_CASE     				=  classifier_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  initializer_range
									_SCREAMING_SNAKE_CASE     				=  layer_norm_eps



class 				__UpperCAmelCase		(_UpperCAmelCase   ):
			__snake_case    :				List[Any]									=							version.parse("1.11"   )







			@property
			def 							UpperCamelCase				(     self:  Tuple					):







									'''simple docstring'''




									return OrderedDict([("""pixel_values""", {0: """batch"""})]					)







			@property
			def 							UpperCamelCase				(     self:  Dict					):







									'''simple docstring'''




									if self.task == "image-classification":
															return OrderedDict([("""logits""", {0: """batch"""})]					)
									else:
															return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]					)







			@property
			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									return 1E-4

