




'''simple docstring'''


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







							def __init__(  self	:  List[Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[Any]=13							,      lowerCAmelCase_	:  Any=7							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Optional[int]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  List[Any]=99							,      lowerCAmelCase_	:  Union[str, Any]=32							,      lowerCAmelCase_	:  Dict=2							,      lowerCAmelCase_	:  Tuple=4							,      lowerCAmelCase_	:  Tuple=37							,      lowerCAmelCase_	:  Union[str, Any]="gelu"							,      lowerCAmelCase_	:  Optional[Any]=0.1							,      lowerCAmelCase_	:  Optional[int]=0.1							,      lowerCAmelCase_	:  List[str]=5_12							,      lowerCAmelCase_	:  str=16							,      lowerCAmelCase_	:  str=2							,      lowerCAmelCase_	:  Dict=0.0_2							,      lowerCAmelCase_	:  Tuple=False							,      lowerCAmelCase_	:  Any=True							,      lowerCAmelCase_	:  Any="None"							,      lowerCAmelCase_	:  Any=3							,      lowerCAmelCase_	:  Any=4							,      lowerCAmelCase_	:  List[Any]=None							,      )      ->							List[Any]:


														"""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        __lowerCAmelCase      (  self	:  Optional[int] )      ->							Any:


														"""simple docstring"""


														_a     					=    ids_tensor([self.batch_size, self.seq_length]							,      self.vocab_size )

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

														_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     					=    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        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Any )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel(config=lowerCAmelCase_ )
														_a     					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

														_a     					=    [input_ids, input_mask]
														_a     					=    model(lowerCAmelCase_ )

														_a     					=    model(lowerCAmelCase_ )

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







							def        __lowerCAmelCase      (  self	:  int							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[int] )      ->							str:


														"""simple docstring"""


														_a     					=    TFDebertaVaForMaskedLM(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.vocab_size) )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Optional[Any] )      ->							str:


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    TFDebertaVaForSequenceClassification(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.num_labels) )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[Any] )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    TFDebertaVaForTokenClassification(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														_a     					=    model(lowerCAmelCase_ )
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.seq_length, self.num_labels) )







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Dict )      ->							int:


														"""simple docstring"""


														_a     					=    TFDebertaVaForQuestionAnswering(config=lowerCAmelCase_ )
														_a     					=    {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														_a     					=    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        __lowerCAmelCase      (  self	:  Tuple )      ->							List[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_tf
class       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (
							        TFDebertaVaModel,
							        TFDebertaVaForMaskedLM,
							        TFDebertaVaForQuestionAnswering,
							        TFDebertaVaForSequenceClassification,
							        TFDebertaVaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase_          =    (
							    {
							        'feature-extraction': TFDebertaVaModel,
							        'fill-mask': TFDebertaVaForMaskedLM,
							        'question-answering': TFDebertaVaForQuestionAnswering,
							        'text-classification': TFDebertaVaForSequenceClassification,
							        'token-classification': TFDebertaVaForTokenClassification,
							        'zero-shot': TFDebertaVaForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)

							lowercase_          =    False
							lowercase_          =    False







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


														"""simple docstring"""


														_a     					=    TFDebertaVaModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      hidden_size=37 )







							def        __lowerCAmelCase      (  self	:  Dict )      ->							Tuple:


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							List[str]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Any )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							int:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_ )







							@slow
							def        __lowerCAmelCase      (  self	:  int )      ->							str:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' )
														self.assertIsNotNone(lowerCAmelCase_ )



@require_tf
class       A   (		unittest.TestCase   ):







							@unittest.skip(reason='''Model not available yet''' )
							def        __lowerCAmelCase      (  self	:  Tuple )      ->							int:


														"""simple docstring"""


														pass







							@slow
							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' )
														_a     					=    tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] )
														_a     					=    tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
														_a     					=    model(lowerCAmelCase_							,      attention_mask=lowerCAmelCase_ )[0]

														_a     					=    tf.constant(
														    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]] )
														tf.debugging.assert_near(output[:, 1:4, 1:4]							,      lowerCAmelCase_							,      atol=1e-4 )







'''simple docstring'''


from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand
def 		snake_case_   (UpperCamelCase	:  Namespace				):





							'''simple docstring'''


							return ConvertCommand(
							    args.model_type			,						args.tf_checkpoint			,						args.pytorch_dump_output			,						args.config			,						args.finetuning_task_name				)


_snake_case				:				List[Any]       							=		'\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n'

class       A   (		_a   ):







							@staticmethod
							def        __lowerCAmelCase      (  lowerCAmelCase_	:  ArgumentParser )      ->							Any:


														"""simple docstring"""


														_a     					=    parser.add_parser(
														    '''convert'''							,      help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'''							,      )
														train_parser.add_argument('''--model_type'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''Model\'s type.''' )
														train_parser.add_argument(
														    '''--tf_checkpoint'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''TensorFlow checkpoint path or folder.''' )
														train_parser.add_argument(
														    '''--pytorch_dump_output'''							,      type=lowerCAmelCase_							,      required=lowerCAmelCase_							,      help='''Path to the PyTorch saved model output.''' )
														train_parser.add_argument('''--config'''							,      type=lowerCAmelCase_							,      default=''''''							,      help='''Configuration file path or folder.''' )
														train_parser.add_argument(
														    '''--finetuning_task_name'''							,      type=lowerCAmelCase_							,      default=lowerCAmelCase_							,      help='''Optional fine-tuning task name if the TF model was a finetuned model.'''							,      )
														train_parser.set_defaults(func=lowerCAmelCase_ )







							def __init__(  self	:  int							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      *lowerCAmelCase_	:  Optional[Any]							,      )      ->							List[str]:


														"""simple docstring"""


														_a     					=    logging.get_logger('''transformers-cli/converting''' )

														self._logger.info(F'Loading model {model_type}' )
														_a     					=    model_type
														_a     					=    tf_checkpoint
														_a     					=    pytorch_dump_output
														_a     					=    config
														_a     					=    finetuning_task_name







							def        __lowerCAmelCase      (  self	:  Dict )      ->							Dict:


														"""simple docstring"""


														if self._model_type == "albert":
																					try:
																												from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "bert":
																					try:
																												from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "funnel":
																					try:
																												from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
																												    convert_tf_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "t5":
																					try:
																												from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "gpt":
																					from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
																					    convert_openai_checkpoint_to_pytorch,
																					)

																					convert_openai_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "transfo_xl":
																					try:
																												from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
																												    convert_transfo_xl_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					if "ckpt" in self._tf_checkpoint.lower():
																												_a     					=    self._tf_checkpoint
																												_a     					=    ''''''
																					else:
																												_a     					=    self._tf_checkpoint
																												_a     					=    ''''''
																					convert_transfo_xl_checkpoint_to_pytorch(
																					    lowerCAmelCase_							,      self._config							,      self._pytorch_dump_output							,      lowerCAmelCase_ )
														elif self._model_type == "gpt2":
																					try:
																												from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
																												    convert_gpta_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														elif self._model_type == "xlnet":
																					try:
																												from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
																												    convert_xlnet_checkpoint_to_pytorch,
																												)
																					except ImportError:
																												raise ImportError(lowerCAmelCase_ )

																					convert_xlnet_checkpoint_to_pytorch(
																					    self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output							,      self._finetuning_task_name )
														elif self._model_type == "xlm":
																					from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
																					    convert_xlm_checkpoint_to_pytorch,
																					)

																					convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint							,      self._pytorch_dump_output )
														elif self._model_type == "lxmert":
																					from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
																					    convert_lxmert_checkpoint_to_pytorch,
																					)

																					convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint							,      self._pytorch_dump_output )
														elif self._model_type == "rembert":
																					from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
																					    convert_rembert_tf_checkpoint_to_pytorch,
																					)

																					convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint							,      self._config							,      self._pytorch_dump_output )
														else:
																					raise ValueError(
																					    '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )

