






import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

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


if is_torch_available():
	import torch

	from transformers import (
	    FlaubertForMultipleChoice,
	    FlaubertForQuestionAnswering,
	    FlaubertForQuestionAnsweringSimple,
	    FlaubertForSequenceClassification,
	    FlaubertForTokenClassification,
	    FlaubertModel,
	    FlaubertWithLMHeadModel,
	)
	from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST

class     _SCREAMING_SNAKE_CASE       (			_a      ):





	def __init__(		self				:					List[str] ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Union[str, Any]=13 ,  __lowerCamelCase				:					str=7 ,  __lowerCamelCase				:					Tuple=True ,  __lowerCamelCase				:					Union[str, Any]=True ,  __lowerCamelCase				:					Any=True ,  __lowerCamelCase				:					List[Any]=True ,  __lowerCamelCase				:					Optional[Any]=True ,  __lowerCamelCase				:					Optional[int]=False ,  __lowerCamelCase				:					str=False ,  __lowerCamelCase				:					List[Any]=False ,  __lowerCamelCase				:					List[Any]=2 ,  __lowerCamelCase				:					Union[str, Any]=99 ,  __lowerCamelCase				:					Optional[Any]=0 ,  __lowerCamelCase				:					Tuple=32 ,  __lowerCamelCase				:					Any=5 ,  __lowerCamelCase				:					Optional[Any]=4 ,  __lowerCamelCase				:					Union[str, Any]=0.1 ,  __lowerCamelCase				:					Union[str, Any]=0.1 ,  __lowerCamelCase				:					List[str]=512 ,  __lowerCamelCase				:					List[Any]=12 ,  __lowerCamelCase				:					int=2 ,  __lowerCamelCase				:					List[str]=0.02 ,  __lowerCamelCase				:					Union[str, Any]=3 ,  __lowerCamelCase				:					Tuple=4 ,  __lowerCamelCase				:					Optional[int]="last" ,  __lowerCamelCase				:					Optional[Any]=None ,  __lowerCamelCase				:					List[str]=None ,  ):
		UpperCamelCase     :int          =	parent
		UpperCamelCase     :Optional[int]          =	batch_size
		UpperCamelCase     :str          =	seq_length
		UpperCamelCase     :Optional[int]          =	is_training
		UpperCamelCase     :Optional[int]          =	use_input_lengths
		UpperCamelCase     :Union[str, Any]          =	use_token_type_ids
		UpperCamelCase     :List[str]          =	use_labels
		UpperCamelCase     :Dict          =	gelu_activation
		UpperCamelCase     :Optional[int]          =	sinusoidal_embeddings
		UpperCamelCase     :List[Any]          =	causal
		UpperCamelCase     :Optional[int]          =	asm
		UpperCamelCase     :List[str]          =	n_langs
		UpperCamelCase     :int          =	vocab_size
		UpperCamelCase     :List[Any]          =	n_special
		UpperCamelCase     :List[Any]          =	hidden_size
		UpperCamelCase     :List[str]          =	num_hidden_layers
		UpperCamelCase     :List[Any]          =	num_attention_heads
		UpperCamelCase     :Tuple          =	hidden_dropout_prob
		UpperCamelCase     :List[str]          =	attention_probs_dropout_prob
		UpperCamelCase     :Tuple          =	max_position_embeddings
		UpperCamelCase     :List[str]          =	type_vocab_size
		UpperCamelCase     :Union[str, Any]          =	type_sequence_label_size
		UpperCamelCase     :int          =	initializer_range
		UpperCamelCase     :List[str]          =	num_labels
		UpperCamelCase     :Optional[int]          =	num_choices
		UpperCamelCase     :Optional[Any]          =	summary_type
		UpperCamelCase     :Tuple          =	use_proj
		UpperCamelCase     :Optional[Any]          =	scope





	def 						_A (		self				:					List[str]	):
		UpperCamelCase     :Union[str, Any]          =	ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size	)
		UpperCamelCase     :Any          =	random_attention_mask([self.batch_size, self.seq_length]	)

		UpperCamelCase     :List[Any]          =	None
		if self.use_input_lengths:
			UpperCamelCase     :Dict          =	(
			    ids_tensor([self.batch_size] ,  vocab_size=2	) + self.seq_length - 2
			)  # small variation of seq_length

		UpperCamelCase     :str          =	None
		if self.use_token_type_ids:
			UpperCamelCase     :int          =	ids_tensor([self.batch_size, self.seq_length] ,  self.n_langs	)

		UpperCamelCase     :Optional[int]          =	None
		UpperCamelCase     :int          =	None
		UpperCamelCase     :List[Any]          =	None
		if self.use_labels:
			UpperCamelCase     :Optional[Any]          =	ids_tensor([self.batch_size] ,  self.type_sequence_label_size	)
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size, self.seq_length] ,  self.num_labels	)
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size] ,  2	).float()
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size] ,  self.num_choices	)

		UpperCamelCase     :Union[str, Any]          =	self.get_config()

		return (
		    config,
		    input_ids,
		    token_type_ids,
		    input_lengths,
		    sequence_labels,
		    token_labels,
		    is_impossible_labels,
		    choice_labels,
		    input_mask,
		)





	def 						_A (		self				:					List[Any]	):
		return FlaubertConfig(
		    vocab_size=self.vocab_size ,  n_special=self.n_special ,  emb_dim=self.hidden_size ,  n_layers=self.num_hidden_layers ,  n_heads=self.num_attention_heads ,  dropout=self.hidden_dropout_prob ,  attention_dropout=self.attention_probs_dropout_prob ,  gelu_activation=self.gelu_activation ,  sinusoidal_embeddings=self.sinusoidal_embeddings ,  asm=self.asm ,  causal=self.causal ,  n_langs=self.n_langs ,  max_position_embeddings=self.max_position_embeddings ,  initializer_range=self.initializer_range ,  summary_type=self.summary_type ,  use_proj=self.use_proj ,  )





	def 						_A (		self				:					Union[str, Any] ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					int ,  ):
		UpperCamelCase     :Tuple          =	FlaubertModel(config=__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()
		UpperCamelCase     :int          =	model(__lowerCamelCase ,  lengths=__lowerCamelCase ,  langs=__lowerCamelCase	)
		UpperCamelCase     :List[Any]          =	model(__lowerCamelCase ,  langs=__lowerCamelCase	)
		UpperCamelCase     :int          =	model(__lowerCamelCase	)
		self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size)	)





	def 						_A (		self				:					Optional[int] ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Dict ,  ):
		UpperCamelCase     :Any          =	FlaubertWithLMHeadModel(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Dict          =	model(__lowerCamelCase ,  token_type_ids=__lowerCamelCase ,  labels=__lowerCamelCase	)
		self.parent.assertEqual(result.loss.shape ,  ()	)
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)	)





	def 						_A (		self				:					List[Any] ,  __lowerCamelCase				:					str ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					Union[str, Any] ,  __lowerCamelCase				:					str ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[Any] ,  ):
		UpperCamelCase     :Any          =	FlaubertForQuestionAnsweringSimple(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Any          =	model(__lowerCamelCase	)

		UpperCamelCase     :int          =	model(__lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase	)
		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 						_A (		self				:					str ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					str ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					str ,  ):
		UpperCamelCase     :str          =	FlaubertForQuestionAnswering(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Any          =	model(__lowerCamelCase	)

		UpperCamelCase     :Optional[int]          =	model(
		    __lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase ,  cls_index=__lowerCamelCase ,  is_impossible=__lowerCamelCase ,  p_mask=__lowerCamelCase ,  )

		UpperCamelCase     :Union[str, Any]          =	model(
		    __lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase ,  cls_index=__lowerCamelCase ,  is_impossible=__lowerCamelCase ,  )

		((UpperCamelCase)     ,      )     :int          =	result_with_labels.to_tuple()

		UpperCamelCase     :int          =	model(__lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase	)

		((UpperCamelCase)     ,      )     :List[Any]          =	result_with_labels.to_tuple()

		self.parent.assertEqual(result_with_labels.loss.shape ,  ()	)
		self.parent.assertEqual(result.start_top_log_probs.shape ,  (self.batch_size, model.config.start_n_top)	)
		self.parent.assertEqual(result.start_top_index.shape ,  (self.batch_size, model.config.start_n_top)	)
		self.parent.assertEqual(
		    result.end_top_log_probs.shape ,  (self.batch_size, model.config.start_n_top * model.config.end_n_top)	)
		self.parent.assertEqual(
		    result.end_top_index.shape ,  (self.batch_size, model.config.start_n_top * model.config.end_n_top)	)
		self.parent.assertEqual(result.cls_logits.shape ,  (self.batch_size,)	)





	def 						_A (		self				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					Tuple ,  ):
		UpperCamelCase     :Optional[int]          =	FlaubertForSequenceClassification(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Tuple          =	model(__lowerCamelCase	)
		UpperCamelCase     :List[str]          =	model(__lowerCamelCase ,  labels=__lowerCamelCase	)

		self.parent.assertEqual(result.loss.shape ,  ()	)
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.type_sequence_label_size)	)





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					str ,  __lowerCamelCase				:					Union[str, Any] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					List[Any] ,  ):
		UpperCamelCase     :Dict          =	self.num_labels
		UpperCamelCase     :Tuple          =	FlaubertForTokenClassification(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Optional[Any]          =	model(__lowerCamelCase ,  attention_mask=__lowerCamelCase ,  labels=__lowerCamelCase	)
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.num_labels)	)





	def 						_A (		self				:					Tuple ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[Any] ,  ):
		UpperCamelCase     :Union[str, Any]          =	self.num_choices
		UpperCamelCase     :List[Any]          =	FlaubertForMultipleChoice(config=__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()
		UpperCamelCase     :Optional[Any]          =	input_ids.unsqueeze(1	).expand(-1 ,  self.num_choices ,  -1	).contiguous()
		UpperCamelCase     :Optional[int]          =	token_type_ids.unsqueeze(1	).expand(-1 ,  self.num_choices ,  -1	).contiguous()
		UpperCamelCase     :int          =	input_mask.unsqueeze(1	).expand(-1 ,  self.num_choices ,  -1	).contiguous()
		UpperCamelCase     :Union[str, Any]          =	model(
		    __lowerCamelCase ,  attention_mask=__lowerCamelCase ,  token_type_ids=__lowerCamelCase ,  labels=__lowerCamelCase ,  )
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_choices)	)





	def 						_A (		self				:					str	):
		UpperCamelCase     :List[str]          =	self.prepare_config_and_inputs()
		(
		    (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      
		)     :List[Any]          =	config_and_inputs
		UpperCamelCase     :Union[str, Any]          =	{
		    """input_ids""": input_ids,
		    """token_type_ids""": token_type_ids,
		    """lengths""": input_lengths,
		    """attention_mask""": input_mask,
		}
		return config, inputs_dict

@require_torch
class     _SCREAMING_SNAKE_CASE       (			_a		,   _a		,   unittest.TestCase      ):
	snake_case__							:     Optional[int]     	=  (
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__							:     Tuple     	=  (
	    {
	        """feature-extraction""": FlaubertModel,
	        """fill-mask""": FlaubertWithLMHeadModel,
	        """question-answering""": FlaubertForQuestionAnsweringSimple,
	        """text-classification""": FlaubertForSequenceClassification,
	        """token-classification""": FlaubertForTokenClassification,
	        """zero-shot""": FlaubertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)





	def 						_A (		self				:					int ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Optional[int]	):
		if (
		    pipeline_test_casse_name == "QAPipelineTests"
		    and tokenizer_name is not None
		    and not tokenizer_name.endswith("""Fast"""	)
		):
			# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
			# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
			# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
			return True

		return False





	def 						_A (		self				:					Optional[int] ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Tuple=False	):
		UpperCamelCase     :Tuple          =	super()._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase ,  return_labels=__lowerCamelCase	)

		if return_labels:
			if model_class.__name__ == "FlaubertForQuestionAnswering":
				UpperCamelCase     :Tuple          =	torch.zeros(
				    self.model_tester.batch_size ,  dtype=torch.long ,  device=__lowerCamelCase	)
				UpperCamelCase     :List[Any]          =	torch.zeros(
				    self.model_tester.batch_size ,  dtype=torch.long ,  device=__lowerCamelCase	)

		return inputs_dict





	def 						_A (		self				:					str	):
		UpperCamelCase     :List[Any]          =	FlaubertModelTester(self	)
		UpperCamelCase     :Any          =	ConfigTester(self ,  config_class=__lowerCamelCase ,  emb_dim=37	)





	def 						_A (		self				:					Optional[int]	):
		self.config_tester.run_common_tests()





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :List[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase	)





	def 						_A (		self				:					Optional[int]	):
		UpperCamelCase     :Optional[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Optional[int]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase	)





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase	)





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Tuple          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase	)





	def 						_A (		self				:					int	):
		UpperCamelCase     :Tuple          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase	)





	@slow
	def 						_A (		self				:					Any	):
		for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			UpperCamelCase     :Optional[int]          =	FlaubertModel.from_pretrained(__lowerCamelCase	)
			self.assertIsNotNone(__lowerCamelCase	)





	@slow
	@require_torch_gpu
	def 						_A (		self				:					Tuple	):
		UpperCamelCase     ,      UpperCamelCase     :Union[str, Any]          =	self.model_tester.prepare_config_and_inputs_for_common()
		for model_class in self.all_model_classes:
			# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
			if model_class == FlaubertForMultipleChoice:
				return

			UpperCamelCase     :Optional[Any]          =	True
			UpperCamelCase     :Optional[Any]          =	model_class(config=__lowerCamelCase	)

			UpperCamelCase     :str          =	self._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase	)
			UpperCamelCase     :str          =	torch.jit.trace(
			    __lowerCamelCase ,  (inputs_dict["""input_ids"""].to("""cpu"""	), inputs_dict["""attention_mask"""].to("""cpu"""	))	)

			with tempfile.TemporaryDirectory() as tmp:
				torch.jit.save(__lowerCamelCase ,  os.path.join(__lowerCamelCase ,  """traced_model.pt"""	)	)
				UpperCamelCase     :int          =	torch.jit.load(os.path.join(__lowerCamelCase ,  """traced_model.pt"""	) ,  map_location=__lowerCamelCase	)
				loaded(inputs_dict["""input_ids"""].to(__lowerCamelCase	) ,  inputs_dict["""attention_mask"""].to(__lowerCamelCase	)	)

