





'''simple docstring'''






import json
import os
import re
import unicodedata
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase      :   Tuple								=     logging.get_logger(__name__)

__UpperCamelCase      :   Tuple								=     {
    '''src_vocab_file''': '''vocab-src.json''',
    '''tgt_vocab_file''': '''vocab-tgt.json''',
    '''merges_file''': '''merges.txt''',
}

__UpperCamelCase      :   List[str]								=     {
    '''src_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-src.json'''
    },
    '''tgt_vocab_file''': {
        '''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/vocab-tgt.json'''
    },
    '''merges_file''': {'''stas/tiny-wmt19-en-de''': '''https://huggingface.co/stas/tiny-wmt19-en-de/resolve/main/merges.txt'''},
}

__UpperCamelCase      :   List[str]								=     {'''stas/tiny-wmt19-en-de''': 1024}
__UpperCamelCase      :   Union[str, Any]								=     {
    '''stas/tiny-wmt19-en-de''': {
        '''langs''': ['''en''', '''de'''],
        '''model_max_length''': 1024,
        '''special_tokens_map_file''': None,
        '''full_tokenizer_file''': None,
    }
}





def      lowercase  (  lowerCAmelCase :						List[str]):



				"""simple docstring"""


				_A :				Optional[Any]				=  set()
				_A :				int				=  word[0]
				for char in word[1:]:
								pairs.add((prev_char, char))
								_A :				Optional[Any]				=  char
				return pairs





def      lowercase  (  lowerCAmelCase :						Tuple):



				"""simple docstring"""


				_A :				str				=  text.replace('''，'''   , ''',''')
				_A :				Union[str, Any]				=  re.sub(R'''。\s*'''   , '''. '''   , lowerCAmelCase)
				_A :				List[str]				=  text.replace('''、'''   , ''',''')
				_A :				Optional[int]				=  text.replace('''”'''   , '''"''')
				_A :				Any				=  text.replace('''“'''   , '''"''')
				_A :				List[Any]				=  text.replace('''∶'''   , ''':''')
				_A :				Dict				=  text.replace('''：'''   , ''':''')
				_A :				int				=  text.replace('''？'''   , '''?''')
				_A :				List[str]				=  text.replace('''《'''   , '''"''')
				_A :				List[Any]				=  text.replace('''》'''   , '''"''')
				_A :				Optional[int]				=  text.replace('''）'''   , ''')''')
				_A :				Union[str, Any]				=  text.replace('''！'''   , '''!''')
				_A :				Dict				=  text.replace('''（'''   , '''(''')
				_A :				List[Any]				=  text.replace('''；'''   , ''';''')
				_A :				Optional[Any]				=  text.replace('''１'''   , '''1''')
				_A :				str				=  text.replace('''」'''   , '''"''')
				_A :				Tuple				=  text.replace('''「'''   , '''"''')
				_A :				List[Any]				=  text.replace('''０'''   , '''0''')
				_A :				List[str]				=  text.replace('''３'''   , '''3''')
				_A :				List[Any]				=  text.replace('''２'''   , '''2''')
				_A :				str				=  text.replace('''５'''   , '''5''')
				_A :				List[str]				=  text.replace('''６'''   , '''6''')
				_A :				Union[str, Any]				=  text.replace('''９'''   , '''9''')
				_A :				List[Any]				=  text.replace('''７'''   , '''7''')
				_A :				Dict				=  text.replace('''８'''   , '''8''')
				_A :				Optional[Any]				=  text.replace('''４'''   , '''4''')
				_A :				str				=  re.sub(R'''．\s*'''   , '''. '''   , lowerCAmelCase)
				_A :				Optional[Any]				=  text.replace('''～'''   , '''~''')
				_A :				Dict				=  text.replace('''’'''   , '''\'''')
				_A :				List[str]				=  text.replace('''…'''   , '''...''')
				_A :				int				=  text.replace('''━'''   , '''-''')
				_A :				List[str]				=  text.replace('''〈'''   , '''<''')
				_A :				List[Any]				=  text.replace('''〉'''   , '''>''')
				_A :				Any				=  text.replace('''【'''   , '''[''')
				_A :				Tuple				=  text.replace('''】'''   , ''']''')
				_A :				Tuple				=  text.replace('''％'''   , '''%''')
				return text





