


'''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__			(			__lowerCAmelCase	):




			def __init__(		self  :     Optional[Any]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     Optional[int]=13	,  lowerCamelCase__  :     Optional[Any]=7	,  lowerCamelCase__  :     Dict=True	,  lowerCamelCase__  :     Tuple=True	,  lowerCamelCase__  :     Optional[Any]=True	,  lowerCamelCase__  :     Tuple=True	,  lowerCamelCase__  :     Any=99	,  lowerCamelCase__  :     int=32	,  lowerCamelCase__  :     int=5	,  lowerCamelCase__  :     Optional[int]=4	,  lowerCamelCase__  :     int=37	,  lowerCamelCase__  :     Tuple="gelu"	,  lowerCamelCase__  :     int=0.1	,  lowerCamelCase__  :     Optional[int]=0.1	,  lowerCamelCase__  :     Optional[int]=5_12	,  lowerCamelCase__  :     str=16	,  lowerCamelCase__  :     Tuple=2	,  lowerCamelCase__  :     List[str]=0.0_2	,  lowerCamelCase__  :     Optional[Any]=False	,  lowerCamelCase__  :     int=True	,  lowerCamelCase__  :     Optional[int]="None"	,  lowerCamelCase__  :     str=3	,  lowerCamelCase__  :     List[Any]=4	,  lowerCamelCase__  :     Optional[int]=None	,  )   ->Union[str, Any]:



					'''simple docstring'''







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




			def 							lowerCAmelCase__    (		self  :     Optional[int] )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       int								=      ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size )

					_UpperCAmelCase		:       Optional[int]								=      None
					if self.use_input_mask:
							_UpperCAmelCase		:       Any								=      ids_tensor([self.batch_size, self.seq_length]	,  vocab_size=2 )

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

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

					_UpperCAmelCase		:       str								=      self.get_config()

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




			def 							lowerCAmelCase__    (		self  :     Tuple )   ->Optional[int]:



					'''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 							lowerCAmelCase__    (		self  :     str	,  lowerCamelCase__  :     Any )   ->Any:



					'''simple docstring'''







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




			def 							lowerCAmelCase__    (		self  :     List[str]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     Dict )   ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      DebertaVaModel(config=lowerCAmelCase_ )
					model.to(lowerCAmelCase_ )
					model.eval()
					_UpperCAmelCase		:       List[Any]								=      model(lowerCAmelCase_	,  attention_mask=lowerCAmelCase_	,  token_type_ids=lowerCAmelCase_ )[0]
					_UpperCAmelCase		:       int								=      model(lowerCAmelCase_	,  token_type_ids=lowerCAmelCase_ )[0]
					_UpperCAmelCase		:       Dict								=      model(lowerCAmelCase_ )[0]

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




			def 							lowerCAmelCase__    (		self  :     Tuple	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     Optional[int] )   ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:       List[str]								=      DebertaVaForMaskedLM(config=lowerCAmelCase_ )
					model.to(lowerCAmelCase_ )
					model.eval()
					_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.seq_length, self.vocab_size) )




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     Optional[Any]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     Tuple )   ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:       str								=      self.num_labels
					_UpperCAmelCase		:       Optional[int]								=      DebertaVaForSequenceClassification(lowerCAmelCase_ )
					model.to(lowerCAmelCase_ )
					model.eval()
					_UpperCAmelCase		:       int								=      model(lowerCAmelCase_	,  attention_mask=lowerCAmelCase_	,  token_type_ids=lowerCAmelCase_	,  labels=lowerCAmelCase_ )
					self.parent.assertListEqual(list(result.logits.size() )	,  [self.batch_size, self.num_labels] )
					self.check_loss_output(lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     Optional[Any]	,  lowerCamelCase__  :     Union[str, Any]	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     Optional[Any]	,  lowerCamelCase__  :     List[Any] )   ->str:



					'''simple docstring'''







					_UpperCAmelCase		:       Any								=      self.num_labels
					_UpperCAmelCase		:       List[str]								=      DebertaVaForTokenClassification(config=lowerCAmelCase_ )
					model.to(lowerCAmelCase_ )
					model.eval()
					_UpperCAmelCase		:       Tuple								=      model(lowerCAmelCase_	,  attention_mask=lowerCAmelCase_	,  token_type_ids=lowerCAmelCase_	,  labels=lowerCAmelCase_ )
					self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length, self.num_labels) )




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     Dict	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     List[str] )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       List[Any]								=      DebertaVaForQuestionAnswering(config=lowerCAmelCase_ )
					model.to(lowerCAmelCase_ )
					model.eval()
					_UpperCAmelCase		:       Optional[Any]								=      model(
					    lowerCAmelCase_	,  attention_mask=lowerCAmelCase_	,  token_type_ids=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 							lowerCAmelCase__    (		self  :     int	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     str )   ->str:



					'''simple docstring'''







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






			def 							lowerCAmelCase__    (		self  :     Optional[int] )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       Tuple								=      self.prepare_config_and_inputs()
					(
					    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    (
					    _UpperCAmelCase						
					)					,    						
					)		:       Optional[int]								=      config_and_inputs
					_UpperCAmelCase		:       Any								=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict






@require_torch
class 	lowerCAmelCase__			(			__lowerCAmelCase    ,       __lowerCAmelCase    ,       unittest.TestCase	):
			lowerCAmelCase :	int          =     (
			    (
			        DebertaVaModel,
			        DebertaVaForMaskedLM,
			        DebertaVaForSequenceClassification,
			        DebertaVaForTokenClassification,
			        DebertaVaForQuestionAnswering,
			        DebertaVaForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCAmelCase :	str          =     (
			    {
			        '''feature-extraction''': DebertaVaModel,
			        '''fill-mask''': DebertaVaForMaskedLM,
			        '''question-answering''': DebertaVaForQuestionAnswering,
			        '''text-classification''': DebertaVaForSequenceClassification,
			        '''token-classification''': DebertaVaForTokenClassification,
			        '''zero-shot''': DebertaVaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCAmelCase :	str          =     True
			lowerCAmelCase :	Union[str, Any]          =     False
			lowerCAmelCase :	Optional[int]          =     False
			lowerCAmelCase :	List[str]          =     False
			lowerCAmelCase :	str          =     False




			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       Tuple								=      DebertaVaModelTester(self )
					_UpperCAmelCase		:       Optional[Any]								=      ConfigTester(self	,  config_class=lowerCAmelCase_	,  hidden_size=37 )




			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->List[str]:



					'''simple docstring'''







					self.config_tester.run_common_tests()




			def 							lowerCAmelCase__    (		self  :     Dict )   ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:       Union[str, Any]								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     List[str] )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       Any								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     int )   ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:       List[Any]								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     Optional[Any] )   ->int:



					'''simple docstring'''







					_UpperCAmelCase		:       str								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     List[Any] )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase		:       str								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_ )




			def 							lowerCAmelCase__    (		self  :     Dict )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       Any								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCAmelCase_ )






			@slow
			def 							lowerCAmelCase__    (		self  :     Dict )   ->Any:



					'''simple docstring'''







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






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




			@unittest.skip(reason="Model not available yet" )
			def 							lowerCAmelCase__    (		self  :     List[str] )   ->int:



					'''simple docstring'''







					pass






			@slow
			def 							lowerCAmelCase__    (		self  :     Tuple )   ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:       Union[str, Any]								=      DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge" )

					_UpperCAmelCase		:       Optional[int]								=      torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] )
					_UpperCAmelCase		:       Dict								=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
					with torch.no_grad():
							_UpperCAmelCase		:       str								=      model(lowerCAmelCase_	,  attention_mask=lowerCAmelCase_ )[0]
					# compare the actual values for a slice.
					_UpperCAmelCase		:       List[Any]								=      torch.tensor(
					    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]] )
					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,  lowerCAmelCase_	,  atol=1E-4 )	,  F"""{output[:, 1:4, 1:4]}""" )









'''simple docstring'''


# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowerCamelCase__    	=     abspath(join(dirname(dirname(dirname(__file__))), 'src'))
sys.path.insert(1, git_repo_path)


# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='ignore', category=FutureWarning)






def  __lowerCAmelCase (__lowerCAmelCase   ):
		from transformers.testing_utils import pytest_addoption_shared

		pytest_addoption_shared(__lowerCAmelCase   )






def  __lowerCAmelCase (__lowerCAmelCase   ):
		from transformers.testing_utils import pytest_terminal_summary_main

		_UpperCAmelCase		:       Optional[int]								=      terminalreporter.config.getoption("--make-reports"   )
		if make_reports:
				pytest_terminal_summary_main(__lowerCAmelCase      , id=__lowerCAmelCase   )



