




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    __lowerCAmelCase       :






       def __init__(   self:					Dict       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					List[Any]=13       ,					_lowerCAmelCase:					Dict=7       ,					_lowerCAmelCase:					Union[str, Any]=True       ,					_lowerCAmelCase:					Optional[int]=True       ,					_lowerCAmelCase:					Any=True       ,					_lowerCAmelCase:					str=True       ,					_lowerCAmelCase:					List[Any]=99       ,					_lowerCAmelCase:					Tuple=32       ,					_lowerCAmelCase:					List[str]=2       ,					_lowerCAmelCase:					List[Any]=4       ,					_lowerCAmelCase:					Tuple=37       ,					_lowerCAmelCase:					int="gelu"       ,					_lowerCAmelCase:					Tuple=0.1       ,					_lowerCAmelCase:					List[Any]=0.1       ,					_lowerCAmelCase:					Tuple=5_12       ,					_lowerCAmelCase:					Optional[Any]=16       ,					_lowerCAmelCase:					Optional[int]=2       ,					_lowerCAmelCase:					Optional[int]=0.02       ,					_lowerCAmelCase:					List[str]=False       ,					_lowerCAmelCase:					Dict=True       ,					_lowerCAmelCase:					Optional[Any]="None"       ,					_lowerCAmelCase:					Union[str, Any]=3       ,					_lowerCAmelCase:					Optional[int]=4       ,					_lowerCAmelCase:					Union[str, Any]=None       ,					):
         lowercase    :str      	=						parent
         lowercase    :str      	=						batch_size
         lowercase    :Any      	=						seq_length
         lowercase    :List[Any]      	=						is_training
         lowercase    :List[str]      	=						use_input_mask
         lowercase    :Dict      	=						use_token_type_ids
         lowercase    :Optional[Any]      	=						use_labels
         lowercase    :Optional[int]      	=						vocab_size
         lowercase    :List[Any]      	=						hidden_size
         lowercase    :Optional[int]      	=						num_hidden_layers
         lowercase    :str      	=						num_attention_heads
         lowercase    :List[str]      	=						intermediate_size
         lowercase    :Union[str, Any]      	=						hidden_act
         lowercase    :Optional[int]      	=						hidden_dropout_prob
         lowercase    :List[str]      	=						attention_probs_dropout_prob
         lowercase    :Any      	=						max_position_embeddings
         lowercase    :Optional[int]      	=						type_vocab_size
         lowercase    :int      	=						type_sequence_label_size
         lowercase    :Union[str, Any]      	=						initializer_range
         lowercase    :List[Any]      	=						num_labels
         lowercase    :str      	=						num_choices
         lowercase    :Tuple      	=						relative_attention
         lowercase    :int      	=						position_biased_input
         lowercase    :Optional[Any]      	=						pos_att_type
         lowercase    :Dict      	=						scope






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         lowercase    :int      	=						ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size				)

         lowercase    :List[Any]      	=						None
         if self.use_input_mask:
           lowercase    :Optional[Any]      	=						random_attention_mask([self.batch_size, self.seq_length]				)

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

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

         lowercase    :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=_lowerCAmelCase       ,					)

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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					List[str]				):
         lowercase    :Tuple      	=						TFDebertaVaModel(config=_lowerCAmelCase				)
         lowercase    :Any      	=						{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

         lowercase    :Dict      	=						[input_ids, input_mask]
         lowercase    :str      	=						model(_lowerCAmelCase				)

         lowercase    :str      	=						model(_lowerCAmelCase				)

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






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Optional[Any]				):
         lowercase    :int      	=						TFDebertaVaForMaskedLM(config=_lowerCAmelCase				)
         lowercase    :Any      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         lowercase    :Optional[int]      	=						model(_lowerCAmelCase				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.vocab_size)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Dict				):
         lowercase    :Optional[Any]      	=						self.num_labels
         lowercase    :Tuple      	=						TFDebertaVaForSequenceClassification(config=_lowerCAmelCase				)
         lowercase    :Dict      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         lowercase    :Any      	=						model(_lowerCAmelCase				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Tuple				):
         lowercase    :int      	=						self.num_labels
         lowercase    :Optional[Any]      	=						TFDebertaVaForTokenClassification(config=_lowerCAmelCase				)
         lowercase    :int      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }
         lowercase    :Optional[Any]      	=						model(_lowerCAmelCase				)
         self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.num_labels)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Union[str, Any]				):
         lowercase    :Dict      	=						TFDebertaVaForQuestionAnswering(config=_lowerCAmelCase				)
         lowercase    :int      	=						{
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "token_type_ids": token_type_ids,
         }

         lowercase    :str      	=						model(_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 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Dict      	=						self.prepare_config_and_inputs()
         (
             (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    
         )    :Any      	=						config_and_inputs
         lowercase    :Optional[int]      	=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
         return config, inputs_dict







@require_tf
class    __lowerCAmelCase       (							lowerCAmelCase      , lowerCAmelCase      , unittest.TestCase):
       _a 					=						(
           (
               TFDebertaVaModel,
               TFDebertaVaForMaskedLM,
               TFDebertaVaForQuestionAnswering,
               TFDebertaVaForSequenceClassification,
               TFDebertaVaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       _a 					=						(
           {
               '''feature-extraction''': TFDebertaVaModel,
               '''fill-mask''': TFDebertaVaForMaskedLM,
               '''question-answering''': TFDebertaVaForQuestionAnswering,
               '''text-classification''': TFDebertaVaForSequenceClassification,
               '''token-classification''': TFDebertaVaForTokenClassification,
               '''zero-shot''': TFDebertaVaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       _a 					=						False
       _a 					=						False






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[str]      	=						TFDebertaVaModelTester(self				)
         lowercase    :Optional[Any]      	=						ConfigTester(self       ,					config_class=_lowerCAmelCase       ,					hidden_size=37				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         self.config_tester.run_common_tests()






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :str      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         lowercase    :Any      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         lowercase    :List[str]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         lowercase    :List[str]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :Union[str, Any]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase				)






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Union[str, Any]      	=						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"				)
         self.assertIsNotNone(_lowerCAmelCase				)







@require_tf
class    __lowerCAmelCase       (							unittest.TestCase):






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






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :List[str]      	=						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"				)
         lowercase    :Dict      	=						tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]				)
         lowercase    :Any      	=						tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
         lowercase    :Optional[Any]      	=						model(_lowerCAmelCase       ,					attention_mask=_lowerCAmelCase				)[0]

         lowercase    :List[Any]      	=						tf.constant(
             [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]				)
         tf.debugging.assert_near(output[:, 1:4, 1:4]       ,					_lowerCAmelCase       ,					atol=1e-4				)









_UpperCAmelCase	:						Union[str, Any]			=     "0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
     from .utils import rich



