




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

							UpperCAmelCase__							 =						None
							if self.use_input_mask:
								UpperCAmelCase__							 =						random_attention_mask([self.batch_size, self.seq_length]      )

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

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

							UpperCAmelCase__							 =						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 	UpperCAmelCase_						(			self   :Optional[Any] ,      lowerCamelCase   :int ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Tuple ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :Optional[Any] ,      lowerCamelCase   :Any      )     ->			Dict:
							UpperCAmelCase__							 =						TFDebertaVaModel(config=lowerCamelCase      )
							UpperCAmelCase__							 =						{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

							UpperCAmelCase__							 =						[input_ids, input_mask]
							UpperCAmelCase__							 =						model(lowerCamelCase      )

							UpperCAmelCase__							 =						model(lowerCamelCase      )

							self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)      )
						def 	UpperCAmelCase_						(			self   :List[Any] ,      lowerCamelCase   :int ,      lowerCamelCase   :Any ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Any ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Tuple      )     ->			Any:
							UpperCAmelCase__							 =						TFDebertaVaForMaskedLM(config=lowerCamelCase      )
							UpperCAmelCase__							 =						{
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							UpperCAmelCase__							 =						model(lowerCamelCase      )
							self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.seq_length, self.vocab_size)      )
						def 	UpperCAmelCase_						(			self   :int ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :Union[str, Any] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Optional[Any] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :str      )     ->			List[str]:
							UpperCAmelCase__							 =						self.num_labels
							UpperCAmelCase__							 =						TFDebertaVaForSequenceClassification(config=lowerCamelCase      )
							UpperCAmelCase__							 =						{
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							UpperCAmelCase__							 =						model(lowerCamelCase      )
							self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.num_labels)      )
						def 	UpperCAmelCase_						(			self   :Dict ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :int ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :Any ,      lowerCamelCase   :str ,      lowerCamelCase   :str ,      lowerCamelCase   :List[str]      )     ->			Optional[int]:
							UpperCAmelCase__							 =						self.num_labels
							UpperCAmelCase__							 =						TFDebertaVaForTokenClassification(config=lowerCamelCase      )
							UpperCAmelCase__							 =						{
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							UpperCAmelCase__							 =						model(lowerCamelCase      )
							self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.seq_length, self.num_labels)      )
						def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :List[Any] ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Tuple ,      lowerCamelCase   :int ,      lowerCamelCase   :str ,      lowerCamelCase   :Optional[Any]      )     ->			Dict:
							UpperCAmelCase__							 =						TFDebertaVaForQuestionAnswering(config=lowerCamelCase      )
							UpperCAmelCase__							 =						{
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							UpperCAmelCase__							 =						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 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			str:
							UpperCAmelCase__							 =						self.prepare_config_and_inputs()
							(
							    (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) , (
							    UpperCAmelCase__ 
							) ,  
							)							 =						config_and_inputs
							UpperCAmelCase__							 =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict



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

						UpperCAmelCase_        =		False
						UpperCAmelCase_        =		False
						def 	UpperCAmelCase_						(			self   :Any      )     ->			int:
							UpperCAmelCase__							 =						TFDebertaVaModelTester(self      )
							UpperCAmelCase__							 =						ConfigTester(self ,      config_class=lowerCamelCase ,      hidden_size=37      )
						def 	UpperCAmelCase_						(			self   :Dict      )     ->			Optional[int]:
							self.config_tester.run_common_tests()
						def 	UpperCAmelCase_						(			self   :int      )     ->			Optional[int]:
							UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase      )
						def 	UpperCAmelCase_						(			self   :Any      )     ->			Union[str, Any]:
							UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase      )
						def 	UpperCAmelCase_						(			self   :Any      )     ->			str:
							UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCamelCase      )
						def 	UpperCAmelCase_						(			self   :Dict      )     ->			int:
							UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase      )
						def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Optional[Any]:
							UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCamelCase      )







						@slow
						def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Optional[int]:
							UpperCAmelCase__							 =						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"      )
							self.assertIsNotNone(lowerCamelCase      )



@require_tf
class 			_UpperCamelCase      (				unittest.TestCase		):
						@unittest.skip(reason="Model not available yet"      )
						def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			List[Any]:
							pass







						@slow
						def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			int:
							UpperCAmelCase__							 =						TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"      )
							UpperCAmelCase__							 =						tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]      )
							UpperCAmelCase__							 =						tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
							UpperCAmelCase__							 =						model(lowerCamelCase ,      attention_mask=lowerCamelCase      )[0]

							UpperCAmelCase__							 =						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      )











import argparse

from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird
from transformers.utils import logging


logging.set_verbosity_info()



def 						lowerCAmelCase		(		_lowerCAmelCase							:   Any						,							_lowerCAmelCase							:   List[str]						,							_lowerCAmelCase							:   Optional[int]						,							_lowerCAmelCase							:   Union[str, Any]	):




 """simple docstring"""







 UpperCAmelCase__							 =						BigBirdConfig.from_json_file(_lowerCAmelCase	)
 print(F'''Building PyTorch model from configuration: {config}'''	)

 if is_trivia_qa:
  UpperCAmelCase__							 =						BigBirdForQuestionAnswering(_lowerCAmelCase	)
 else:
  UpperCAmelCase__							 =						BigBirdForPreTraining(_lowerCAmelCase	)

 # Load weights from tf checkpoint
 load_tf_weights_in_big_bird(_lowerCAmelCase						,							_lowerCAmelCase						,							is_trivia_qa=_lowerCAmelCase	)

 # Save pytorch-model
 print(F'''Save PyTorch model to {pytorch_dump_path}'''	)
 model.save_pretrained(_lowerCAmelCase	)


if __name__ == "__main__":
  _lowerCAmelCase						:   List[Any]		=   argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
  )
  parser.add_argument(
      "--big_bird_config_file",
      default=None,
      type=str,
      required=True,
      help=(
          "The config json file corresponding to the pre-trained BERT model. \n"
          "This specifies the model architecture."
      ),
  )
  parser.add_argument(
      "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
  )
  parser.add_argument(
      "--is_trivia_qa", action="store_true", help="Whether to convert a model with a trivia_qa head."
  )
  _lowerCAmelCase						:   Dict		=   parser.parse_args()
  convert_tf_checkpoint_to_pytorch(
      args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa
  )