def      lowercase  (  lowerCAmelCase :						Any):



				"""simple docstring"""


				_A :				Tuple				=  []
				for char in text:
								_A :				Tuple				=  unicodedata.category(lowerCAmelCase)
								if cat.startswith('''C'''):
												continue
								output.append(lowerCAmelCase)
				return "".join(lowerCAmelCase)




class  lowerCamelCase__				(						snake_case_						):


	"""simple docstring"""





	__magic_name__							   =		VOCAB_FILES_NAMES
	__magic_name__							   =		PRETRAINED_VOCAB_FILES_MAP
	__magic_name__							   =		PRETRAINED_INIT_CONFIGURATION
	__magic_name__							   =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	__magic_name__							   =		["""input_ids""", """attention_mask"""]




	def __init__(					self   ,     UpperCAmelCase__=None   ,     UpperCAmelCase__=None   ,     UpperCAmelCase__=None   ,     UpperCAmelCase__=None   ,     UpperCAmelCase__=False   ,     UpperCAmelCase__="<unk>"   ,     UpperCAmelCase__="<s>"   ,     UpperCAmelCase__="</s>"   ,     UpperCAmelCase__="<pad>"   ,     **UpperCAmelCase__   ,     )       ->				int:
					super().__init__(
					    langs=UpperCAmelCase__   ,     src_vocab_file=UpperCAmelCase__   ,     tgt_vocab_file=UpperCAmelCase__   ,     merges_file=UpperCAmelCase__   ,     do_lower_case=UpperCAmelCase__   ,     unk_token=UpperCAmelCase__   ,     bos_token=UpperCAmelCase__   ,     sep_token=UpperCAmelCase__   ,     pad_token=UpperCAmelCase__   ,     **UpperCAmelCase__   ,     )

					try:
									import sacremoses
					except ImportError:
									raise ImportError(
									    '''You need to install sacremoses to use XLMTokenizer. '''
									    '''See https://pypi.org/project/sacremoses/ for installation.'''			)

					_A :				Optional[int]				=  sacremoses

					_A :				Optional[Any]				=  src_vocab_file
					_A :				Optional[Any]				=  tgt_vocab_file
					_A :				List[str]				=  merges_file
					_A :				List[Any]				=  do_lower_case

					# cache of sm.MosesPunctNormalizer instance
					_A :				Optional[Any]				=  {}
					# cache of sm.MosesTokenizer instance
					_A :				Dict				=  {}
					_A :				List[Any]				=  {}

					if langs and len(UpperCAmelCase__			) == 2:
									_A	,       _A :				List[str]				=  langs
					else:
									raise ValueError(
									    F"""arg `langs` needs to be a list of 2 langs, e.g. ['en', 'ru'], but got {langs}. """
									    '''Usually that means that tokenizer can\'t find a mapping for the given model path '''
									    '''in PRETRAINED_VOCAB_FILES_MAP, and other maps of this tokenizer.'''			)

					with open(UpperCAmelCase__   ,     encoding='''utf-8'''			) as src_vocab_handle:
									_A :				str				=  json.load(UpperCAmelCase__			)
					with open(UpperCAmelCase__   ,     encoding='''utf-8'''			) as tgt_vocab_handle:
									_A :				List[Any]				=  json.load(UpperCAmelCase__			)
									_A :				Optional[Any]				=  {v: k for k, v in tgt_vocab.items()}
					with open(UpperCAmelCase__   ,     encoding='''utf-8'''			) as merges_handle:
									_A :				Any				=  merges_handle.read().split('''\n'''			)[:-1]
					_A :				List[str]				=  [tuple(merge.split()[:2]			) for merge in merges]
					_A :				Any				=  dict(zip(UpperCAmelCase__   ,     range(len(UpperCAmelCase__			)			)			)			)
					_A :				Any				=  {}




	def 				_lowerCamelCase				(					self			)       ->				Dict[str, int]:
					return self.get_src_vocab()




	@property
	def 				_lowerCamelCase				(					self			)       ->				int:
					return self.src_vocab_size




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__			)       ->				Optional[int]:
					if lang not in self.cache_moses_punct_normalizer:
									_A :				List[str]				=  self.sm.MosesPunctNormalizer(lang=UpperCAmelCase__			)
									_A :				Dict				=  punct_normalizer
					return self.cache_moses_punct_normalizer[lang].normalize(UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__			)       ->				Tuple:
					if lang not in self.cache_moses_tokenizer:
									_A :				Union[str, Any]				=  self.sm.MosesTokenizer(lang=UpperCAmelCase__			)
									_A :				Union[str, Any]				=  moses_tokenizer
					return self.cache_moses_tokenizer[lang].tokenize(
					    UpperCAmelCase__   ,     aggressive_dash_splits=UpperCAmelCase__   ,     return_str=UpperCAmelCase__   ,     escape=UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__			)       ->				Any:
					if lang not in self.cache_moses_detokenizer:
									_A :				Tuple				=  self.sm.MosesDetokenizer(lang=UpperCAmelCase__			)
									_A :				List[str]				=  moses_detokenizer
					return self.cache_moses_detokenizer[lang].detokenize(UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__			)       ->				Optional[int]:
					_A :				str				=  replace_unicode_punct(UpperCAmelCase__			)
					_A :				List[str]				=  self.moses_punct_norm(UpperCAmelCase__   ,     UpperCAmelCase__			)
					_A :				Any				=  remove_non_printing_char(UpperCAmelCase__			)
					return text




	@property
	def 				_lowerCamelCase				(					self			)       ->				Dict:
					return len(self.encoder			)




	@property
	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					return len(self.decoder			)




	def 				_lowerCamelCase				(					self			)       ->				Union[str, Any]:
					return dict(self.encoder   ,     **self.added_tokens_encoder			)




	def 				_lowerCamelCase				(					self			)       ->				Optional[int]:
					return dict(self.decoder   ,     **self.added_tokens_decoder			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__			)       ->				str:
					_A :				Optional[Any]				=  tuple(token[:-1]			) + (token[-1] + '''</w>''',)
					if token in self.cache:
									return self.cache[token]
					_A :				Union[str, Any]				=  get_pairs(UpperCAmelCase__			)

					if not pairs:
									return token + "</w>"

					while True:
									_A :				Optional[int]				=  min(UpperCAmelCase__   ,     key=lambda UpperCAmelCase__			: self.bpe_ranks.get(UpperCAmelCase__   ,     float('''inf'''			)			)			)
									if bigram not in self.bpe_ranks:
													break
									_A	,       _A :				Any				=  bigram
									_A :				str				=  []
									_A :				List[str]				=  0
									while i < len(UpperCAmelCase__			):
													try:
																	_A :				Any				=  word.index(UpperCAmelCase__   ,     UpperCAmelCase__			)
													except ValueError:
																	new_word.extend(word[i:]			)
																	break
													else:
																	new_word.extend(word[i:j]			)
																	_A :				str				=  j

													if word[i] == first and i < len(UpperCAmelCase__			) - 1 and word[i + 1] == second:
																	new_word.append(first + second			)
																	i += 2
													else:
																	new_word.append(word[i]			)
																	i += 1
									_A :				List[str]				=  tuple(UpperCAmelCase__			)
									_A :				Dict				=  new_word
									if len(UpperCAmelCase__			) == 1:
													break
									else:
													_A :				Optional[Any]				=  get_pairs(UpperCAmelCase__			)
					_A :				Optional[Any]				=  ''' '''.join(UpperCAmelCase__			)
					if word == "\n  </w>":
									_A :				Tuple				=  '''\n</w>'''
					_A :				List[str]				=  word
					return word




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__="en"   ,     UpperCAmelCase__=False			)       ->				Optional[Any]:
					_A :				Optional[int]				=  self.src_lang

					if self.do_lower_case:
									_A :				List[Any]				=  text.lower()

					if bypass_tokenizer:
									_A :				Union[str, Any]				=  text.split()
					else:
									_A :				Dict				=  self.moses_pipeline(UpperCAmelCase__   ,     lang=UpperCAmelCase__			)
									_A :				Dict				=  self.moses_tokenize(UpperCAmelCase__   ,     lang=UpperCAmelCase__			)

					_A :				List[Any]				=  []
					for token in text:
									if token:
													split_tokens.extend(list(self.bpe(UpperCAmelCase__			).split(''' '''			)			)			)

					return split_tokens




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__			)       ->				str:
					return self.encoder.get(UpperCAmelCase__   ,     self.encoder.get(self.unk_token			)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__			)       ->				Optional[Any]:
					return self.decoder.get(UpperCAmelCase__   ,     self.unk_token			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__			)       ->				List[Any]:
					_A :				Dict				=  [t.replace(''' '''   ,     ''''''			).replace('''</w>'''   ,     ''' '''			) for t in tokens]
					_A :				List[Any]				=  ''''''.join(UpperCAmelCase__			).split()
					# detokenize
					_A :				List[Any]				=  self.moses_detokenize(UpperCAmelCase__   ,     self.tgt_lang			)
					return text




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__ = None			)       ->				List[int]:
					_A :				int				=  [self.sep_token_id]

					# no bos used in fairseq
					if token_ids_a is None:
									return token_ids_a + sep
					return token_ids_a + sep + token_ids_a + sep




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__ = None   ,     UpperCAmelCase__ = False			)       ->				List[int]:

					if already_has_special_tokens:
									return super().get_special_tokens_mask(
									    token_ids_a=UpperCAmelCase__   ,     token_ids_a=UpperCAmelCase__   ,     already_has_special_tokens=UpperCAmelCase__			)
					# no bos used in fairseq
					if token_ids_a is not None:
									return ([0] * len(UpperCAmelCase__			)) + [1] + ([0] * len(UpperCAmelCase__			)) + [1]
					return ([0] * len(UpperCAmelCase__			)) + [1]




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__ = None			)       ->				List[int]:
					_A :				List[str]				=  [self.sep_token_id]

					# no bos used in fairseq
					if token_ids_a is None:
									return len(token_ids_a + sep			) * [0]
					return len(token_ids_a + sep			) * [0] + len(token_ids_a + sep			) * [1]




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__ = None			)       ->				Tuple[str]:
					if not os.path.isdir(UpperCAmelCase__			):
									logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""			)
									return

					_A :				List[Any]				=  os.path.join(
					    UpperCAmelCase__   ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''src_vocab_file''']			)
					_A :				Tuple				=  os.path.join(
					    UpperCAmelCase__   ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''tgt_vocab_file''']			)
					_A :				List[str]				=  os.path.join(
					    UpperCAmelCase__   ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']			)

					with open(UpperCAmelCase__   ,     '''w'''   ,     encoding='''utf-8'''			) as f:
									f.write(json.dumps(self.encoder   ,     indent=2   ,     sort_keys=UpperCAmelCase__   ,     ensure_ascii=UpperCAmelCase__			) + '''\n'''			)

					with open(UpperCAmelCase__   ,     '''w'''   ,     encoding='''utf-8'''			) as f:
									_A :				List[str]				=  {v: k for k, v in self.decoder.items()}
									f.write(json.dumps(UpperCAmelCase__   ,     indent=2   ,     sort_keys=UpperCAmelCase__   ,     ensure_ascii=UpperCAmelCase__			) + '''\n'''			)

					_A :				Optional[Any]				=  0
					with open(UpperCAmelCase__   ,     '''w'''   ,     encoding='''utf-8'''			) as writer:
									for bpe_tokens, token_index in sorted(self.bpe_ranks.items()   ,     key=lambda UpperCAmelCase__			: kv[1]			):
													if index != token_index:
																	logger.warning(
																	    F"""Saving vocabulary to {merges_file}: BPE merge indices are not consecutive."""
																	    ''' Please check that the tokenizer is not corrupted!'''			)
																	_A :				Dict				=  token_index
													writer.write(''' '''.join(UpperCAmelCase__			) + '''\n'''			)
													index += 1

					return src_vocab_file, tgt_vocab_file, merges_file




	def __getstate__(					self			)       ->				List[str]:
					_A :				Optional[int]				=  self.__dict__.copy()
					_A :				List[str]				=  None
					return state




	def __setstate__(					self   ,     UpperCAmelCase__			)       ->				str:
					_A :				Dict				=  d

					try:
									import sacremoses
					except ImportError:
									raise ImportError(
									    '''You need to install sacremoses to use XLMTokenizer. '''
									    '''See https://pypi.org/project/sacremoses/ for installation.'''			)

					_A :				Union[str, Any]				=  sacremoses







'''simple docstring'''






import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    ElectraForCausalLM,
	    ElectraForMaskedLM,
	    ElectraForMultipleChoice,
	    ElectraForPreTraining,
	    ElectraForQuestionAnswering,
	    ElectraForSequenceClassification,
	    ElectraForTokenClassification,
	    ElectraModel,
	)
	from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST



