import unittest

from transformers import DebertaConfig, 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 (
      DebertaForMaskedLM,
      DebertaForQuestionAnswering,
      DebertaForSequenceClassification,
      DebertaForTokenClassification,
      DebertaModel,
  )
  from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST
class  A_			(							a_			):

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

  def 		_UpperCAmelCase (		self			:   str							):
   __a							=	ids_tensor([self.batch_size, self.seq_length]  ,				self.vocab_size							)

   __a							=	None
   if self.use_input_mask:
    __a							=	ids_tensor([self.batch_size, self.seq_length]  ,				vocab_size=2							)

   __a							=	None
   if self.use_token_type_ids:
    __a							=	ids_tensor([self.batch_size, self.seq_length]  ,				self.type_vocab_size							)

   __a							=	None
   __a							=	None
   __a							=	None
   if self.use_labels:
    __a							=	ids_tensor([self.batch_size]  ,				self.type_sequence_label_size							)
    __a							=	ids_tensor([self.batch_size, self.seq_length]  ,				self.num_labels							)
    __a							=	ids_tensor([self.batch_size]  ,				self.num_choices							)

   __a							=	self.get_config()

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

  def 		_UpperCAmelCase (		self			:   List[str]							):
   return DebertaConfig(
       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 		_UpperCAmelCase (		self			:   List[str]							):
   __a							=	self.get_config()
   __a							=	3_00
   return config

  def 		_UpperCAmelCase (		self			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   str							):
   self.parent.assertListEqual(list(result.loss.size()							)  ,				[]							)

  def 		_UpperCAmelCase (		self			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   int  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Any							):
   __a							=	DebertaModel(config=__SCREAMING_SNAKE_CASE							)
   model.to(__SCREAMING_SNAKE_CASE							)
   model.eval()
   __a							=	model(__SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE							)[0]
   __a							=	model(__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE							)[0]
   __a							=	model(__SCREAMING_SNAKE_CASE							)[0]

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

  def 		_UpperCAmelCase (		self			:   int  ,				__SCREAMING_SNAKE_CASE			:   Any  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]							):
   __a							=	DebertaForMaskedLM(config=__SCREAMING_SNAKE_CASE							)
   model.to(__SCREAMING_SNAKE_CASE							)
   model.eval()
   __a							=	model(__SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE  ,				labels=__SCREAMING_SNAKE_CASE							)

   self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)							)

  def 		_UpperCAmelCase (		self			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]							):
   __a							=	self.num_labels
   __a							=	DebertaForSequenceClassification(__SCREAMING_SNAKE_CASE							)
   model.to(__SCREAMING_SNAKE_CASE							)
   model.eval()
   __a							=	model(__SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE  ,				labels=__SCREAMING_SNAKE_CASE							)
   self.parent.assertListEqual(list(result.logits.size()							)  ,				[self.batch_size, self.num_labels]							)
   self.check_loss_output(__SCREAMING_SNAKE_CASE							)

  def 		_UpperCAmelCase (		self			:   Optional[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   Any  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Union[str, Any]  ,				__SCREAMING_SNAKE_CASE			:   Tuple							):
   __a							=	self.num_labels
   __a							=	DebertaForTokenClassification(config=__SCREAMING_SNAKE_CASE							)
   model.to(__SCREAMING_SNAKE_CASE							)
   model.eval()
   __a							=	model(__SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE  ,				labels=__SCREAMING_SNAKE_CASE							)
   self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.num_labels)							)

  def 		_UpperCAmelCase (		self			:   Any  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   Optional[int]  ,				__SCREAMING_SNAKE_CASE			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   List[Any]  ,				__SCREAMING_SNAKE_CASE			:   str							):
   __a							=	DebertaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE							)
   model.to(__SCREAMING_SNAKE_CASE							)
   model.eval()
   __a							=	model(
       __SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE  ,				token_type_ids=__SCREAMING_SNAKE_CASE  ,				start_positions=__SCREAMING_SNAKE_CASE  ,				end_positions=__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 		_UpperCAmelCase (		self			:   str							):
   __a							=	self.prepare_config_and_inputs()
   (
       (
       __a
   )							,  (
       __a
   )							,  (
       __a
   )							,  (
       __a
   )							,  (
       __a
   )							,  (
       __a
   )							,  (
       __a
   )							,  
   )							=	config_and_inputs
   __a							=	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
   return config, inputs_dict
@require_torch
class  A_			(							a_      ,							a_      ,							unittest.TestCase			):
  _SCREAMING_SNAKE_CASE     					=      (
      (
          DebertaModel,
          DebertaForMaskedLM,
          DebertaForSequenceClassification,
          DebertaForTokenClassification,
          DebertaForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  _SCREAMING_SNAKE_CASE     					=      (
      {
          """feature-extraction""": DebertaModel,
          """fill-mask""": DebertaForMaskedLM,
          """question-answering""": DebertaForQuestionAnswering,
          """text-classification""": DebertaForSequenceClassification,
          """token-classification""": DebertaForTokenClassification,
          """zero-shot""": DebertaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  _SCREAMING_SNAKE_CASE     					=      True
  _SCREAMING_SNAKE_CASE     					=      False
  _SCREAMING_SNAKE_CASE     					=      False
  _SCREAMING_SNAKE_CASE     					=      False
  _SCREAMING_SNAKE_CASE     					=      False

  def 		_UpperCAmelCase (		self			:   int							):
   __a							=	DebertaModelTester(self							)
   __a							=	ConfigTester(self  ,				config_class=__SCREAMING_SNAKE_CASE  ,				hidden_size=37							)

  def 		_UpperCAmelCase (		self			:   str							):
   self.config_tester.run_common_tests()

  def 		_UpperCAmelCase (		self			:   List[Any]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_model(*__SCREAMING_SNAKE_CASE							)

  def 		_UpperCAmelCase (		self			:   Tuple							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_sequence_classification(*__SCREAMING_SNAKE_CASE							)

  def 		_UpperCAmelCase (		self			:   Optional[int]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_masked_lm(*__SCREAMING_SNAKE_CASE							)

  def 		_UpperCAmelCase (		self			:   str							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_question_answering(*__SCREAMING_SNAKE_CASE							)

  def 		_UpperCAmelCase (		self			:   Union[str, Any]							):
   __a							=	self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_token_classification(*__SCREAMING_SNAKE_CASE							)

  @slow
  def 		_UpperCAmelCase (		self			:   List[str]							):
   for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __a							=	DebertaModel.from_pretrained(__SCREAMING_SNAKE_CASE							)
    self.assertIsNotNone(__SCREAMING_SNAKE_CASE							)
@require_torch
@require_sentencepiece
@require_tokenizers
class  A_			(							unittest.TestCase			):

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

  @slow
  def 		_UpperCAmelCase (		self			:   List[str]							):
   __a							=	DebertaModel.from_pretrained("microsoft/deberta-base"							)

   __a							=	torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]							)
   __a							=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
   with torch.no_grad():
    __a							=	model(__SCREAMING_SNAKE_CASE  ,				attention_mask=__SCREAMING_SNAKE_CASE							)[0]
   # compare the actual values for a slice.
   __a							=	torch.tensor(
       [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]							)
   self.assertTrue(torch.allclose(output[:, 1:4, 1:4]  ,				__SCREAMING_SNAKE_CASE  ,				atol=1E-4							)  ,				f"""{output[:, 1:4, 1:4]}"""							)



import argparse
import json

import numpy
import torch

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()



def 	__A		(      _A				,					_A       ):



 """simple docstring"""


 __a							=	torch.load(_A				,					map_location="cpu"       )

 __a							=	chkpt["model"]

 # We have the base model one level deeper than the original XLM repository
 __a							=	{}
 for k, v in state_dict.items():
  if "pred_layer" in k:
   __a							=	v
  else:
   __a							=	v

 __a							=	chkpt["params"]
 __a							=	{n: v for n, v in config.items() if not isinstance(_A				,					(torch.FloatTensor, numpy.ndarray)       )}

 __a							=	chkpt["dico_word2id"]
 __a							=	{s + "</w>" if s.find("@@"       ) == -1 and i > 13 else s.replace("@@"				,					""       ): i for s, i in vocab.items()}

 # Save pytorch-model
 __a							=	pytorch_dump_folder_path + "/" + WEIGHTS_NAME
 __a							=	pytorch_dump_folder_path + "/" + CONFIG_NAME
 __a							=	pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"]

 print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""       )
 torch.save(_A				,					_A       )

 print(f"""Save configuration file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )

 print(f"""Save vocab file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE			:						Union[str, Any]				  =		argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--xlm_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump."""
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  SCREAMING_SNAKE_CASE			:						Any				  =		parser.parse_args()
  convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)


