
from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 (
        TFConvBertForMaskedLM,
        TFConvBertForMultipleChoice,
        TFConvBertForQuestionAnswering,
        TFConvBertForSequenceClassification,
        TFConvBertForTokenClassification,
        TFConvBertModel,
    )




class        UpperCAmelCase_			:


  '''simple docstring'''







  def __init__( self			: Optional[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Optional[Any]=13			,							UpperCamelCase__			: Optional[int]=7			,							UpperCamelCase__			: int=True			,							UpperCamelCase__			: List[Any]=True			,							UpperCamelCase__			: Optional[int]=True			,							UpperCamelCase__			: Dict=True			,							UpperCamelCase__			: List[Any]=99			,							UpperCamelCase__			: Optional[Any]=32			,							UpperCamelCase__			: Optional[int]=2			,							UpperCamelCase__			: Tuple=4			,							UpperCamelCase__			: Any=37			,							UpperCamelCase__			: str="gelu"			,							UpperCamelCase__			: Dict=0.1			,							UpperCamelCase__			: int=0.1			,							UpperCamelCase__			: Optional[int]=512			,							UpperCamelCase__			: str=16			,							UpperCamelCase__			: str=2			,							UpperCamelCase__			: List[Any]=0.02			,							UpperCamelCase__			: List[Any]=3			,							UpperCamelCase__			: Union[str, Any]=4			,							UpperCamelCase__			: Dict=None			,							)			->	List[Any]:







    """simple docstring"""






    __magic_name__				     =       parent
    __magic_name__				     =       13
    __magic_name__				     =       7
    __magic_name__				     =       True
    __magic_name__				     =       True
    __magic_name__				     =       True
    __magic_name__				     =       True
    __magic_name__				     =       99
    __magic_name__				     =       384
    __magic_name__				     =       2
    __magic_name__				     =       4
    __magic_name__				     =       37
    __magic_name__				     =       """gelu"""
    __magic_name__				     =       0.1
    __magic_name__				     =       0.1
    __magic_name__				     =       512
    __magic_name__				     =       16
    __magic_name__				     =       2
    __magic_name__				     =       0.02
    __magic_name__				     =       3
    __magic_name__				     =       4
    __magic_name__				     =       128
    __magic_name__				     =       2
    __magic_name__				     =       9
    __magic_name__				     =       1
    __magic_name__				     =       None
  def 							_lowercase							( self			: List[Any]    )			->	Union[str, Any]:







    """simple docstring"""






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

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

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

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

    __magic_name__				     =       ConvBertConfig(
        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			,							return_dict=UpperCamelCase__			,							)

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
  def 							_lowercase							( self			: List[Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: Union[str, Any]			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: str			,							UpperCamelCase__			: Union[str, Any]			,							UpperCamelCase__			: str    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       TFConvBertModel(config=UpperCamelCase__    )
    __magic_name__				     =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

    __magic_name__				     =       [input_ids, input_mask]
    __magic_name__				     =       model(UpperCamelCase__    )

    __magic_name__				     =       model(UpperCamelCase__    )

    self.parent.assertEqual(result.last_hidden_state.shape			,							(self.batch_size, self.seq_length, self.hidden_size)    )
  def 							_lowercase							( self			: int			,							UpperCamelCase__			: int			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: List[Any]    )			->	Dict:







    """simple docstring"""






    __magic_name__				     =       TFConvBertForMaskedLM(config=UpperCamelCase__    )
    __magic_name__				     =       {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }
    __magic_name__				     =       model(UpperCamelCase__    )
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.vocab_size)    )
  def 							_lowercase							( self			: Union[str, Any]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: List[str]    )			->	Union[str, Any]:







    """simple docstring"""






    __magic_name__				     =       self.num_labels
    __magic_name__				     =       TFConvBertForSequenceClassification(config=UpperCamelCase__    )
    __magic_name__				     =       {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }

    __magic_name__				     =       model(UpperCamelCase__    )
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_labels)    )
  def 							_lowercase							( self			: List[str]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: int    )			->	str:







    """simple docstring"""






    __magic_name__				     =       self.num_choices
    __magic_name__				     =       TFConvBertForMultipleChoice(config=UpperCamelCase__    )
    __magic_name__				     =       tf.tile(tf.expand_dims(UpperCamelCase__			,							1    )			,							(1, self.num_choices, 1)    )
    __magic_name__				     =       tf.tile(tf.expand_dims(UpperCamelCase__			,							1    )			,							(1, self.num_choices, 1)    )
    __magic_name__				     =       tf.tile(tf.expand_dims(UpperCamelCase__			,							1    )			,							(1, self.num_choices, 1)    )
    __magic_name__				     =       {
        """input_ids""": multiple_choice_inputs_ids,
        """attention_mask""": multiple_choice_input_mask,
        """token_type_ids""": multiple_choice_token_type_ids,
    }
    __magic_name__				     =       model(UpperCamelCase__    )
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_choices)    )
  def 							_lowercase							( self			: List[str]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: str    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__				     =       self.num_labels
    __magic_name__				     =       TFConvBertForTokenClassification(config=UpperCamelCase__    )
    __magic_name__				     =       {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }
    __magic_name__				     =       model(UpperCamelCase__    )
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.num_labels)    )
  def 							_lowercase							( self			: List[Any]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: Union[str, Any]    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       TFConvBertForQuestionAnswering(config=UpperCamelCase__    )
    __magic_name__				     =       {
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
    }

    __magic_name__				     =       model(UpperCamelCase__    )
    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]    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__				     =       self.prepare_config_and_inputs()
    (
        (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    
    )				     =       config_and_inputs
    __magic_name__				     =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
    return config, inputs_dict







@require_tf
class        UpperCAmelCase_			( _A  ,       _A  ,       unittest.TestCase     ):


  '''simple docstring'''







  a__           =		(
      (
          TFConvBertModel,
          TFConvBertForMaskedLM,
          TFConvBertForQuestionAnswering,
          TFConvBertForSequenceClassification,
          TFConvBertForTokenClassification,
          TFConvBertForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  a__           =		(
      {
          """feature-extraction""": TFConvBertModel,
          """fill-mask""": TFConvBertForMaskedLM,
          """question-answering""": TFConvBertForQuestionAnswering,
          """text-classification""": TFConvBertForSequenceClassification,
          """token-classification""": TFConvBertForTokenClassification,
          """zero-shot""": TFConvBertForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  a__           =		False
  a__           =		False
  a__           =		False
  def 							_lowercase							( self			: List[str]    )			->	Any:







    """simple docstring"""






    __magic_name__				     =       TFConvBertModelTester(self    )
    __magic_name__				     =       ConfigTester(self			,							config_class=UpperCamelCase__			,							hidden_size=37    )
  def 							_lowercase							( self			: str    )			->	int:







    """simple docstring"""






    self.config_tester.run_common_tests()
  def 							_lowercase							( self			: int    )			->	str:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__    )
  def 							_lowercase							( self			: Dict    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__    )
  def 							_lowercase							( self			: List[Any]    )			->	List[Any]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__    )
  def 							_lowercase							( self			: int    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__    )
  def 							_lowercase							( self			: Any    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__    )
  def 							_lowercase							( self			: Optional[Any]    )			->	int:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__    )
  @slow
  def 							_lowercase							( self			: Tuple    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()
    __magic_name__				     =       True
    __magic_name__				     =       True

    if hasattr(UpperCamelCase__			,							"""use_cache"""    ):
      __magic_name__				     =       True

    __magic_name__				     =       getattr(self.model_tester			,							"""encoder_seq_length"""			,							self.model_tester.seq_length    )
    __magic_name__				     =       getattr(self.model_tester			,							"""key_length"""			,							UpperCamelCase__    )

    for model_class in self.all_model_classes:
      __magic_name__				     =       self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )
      __magic_name__				     =       model_class(UpperCamelCase__    )
      __magic_name__				     =       len(model(UpperCamelCase__    )    )

      with tempfile.TemporaryDirectory() as tmpdirname:
        model.save_pretrained(UpperCamelCase__			,							saved_model=UpperCamelCase__    )
        __magic_name__				     =       os.path.join(UpperCamelCase__			,							"""saved_model"""			,							"""1"""    )
        __magic_name__				     =       tf.keras.models.load_model(UpperCamelCase__    )
        __magic_name__				     =       model(UpperCamelCase__    )

        if self.is_encoder_decoder:
          __magic_name__				     =       outputs["""encoder_hidden_states"""]
          __magic_name__				     =       outputs["""encoder_attentions"""]
        else:
          __magic_name__				     =       outputs["""hidden_states"""]
          __magic_name__				     =       outputs["""attentions"""]

        self.assertEqual(len(UpperCamelCase__    )			,							UpperCamelCase__    )

        __magic_name__				     =       getattr(
            self.model_tester			,							"""expected_num_hidden_layers"""			,							self.model_tester.num_hidden_layers + 1    )

        self.assertEqual(len(UpperCamelCase__    )			,							UpperCamelCase__    )
        self.assertListEqual(
            list(output_hidden_states[0].shape[-2:]    )			,							[self.model_tester.seq_length, self.model_tester.hidden_size]			,							)

        self.assertEqual(len(UpperCamelCase__    )			,							self.model_tester.num_hidden_layers    )
        self.assertListEqual(
            list(output_attentions[0].shape[-3:]    )			,							[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			,							)
  @slow
  def 							_lowercase							( self			: Dict    )			->	Tuple:







    """simple docstring"""






    __magic_name__				     =       TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""    )
    self.assertIsNotNone(UpperCamelCase__    )





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







    """simple docstring"""






    __magic_name__     ,    __magic_name__				     =       self.model_tester.prepare_config_and_inputs_for_common()
    __magic_name__				     =       True
    __magic_name__				     =       getattr(self.model_tester			,							"""decoder_seq_length"""			,							self.model_tester.seq_length    )
    __magic_name__				     =       getattr(self.model_tester			,							"""encoder_seq_length"""			,							self.model_tester.seq_length    )
    __magic_name__				     =       getattr(self.model_tester			,							"""key_length"""			,							UpperCamelCase__    )
    __magic_name__				     =       getattr(self.model_tester			,							"""key_length"""			,							UpperCamelCase__    )

    def check_decoder_attentions_output(UpperCamelCase__			: List[str]    ):
      __magic_name__				     =       len(UpperCamelCase__    )
      self.assertEqual(out_len % 2			,							0    )
      __magic_name__				     =       outputs.decoder_attentions
      self.assertEqual(len(UpperCamelCase__    )			,							self.model_tester.num_hidden_layers    )
      self.assertListEqual(
          list(decoder_attentions[0].shape[-3:]    )			,							[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]			,							)

    def check_encoder_attentions_output(UpperCamelCase__			: Optional[Any]    ):
      __magic_name__				     =       [
          t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
      ]
      self.assertEqual(len(UpperCamelCase__    )			,							self.model_tester.num_hidden_layers    )
      self.assertListEqual(
          list(attentions[0].shape[-3:]    )			,							[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			,							)

    for model_class in self.all_model_classes:
      __magic_name__				     =       True
      __magic_name__				     =       False
      __magic_name__				     =       model_class(UpperCamelCase__    )
      __magic_name__				     =       model(self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )    )
      __magic_name__				     =       len(UpperCamelCase__    )
      self.assertEqual(config.output_hidden_states			,							UpperCamelCase__    )
      check_encoder_attentions_output(UpperCamelCase__    )

      if self.is_encoder_decoder:
        __magic_name__				     =       model_class(UpperCamelCase__    )
        __magic_name__				     =       model(self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )    )
        self.assertEqual(config.output_hidden_states			,							UpperCamelCase__    )
        check_decoder_attentions_output(UpperCamelCase__    )

      # Check that output attentions can also be changed via the config
      del inputs_dict["output_attentions"]
      __magic_name__				     =       True
      __magic_name__				     =       model_class(UpperCamelCase__    )
      __magic_name__				     =       model(self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )    )
      self.assertEqual(config.output_hidden_states			,							UpperCamelCase__    )
      check_encoder_attentions_output(UpperCamelCase__    )

      # Check attention is always last and order is fine
      __magic_name__				     =       True
      __magic_name__				     =       True
      __magic_name__				     =       model_class(UpperCamelCase__    )
      __magic_name__				     =       model(self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__    )    )

      self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)			,							len(UpperCamelCase__    )    )
      self.assertEqual(model.config.output_hidden_states			,							UpperCamelCase__    )
      check_encoder_attentions_output(UpperCamelCase__    )







