

"""simple docstring"""





import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


__UpperCamelCase						:   int							   =	logging.get_logger(__name__)







@dataclass
class        SCREAMING_SNAKE_CASE      :






					"""simple docstring"""



					lowercase__       				= 42
					lowercase__       				= 42
					lowercase__       				= None
					lowercase__       				= None







					def  __lowerCAmelCase				( self    :       Dict	):
						return json.dumps(dataclasses.asdict(self	)     ,indent=2	) + "\n"







@dataclass(frozen=a_ )
class        SCREAMING_SNAKE_CASE      :






					"""simple docstring"""



					lowercase__       				= 42
					lowercase__       				= None
					lowercase__       				= None
					lowercase__       				= None







					def  __lowerCAmelCase				( self    :       Optional[int]	):
						return json.dumps(dataclasses.asdict(self	)	) + "\n"







class        SCREAMING_SNAKE_CASE      :






					"""simple docstring"""



					def  __lowerCAmelCase				( self    :       str     ,lowercase_    :       Tuple	):
						raise NotImplementedError()







					def  __lowerCAmelCase				( self    :       Optional[Any]     ,lowercase_    :       int	):
						raise NotImplementedError()







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       Optional[Any]	):
						raise NotImplementedError()







					def  __lowerCAmelCase				( self    :       Dict     ,lowercase_    :       List[str]	):
						raise NotImplementedError()







					def  __lowerCAmelCase				( self    :       List[str]	):
						raise NotImplementedError()







					def  __lowerCAmelCase				( self    :       Tuple     ,lowercase_    :       str	):
						if len(self.get_labels()	) > 1:
							lowerCAmelCase__			:		Optional[int]						       =			self.get_labels()[int(example.label	)]
						return example







					@classmethod
					def  __lowerCAmelCase				( cls    :       List[Any]     ,lowercase_    :       List[str]     ,lowercase_    :       str=None	):
						with open(lowercase_     ,'''r'''     ,encoding='''utf-8-sig'''	) as f:
							return list(csv.reader(lowercase_     ,delimiter='''\t'''     ,quotechar=lowercase_	)	)