@require_torch
class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	@slow
	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :Union[str, Any]          =	FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""	)
		UpperCamelCase     :Optional[Any]          =	torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
		with torch.no_grad():
			UpperCamelCase     :Tuple          =	model(__lowerCamelCase	)[0]
		UpperCamelCase     :Union[str, Any]          =	torch.Size((1, 11, 768)	)
		self.assertEqual(output.shape ,  __lowerCamelCase	)
		UpperCamelCase     :int          =	torch.tensor(
		    [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]	)

		self.assertTrue(torch.allclose(output[:, :3, :3] ,  __lowerCamelCase ,  atol=1E-4	)	)



'''simple docstring'''






import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
 import torch

 from transformers import (
     DebertaForMaskedLM,
     DebertaForQuestionAnswering,
     DebertaForSequenceClassification,
     DebertaForTokenClassification,
     DebertaModel,
 )
 from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST






class 			lowercase_	(lowerCamelCase__      ):




  """simple docstring"""




  def __init__(      self   :     Optional[Any]					,lowercase__   :     Optional[Any]					,lowercase__   :     Optional[int]=1_3					,lowercase__   :     Any=7					,lowercase__   :     Union[str, Any]=True					,lowercase__   :     Optional[int]=True					,lowercase__   :     List[str]=True					,lowercase__   :     str=True					,lowercase__   :     Dict=9_9					,lowercase__   :     Union[str, Any]=3_2					,lowercase__   :     List[str]=5					,lowercase__   :     int=4					,lowercase__   :     Dict=3_7					,lowercase__   :     Union[str, Any]="gelu"					,lowercase__   :     str=0.1					,lowercase__   :     List[str]=0.1					,lowercase__   :     Any=5_1_2					,lowercase__   :     Optional[int]=1_6					,lowercase__   :     Optional[int]=2					,lowercase__   :     Optional[int]=0.0_2					,lowercase__   :     Dict=False					,lowercase__   :     Optional[int]=True					,lowercase__   :     str="None"					,lowercase__   :     Optional[int]=3					,lowercase__   :     List[Any]=4					,lowercase__   :     Union[str, Any]=None					,):
   __lowercase							  = parent
   __lowercase							  = batch_size
   __lowercase							  = seq_length
   __lowercase							  = is_training
   __lowercase							  = use_input_mask
   __lowercase							  = use_token_type_ids
   __lowercase							  = use_labels
   __lowercase							  = vocab_size
   __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							  = max_position_embeddings
   __lowercase							  = type_vocab_size
   __lowercase							  = type_sequence_label_size
   __lowercase							  = initializer_range
   __lowercase							  = num_labels
   __lowercase							  = num_choices
   __lowercase							  = relative_attention
   __lowercase							  = position_biased_input
   __lowercase							  = pos_att_type
   __lowercase							  = scope






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   __lowercase							  = ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size			)

   __lowercase							  = None
   if self.use_input_mask:
    __lowercase							  = ids_tensor([self.batch_size, self.seq_length]					,vocab_size=2			)

   __lowercase							  = None
   if self.use_token_type_ids:
    __lowercase							  = ids_tensor([self.batch_size, self.seq_length]					,self.type_vocab_size			)

   __lowercase							  = None
   __lowercase							  = None
   __lowercase							  = None
   if self.use_labels:
    __lowercase							  = ids_tensor([self.batch_size]					,self.type_sequence_label_size			)
    __lowercase							  = ids_tensor([self.batch_size, self.seq_length]					,self.num_labels			)
    __lowercase							  = ids_tensor([self.batch_size]					,self.num_choices			)

   __lowercase							  = self.get_config()

   return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   return DebertaConfig(
       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					,initializer_range=self.initializer_range					,relative_attention=self.relative_attention					,position_biased_input=self.position_biased_input					,pos_att_type=self.pos_att_type					,)






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   __lowercase							  = self.get_config()
   __lowercase							  = 3_0_0
   return config






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]					,lowercase__   :     Optional[Any]			):
   self.parent.assertListEqual(list(result.loss.size()			)					,[]			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]					,lowercase__   :     List[Any]					,lowercase__   :     Dict					,lowercase__   :     List[Any]					,lowercase__   :     Tuple					,lowercase__   :     Dict					,lowercase__   :     str					,lowercase__   :     Union[str, Any]			):
   __lowercase							  = DebertaModel(config=lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__					,attention_mask=lowercase__					,token_type_ids=lowercase__			)[0]
   __lowercase							  = model(lowercase__					,token_type_ids=lowercase__			)[0]
   __lowercase							  = model(lowercase__			)[0]

   self.parent.assertListEqual(list(sequence_output.size()			)					,[self.batch_size, self.seq_length, self.hidden_size]			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple					,lowercase__   :     str					,lowercase__   :     Tuple					,lowercase__   :     Optional[Any]					,lowercase__   :     Dict					,lowercase__   :     int					,lowercase__   :     Tuple					,lowercase__   :     int			):
   __lowercase							  = DebertaForMaskedLM(config=lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__					,attention_mask=lowercase__					,token_type_ids=lowercase__					,labels=lowercase__			)

   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     int					,lowercase__   :     Optional[int]					,lowercase__   :     Optional[int]					,lowercase__   :     int					,lowercase__   :     Union[str, Any]					,lowercase__   :     Dict					,lowercase__   :     List[Any]					,lowercase__   :     Optional[Any]			):
   __lowercase							  = self.num_labels
   __lowercase							  = DebertaForSequenceClassification(lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__					,attention_mask=lowercase__					,token_type_ids=lowercase__					,labels=lowercase__			)
   self.parent.assertListEqual(list(result.logits.size()			)					,[self.batch_size, self.num_labels]			)
   self.check_loss_output(lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]					,lowercase__   :     Optional[int]					,lowercase__   :     Optional[int]					,lowercase__   :     Optional[int]					,lowercase__   :     Tuple					,lowercase__   :     List[str]					,lowercase__   :     List[str]					,lowercase__   :     Optional[int]			):
   __lowercase							  = self.num_labels
   __lowercase							  = DebertaForTokenClassification(config=lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(lowercase__					,attention_mask=lowercase__					,token_type_ids=lowercase__					,labels=lowercase__			)
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any					,lowercase__   :     Any					,lowercase__   :     Any					,lowercase__   :     Dict					,lowercase__   :     Any					,lowercase__   :     Optional[Any]					,lowercase__   :     int					,lowercase__   :     List[str]			):
   __lowercase							  = DebertaForQuestionAnswering(config=lowercase__			)
   model.to(lowercase__			)
   model.eval()
   __lowercase							  = model(
       lowercase__					,attention_mask=lowercase__					,token_type_ids=lowercase__					,start_positions=lowercase__					,end_positions=lowercase__					,)
   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 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase							  = self.prepare_config_and_inputs()
   (
       (
       __lowercase
   )  ,	(
       __lowercase
   )  ,	(
       __lowercase
   )  ,	(
       __lowercase
   )  ,	(
       __lowercase
   )  ,	(
       __lowercase
   )  ,	(
       __lowercase
   )  ,	
   )							  = config_and_inputs
   __lowercase							  = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
   return config, inputs_dict






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




  """simple docstring"""




  SCREAMING_SNAKE_CASE    :  Tuple							=			(
      (
          DebertaModel,
          DebertaForMaskedLM,
          DebertaForSequenceClassification,
          DebertaForTokenClassification,
          DebertaForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			(
      {
          'feature-extraction': DebertaModel,
          'fill-mask': DebertaForMaskedLM,
          'question-answering': DebertaForQuestionAnswering,
          'text-classification': DebertaForSequenceClassification,
          'token-classification': DebertaForTokenClassification,
          'zero-shot': DebertaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE    :  Tuple							=			True
  SCREAMING_SNAKE_CASE    :  List[str]							=			False
  SCREAMING_SNAKE_CASE    :  List[str]							=			False
  SCREAMING_SNAKE_CASE    :  Any							=			False
  SCREAMING_SNAKE_CASE    :  List[Any]							=			False






  def 					SCREAMING_SNAKE_CASE      (      self   :     int			):
   __lowercase							  = DebertaModelTester(self			)
   __lowercase							  = ConfigTester(self					,config_class=lowercase__					,hidden_size=3_7			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     int			):
   self.config_tester.run_common_tests()






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_model(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_question_answering(*lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase							  = self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_token_classification(*lowercase__			)






  @slow
  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowercase							  = DebertaModel.from_pretrained(lowercase__			)
    self.assertIsNotNone(lowercase__			)






@require_torch
@require_sentencepiece
@require_tokenizers
class 			lowercase_	(unittest.TestCase      ):




  """simple docstring"""




  @unittest.skip(reason='''Model not available yet'''			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     str			):
   pass






  @slow
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = DebertaModel.from_pretrained('''microsoft/deberta-base'''			)

   __lowercase							  = torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]			)
   __lowercase							  = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
   with torch.no_grad():
    __lowercase							  = model(lowercase__					,attention_mask=lowercase__			)[0]
   # compare the actual values for a slice.
   __lowercase							  = torch.tensor(
       [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]			)
   self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,lowercase__					,atol=1e-4			)					,F"{output[:, 1:4, 1:4]}"			)

