


from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFDebertaVaForMaskedLM,
      TFDebertaVaForQuestionAnswering,
      TFDebertaVaForSequenceClassification,
      TFDebertaVaForTokenClassification,
      TFDebertaVaModel,
  )



class    UpperCAmelCase_	:
       '''simple docstring'''






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




              """simple docstring"""




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






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size						)

              UpperCamelCase    :	Any   	=		None
              if self.use_input_mask:
                     UpperCamelCase    :	Tuple   	=		random_attention_mask([self.batch_size, self.seq_length]						)

              UpperCamelCase    :	List[str]   	=		None
              if self.use_token_type_ids:
                     UpperCamelCase    :	Dict   	=		ids_tensor([self.batch_size, self.seq_length] ,					self.type_vocab_size						)

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

              UpperCamelCase    :	List[str]   	=		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 ,					relative_attention=self.relative_attention ,					position_biased_input=self.position_biased_input ,					initializer_range=self.initializer_range ,					return_dict=__SCREAMING_SNAKE_CASE ,					)

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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		TFDebertaVaModel(config=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	str   	=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

              UpperCamelCase    :	List[str]   	=		[input_ids, input_mask]
              UpperCamelCase    :	List[str]   	=		model(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Tuple   	=		model(__SCREAMING_SNAKE_CASE						)

              self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		TFDebertaVaForMaskedLM(config=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Tuple   	=		{
                  '''input_ids''': input_ids,
                  '''attention_mask''': input_mask,
                  '''token_type_ids''': token_type_ids,
              }
              UpperCamelCase    :	str   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		self.num_labels
              UpperCamelCase    :	Any   	=		TFDebertaVaForSequenceClassification(config=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		{
                  '''input_ids''': input_ids,
                  '''attention_mask''': input_mask,
                  '''token_type_ids''': token_type_ids,
              }

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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		self.num_labels
              UpperCamelCase    :	Optional[int]   	=		TFDebertaVaForTokenClassification(config=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		{
                  '''input_ids''': input_ids,
                  '''attention_mask''': input_mask,
                  '''token_type_ids''': token_type_ids,
              }
              UpperCamelCase    :	Optional[int]   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		TFDebertaVaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	str   	=		{
                  '''input_ids''': input_ids,
                  '''attention_mask''': input_mask,
                  '''token_type_ids''': token_type_ids,
              }

              UpperCamelCase    :	Optional[Any]   	=		model(__SCREAMING_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  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		self.prepare_config_and_inputs()
              (
                  (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, 
              )    :	Tuple   	=		config_and_inputs
              UpperCamelCase    :	int   	=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
              return config, inputs_dict




@require_tf
class    UpperCAmelCase_	(							_a,     _a,     unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     int							=					(
           (
               TFDebertaVaModel,
               TFDebertaVaForMaskedLM,
               TFDebertaVaForQuestionAnswering,
               TFDebertaVaForSequenceClassification,
               TFDebertaVaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       __UpperCamelCase      :     List[str]							=					(
           {
               "feature-extraction": TFDebertaVaModel,
               "fill-mask": TFDebertaVaForMaskedLM,
               "question-answering": TFDebertaVaForQuestionAnswering,
               "text-classification": TFDebertaVaForSequenceClassification,
               "token-classification": TFDebertaVaForTokenClassification,
               "zero-shot": TFDebertaVaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       __UpperCamelCase      :     int							=					False
       __UpperCamelCase      :     List[Any]							=					False






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		TFDebertaVaModelTester(self						)
              UpperCamelCase    :	Union[str, Any]   	=		ConfigTester(self ,					config_class=__SCREAMING_SNAKE_CASE ,					hidden_size=37						)






       def  _lowercase   (     self						):




              """simple docstring"""




              self.config_tester.run_common_tests()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''						)
              self.assertIsNotNone(__SCREAMING_SNAKE_CASE						)




@require_tf
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''






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




              """simple docstring"""




              pass






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''						)
              UpperCamelCase    :	List[str]   	=		tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]						)
              UpperCamelCase    :	Union[str, Any]   	=		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
              UpperCamelCase    :	Any   	=		model(__SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE						)[0]

              UpperCamelCase    :	List[Any]   	=		tf.constant(
                  [[[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]]]						)
              tf.debugging.assert_near(output[:, 1:4, 1:4] ,					__SCREAMING_SNAKE_CASE ,					atol=1e-4						)





import torch

from transformers import AutoModel



class    UpperCAmelCase_	(							torch.nn.Module):
       '''simple docstring'''






       def __init__(     self ,					__SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased"						):




              """simple docstring"""




              super(__SCREAMING_SNAKE_CASE ,					self						).__init__()

              UpperCamelCase    :	List[str]   	=		AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE ,					return_dict=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		torch.nn.CosineSimilarity(3 ,					1e-08						)
              UpperCamelCase    :	List[Any]   	=		torch.nn.Softmax(dim=1						)






       def  _lowercase   (     self ,					**__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return self.bert(**__SCREAMING_SNAKE_CASE						).last_hidden_state






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return token_embeddings.sum(2 ,					keepdim=__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=1						):




              """simple docstring"""




              return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		W_supports['''sizes'''].tolist()
              UpperCamelCase    :	Optional[int]   	=		W_supports['''start_token_id'''].item()
              UpperCamelCase    :	Any   	=		W_supports['''end_token_id'''].item()

              del W_supports["sizes"]
              del W_supports["start_token_id"]
              del W_supports["end_token_id"]

              UpperCamelCase    :	Union[str, Any]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	List[str]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[Any]   	=		None
              UpperCamelCase    :	Any   	=		None

              UpperCamelCase    :	Optional[Any]   	=		W_supports['''input_ids'''] == start_token_id
              UpperCamelCase    :	Any   	=		W_supports['''input_ids'''] == end_token_id

              for i, size in enumerate(__SCREAMING_SNAKE_CASE						):
                     if i == 0:
                            UpperCamelCase    :	Optional[int]   	=		0
                     else:
                            UpperCamelCase    :	Tuple   	=		support_sizes[i - 1]

                     UpperCamelCase    :	Tuple   	=		S[s : s + size][start_token_masks[s : s + size]]
                     UpperCamelCase    :	List[str]   	=		S[s : s + size][end_token_masks[s : s + size]]

                     UpperCamelCase    :	Dict   	=		torch.matmul(q[i] ,					s_start.T						).sum(1						).softmax(0						)
                     UpperCamelCase    :	Tuple   	=		torch.matmul(q[i] ,					s_end.T						).sum(1						).softmax(0						)

                     if p_starts is not None:
                            UpperCamelCase    :	List[str]   	=		torch.vstack((p_starts, p_start)						)
                            UpperCamelCase    :	Union[str, Any]   	=		torch.vstack((p_ends, p_end)						)
                     else:
                            UpperCamelCase    :	str   	=		p_start
                            UpperCamelCase    :	Optional[int]   	=		p_end

              return p_starts, p_ends

