



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


snake_case__					:     Optional[int]    		=   logging.getLogger(__name__)

@dataclass(frozen=_lowercase	)
class   _A   :


						'''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=_lowercase	)
class   _A   :


						'''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   _A   ( _lowercase	):


							'''simple docstring'''





							_snake_case			:     List[InputFeatures]



							def __init__(	self	:							Union[str, Any]							,      lowerCamelCase	:							str							,      lowerCamelCase	:							PreTrainedTokenizer							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Optional[int] = None							,      lowerCamelCase	:							Dict=False							,      lowerCamelCase	:							bool = False							,      ):






								'''simple docstring'''



								__lowercase		       =			hans_processors[task]()

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

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

										__lowercase		       =			(
										    processor.get_dev_examples(lowerCamelCase	) if evaluate else processor.get_train_examples(lowerCamelCase	)
										)

										logger.info("Training examples: %s"							,      len(lowerCamelCase	)	)
										__lowercase		       =			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	:							str	):






								'''simple docstring'''



								return len(self.features	)



							def __getitem__(	self	:							Tuple							,      lowerCamelCase	:							Dict	):






								'''simple docstring'''



								return self.features[i]



							def     _snake_case	(	self	:							Tuple	):






								'''simple docstring'''



								return self.label_list


if is_tf_available():
	import tensorflow as tf

	class   _A   :


							'''simple docstring'''





							_snake_case			:     List[InputFeatures]



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






								'''simple docstring'''



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

								__lowercase		       =			processor.get_dev_examples(lowerCamelCase	) if evaluate else processor.get_train_examples(lowerCamelCase	)
								__lowercase		       =			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,
										)

								__lowercase		       =			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     _snake_case	(	self	:							Dict	):






								'''simple docstring'''



								return self.dataset



							def __len__(	self	:							List[str]	):






								'''simple docstring'''



								return len(self.features	)



							def __getitem__(	self	:							Optional[int]							,      lowerCamelCase	:							Optional[Any]	):






								'''simple docstring'''



								return self.features[i]



							def     _snake_case	(	self	:							List[Any]	):






								'''simple docstring'''



								return self.label_list

class   _A   ( _lowercase	):


						'''simple docstring'''





						def     _snake_case	(	self	:							Union[str, Any]							,      lowerCamelCase	:							Tuple	):






							'''simple docstring'''



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



						def     _snake_case	(	self	:							Any							,      lowerCamelCase	:							Tuple	):






							'''simple docstring'''



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



						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



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



						def     _snake_case	(	self	:							Tuple							,      lowerCamelCase	:							int							,      lowerCamelCase	:							List[Any]	):






							'''simple docstring'''



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



def    snake_case_				(    _SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	):
	__lowercase		       =			{label: i for i, label in enumerate(_SCREAMING_SNAKE_CASE   )}

	__lowercase		       =			[]
	for ex_index, example in tqdm.tqdm(enumerate(_SCREAMING_SNAKE_CASE   )  ,	desc="convert examples to features"   ):
		if ex_index % 1_0_0_0_0 == 0:
			logger.info("Writing example %d" % (ex_index)   )

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

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

		__lowercase		       =			int(example.pairID   )

		features.append(InputFeatures(**_SCREAMING_SNAKE_CASE  ,	label=_SCREAMING_SNAKE_CASE  ,	pairID=_SCREAMING_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


snake_case__					:     Dict    		=   {
    """hans""": 3,
}
snake_case__					:     Tuple    		=   {
    """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


snake_case__					:     List[Any]    		=   logging.get_logger(__name__)

snake_case__					:     List[str]    		=   {
    """hustvl/yolos-small""": """https://huggingface.co/hustvl/yolos-small/resolve/main/config.json""",
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     List[Any]      							=      """yolos"""



						def __init__(	self	:							Union[str, Any]							,      lowerCamelCase	:							Union[str, Any]=768							,      lowerCamelCase	:							int=12							,      lowerCamelCase	:							Union[str, Any]=12							,      lowerCamelCase	:							Optional[Any]=3_072							,      lowerCamelCase	:							Optional[int]="gelu"							,      lowerCamelCase	:							Dict=0.0							,      lowerCamelCase	:							Optional[Any]=0.0							,      lowerCamelCase	:							Any=0.02							,      lowerCamelCase	:							Optional[Any]=1e-12							,      lowerCamelCase	:							Optional[Any]=[512, 864]							,      lowerCamelCase	:							str=16							,      lowerCamelCase	:							Dict=3							,      lowerCamelCase	:							str=True							,      lowerCamelCase	:							List[Any]=100							,      lowerCamelCase	:							Dict=True							,      lowerCamelCase	:							Dict=False							,      lowerCamelCase	:							List[str]=1							,      lowerCamelCase	:							str=5							,      lowerCamelCase	:							Any=2							,      lowerCamelCase	:							str=5							,      lowerCamelCase	:							Optional[int]=2							,      lowerCamelCase	:							List[Any]=0.1							,      **lowerCamelCase	:							List[Any]							,      ):






							'''simple docstring'''



							super().__init__(**lowerCamelCase	)

							__lowercase		       =			hidden_size
							__lowercase		       =			num_hidden_layers
							__lowercase		       =			num_attention_heads
							__lowercase		       =			intermediate_size
							__lowercase		       =			hidden_act
							__lowercase		       =			hidden_dropout_prob
							__lowercase		       =			attention_probs_dropout_prob
							__lowercase		       =			initializer_range
							__lowercase		       =			layer_norm_eps
							__lowercase		       =			image_size
							__lowercase		       =			patch_size
							__lowercase		       =			num_channels
							__lowercase		       =			qkv_bias
							__lowercase		       =			num_detection_tokens
							__lowercase		       =			use_mid_position_embeddings
							__lowercase		       =			auxiliary_loss
							# Hungarian matcher
							__lowercase		       =			class_cost
							__lowercase		       =			bbox_cost
							__lowercase		       =			giou_cost
							# Loss coefficients
							__lowercase		       =			bbox_loss_coefficient
							__lowercase		       =			giou_loss_coefficient
							__lowercase		       =			eos_coefficient

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     Dict      							=      version.parse("""1.11"""	)



						@property
						def     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



							return OrderedDict(
							    [
							        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
							    ]	)



						@property
						def     _snake_case	(	self	:							str	):






							'''simple docstring'''



							return 1e-4



						@property
						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



							return 12