@require_tf
class        UpperCAmelCase_			( unittest.TestCase     ):


  '''simple docstring'''







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







    """simple docstring"""






    __magic_name__				     =       TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""    )
    __magic_name__				     =       tf.constant([[0, 1, 2, 3, 4, 5]]    )
    __magic_name__				     =       model(UpperCamelCase__    )[0]

    __magic_name__				     =       [1, 6, 768]
    self.assertEqual(output.shape			,							UpperCamelCase__    )

    __magic_name__				     =       tf.constant(
        [
            [
                [-0.03475493, -0.4686034, -0.30638832],
                [0.22637248, -0.26988646, -0.7423424],
                [0.10324868, -0.45013508, -0.58280784],
            ]
        ]    )
    tf.debugging.assert_near(output[:, :3, :3]			,							UpperCamelCase__			,							atol=1E-4    )








import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     str:
    lowerCamelCase__    :      Optional[int]					 =		[
        'encoder.version',
        'decoder.version',
        'model.encoder.version',
        'model.decoder.version',
        'decoder.output_projection.weight',
        '_float_tensor',
        'encoder.embed_positions._float_tensor',
        'decoder.embed_positions._float_tensor',
    ]
    for k in ignore_keys:
        state_dict.pop(_UpperCAmelCase						,	_UpperCAmelCase )


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     List[Any]:
    lowerCamelCase__ ,							lowerCamelCase__    :      List[str]					 =		emb.weight.shape
    lowerCamelCase__    :      Tuple					 =		nn.Linear(_UpperCAmelCase						,	_UpperCAmelCase						,	bias=_UpperCAmelCase )
    lowerCamelCase__    :      Dict					 =		emb.weight.data
    return lin_layer


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     str:
    lowerCamelCase__    :      Tuple					 =		torch.load(_UpperCAmelCase						,	map_location='cpu' )
    lowerCamelCase__    :      List[str]					 =		mam_aaa['args'] or mam_aaa['cfg']['model']
    lowerCamelCase__    :      Optional[int]					 =		mam_aaa['model']
    remove_ignore_keys_(_UpperCAmelCase )
    lowerCamelCase__    :      str					 =		state_dict['encoder.embed_tokens.weight'].shape[0]

    lowerCamelCase__    :      Union[str, Any]					 =		MaMaaaConfig(
        vocab_size=_UpperCAmelCase						,	max_position_embeddings=1024						,	encoder_layers=args.encoder_layers						,	decoder_layers=args.decoder_layers						,	encoder_attention_heads=args.encoder_attention_heads						,	decoder_attention_heads=args.decoder_attention_heads						,	encoder_ffn_dim=args.encoder_ffn_embed_dim						,	decoder_ffn_dim=args.decoder_ffn_embed_dim						,	d_model=args.encoder_embed_dim						,	encoder_layerdrop=args.encoder_layerdrop						,	decoder_layerdrop=args.decoder_layerdrop						,	dropout=args.dropout						,	attention_dropout=args.attention_dropout						,	activation_dropout=args.activation_dropout						,	activation_function='relu'						,	)

    lowerCamelCase__    :      Optional[Any]					 =		state_dict['decoder.embed_tokens.weight']
    lowerCamelCase__    :      Union[str, Any]					 =		MaMaaaForConditionalGeneration(_UpperCAmelCase )
    model.model.load_state_dict(_UpperCAmelCase						,	strict=_UpperCAmelCase )
    lowerCamelCase__    :      List[str]					 =		make_linear_from_emb(model.model.shared )

    return model


if __name__ == "__main__":
    _UpperCAmelCase				:       Tuple	   =				argparse.ArgumentParser()
    # Required parameters
    parser.add_argument("""fairseq_path""", type=str, help="""path to a model.pt on local filesystem.""")
    parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
    _UpperCAmelCase				:       str	   =				parser.parse_args()
    _UpperCAmelCase				:       Optional[Any]	   =				convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
    model.save_pretrained(args.pytorch_dump_folder_path)