class  lowerCamelCase__				:


	"""simple docstring"""





	def __init__(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__=1_3   ,     UpperCAmelCase__=7   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=9_9   ,     UpperCAmelCase__=3_2   ,     UpperCAmelCase__=5   ,     UpperCAmelCase__=4   ,     UpperCAmelCase__=3_7   ,     UpperCAmelCase__="gelu"   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=5_1_2   ,     UpperCAmelCase__=1_6   ,     UpperCAmelCase__=2   ,     UpperCAmelCase__=0.0_2   ,     UpperCAmelCase__=3   ,     UpperCAmelCase__=4   ,     UpperCAmelCase__=None   ,     )       ->				Optional[Any]:
					_A :				Optional[int]				=  parent
					_A :				List[Any]				=  batch_size
					_A :				Optional[int]				=  seq_length
					_A :				Dict				=  is_training
					_A :				Union[str, Any]				=  use_input_mask
					_A :				Tuple				=  use_token_type_ids
					_A :				str				=  use_labels
					_A :				List[str]				=  vocab_size
					_A :				Tuple				=  hidden_size
					_A :				Tuple				=  num_hidden_layers
					_A :				List[str]				=  num_attention_heads
					_A :				Optional[Any]				=  intermediate_size
					_A :				str				=  hidden_act
					_A :				Optional[int]				=  hidden_dropout_prob
					_A :				int				=  attention_probs_dropout_prob
					_A :				Optional[Any]				=  max_position_embeddings
					_A :				List[Any]				=  type_vocab_size
					_A :				Tuple				=  type_sequence_label_size
					_A :				Any				=  initializer_range
					_A :				Any				=  num_labels
					_A :				str				=  num_choices
					_A :				List[str]				=  scope




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				List[Any]				=  ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size			)

					_A :				Union[str, Any]				=  None
					if self.use_input_mask:
									_A :				Tuple				=  random_attention_mask([self.batch_size, self.seq_length]			)

					_A :				List[str]				=  None
					if self.use_token_type_ids:
									_A :				List[Any]				=  ids_tensor([self.batch_size, self.seq_length]   ,     self.type_vocab_size			)

					_A :				Tuple				=  None
					_A :				Dict				=  None
					_A :				int				=  None
					if self.use_labels:
									_A :				Any				=  ids_tensor([self.batch_size]   ,     self.type_sequence_label_size			)
									_A :				Dict				=  ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels			)
									_A :				int				=  ids_tensor([self.batch_size]   ,     self.num_choices			)
									_A :				List[str]				=  ids_tensor([self.batch_size, self.seq_length]   ,     1			)

					_A :				List[Any]				=  self.get_config()

					return (
					    config,
					    input_ids,
					    token_type_ids,
					    input_mask,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					    fake_token_labels,
					)




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					return ElectraConfig(
					    vocab_size=self.vocab_size   ,     hidden_size=self.hidden_size   ,     num_hidden_layers=self.num_hidden_layers   ,     num_attention_heads=self.num_attention_heads   ,     intermediate_size=self.intermediate_size   ,     hidden_act=self.hidden_act   ,     hidden_dropout_prob=self.hidden_dropout_prob   ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,     max_position_embeddings=self.max_position_embeddings   ,     type_vocab_size=self.type_vocab_size   ,     is_decoder=UpperCAmelCase__   ,     initializer_range=self.initializer_range   ,     )




	def 				_lowerCamelCase				(					self			)       ->				int:
					(
					    (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       
					) :				List[str]				=  self.prepare_config_and_inputs()

					_A :				Tuple				=  True
					_A :				List[str]				=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
					_A :				str				=  ids_tensor([self.batch_size, self.seq_length]   ,     vocab_size=2			)

					return (
					    config,
					    input_ids,
					    token_type_ids,
					    input_mask,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					    encoder_hidden_states,
					    encoder_attention_mask,
					)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Tuple:
					_A :				List[str]				=  ElectraModel(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Tuple				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__			)
					_A :				List[Any]				=  model(UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__			)
					_A :				Tuple				=  model(UpperCAmelCase__			)
					self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[str]:
					_A :				Dict				=  True
					_A :				Any				=  ElectraModel(UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				List[str]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     )
					_A :				Optional[int]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     )
					_A :				List[Any]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__			)
					self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[str]:
					_A :				Optional[int]				=  ElectraForMaskedLM(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				List[str]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Union[str, Any]:
					_A :				List[Any]				=  ElectraForCausalLM(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Optional[int]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[str]:
					_A :				str				=  self.num_labels
					_A :				str				=  ElectraForTokenClassification(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				str				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[Any]:
					_A :				Optional[int]				=  self.num_labels
					_A :				Optional[int]				=  ElectraForPreTraining(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Dict				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Tuple:
					_A :				Optional[Any]				=  self.num_labels
					_A :				Optional[Any]				=  ElectraForSequenceClassification(UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Any				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[Any]:
					_A :				Optional[int]				=  ElectraForQuestionAnswering(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Union[str, Any]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     start_positions=UpperCAmelCase__   ,     end_positions=UpperCAmelCase__   ,     )
					self.parent.assertEqual(result.start_logits.shape   ,     (self.batch_size, self.seq_length)			)
					self.parent.assertEqual(result.end_logits.shape   ,     (self.batch_size, self.seq_length)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Union[str, Any]:
					_A :				Union[str, Any]				=  self.num_choices
					_A :				Union[str, Any]				=  ElectraForMultipleChoice(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Dict				=  input_ids.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				str				=  token_type_ids.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				Dict				=  input_mask.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				str				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__   ,     )
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_choices)			)




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				Optional[Any]				=  self.prepare_config_and_inputs()
					(
					    (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       
					) :				int				=  config_and_inputs
					_A :				Dict				=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict



@require_torch
class  lowerCamelCase__				(						snake_case_  ,							snake_case_  ,							unittest.TestCase						):


	"""simple docstring"""





	__magic_name__							   =		(
	    (
	        ElectraModel,
	        ElectraForPreTraining,
	        ElectraForMaskedLM,
	        ElectraForCausalLM,
	        ElectraForMultipleChoice,
	        ElectraForTokenClassification,
	        ElectraForSequenceClassification,
	        ElectraForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	__magic_name__							   =		(
	    {
	        """feature-extraction""": ElectraModel,
	        """fill-mask""": ElectraForMaskedLM,
	        """question-answering""": ElectraForQuestionAnswering,
	        """text-classification""": ElectraForSequenceClassification,
	        """text-generation""": ElectraForCausalLM,
	        """token-classification""": ElectraForTokenClassification,
	        """zero-shot""": ElectraForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__magic_name__							   =		True




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__=False			)       ->				Optional[Any]:
					_A :				List[str]				=  super()._prepare_for_class(UpperCAmelCase__   ,     UpperCAmelCase__   ,     return_labels=UpperCAmelCase__			)

					if return_labels:
									if model_class in get_values(UpperCAmelCase__			):
													_A :				Tuple				=  torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)   ,     dtype=torch.long   ,     device=UpperCAmelCase__			)
					return inputs_dict




	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Optional[Any]				=  ElectraModelTester(self			)
					_A :				List[str]				=  ConfigTester(self   ,     config_class=UpperCAmelCase__   ,     hidden_size=3_7			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					self.config_tester.run_common_tests()




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_model(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_model_as_decoder(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				int				=  self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									_A :				List[str]				=  type
									self.model_tester.create_and_check_electra_model(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_masked_lm(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Any:
					_A :				Dict				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_token_classification(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Union[str, Any]:
					_A :				List[str]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_pretraining(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Any				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_sequence_classification(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_question_answering(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_multiple_choice(*UpperCAmelCase__			)




	@slow
	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_A :				List[str]				=  ElectraModel.from_pretrained(UpperCAmelCase__			)
									self.assertIsNotNone(UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				str				=  self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_for_causal_lm(*UpperCAmelCase__			)



@require_torch
class  lowerCamelCase__				(						unittest.TestCase						):


	"""simple docstring"""





	@slow
	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Any				=  ElectraModel.from_pretrained('''google/electra-small-discriminator'''			)
					_A :				Union[str, Any]				=  torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]			)
					_A :				Union[str, Any]				=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
					_A :				Optional[int]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__			)[0]
					_A :				Tuple				=  torch.Size((1, 1_1, 2_5_6)			)
					self.assertEqual(output.shape   ,     UpperCAmelCase__			)
					_A :				Optional[Any]				=  torch.tensor(
					    [[[0.4_4_7_1, 0.6_8_2_1, -0.3_2_6_5], [0.4_6_2_7, 0.5_2_5_5, -0.3_6_6_8], [0.4_5_3_2, 0.3_3_1_3, -0.4_3_4_4]]]			)

					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]   ,     UpperCAmelCase__   ,     atol=1e-4			)			)
