
"""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       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							def __init__( self    ,				snake_case_    ,				snake_case_=13    ,				snake_case_=7    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=99    ,				snake_case_=32    ,				snake_case_=5    ,				snake_case_=4    ,				snake_case_=37    ,				snake_case_="gelu"    ,				snake_case_=0.1    ,				snake_case_=0.1    ,				snake_case_=512    ,				snake_case_=16    ,				snake_case_=2    ,				snake_case_=0.02    ,				snake_case_=False    ,				snake_case_=True    ,				snake_case_="None"    ,				snake_case_=3    ,				snake_case_=4    ,				snake_case_=None    ,				)       ->  int:
													__lowerCAmelCase     =  parent
													__lowerCAmelCase     =  batch_size
													__lowerCAmelCase     =  seq_length
													__lowerCAmelCase     =  is_training
													__lowerCAmelCase     =  use_input_mask
													__lowerCAmelCase     =  use_token_type_ids
													__lowerCAmelCase     =  use_labels
													__lowerCAmelCase     =  vocab_size
													__lowerCAmelCase     =  hidden_size
													__lowerCAmelCase     =  num_hidden_layers
													__lowerCAmelCase     =  num_attention_heads
													__lowerCAmelCase     =  intermediate_size
													__lowerCAmelCase     =  hidden_act
													__lowerCAmelCase     =  hidden_dropout_prob
													__lowerCAmelCase     =  attention_probs_dropout_prob
													__lowerCAmelCase     =  max_position_embeddings
													__lowerCAmelCase     =  type_vocab_size
													__lowerCAmelCase     =  type_sequence_label_size
													__lowerCAmelCase     =  initializer_range
													__lowerCAmelCase     =  num_labels
													__lowerCAmelCase     =  num_choices
													__lowerCAmelCase     =  relative_attention
													__lowerCAmelCase     =  position_biased_input
													__lowerCAmelCase     =  pos_att_type
													__lowerCAmelCase     =  scope




							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size				)

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

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

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

													__lowerCAmelCase     =  self.get_config()

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




							def    A__						( self				)       ->  List[str]:
													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    A__						( self    ,				snake_case_				)       ->  str:
													self.parent.assertListEqual(list(result.loss.size()				)    ,				[]				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Any:
													__lowerCAmelCase     =  DebertaVaModel(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_				)[0]
													__lowerCAmelCase     =  model(snake_case_    ,				token_type_ids=snake_case_				)[0]
													__lowerCAmelCase     =  model(snake_case_				)[0]

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




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Tuple:
													__lowerCAmelCase     =  DebertaVaForMaskedLM(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_    ,				labels=snake_case_				)

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




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  List[str]:
													__lowerCAmelCase     =  self.num_labels
													__lowerCAmelCase     =  DebertaVaForSequenceClassification(snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_    ,				labels=snake_case_				)
													self.parent.assertListEqual(list(result.logits.size()				)    ,				[self.batch_size, self.num_labels]				)
													self.check_loss_output(snake_case_				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  List[str]:
													__lowerCAmelCase     =  self.num_labels
													__lowerCAmelCase     =  DebertaVaForTokenClassification(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_    ,				labels=snake_case_				)
													self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.num_labels)				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  List[str]:
													__lowerCAmelCase     =  DebertaVaForQuestionAnswering(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(
													    snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_    ,				start_positions=snake_case_    ,				end_positions=snake_case_    ,				)
													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    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Dict:
													__lowerCAmelCase     =  DebertaVaForMultipleChoice(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  input_ids.unsqueeze(1				).expand(-1    ,				self.num_choices    ,				-1				).contiguous()
													__lowerCAmelCase     =  token_type_ids.unsqueeze(1				).expand(-1    ,				self.num_choices    ,				-1				).contiguous()
													__lowerCAmelCase     =  input_mask.unsqueeze(1				).expand(-1    ,				self.num_choices    ,				-1				).contiguous()
													__lowerCAmelCase     =  model(
													    snake_case_    ,				attention_mask=snake_case_    ,				token_type_ids=snake_case_    ,				labels=snake_case_    ,				)
													self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_choices)				)




							def    A__						( self				)       ->  List[Any]:
													__lowerCAmelCase     =  self.prepare_config_and_inputs()
													(
													    (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     (
													    __lowerCAmelCase
													)   ,     
													)     =  config_and_inputs
													__lowerCAmelCase     =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
													return config, inputs_dict
@require_torch
class       lowerCAmelCase_		(				A__				,				A__				,				unittest.TestCase ):



							'''simple docstring'''





							_snake_case						      =		(
							    (
							        DebertaVaModel,
							        DebertaVaForMaskedLM,
							        DebertaVaForSequenceClassification,
							        DebertaVaForTokenClassification,
							        DebertaVaForQuestionAnswering,
							        DebertaVaForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							_snake_case						      =		(
							    {
							        '''feature-extraction''': DebertaVaModel,
							        '''fill-mask''': DebertaVaForMaskedLM,
							        '''question-answering''': DebertaVaForQuestionAnswering,
							        '''text-classification''': DebertaVaForSequenceClassification,
							        '''token-classification''': DebertaVaForTokenClassification,
							        '''zero-shot''': DebertaVaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							_snake_case						      =		True
							_snake_case						      =		False
							_snake_case						      =		False
							_snake_case						      =		False
							_snake_case						      =		False




							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  DebertaVaModelTester(self				)
													__lowerCAmelCase     =  ConfigTester(self    ,				config_class=snake_case_    ,				hidden_size=37				)




							def    A__						( self				)       ->  Union[str, Any]:
													self.config_tester.run_common_tests()




							def    A__						( self				)       ->  int:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_model(*snake_case_				)




							def    A__						( self				)       ->  Optional[int]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_				)




							def    A__						( self				)       ->  str:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_				)




							def    A__						( self				)       ->  str:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_				)




							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_				)




							def    A__						( self				)       ->  Dict:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_deberta_for_multiple_choice(*snake_case_				)




							@slow
							def    A__						( self				)       ->  List[Any]:
													for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowerCAmelCase     =  DebertaVaModel.from_pretrained(snake_case_				)
																			self.assertIsNotNone(snake_case_				)
@require_torch
@require_sentencepiece
@require_tokenizers
class       lowerCAmelCase_		(				unittest.TestCase ):



							'''simple docstring'''





							@unittest.skip(reason="""Model not available yet"""				)
							def    A__						( self				)       ->  Tuple:
													pass




							@slow
							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""				)

													__lowerCAmelCase     =  torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]				)
													__lowerCAmelCase     =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
													with torch.no_grad():
																			__lowerCAmelCase     =  model(snake_case_    ,				attention_mask=snake_case_				)[0]
													# compare the actual values for a slice.
													__lowerCAmelCase     =  torch.tensor(
													    [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]				)
													self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,				snake_case_    ,				atol=1e-4				)    ,				f"""{output[:, 1:4, 1:4]}"""				)


"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


SCREAMING_SNAKE_CASE_					     =	{
    '''configuration_rag''': ['''RagConfig'''],
    '''retrieval_rag''': ['''RagRetriever'''],
    '''tokenization_rag''': ['''RagTokenizer'''],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			SCREAMING_SNAKE_CASE_					     =	[
			    '''RagModel''',
			    '''RagPreTrainedModel''',
			    '''RagSequenceForGeneration''',
			    '''RagTokenForGeneration''',
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			SCREAMING_SNAKE_CASE_					     =	[
			    '''TFRagModel''',
			    '''TFRagPreTrainedModel''',
			    '''TFRagSequenceForGeneration''',
			    '''TFRagTokenForGeneration''',
			]


if TYPE_CHECKING:
			from .configuration_rag import RagConfig
			from .retrieval_rag import RagRetriever
			from .tokenization_rag import RagTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_rag import (
						    TFRagModel,
						    TFRagPreTrainedModel,
						    TFRagSequenceForGeneration,
						    TFRagTokenForGeneration,
						)

else:
			import sys

			SCREAMING_SNAKE_CASE_					     =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