class        SCREAMING_SNAKE_CASE      (	a_ ):






					"""simple docstring"""



					def __init__( self    :       Any     ,lowercase_    :       List[Any]=None     ,lowercase_    :       str=None     ,lowercase_    :       Optional[Any]="classification"     ,lowercase_    :       Optional[int]=False	):
						lowerCAmelCase__			:		Optional[int]						       =			[] if labels is None else labels
						lowerCAmelCase__			:		Any						       =			[] if examples is None else examples
						lowerCAmelCase__			:		Tuple						       =			mode
						lowerCAmelCase__			:		int						       =			verbose







					def __len__( self    :       List[Any]	):
						return len(self.examples	)







					def __getitem__( self    :       Union[str, Any]     ,lowercase_    :       Optional[int]	):
						if isinstance(lowercase_     ,lowercase_	):
							return SingleSentenceClassificationProcessor(labels=self.labels     ,examples=self.examples[idx]	)
						return self.examples[idx]







					@classmethod
					def  __lowerCAmelCase				( cls    :       str     ,lowercase_    :       int     ,lowercase_    :       Optional[Any]=""     ,lowercase_    :       int=0     ,lowercase_    :       Union[str, Any]=1     ,lowercase_    :       List[str]=None     ,lowercase_    :       int=False     ,**lowercase_    :       Any	):
						lowerCAmelCase__			:		Tuple						       =			cls(**lowercase_	)
						processor.add_examples_from_csv(
						    lowercase_     ,split_name=lowercase_     ,column_label=lowercase_     ,column_text=lowercase_     ,column_id=lowercase_     ,skip_first_row=lowercase_     ,overwrite_labels=lowercase_     ,overwrite_examples=lowercase_     ,)
						return processor







					@classmethod
					def  __lowerCAmelCase				( cls    :       Optional[Any]     ,lowercase_    :       Tuple     ,lowercase_    :       Any=None     ,**lowercase_    :       Any	):
						lowerCAmelCase__			:		Optional[Any]						       =			cls(**lowercase_	)
						processor.add_examples(lowercase_     ,labels=lowercase_	)
						return processor







					def  __lowerCAmelCase				( self    :       Dict     ,lowercase_    :       List[Any]     ,lowercase_    :       List[str]=""     ,lowercase_    :       Optional[Any]=0     ,lowercase_    :       int=1     ,lowercase_    :       Optional[Any]=None     ,lowercase_    :       str=False     ,lowercase_    :       Optional[Any]=False     ,lowercase_    :       Tuple=False     ,):
						lowerCAmelCase__			:		Union[str, Any]						       =			self._read_tsv(lowercase_	)
						if skip_first_row:
							lowerCAmelCase__			:		Dict						       =			lines[1:]
						lowerCAmelCase__			:		str						       =			[]
						lowerCAmelCase__			:		Any						       =			[]
						lowerCAmelCase__			:		Dict						       =			[]
						for i, line in enumerate(lowercase_	):
							texts.append(line[column_text]	)
							labels.append(line[column_label]	)
							if column_id is not None:
								ids.append(line[column_id]	)
							else:
								lowerCAmelCase__			:		List[str]						       =			F'{split_name}-{i}' if split_name else str(lowercase_	)
								ids.append(lowercase_	)

						return self.add_examples(
						    lowercase_     ,lowercase_     ,lowercase_     ,overwrite_labels=lowercase_     ,overwrite_examples=lowercase_	)







					def  __lowerCAmelCase				( self    :       Tuple     ,lowercase_    :       Union[str, Any]     ,lowercase_    :       Optional[Any]=None     ,lowercase_    :       Optional[int]=None     ,lowercase_    :       Union[str, Any]=False     ,lowercase_    :       List[Any]=False	):
						if labels is not None and len(lowercase_	) != len(lowercase_	):
							raise ValueError(
							    F'Text and labels have mismatched lengths {len(lowercase_	)} and {len(lowercase_	)}'	)
						if ids is not None and len(lowercase_	) != len(lowercase_	):
							raise ValueError(F'Text and ids have mismatched lengths {len(lowercase_	)} and {len(lowercase_	)}'	)
						if ids is None:
							lowerCAmelCase__			:		Union[str, Any]						       =			[None] * len(lowercase_	)
						if labels is None:
							lowerCAmelCase__			:		Optional[int]						       =			[None] * len(lowercase_	)
						lowerCAmelCase__			:		Optional[Any]						       =			[]
						lowerCAmelCase__			:		Optional[Any]						       =			set()
						for text_or_text_and_label, label, guid in zip(lowercase_     ,lowercase_     ,lowercase_	):
							if isinstance(lowercase_     ,(tuple, list)	) and label is None:
								lowerCAmelCase__       ,lowerCAmelCase__			:		Union[str, Any]						       =			text_or_text_and_label
							else:
								lowerCAmelCase__			:		Union[str, Any]						       =			text_or_text_and_label
							added_labels.add(lowercase_	)
							examples.append(InputExample(guid=lowercase_     ,text_a=lowercase_     ,text_b=lowercase_     ,label=lowercase_	)	)

						# Update examples
						if overwrite_examples:
							lowerCAmelCase__			:		Tuple						       =			examples
						else:
							self.examples.extend(lowercase_	)

						# Update labels
						if overwrite_labels:
							lowerCAmelCase__			:		Dict						       =			list(lowercase_	)
						else:
							lowerCAmelCase__			:		List[str]						       =			list(set(self.labels	).union(lowercase_	)	)

						return self.examples







					def  __lowerCAmelCase				( self    :       int     ,lowercase_    :       Union[str, Any]     ,lowercase_    :       Dict=None     ,lowercase_    :       List[Any]=False     ,lowercase_    :       Dict=0     ,lowercase_    :       Optional[Any]=True     ,lowercase_    :       Any=None     ,):
						if max_length is None:
							lowerCAmelCase__			:		Union[str, Any]						       =			tokenizer.max_len

						lowerCAmelCase__			:		Tuple						       =			{label: i for i, label in enumerate(self.labels	)}

						lowerCAmelCase__			:		List[str]						       =			[]
						for ex_index, example in enumerate(self.examples	):
							if ex_index % 1_0_0_0_0 == 0:
								logger.info(F'Tokenizing example {ex_index}'	)

							lowerCAmelCase__			:		List[Any]						       =			tokenizer.encode(
							    example.text_a     ,add_special_tokens=lowercase_     ,max_length=min(lowercase_     ,tokenizer.max_len	)     ,)
							all_input_ids.append(lowercase_	)

						lowerCAmelCase__			:		Optional[Any]						       =			max(len(lowercase_	) for input_ids in all_input_ids	)

						lowerCAmelCase__			:		Union[str, Any]						       =			[]
						for ex_index, (input_ids, example) in enumerate(zip(lowercase_     ,self.examples	)	):
							if ex_index % 1_0_0_0_0 == 0:
								logger.info(F'Writing example {ex_index}/{len(self.examples	)}'	)
							# The mask has 1 for real tokens and 0 for padding tokens. Only real
							# tokens are attended to.
							lowerCAmelCase__			:		Tuple						       =			[1 if mask_padding_with_zero else 0] * len(lowercase_	)

							# Zero-pad up to the sequence length.
							lowerCAmelCase__			:		str						       =			batch_length - len(lowercase_	)
							if pad_on_left:
								lowerCAmelCase__			:		str						       =			([pad_token] * padding_length) + input_ids
								lowerCAmelCase__			:		int						       =			([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
							else:
								lowerCAmelCase__			:		int						       =			input_ids + ([pad_token] * padding_length)
								lowerCAmelCase__			:		List[str]						       =			attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

							if len(lowercase_	) != batch_length:
								raise ValueError(F'Error with input length {len(lowercase_	)} vs {batch_length}'	)
							if len(lowercase_	) != batch_length:
								raise ValueError(F'Error with input length {len(lowercase_	)} vs {batch_length}'	)

							if self.mode == "classification":
								lowerCAmelCase__			:		Tuple						       =			label_map[example.label]
							elif self.mode == "regression":
								lowerCAmelCase__			:		Any						       =			float(example.label	)
							else:
								raise ValueError(self.mode	)

							if ex_index < 5 and self.verbose:
								logger.info('''*** Example ***'''	)
								logger.info(F'guid: {example.guid}'	)
								logger.info(F'input_ids: {" ".join([str(lowercase_	) for x in input_ids]	)}'	)
								logger.info(F'attention_mask: {" ".join([str(lowercase_	) for x in attention_mask]	)}'	)
								logger.info(F'label: {example.label} (id = {label})'	)

							features.append(InputFeatures(input_ids=lowercase_     ,attention_mask=lowercase_     ,label=lowercase_	)	)

						if return_tensors is None:
							return features
						elif return_tensors == "tf":
							if not is_tf_available():
								raise RuntimeError('''return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'''	)
							import tensorflow as tf

							def gen():
								for ex in features:
									yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

							lowerCAmelCase__			:		Union[str, Any]						       =			tf.data.Dataset.from_generator(
							    lowercase_     ,({'''input_ids''': tf.intaa, '''attention_mask''': tf.intaa}, tf.intaa)     ,({'''input_ids''': tf.TensorShape([None]	), '''attention_mask''': tf.TensorShape([None]	)}, tf.TensorShape([]	))     ,)
							return dataset
						elif return_tensors == "pt":
							if not is_torch_available():
								raise RuntimeError('''return_tensors set to \'pt\' but PyTorch can\'t be imported'''	)
							import torch
							from torch.utils.data import TensorDataset

							lowerCAmelCase__			:		List[str]						       =			torch.tensor([f.input_ids for f in features]     ,dtype=torch.long	)
							lowerCAmelCase__			:		Dict						       =			torch.tensor([f.attention_mask for f in features]     ,dtype=torch.long	)
							if self.mode == "classification":
								lowerCAmelCase__			:		Union[str, Any]						       =			torch.tensor([f.label for f in features]     ,dtype=torch.long	)
							elif self.mode == "regression":
								lowerCAmelCase__			:		str						       =			torch.tensor([f.label for f in features]     ,dtype=torch.float	)

							lowerCAmelCase__			:		List[str]						       =			TensorDataset(lowercase_     ,lowercase_     ,lowercase_	)
							return dataset
						else:
							raise ValueError('''return_tensors should be one of \'tf\' or \'pt\''''	)







"""simple docstring"""





from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


__UpperCamelCase						:   str							   =	logging.get_logger(__name__)

__UpperCamelCase						:   Union[str, Any]							   =	{
    '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/config.json''',
    '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/config.json''',
    '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/config.json''',
    '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/config.json''',
    '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/config.json''',
}







class        SCREAMING_SNAKE_CASE      (	a_ ):






					"""simple docstring"""



					lowercase__       				= "gpt2"
					lowercase__       				= ["past_key_values"]
					lowercase__       				= {
					    "hidden_size": "n_embd",
					    "max_position_embeddings": "n_positions",
					    "num_attention_heads": "n_head",
					    "num_hidden_layers": "n_layer",
					}







					def __init__( self    :       List[str]     ,lowercase_    :       Dict=5_0_2_5_7     ,lowercase_    :       Dict=1_0_2_4     ,lowercase_    :       Any=7_6_8     ,lowercase_    :       str=1_2     ,lowercase_    :       int=1_2     ,lowercase_    :       List[Any]=None     ,lowercase_    :       Dict="gelu_new"     ,lowercase_    :       Any=0.1     ,lowercase_    :       Optional[Any]=0.1     ,lowercase_    :       Tuple=0.1     ,lowercase_    :       Dict=1E-5     ,lowercase_    :       List[Any]=0.02     ,lowercase_    :       Tuple="cls_index"     ,lowercase_    :       Optional[int]=True     ,lowercase_    :       Union[str, Any]=None     ,lowercase_    :       Optional[Any]=True     ,lowercase_    :       List[Any]=0.1     ,lowercase_    :       List[str]=True     ,lowercase_    :       int=True     ,lowercase_    :       Optional[Any]=5_0_2_5_6     ,lowercase_    :       Dict=5_0_2_5_6     ,lowercase_    :       Union[str, Any]=False     ,lowercase_    :       Optional[Any]=False     ,**lowercase_    :       Dict     ,):
						lowerCAmelCase__			:		Tuple						       =			vocab_size
						lowerCAmelCase__			:		List[Any]						       =			n_positions
						lowerCAmelCase__			:		Optional[int]						       =			n_embd
						lowerCAmelCase__			:		Tuple						       =			n_layer
						lowerCAmelCase__			:		Optional[Any]						       =			n_head
						lowerCAmelCase__			:		List[Any]						       =			n_inner
						lowerCAmelCase__			:		Any						       =			activation_function
						lowerCAmelCase__			:		Optional[int]						       =			resid_pdrop
						lowerCAmelCase__			:		str						       =			embd_pdrop
						lowerCAmelCase__			:		Tuple						       =			attn_pdrop
						lowerCAmelCase__			:		List[str]						       =			layer_norm_epsilon
						lowerCAmelCase__			:		Union[str, Any]						       =			initializer_range
						lowerCAmelCase__			:		Optional[int]						       =			summary_type
						lowerCAmelCase__			:		Optional[Any]						       =			summary_use_proj
						lowerCAmelCase__			:		List[str]						       =			summary_activation
						lowerCAmelCase__			:		Optional[int]						       =			summary_first_dropout
						lowerCAmelCase__			:		Dict						       =			summary_proj_to_labels
						lowerCAmelCase__			:		Dict						       =			scale_attn_weights
						lowerCAmelCase__			:		Dict						       =			use_cache
						lowerCAmelCase__			:		List[Any]						       =			scale_attn_by_inverse_layer_idx
						lowerCAmelCase__			:		int						       =			reorder_and_upcast_attn

						lowerCAmelCase__			:		List[str]						       =			bos_token_id
						lowerCAmelCase__			:		List[str]						       =			eos_token_id

						super().__init__(bos_token_id=lowercase_     ,eos_token_id=lowercase_     ,**lowercase_	)







class        SCREAMING_SNAKE_CASE      (	a_ ):






					"""simple docstring"""



					def __init__( self    :       Optional[int]     ,lowercase_    :       PretrainedConfig     ,lowercase_    :       str = "default"     ,lowercase_    :       List[PatchingSpec] = None     ,lowercase_    :       bool = False     ,):
						super().__init__(lowercase_     ,task=lowercase_     ,patching_specs=lowercase_     ,use_past=lowercase_	)
						if not getattr(self._config     ,'''pad_token_id'''     ,lowercase_	):
							# TODO: how to do that better?
							lowerCAmelCase__			:		int						       =			0







					@property
					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		Any						       =			OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}	)
						if self.use_past:
							self.fill_with_past_key_values_(lowercase_     ,direction='''inputs'''	)
							lowerCAmelCase__			:		str						       =			{0: '''batch''', 1: '''past_sequence + sequence'''}
						else:
							lowerCAmelCase__			:		List[str]						       =			{0: '''batch''', 1: '''sequence'''}

						return common_inputs







					@property
					def  __lowerCAmelCase				( self    :       Any	):
						return self._config.n_layer







					@property
					def  __lowerCAmelCase				( self    :       List[str]	):
						return self._config.n_head







					def  __lowerCAmelCase				( self    :       Union[str, Any]     ,lowercase_    :       PreTrainedTokenizer     ,lowercase_    :       int = -1     ,lowercase_    :       int = -1     ,lowercase_    :       bool = False     ,lowercase_    :       Optional[TensorType] = None     ,):
						lowerCAmelCase__			:		Any						       =			super(lowercase_     ,self	).generate_dummy_inputs(
						    lowercase_     ,batch_size=lowercase_     ,seq_length=lowercase_     ,is_pair=lowercase_     ,framework=lowercase_	)

						# We need to order the input in the way they appears in the forward()
						lowerCAmelCase__			:		List[Any]						       =			OrderedDict({'''input_ids''': common_inputs['''input_ids''']}	)

						# Need to add the past_keys
						if self.use_past:
							if not is_torch_available():
								raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
							else:
								import torch

								lowerCAmelCase__       ,lowerCAmelCase__			:		List[Any]						       =			common_inputs['''input_ids'''].shape
								# Not using the same length for past_key_values
								lowerCAmelCase__			:		Dict						       =			seqlen + 2
								lowerCAmelCase__			:		Optional[Any]						       =			(
								    batch,
								    self.num_attention_heads,
								    past_key_values_length,
								    self._config.hidden_size // self.num_attention_heads,
								)
								lowerCAmelCase__			:		Tuple						       =			[
								    (torch.zeros(lowercase_	), torch.zeros(lowercase_	)) for _ in range(self.num_layers	)
								]

						lowerCAmelCase__			:		Any						       =			common_inputs['''attention_mask''']
						if self.use_past:
							lowerCAmelCase__			:		Optional[Any]						       =			ordered_inputs['''attention_mask'''].dtype
							lowerCAmelCase__			:		Union[str, Any]						       =			torch.cat(
							    [ordered_inputs['''attention_mask'''], torch.ones(lowercase_     ,lowercase_     ,dtype=lowercase_	)]     ,dim=1	)

						return ordered_inputs







					@property
					def  __lowerCAmelCase				( self    :       str	):
						return 1_3




