



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


__snake_case				:  Optional[Any] =       logging.getLogger(__name__)



@dataclass(frozen=__SCREAMING_SNAKE_CASE				)
class      A__  :




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      42
	SCREAMING_SNAKE_CASE        =      42
	SCREAMING_SNAKE_CASE        =      None
	SCREAMING_SNAKE_CASE        =      None
	SCREAMING_SNAKE_CASE        =      None






@dataclass(frozen=__SCREAMING_SNAKE_CASE				)
class      A__  :




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      42
	SCREAMING_SNAKE_CASE        =      None
	SCREAMING_SNAKE_CASE        =      None
	SCREAMING_SNAKE_CASE        =      None
	SCREAMING_SNAKE_CASE        =      None


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



	class      A__  (    __SCREAMING_SNAKE_CASE				):




		'''simple docstring'''





		SCREAMING_SNAKE_CASE        =      42

		def __init__(	self:						int			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						PreTrainedTokenizer			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						Optional[int] = None			,     _SCREAMING_SNAKE_CASE:						Dict=False			,     _SCREAMING_SNAKE_CASE:						bool = False			,     )							->			Dict:

					"""simple docstring"""

					__lowerCAmelCase :    Tuple				=			hans_processors[task]()

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

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

											__lowerCAmelCase :    Any				=			(
											    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))
											__lowerCAmelCase :    Union[str, Any]				=			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:						Any)							->			Optional[Any]:

					"""simple docstring"""

					return len(self.features)

		def __getitem__(	self:						str			,     _SCREAMING_SNAKE_CASE:						Union[str, Any])							->			InputFeatures:

					"""simple docstring"""

					return self.features[i]







		def 				_SCREAMING_SNAKE_CASE  (	self:						Union[str, Any])							->			Any:

					"""simple docstring"""

					return self.label_list


if is_tf_available():
	import tensorflow as tf



	class      A__  :




		'''simple docstring'''





		SCREAMING_SNAKE_CASE        =      42

		def __init__(	self:						Dict			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						PreTrainedTokenizer			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						Optional[int] = 128			,     _SCREAMING_SNAKE_CASE:						Optional[Any]=False			,     _SCREAMING_SNAKE_CASE:						bool = False			,     )							->			Tuple:

					"""simple docstring"""

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

					__lowerCAmelCase :    Dict				=			processor.get_dev_examples(_SCREAMING_SNAKE_CASE) if evaluate else processor.get_train_examples(_SCREAMING_SNAKE_CASE)
					__lowerCAmelCase :    Any				=			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_0000 == 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,
											)

					__lowerCAmelCase :    Optional[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 				_SCREAMING_SNAKE_CASE  (	self:						str)							->			Dict:

					"""simple docstring"""

					return self.dataset

		def __len__(	self:						int)							->			Dict:

					"""simple docstring"""

					return len(self.features)

		def __getitem__(	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						Optional[int])							->			InputFeatures:

					"""simple docstring"""

					return self.features[i]







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

					"""simple docstring"""

					return self.label_list



class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	def 				_SCREAMING_SNAKE_CASE  (	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						Tuple)							->			Union[str, Any]:

				"""simple docstring"""

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						str)							->			str:

				"""simple docstring"""

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Union[str, Any])							->			int:

				"""simple docstring"""

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







	def 				_SCREAMING_SNAKE_CASE  (	self:						Any			,     _SCREAMING_SNAKE_CASE:						int			,     _SCREAMING_SNAKE_CASE:						Optional[Any])							->			int:

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			[]
				for i, line in enumerate(_SCREAMING_SNAKE_CASE):
							if i == 0:
										continue
							__lowerCAmelCase :    List[str]				=			"%s-%s" % (set_type, line[0])
							__lowerCAmelCase :    Dict				=			line[5]
							__lowerCAmelCase :    str				=			line[6]
							__lowerCAmelCase :    Optional[int]				=			line[7][2:] if line[7].startswith("ex") else line[7]
							__lowerCAmelCase :    Optional[int]				=			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      _lowercase							(    __snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ,)		->  Optional[int]:
			__lowerCAmelCase :    Tuple				=			{label: i for i, label in enumerate(__snake_case      )}

			__lowerCAmelCase :    int				=			[]
			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)      )

						__lowerCAmelCase :    Optional[Any]				=			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   ,)

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

						__lowerCAmelCase :    List[str]				=			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


__snake_case				:  int =       {
    'hans': 3,
}





__snake_case				:  int =       {
    'hans': HansProcessor,
}




"""simple docstring"""






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


__snake_case				:  Tuple =       logging.get_logger(__name__)

__snake_case				:  Any =       {
    'facebook/xglm-564M': 'https://huggingface.co/facebook/xglm-564M/resolve/main/config.json',
    # See all XGLM models at https://huggingface.co/models?filter=xglm
}



class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      'xglm'
	SCREAMING_SNAKE_CASE        =      ['past_key_values']

	SCREAMING_SNAKE_CASE        =      {
	    'num_attention_heads': 'attention_heads',
	    'hidden_size': 'd_model',
	    'num_hidden_layers': 'num_layers',
	}

	def __init__(	self:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]=25_6008			,     _SCREAMING_SNAKE_CASE:						Dict=2048			,     _SCREAMING_SNAKE_CASE:						int=1024			,     _SCREAMING_SNAKE_CASE:						Dict=4096			,     _SCREAMING_SNAKE_CASE:						Optional[Any]=24			,     _SCREAMING_SNAKE_CASE:						int=16			,     _SCREAMING_SNAKE_CASE:						List[str]="gelu"			,     _SCREAMING_SNAKE_CASE:						Optional[Any]=0.1			,     _SCREAMING_SNAKE_CASE:						int=0.1			,     _SCREAMING_SNAKE_CASE:						List[Any]=0.0			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=0.0			,     _SCREAMING_SNAKE_CASE:						Any=0.02			,     _SCREAMING_SNAKE_CASE:						Any=True			,     _SCREAMING_SNAKE_CASE:						Tuple=True			,     _SCREAMING_SNAKE_CASE:						Any=2			,     _SCREAMING_SNAKE_CASE:						str=1			,     _SCREAMING_SNAKE_CASE:						Dict=0			,     _SCREAMING_SNAKE_CASE:						Dict=2			,     **_SCREAMING_SNAKE_CASE:						List[Any]			,     )							->			List[str]:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			vocab_size
				__lowerCAmelCase :    int				=			max_position_embeddings
				__lowerCAmelCase :    Optional[Any]				=			d_model
				__lowerCAmelCase :    List[Any]				=			ffn_dim
				__lowerCAmelCase :    int				=			num_layers
				__lowerCAmelCase :    Any				=			attention_heads
				__lowerCAmelCase :    int				=			activation_function
				__lowerCAmelCase :    List[Any]				=			dropout
				__lowerCAmelCase :    Optional[int]				=			attention_dropout
				__lowerCAmelCase :    Optional[int]				=			activation_dropout
				__lowerCAmelCase :    Optional[int]				=			layerdrop
				__lowerCAmelCase :    Optional[int]				=			init_std
				__lowerCAmelCase :    Tuple				=			scale_embedding  # scale factor will be sqrt(d_model) if True
				__lowerCAmelCase :    Dict				=			use_cache

				super().__init__(
				    pad_token_id=_SCREAMING_SNAKE_CASE			,     bos_token_id=_SCREAMING_SNAKE_CASE			,     eos_token_id=_SCREAMING_SNAKE_CASE			,     decoder_start_token_id=_SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE			,     )