

'''simple docstring'''



import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
	from transformers.models.roberta.modeling_flax_roberta import (
	    FlaxRobertaForCausalLM,
	    FlaxRobertaForMaskedLM,
	    FlaxRobertaForMultipleChoice,
	    FlaxRobertaForQuestionAnswering,
	    FlaxRobertaForSequenceClassification,
	    FlaxRobertaForTokenClassification,
	    FlaxRobertaModel,
	)





class 							__UpperCamelCase    (		unittest.TestCase ):




	def __init__(       self						,    __a						,    __a=13						,    __a=7						,    __a=True						,    __a=True						,    __a=True						,    __a=True						,    __a=99						,    __a=32						,    __a=5						,    __a=4						,    __a=37						,    __a="gelu"						,    __a=0.1						,    __a=0.1						,    __a=512						,    __a=16						,    __a=2						,    __a=0.02						,    __a=4						,    ):

						'''simple docstring'''





						__a      :	int    =  parent
						__a      :	Union[str, Any]    =  batch_size
						__a      :	Optional[int]    =  seq_length
						__a      :	Dict    =  is_training
						__a      :	Union[str, Any]    =  use_attention_mask
						__a      :	Any    =  use_token_type_ids
						__a      :	List[Any]    =  use_labels
						__a      :	Optional[Any]    =  vocab_size
						__a      :	Dict    =  hidden_size
						__a      :	Tuple    =  num_hidden_layers
						__a      :	Tuple    =  num_attention_heads
						__a      :	Tuple    =  intermediate_size
						__a      :	Tuple    =  hidden_act
						__a      :	int    =  hidden_dropout_prob
						__a      :	Optional[int]    =  attention_probs_dropout_prob
						__a      :	str    =  max_position_embeddings
						__a      :	List[str]    =  type_vocab_size
						__a      :	List[Any]    =  type_sequence_label_size
						__a      :	Tuple    =  initializer_range
						__a      :	str    =  num_choices




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[Any]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size							)

						__a      :	List[str]    =  None
						if self.use_attention_mask:
											__a      :	Dict    =  random_attention_mask([self.batch_size, self.seq_length]							)

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

						__a      :	Dict    =  RobertaConfig(
						    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						,    )

						return config, input_ids, token_type_ids, attention_mask




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  self.prepare_config_and_inputs()
						__a   ,    __a   ,    __a   ,    __a      :	int    =  config_and_inputs
						__a      :	str    =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
						return config, inputs_dict





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.prepare_config_and_inputs()
						__a   ,    __a   ,    __a   ,    __a      :	Optional[int]    =  config_and_inputs

						__a      :	List[Any]    =  True
						__a      :	str    =  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
						__a      :	Optional[int]    =  ids_tensor([self.batch_size, self.seq_length]						,    vocab_size=2							)

						return (
						    config,
						    input_ids,
						    token_type_ids,
						    encoder_hidden_states,
						    encoder_attention_mask,
						)





