




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        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

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



   '''simple docstring'''



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

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



   '''simple docstring'''



   lowerCamelCase__:				str        =ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size)

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

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

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

   lowerCamelCase__:				int        =self.get_config()

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

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



   '''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 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       List[Any])	->Dict:



   '''simple docstring'''



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

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



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =DebertaVaModel(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_	,			attention_mask=UpperCAmelCase_	,			token_type_ids=UpperCAmelCase_)[0]
   lowerCamelCase__:				int        =model(UpperCAmelCase_	,			token_type_ids=UpperCAmelCase_)[0]
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_)[0]

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Union[str, Any]	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       Any)	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =DebertaVaForMaskedLM(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:				Optional[Any]        =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 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       int)	->Tuple:



   '''simple docstring'''



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

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



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =self.num_labels
   lowerCamelCase__:				str        =DebertaVaForTokenClassification(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:				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.num_labels))

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       Union[str, Any])	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =DebertaVaForQuestionAnswering(config=UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.eval()
   lowerCamelCase__:				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 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Optional[Any])	->List[str]:



   '''simple docstring'''



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







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				str        =self.prepare_config_and_inputs()
   (
       lowerCamelCase__ 
   ):				int        =config_and_inputs
   lowerCamelCase__:				List[str]        ={"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
   return config, inputs_dict



@require_torch
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ,							__SCREAMING_SNAKE_CASE   ,							unittest.TestCase   ):







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

 lowercase_		=      True
 lowercase_		=      False
 lowercase_		=      False
 lowercase_		=      False
 lowercase_		=      False

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =DebertaVaModelTester(self)
   lowerCamelCase__:				Optional[int]        =ConfigTester(self	,			config_class=UpperCAmelCase_	,			hidden_size=37)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Dict:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_model(*UpperCAmelCase_)

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



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Dict:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_question_answering(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->str:



   '''simple docstring'''



   lowerCamelCase__:				str        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_token_classification(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->Any:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->int:



   '''simple docstring'''



   for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowerCamelCase__:				Dict        =DebertaVaModel.from_pretrained(UpperCAmelCase_)
     self.assertIsNotNone(UpperCAmelCase_)



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







 '''simple docstring'''

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



   '''simple docstring'''



   pass







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Any        =DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge")

   lowerCamelCase__:				str        =torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]])
   lowerCamelCase__:				Dict        =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
   with torch.no_grad():
     lowerCamelCase__:				Union[str, Any]        =model(UpperCAmelCase_	,			attention_mask=UpperCAmelCase_)[0]
   # compare the actual values for a slice.
   lowerCamelCase__:				List[Any]        =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]	,			UpperCAmelCase_	,			atol=1E-4)	,			F"""{output[:, 1:4, 1:4]}""")








import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment



@require_huggingface_suite
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

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



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =inspect.getfile(accelerate.test_utils)
   lowerCamelCase__:				str        =os.path.sep.join(
       mod_file.split(os.path.sep)[:-1] + ["scripts", "external_deps", "test_metrics.py"])

   from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

   lowerCamelCase__:				str        =test_metrics

 @require_cpu
 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Any:



   '''simple docstring'''



   debug_launcher(self.test_metrics.main	,			num_processes=1)

 @require_cpu
 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->int:



   '''simple docstring'''



   debug_launcher(self.test_metrics.main)

 @require_single_gpu
 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->int:



   '''simple docstring'''



   self.test_metrics.main()







 @require_multi_gpu
 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Dict:



   '''simple docstring'''



   print(F"""Found {torch.cuda.device_count()} devices.""")
   lowerCamelCase__:				Optional[Any]        =["torchrun", F"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
   with patch_environment(omp_num_threads=1):
     execute_subprocess_async(UpperCAmelCase_	,			env=os.environ.copy())

