


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







    """simple docstring"""



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

    def __lowercase  (  self       :    int						)  ->    List[Any]:
      lowerCAmelCase_     : Optional[Any]     =  ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size						)

      lowerCAmelCase_     : List[Any]     =  None
      if self.use_input_mask:
        lowerCAmelCase_     : Dict     =  ids_tensor([self.batch_size, self.seq_length]       ,		vocab_size=2						)

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

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

      lowerCAmelCase_     : Any     =  self.get_config()

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

    def __lowercase  (  self       :    str						)  ->    List[Any]:
      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 __lowercase  (  self       :    Dict       ,		lowerCamelCase       :    Optional[Any]						)  ->    Optional[int]:
      self.parent.assertListEqual(list(result.loss.size()						)       ,		[]						)

    def __lowercase  (  self       :    str       ,		lowerCamelCase       :    List[Any]       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    Dict						)  ->    Any:
      lowerCAmelCase_     : str     =  DebertaVaModel(config=lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : Any     =  model(lowerCamelCase       ,		attention_mask=lowerCamelCase       ,		token_type_ids=lowerCamelCase						)[0]
      lowerCAmelCase_     : Tuple     =  model(lowerCamelCase       ,		token_type_ids=lowerCamelCase						)[0]
      lowerCAmelCase_     : Optional[int]     =  model(lowerCamelCase						)[0]

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

    def __lowercase  (  self       :    int       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    List[Any]       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    Union[str, Any]       ,		lowerCamelCase       :    Dict						)  ->    List[str]:
      lowerCAmelCase_     : str     =  DebertaVaForMaskedLM(config=lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : Optional[int]     =  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 __lowercase  (  self       :    Dict       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    Tuple						)  ->    int:
      lowerCAmelCase_     : Any     =  self.num_labels
      lowerCAmelCase_     : Optional[Any]     =  DebertaVaForSequenceClassification(lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : Dict     =  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 __lowercase  (  self       :    int       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    Any						)  ->    Union[str, Any]:
      lowerCAmelCase_     : Optional[Any]     =  self.num_labels
      lowerCAmelCase_     : List[str]     =  DebertaVaForTokenClassification(config=lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : Optional[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.num_labels)						)

    def __lowercase  (  self       :    str       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    Any						)  ->    Optional[Any]:
      lowerCAmelCase_     : Optional[int]     =  DebertaVaForQuestionAnswering(config=lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : List[str]     =  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 __lowercase  (  self       :    List[str]       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    str       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    List[str]						)  ->    Optional[int]:
      lowerCAmelCase_     : Any     =  DebertaVaForMultipleChoice(config=lowerCamelCase						)
      model.to(lowerCamelCase						)
      model.eval()
      lowerCAmelCase_     : Dict     =  input_ids.unsqueeze(1						).expand(-1       ,		self.num_choices       ,		-1						).contiguous()
      lowerCAmelCase_     : Any     =  token_type_ids.unsqueeze(1						).expand(-1       ,		self.num_choices       ,		-1						).contiguous()
      lowerCAmelCase_     : List[Any]     =  input_mask.unsqueeze(1						).expand(-1       ,		self.num_choices       ,		-1						).contiguous()
      lowerCAmelCase_     : str     =  model(
          lowerCamelCase       ,		attention_mask=lowerCamelCase       ,		token_type_ids=lowerCamelCase       ,		labels=lowerCamelCase       ,		)
      self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_choices)						)





    def __lowercase  (  self       :    Optional[int]						)  ->    List[str]:
      lowerCAmelCase_     : Dict     =  self.prepare_config_and_inputs()
      (
          (
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				
      )     : Union[str, Any]     =  config_and_inputs
      lowerCAmelCase_     : str     =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict







@require_torch
class   __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 __lowercase  (  self       :    str						)  ->    List[str]:
      lowerCAmelCase_     : Tuple     =  DebertaVaModelTester(self						)
      lowerCAmelCase_     : List[Any]     =  ConfigTester(self       ,		config_class=lowerCamelCase       ,		hidden_size=37						)

    def __lowercase  (  self       :    Optional[int]						)  ->    List[str]:
      self.config_tester.run_common_tests()

    def __lowercase  (  self       :    Optional[int]						)  ->    int:
      lowerCAmelCase_     : str     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*lowerCamelCase						)

    def __lowercase  (  self       :    Tuple						)  ->    Tuple:
      lowerCAmelCase_     : Optional[Any]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCamelCase						)

    def __lowercase  (  self       :    Union[str, Any]						)  ->    Optional[Any]:
      lowerCAmelCase_     : str     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCamelCase						)

    def __lowercase  (  self       :    Any						)  ->    Tuple:
      lowerCAmelCase_     : Dict     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*lowerCamelCase						)

    def __lowercase  (  self       :    Dict						)  ->    Dict:
      lowerCAmelCase_     : int     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*lowerCamelCase						)

    def __lowercase  (  self       :    Optional[int]						)  ->    Optional[int]:
      lowerCAmelCase_     : Tuple     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCamelCase						)





    @slow
    def __lowercase  (  self       :    Optional[Any]						)  ->    List[str]:
      for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCAmelCase_     : Optional[int]     =  DebertaVaModel.from_pretrained(lowerCamelCase						)
        self.assertIsNotNone(lowerCamelCase						)







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







    """simple docstring"""



    @unittest.skip(reason="""Model not available yet"""						)
    def __lowercase  (  self       :    Union[str, Any]						)  ->    Optional[int]:
      pass





    @slow
    def __lowercase  (  self       :    Optional[Any]						)  ->    Optional[int]:
      lowerCAmelCase_     : List[str]     =  DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""						)

      lowerCAmelCase_     : str     =  torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]						)
      lowerCAmelCase_     : Tuple     =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
      with torch.no_grad():
        lowerCAmelCase_     : List[Any]     =  model(lowerCamelCase       ,		attention_mask=lowerCamelCase						)[0]
      # compare the actual values for a slice.
      lowerCAmelCase_     : Optional[Any]     =  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]       ,		lowerCamelCase       ,		atol=1E-4						)       ,		F'{output[:, 1:4, 1:4]}'						)







'''simple docstring'''






from typing import Union

import fire
import torch
from tqdm import tqdm







def 							UpperCamelCase_     (       A__   :  str				,       A__   :  str = "cpu"				,       A__   :  Union[str, None] = None   ):
  '''simple docstring'''





  lowerCAmelCase_     : Dict     =  torch.load(A__				,       map_location=A__   )
  for k, v in tqdm(state_dict.items()   ):
    if not isinstance(A__				,       torch.Tensor   ):
      raise TypeError("""FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin"""   )
    lowerCAmelCase_     : List[str]     =  v.half()
  if save_path is None:  # overwrite src_path
    lowerCAmelCase_     : List[str]     =  src_path
  torch.save(A__				,       A__   )


if __name__ == "__main__":
  fire.Fire(convert)



