
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		,     _snake_case		,     _snake_case=13		,     _snake_case=7		,     _snake_case=True		,     _snake_case=True		,     _snake_case=True		,     _snake_case=True		,     _snake_case=99		,     _snake_case=32		,     _snake_case=5		,     _snake_case=4		,     _snake_case=37		,     _snake_case="gelu"		,     _snake_case=0.1		,     _snake_case=0.1		,     _snake_case=512		,     _snake_case=16		,     _snake_case=2		,     _snake_case=0.02		,     _snake_case=False		,     _snake_case=True		,     _snake_case="None"		,     _snake_case=3		,     _snake_case=4		,     _snake_case=None		,     )    ->      Optional[int]:


								'''simple docstring'''




								__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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      List[Any]:


								'''simple docstring'''




								__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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Union[str, Any]:


								'''simple docstring'''




								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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Tuple:


								'''simple docstring'''




								__a										=    self.get_config()
								__a										=    300
								return config




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


								'''simple docstring'''




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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case				)    ->      Any:


								'''simple docstring'''




								__a										=    DebertaModel(config=_snake_case				)
								model.to(_snake_case				)
								model.eval()
								__a										=    model(_snake_case		,     attention_mask=_snake_case		,     token_type_ids=_snake_case				)[0]
								__a										=    model(_snake_case		,     token_type_ids=_snake_case				)[0]
								__a										=    model(_snake_case				)[0]

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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case				)    ->      str:


								'''simple docstring'''




								__a										=    DebertaForMaskedLM(config=_snake_case				)
								model.to(_snake_case				)
								model.eval()
								__a										=    model(_snake_case		,     attention_mask=_snake_case		,     token_type_ids=_snake_case		,     labels=_snake_case				)

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




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case				)    ->      Dict:


								'''simple docstring'''




								__a										=    self.num_labels
								__a										=    DebertaForSequenceClassification(_snake_case				)
								model.to(_snake_case				)
								model.eval()
								__a										=    model(_snake_case		,     attention_mask=_snake_case		,     token_type_ids=_snake_case		,     labels=_snake_case				)
								self.parent.assertListEqual(list(result.logits.size()				)		,     [self.batch_size, self.num_labels]				)
								self.check_loss_output(_snake_case				)




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


								'''simple docstring'''




								__a										=    self.num_labels
								__a										=    DebertaForTokenClassification(config=_snake_case				)
								model.to(_snake_case				)
								model.eval()
								__a										=    model(_snake_case		,     attention_mask=_snake_case		,     token_type_ids=_snake_case		,     labels=_snake_case				)
								self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)				)




				def 				SCREAMING_SNAKE_CASE_		(     self		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case		,     _snake_case				)    ->      Dict:


								'''simple docstring'''




								__a										=    DebertaForQuestionAnswering(config=_snake_case				)
								model.to(_snake_case				)
								model.eval()
								__a										=    model(
								    _snake_case		,     attention_mask=_snake_case		,     token_type_ids=_snake_case		,     start_positions=_snake_case		,     end_positions=_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 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Union[str, Any]:


								'''simple docstring'''




								__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			):
				snake_case_						   =    (
				    (
				        DebertaModel,
				        DebertaForMaskedLM,
				        DebertaForSequenceClassification,
				        DebertaForTokenClassification,
				        DebertaForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				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 {}
				)

				snake_case_						   =    True
				snake_case_						   =    False
				snake_case_						   =    False
				snake_case_						   =    False
				snake_case_						   =    False




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Dict:


								'''simple docstring'''




								__a										=    DebertaModelTester(self				)
								__a										=    ConfigTester(self		,     config_class=_snake_case		,     hidden_size=37				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      str:


								'''simple docstring'''




								self.config_tester.run_common_tests()




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Any:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*_snake_case				)




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


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*_snake_case				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Dict:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*_snake_case				)




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


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*_snake_case				)




				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Tuple:


								'''simple docstring'''




								__a										=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*_snake_case				)




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


								'''simple docstring'''




								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												__a										=    DebertaModel.from_pretrained(_snake_case				)
												self.assertIsNotNone(_snake_case				)







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




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


								'''simple docstring'''




								pass




				@slow
				def 				SCREAMING_SNAKE_CASE_		(     self				)    ->      Tuple:


								'''simple docstring'''




								__a										=    DebertaModel.from_pretrained('''microsoft/deberta-base'''				)

								__a										=    torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]				)
								__a										=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
								with torch.no_grad():
												__a										=    model(_snake_case		,     attention_mask=_snake_case				)[0]
								# compare the actual values for a slice.
								__a										=    torch.tensor(
								    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]				)
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]		,     _snake_case		,     atol=1E-4				)		,     F"""{output[:, 1:4, 1:4]}"""				)