@require_flax
class 							__UpperCamelCase    (		__lowercase		,				unittest.TestCase ):
	A_           =   True

	A_           =   (
	    (
	        FlaxRobertaModel,
	        FlaxRobertaForCausalLM,
	        FlaxRobertaForMaskedLM,
	        FlaxRobertaForSequenceClassification,
	        FlaxRobertaForTokenClassification,
	        FlaxRobertaForMultipleChoice,
	        FlaxRobertaForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  FlaxRobertaModelTester(self							)





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						for model_class_name in self.all_model_classes:
											__a      :	Union[str, Any]    =  model_class_name.from_pretrained('roberta-base'						,    from_pt=UpperCAmelCase__							)
											__a      :	Dict    =  model(np.ones((1, 1)							)							)
											self.assertIsNotNone(UpperCAmelCase__							)







'''simple docstring'''



import unittest

from transformers import DebertaVaConfig, 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 (
	    DebertaVaForMaskedLM,
	    DebertaVaForMultipleChoice,
	    DebertaVaForQuestionAnswering,
	    DebertaVaForSequenceClassification,
	    DebertaVaForTokenClassification,
	    DebertaVaModel,
	)
	from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST





class 							__UpperCamelCase    (		lowerCAmelCase_ ):




	def __init__(       self						,    __a						,    __a=13						,    __a=7						,    __a=True						,    __a=True						,    __a=True						,    __a=True						,    __a=99						,    __a=32						,    __a=5						,    __a=4						,    __a=37						,    __a="gelu"						,    __a=0.1						,    __a=0.1						,    __a=512						,    __a=16						,    __a=2						,    __a=0.02						,    __a=False						,    __a=True						,    __a="None"						,    __a=3						,    __a=4						,    __a=None						,    ):

						'''simple docstring'''





						__a      :	int    =  parent
						__a      :	Union[str, Any]    =  batch_size
						__a      :	Optional[int]    =  seq_length
						__a      :	List[str]    =  is_training
						__a      :	Any    =  use_input_mask
						__a      :	Optional[int]    =  use_token_type_ids
						__a      :	Any    =  use_labels
						__a      :	List[str]    =  vocab_size
						__a      :	str    =  hidden_size
						__a      :	List[str]    =  num_hidden_layers
						__a      :	str    =  num_attention_heads
						__a      :	Optional[int]    =  intermediate_size
						__a      :	Tuple    =  hidden_act
						__a      :	Union[str, Any]    =  hidden_dropout_prob
						__a      :	Dict    =  attention_probs_dropout_prob
						__a      :	Optional[int]    =  max_position_embeddings
						__a      :	Dict    =  type_vocab_size
						__a      :	Any    =  type_sequence_label_size
						__a      :	Dict    =  initializer_range
						__a      :	Optional[Any]    =  num_labels
						__a      :	Optional[Any]    =  num_choices
						__a      :	Union[str, Any]    =  relative_attention
						__a      :	List[str]    =  position_biased_input
						__a      :	List[Any]    =  pos_att_type
						__a      :	Tuple    =  scope




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size							)

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

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

						__a      :	Optional[int]    =  None
						__a      :	int    =  None
						__a      :	Dict    =  None
						if self.use_labels:
											__a      :	List[Any]    =  ids_tensor([self.batch_size]						,    self.type_sequence_label_size							)
											__a      :	Union[str, Any]    =  ids_tensor([self.batch_size, self.seq_length]						,    self.num_labels							)
											__a      :	List[str]    =  ids_tensor([self.batch_size]						,    self.num_choices							)

						__a      :	Optional[int]    =  self.get_config()

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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						return DebertaVaConfig(
						    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 				__UpperCAmelCase							(       self						,    __a							):

						'''simple docstring'''





						self.parent.assertListEqual(list(result.loss.size()							)						,    []							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Dict    =  DebertaVaModel(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	Optional[int]    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a							)[0]
						__a      :	str    =  model(__a						,    token_type_ids=__a							)[0]
						__a      :	Optional[int]    =  model(__a							)[0]

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




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	int    =  DebertaVaForMaskedLM(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	List[Any]    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)

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




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.num_labels
						__a      :	List[Any]    =  DebertaVaForSequenceClassification(__a							)
						model.to(__a							)
						model.eval()
						__a      :	Any    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)
						self.parent.assertListEqual(list(result.logits.size()							)						,    [self.batch_size, self.num_labels]							)
						self.check_loss_output(__a							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Any    =  self.num_labels
						__a      :	Dict    =  DebertaVaForTokenClassification(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	str    =  model(__a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.num_labels)							)




	def 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	List[str]    =  DebertaVaForQuestionAnswering(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	str    =  model(
						    __a						,    attention_mask=__a						,    token_type_ids=__a						,    start_positions=__a						,    end_positions=__a						,    )
						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 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Optional[int]    =  DebertaVaForMultipleChoice(config=__a							)
						model.to(__a							)
						model.eval()
						__a      :	Any    =  input_ids.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	Optional[Any]    =  token_type_ids.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	Optional[int]    =  input_mask.unsqueeze(1							).expand(-1						,    self.num_choices						,    -1							).contiguous()
						__a      :	int    =  model(
						    __a						,    attention_mask=__a						,    token_type_ids=__a						,    labels=__a						,    )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_choices)							)





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.prepare_config_and_inputs()
						(
						    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    (
						    __a
						)   ,    
						)      :	Dict    =  config_and_inputs
						__a      :	Optional[int]    =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict





@require_torch
class 							__UpperCamelCase    (		lowerCAmelCase_		,				lowerCAmelCase_		,				unittest.TestCase ):
	A_           =   (
	    (
	        DebertaVaModel,
	        DebertaVaForMaskedLM,
	        DebertaVaForSequenceClassification,
	        DebertaVaForTokenClassification,
	        DebertaVaForQuestionAnswering,
	        DebertaVaForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	A_           =   (
	    {
	        "feature-extraction": DebertaVaModel,
	        "fill-mask": DebertaVaForMaskedLM,
	        "question-answering": DebertaVaForQuestionAnswering,
	        "text-classification": DebertaVaForSequenceClassification,
	        "token-classification": DebertaVaForTokenClassification,
	        "zero-shot": DebertaVaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	A_           =   True
	A_           =   False
	A_           =   False
	A_           =   False
	A_           =   False




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  DebertaVaModelTester(self							)
						__a      :	List[str]    =  ConfigTester(self						,    config_class=__a						,    hidden_size=37							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[Any]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*__a							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*__a							)





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a      :	str    =  DebertaVaModel.from_pretrained(__a							)
											self.assertIsNotNone(__a							)





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




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

						'''simple docstring'''





						pass





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'							)

						__a      :	Optional[Any]    =  torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]							)
						__a      :	str    =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
						with torch.no_grad():
											__a      :	int    =  model(__a						,    attention_mask=__a							)[0]
						# compare the actual values for a slice.
						__a      :	str    =  torch.tensor(
						    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]							)
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,    __a						,    atol=1E-4							)						,    f"""{output[:, 1:4, 1:4]}"""							)



