


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


A__						:						Any          =      logging.get_logger(__name__)



@dataclass
class        __snake_case		:
							_a       =				42
							_a       =				42
							_a       =				None
							_a       =				None




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





@dataclass(frozen=snake_case__		)
class        __snake_case		:
							_a       =				42
							_a       =				None
							_a       =				None
							_a       =				None




							def      UpperCAmelCase__    ( self  : List[Any]):
									return json.dumps(dataclasses.asdict(self)) + "\n"





class        __snake_case		:




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tuple):
									raise NotImplementedError()




							def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : int):
									raise NotImplementedError()




							def      UpperCAmelCase__    ( self  : str       ,       A_  : Optional[Any]):
									raise NotImplementedError()




							def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : Optional[int]):
									raise NotImplementedError()




							def      UpperCAmelCase__    ( self  : Union[str, Any]):
									raise NotImplementedError()




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Any):
									if len(self.get_labels()) > 1:
											lowerCAmelCase_    :  Optional[Any]             =				self.get_labels()[int(example.label)]
									return example







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





class        __snake_case		(  snake_case__		):




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




							def __len__( self  : Any):
									return len(self.examples)




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




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




							@classmethod
							def      UpperCAmelCase__    ( cls  : Tuple       ,       A_  : Optional[Any]       ,       A_  : Any=None       ,       **A_  : Optional[int]):
									lowerCAmelCase_    :  Tuple             =				cls(**A_)
									processor.add_examples(A_       ,       labels=A_)
									return processor




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : str       ,       A_  : int=""       ,       A_  : Optional[int]=0       ,       A_  : Dict=1       ,       A_  : Dict=None       ,       A_  : Optional[Any]=False       ,       A_  : Tuple=False       ,       A_  : Optional[int]=False       ,       ):
									lowerCAmelCase_    :  Tuple             =				self._read_tsv(A_)
									if skip_first_row:
											lowerCAmelCase_    :  List[Any]             =				lines[1:]
									lowerCAmelCase_    :  Any             =				[]
									lowerCAmelCase_    :  str             =				[]
									lowerCAmelCase_    :  str             =				[]
									for i, line in enumerate(A_):
											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(A_)
													ids.append(A_)

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




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

									# Update examples
									if overwrite_examples:
											lowerCAmelCase_    :  Tuple             =				examples
									else:
											self.examples.extend(A_)

									# Update labels
									if overwrite_labels:
											lowerCAmelCase_    :  int             =				list(A_)
									else:
											lowerCAmelCase_    :  List[str]             =				list(set(self.labels).union(A_))

									return self.examples







							def      UpperCAmelCase__    ( self  : Optional[int]       ,       A_  : Optional[Any]       ,       A_  : Tuple=None       ,       A_  : Tuple=False       ,       A_  : Optional[int]=0       ,       A_  : Optional[Any]=True       ,       A_  : List[str]=None       ,       ):
									if max_length is None:
											lowerCAmelCase_    :  List[Any]             =				tokenizer.max_len

									lowerCAmelCase_    :  List[Any]             =				{label: i for i, label in enumerate(self.labels)}

									lowerCAmelCase_    :  Union[str, Any]             =				[]
									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_    :  Tuple             =				tokenizer.encode(
											    example.text_a       ,       add_special_tokens=A_       ,       max_length=min(A_       ,       tokenizer.max_len)       ,       )
											all_input_ids.append(A_)

									lowerCAmelCase_    :  Optional[int]             =				max(len(A_) for input_ids in all_input_ids)

									lowerCAmelCase_    :  Dict             =				[]
									for ex_index, (input_ids, example) in enumerate(zip(A_       ,       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(A_)

											# Zero-pad up to the sequence length.
											lowerCAmelCase_    :  Any             =				batch_length - len(A_)
											if pad_on_left:
													lowerCAmelCase_    :  Any             =				([pad_token] * padding_length) + input_ids
													lowerCAmelCase_    :  int             =				([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
											else:
													lowerCAmelCase_    :  Any             =				input_ids + ([pad_token] * padding_length)
													lowerCAmelCase_    :  Optional[Any]             =				attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

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

											if self.mode == "classification":
													lowerCAmelCase_    :  Optional[int]             =				label_map[example.label]
											elif self.mode == "regression":
													lowerCAmelCase_    :  List[str]             =				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(A_) for x in input_ids])}""")
													logger.info(F"""attention_mask: {" ".join([str(A_) for x in attention_mask])}""")
													logger.info(F"""label: {example.label} (id = {label})""")

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

									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_    :  Dict             =				tf.data.Dataset.from_generator(
											    A_       ,       ({'''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_    :  Optional[int]             =				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_    :  Tuple             =				torch.tensor([f.label for f in features]       ,       dtype=torch.long)
											elif self.mode == "regression":
													lowerCAmelCase_    :  Union[str, Any]             =				torch.tensor([f.label for f in features]       ,       dtype=torch.float)

											lowerCAmelCase_    :  Dict             =				TensorDataset(A_       ,       A_       ,       A_)
											return dataset
									else:
											raise ValueError('''return_tensors should be one of \'tf\' or \'pt\'''')










from collections import OrderedDict
from typing import Mapping

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


A__						:						int          =      logging.get_logger(__name__)

A__						:						Dict          =      {
    '''mnaylor/mega-base-wikitext''': '''https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json''',
}



class        __snake_case		(  UpperCamelCase_		):
							_a       =				'''mega'''




							def __init__( self  : Any       ,       A_  : Dict=3_0_5_2_2       ,       A_  : int=1_2_8       ,       A_  : str=4       ,       A_  : int=2_5_6       ,       A_  : int=1_6       ,       A_  : str=True       ,       A_  : List[str]=6_4       ,       A_  : Optional[Any]=False       ,       A_  : str=-1       ,       A_  : Optional[int]=None       ,       A_  : Tuple=True       ,       A_  : Tuple="scalenorm"       ,       A_  : Optional[Any]=True       ,       A_  : int="silu"       ,       A_  : Optional[int]="softmax"       ,       A_  : Optional[int]=0.1       ,       A_  : Union[str, Any]=0.1       ,       A_  : Any=0.1       ,       A_  : Dict=False       ,       A_  : int=True       ,       A_  : Tuple=2_5_6       ,       A_  : int=True       ,       A_  : List[Any]=0.1       ,       A_  : Optional[Any]=2_0_4_8       ,       A_  : Dict=False       ,       A_  : Dict=2       ,       A_  : Tuple=0.02       ,       A_  : List[Any]=0.2       ,       A_  : str=0.02       ,       A_  : List[str]=1.0       ,       A_  : Union[str, Any]=1       ,       A_  : List[Any]=0       ,       A_  : int=2       ,       A_  : Tuple="rotary"       ,       A_  : Any=None       ,       A_  : Tuple=True       ,       A_  : Any=True       ,       **A_  : Optional[int]       ,       ):
									super().__init__(pad_token_id=A_       ,       bos_token_id=A_       ,       eos_token_id=A_       ,       **A_)

									lowerCAmelCase_    :  Tuple             =				vocab_size
									lowerCAmelCase_    :  str             =				hidden_size
									lowerCAmelCase_    :  Any             =				num_hidden_layers
									lowerCAmelCase_    :  Optional[int]             =				activation
									lowerCAmelCase_    :  Any             =				attention_activation
									lowerCAmelCase_    :  List[Any]             =				intermediate_size
									lowerCAmelCase_    :  Any             =				ema_projection_size
									lowerCAmelCase_    :  Optional[int]             =				bidirectional
									lowerCAmelCase_    :  Tuple             =				shared_representation_size
									lowerCAmelCase_    :  Any             =				use_chunking
									lowerCAmelCase_    :  List[str]             =				chunk_size
									lowerCAmelCase_    :  int             =				truncation
									lowerCAmelCase_    :  Tuple             =				normalize_before_mega
									lowerCAmelCase_    :  Tuple             =				normalization_type
									lowerCAmelCase_    :  List[str]             =				norm_affine
									lowerCAmelCase_    :  List[str]             =				dropout_prob
									lowerCAmelCase_    :  List[str]             =				hidden_dropout_prob
									lowerCAmelCase_    :  Tuple             =				attention_probs_dropout_prob
									lowerCAmelCase_    :  str             =				use_feature_dropout
									lowerCAmelCase_    :  Union[str, Any]             =				use_normalized_ffn
									lowerCAmelCase_    :  Union[str, Any]             =				nffn_hidden_size
									lowerCAmelCase_    :  int             =				normalize_before_ffn
									lowerCAmelCase_    :  Tuple             =				nffn_activation_dropout_prob
									lowerCAmelCase_    :  List[str]             =				max_positions
									lowerCAmelCase_    :  Union[str, Any]             =				add_token_type_embeddings
									lowerCAmelCase_    :  str             =				type_vocab_size
									lowerCAmelCase_    :  int             =				initializer_range
									lowerCAmelCase_    :  str             =				ema_delta_alpha_range
									lowerCAmelCase_    :  Tuple             =				ema_beta_range
									lowerCAmelCase_    :  Any             =				ema_gamma_omega_range
									lowerCAmelCase_    :  Any             =				relative_positional_bias
									lowerCAmelCase_    :  Optional[int]             =				use_cache
									lowerCAmelCase_    :  int             =				classifier_dropout
									lowerCAmelCase_    :  List[Any]             =				add_lm_hidden_dense_layer
									lowerCAmelCase_    :  Optional[Any]             =				1  # not used but required by Hugging Face





class        __snake_case		(  UpperCamelCase_		):




							@property
							def      UpperCAmelCase__    ( self  : Any):
									if self.task == "multiple-choice":
											lowerCAmelCase_    :  Dict             =				{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
									else:
											lowerCAmelCase_    :  int             =				{0: '''batch''', 1: '''sequence'''}
									return OrderedDict(
									    [
									        ('''input_ids''', dynamic_axis),
									        ('''attention_mask''', dynamic_axis),
									    ])