import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel






def   a			(						lowerCamelCase_		,   lowerCamelCase_		,   lowerCamelCase_				):






				'''simple docstring'''
				lowercase__    =			('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

				lowercase__    =			(
				    ('''layer.''', '''layer_'''),
				    ('''word_embeddings.weight''', '''word_embeddings'''),
				    ('''position_embeddings.weight''', '''position_embeddings'''),
				    ('''token_type_embeddings.weight''', '''token_type_embeddings'''),
				    ('''.''', '''/'''),
				    ('''LayerNorm/weight''', '''LayerNorm/gamma'''),
				    ('''LayerNorm/bias''', '''LayerNorm/beta'''),
				    ('''weight''', '''kernel'''),
				)

				if not os.path.isdir(lowerCamelCase_				):
								os.makedirs(lowerCamelCase_				)

				lowercase__    =			model.state_dict()

				def to_tf_var_name(lowerCamelCase_				):
								for patt, repl in iter(lowerCamelCase_				):
												lowercase__    =			name.replace(lowerCamelCase_		,   lowerCamelCase_				)
								return F"""bert/{name}"""

				def create_tf_var(lowerCamelCase_		,   lowerCamelCase_		,   lowerCamelCase_				):
								lowercase__    =			tf.dtypes.as_dtype(tensor.dtype				)
								lowercase__    =			tf.get_variable(dtype=lowerCamelCase_		,   shape=tensor.shape		,   name=lowerCamelCase_		,   initializer=tf.zeros_initializer()				)
								session.run(tf.variables_initializer([tf_var]				)				)
								session.run(lowerCamelCase_				)
								return tf_var

				tf.reset_default_graph()
				with tf.Session() as session:
								for var_name in state_dict:
												lowercase__    =			to_tf_var_name(lowerCamelCase_				)
												lowercase__    =			state_dict[var_name].numpy()
												if any(x in var_name for x in tensors_to_transpose				):
																lowercase__    =			torch_tensor.T
												lowercase__    =			create_tf_var(tensor=lowerCamelCase_		,   name=lowerCamelCase_		,   session=lowerCamelCase_				)
												tf.keras.backend.set_value(lowerCamelCase_		,   lowerCamelCase_				)
												lowercase__    =			session.run(lowerCamelCase_				)
												print(F"""Successfully created {tf_name}: {np.allclose(lowerCamelCase_		,   lowerCamelCase_				)}"""				)

								lowercase__    =			tf.train.Saver(tf.trainable_variables()				)
								saver.save(lowerCamelCase_		,   os.path.join(lowerCamelCase_		,   model_name.replace('''-'''		,   '''_'''				) + '''.ckpt'''				)				)






def   a			(						lowerCamelCase_=None				):






				'''simple docstring'''
				lowercase__    =			argparse.ArgumentParser()
				parser.add_argument('''--model_name'''		,   type=lowerCamelCase_		,   required=lowerCamelCase_		,   help='''model name e.g. bert-base-uncased'''				)
				parser.add_argument(
				    '''--cache_dir'''		,   type=lowerCamelCase_		,   default=lowerCamelCase_		,   required=lowerCamelCase_		,   help='''Directory containing pytorch model'''				)
				parser.add_argument('''--pytorch_model_path'''		,   type=lowerCamelCase_		,   required=lowerCamelCase_		,   help='''/path/to/<pytorch-model-name>.bin'''				)
				parser.add_argument('''--tf_cache_dir'''		,   type=lowerCamelCase_		,   required=lowerCamelCase_		,   help='''Directory in which to save tensorflow model'''				)
				lowercase__    =			parser.parse_args(lowerCamelCase_				)

				lowercase__    =			BertModel.from_pretrained(
				    pretrained_model_name_or_path=args.model_name		,   state_dict=torch.load(args.pytorch_model_path				)		,   cache_dir=args.cache_dir		,   )

				convert_pytorch_checkpoint_to_tf(model=lowerCamelCase_		,   ckpt_dir=args.tf_cache_dir		,   model_name=args.model_name				)


if __name__ == "__main__":
							main()




