



'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np
     import tensorflow as tf

     from transformers import (
         TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
         FlaubertConfig,
         TFFlaubertForMultipleChoice,
         TFFlaubertForQuestionAnsweringSimple,
         TFFlaubertForSequenceClassification,
         TFFlaubertForTokenClassification,
         TFFlaubertModel,
         TFFlaubertWithLMHeadModel,
     )

class 				a__:







    def __init__( self					:    Any		,     __snake_case					:    Any		,     ):
      a		:   Union[str, Any]	  =	parent
      a		:   Union[str, Any]	  =	13
      a		:   int	  =	7
      a		:   List[str]	  =	True
      a		:   str	  =	True
      a		:   str	  =	True
      a		:   str	  =	True
      a		:   List[Any]	  =	True
      a		:   Dict	  =	False
      a		:   int	  =	False
      a		:   Optional[Any]	  =	False
      a		:   str	  =	2
      a		:   Any	  =	99
      a		:   str	  =	0
      a		:   int	  =	32
      a		:   Union[str, Any]	  =	2
      a		:   Dict	  =	4
      a		:   List[Any]	  =	0.1
      a		:   Tuple	  =	0.1
      a		:   Optional[int]	  =	5_12
      a		:   Union[str, Any]	  =	16
      a		:   str	  =	2
      a		:   Union[str, Any]	  =	0.02
      a		:   int	  =	3
      a		:   List[Any]	  =	4
      a		:   Optional[int]	  =	'last'
      a		:   Union[str, Any]	  =	True
      a		:   Dict	  =	None
      a		:   Dict	  =	0







    def      lowercase_			( self					:    Optional[int]      ):
      a		:   Any	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )
      a		:   Optional[Any]	  =	random_attention_mask([self.batch_size, self.seq_length]		,     dtype=tf.floataa      )

      a		:   Union[str, Any]	  =	None
      if self.use_input_lengths:
        a		:   Any	  =	(
            ids_tensor([self.batch_size]		,     vocab_size=2      ) + self.seq_length - 2
        )  # small variation of seq_length

      a		:   Any	  =	None
      if self.use_token_type_ids:
        a		:   Tuple	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.n_langs      )

      a		:   int	  =	None
      a		:   List[Any]	  =	None
      a		:   List[str]	  =	None
      if self.use_labels:
        a		:   str	  =	ids_tensor([self.batch_size]		,     self.type_sequence_label_size      )
        a		:   str	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels      )
        a		:   Any	  =	ids_tensor([self.batch_size]		,     2		,     dtype=tf.floataa      )
        a		:   str	  =	ids_tensor([self.batch_size]		,     self.num_choices      )

      a		:   int	  =	FlaubertConfig(
          vocab_size=self.vocab_size		,     n_special=self.n_special		,     emb_dim=self.hidden_size		,     n_layers=self.num_hidden_layers		,     n_heads=self.num_attention_heads		,     dropout=self.hidden_dropout_prob		,     attention_dropout=self.attention_probs_dropout_prob		,     gelu_activation=self.gelu_activation		,     sinusoidal_embeddings=self.sinusoidal_embeddings		,     asm=self.asm		,     causal=self.causal		,     n_langs=self.n_langs		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     summary_type=self.summary_type		,     use_proj=self.use_proj		,     bos_token_id=self.bos_token_id		,     )

      return (
          config,
          input_ids,
          token_type_ids,
          input_lengths,
          sequence_labels,
          token_labels,
          is_impossible_labels,
          choice_labels,
          input_mask,
      )







    def      lowercase_			( self					:    str		,     __snake_case					:    int		,     __snake_case					:    Tuple		,     __snake_case					:    int		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    int		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Dict		,     __snake_case					:    Dict		,     __snake_case					:    List[str]		,     ):
      a		:   Union[str, Any]	  =	TFFlaubertModel(config=__snake_case      )
      a		:   List[Any]	  =	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
      a		:   Any	  =	model(__snake_case      )

      a		:   List[str]	  =	[input_ids, input_mask]
      a		:   Optional[int]	  =	model(__snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )







    def      lowercase_			( self					:    Optional[Any]		,     __snake_case					:    Any		,     __snake_case					:    str		,     __snake_case					:    List[Any]		,     __snake_case					:    Optional[int]		,     __snake_case					:    int		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Tuple		,     __snake_case					:    str		,     __snake_case					:    str		,     ):
      a		:   Optional[Any]	  =	TFFlaubertWithLMHeadModel(__snake_case      )

      a		:   str	  =	{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
      a		:   Tuple	  =	model(__snake_case      )

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







    def      lowercase_			( self					:    str		,     __snake_case					:    int		,     __snake_case					:    Optional[int]		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[int]		,     __snake_case					:    str		,     __snake_case					:    str		,     __snake_case					:    List[Any]		,     __snake_case					:    List[Any]		,     __snake_case					:    Tuple		,     ):
      a		:   int	  =	TFFlaubertForQuestionAnsweringSimple(__snake_case      )

      a		:   Optional[Any]	  =	{'input_ids': input_ids, 'lengths': input_lengths}

      a		:   Tuple	  =	model(__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      lowercase_			( self					:    List[str]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    List[str]		,     __snake_case					:    Any		,     __snake_case					:    Tuple		,     __snake_case					:    Any		,     __snake_case					:    List[Any]		,     __snake_case					:    str		,     __snake_case					:    Optional[Any]		,     __snake_case					:    str		,     ):
      a		:   str	  =	TFFlaubertForSequenceClassification(__snake_case      )

      a		:   Dict	  =	{'input_ids': input_ids, 'lengths': input_lengths}

      a		:   int	  =	model(__snake_case      )

      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)      )







    def      lowercase_			( self					:    Optional[int]		,     __snake_case					:    List[Any]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Optional[int]		,     __snake_case					:    Tuple		,     __snake_case					:    Tuple		,     __snake_case					:    Tuple		,     __snake_case					:    int		,     __snake_case					:    Dict		,     __snake_case					:    List[Any]		,     ):
      a		:   Optional[Any]	  =	self.num_labels
      a		:   Dict	  =	TFFlaubertForTokenClassification(config=__snake_case      )
      a		:   Any	  =	{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      a		:   Optional[Any]	  =	model(__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )







    def      lowercase_			( self					:    Tuple		,     __snake_case					:    Any		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    int		,     __snake_case					:    List[str]		,     __snake_case					:    Tuple		,     __snake_case					:    Dict		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Optional[int]		,     ):
      a		:   Any	  =	self.num_choices
      a		:   Dict	  =	TFFlaubertForMultipleChoice(config=__snake_case      )
      a		:   int	  =	tf.tile(tf.expand_dims(__snake_case		,     1      )		,     (1, self.num_choices, 1)      )
      a		:   int	  =	tf.tile(tf.expand_dims(__snake_case		,     1      )		,     (1, self.num_choices, 1)      )
      a		:   Union[str, Any]	  =	tf.tile(tf.expand_dims(__snake_case		,     1      )		,     (1, self.num_choices, 1)      )
      a		:   Any	  =	{
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      a		:   Dict	  =	model(__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)      )







    def      lowercase_			( self					:    Dict      ):
      a		:   str	  =	self.prepare_config_and_inputs()
      (
          (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      
      )		:   Optional[int]	  =	config_and_inputs
      a		:   Tuple	  =	{
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'langs': token_type_ids,
          'lengths': input_lengths,
      }
      return config, inputs_dict

@require_tf
class 				a__(   lowerCamelCase__						,					lowerCamelCase__						,					unittest.TestCase      ):
    lowercase__					=       (
        (
            TFFlaubertModel,
            TFFlaubertWithLMHeadModel,
            TFFlaubertForSequenceClassification,
            TFFlaubertForQuestionAnsweringSimple,
            TFFlaubertForTokenClassification,
            TFFlaubertForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    lowercase__					=       (
        (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    lowercase__					=       (
        {
            """feature-extraction""": TFFlaubertModel,
            """fill-mask""": TFFlaubertWithLMHeadModel,
            """question-answering""": TFFlaubertForQuestionAnsweringSimple,
            """text-classification""": TFFlaubertForSequenceClassification,
            """token-classification""": TFFlaubertForTokenClassification,
            """zero-shot""": TFFlaubertForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    lowercase__					=       False
    lowercase__					=       False







    def      lowercase_			( self					:    List[Any]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Optional[int]		,     __snake_case					:    Any		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    int      ):
      if (
          pipeline_test_casse_name == "QAPipelineTests"
          and tokenizer_name is not None
          and not tokenizer_name.endswith('Fast'      )
      ):
        # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
        # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
        # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
        return True

      return False







    def      lowercase_			( self					:    Optional[int]      ):
      a		:   Dict	  =	TFFlaubertModelTester(self      )
      a		:   Optional[int]	  =	ConfigTester(self		,     config_class=__snake_case		,     emb_dim=37      )







    def      lowercase_			( self					:    Union[str, Any]      ):
      self.config_tester.run_common_tests()







    def      lowercase_			( self					:    Any      ):
      a		:   List[Any]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*__snake_case      )







    def      lowercase_			( self					:    Any      ):
      a		:   Optional[Any]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*__snake_case      )







    def      lowercase_			( self					:    Tuple      ):
      a		:   Dict	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*__snake_case      )







    def      lowercase_			( self					:    int      ):
      a		:   int	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*__snake_case      )







    def      lowercase_			( self					:    Union[str, Any]      ):
      a		:   Optional[int]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_token_classification(*__snake_case      )







    def      lowercase_			( self					:    List[Any]      ):
      a		:   List[str]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_multiple_choice(*__snake_case      )







    @slow
    def      lowercase_			( self					:    Tuple      ):
      for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:   Dict	  =	TFFlaubertModel.from_pretrained(__snake_case      )
        self.assertIsNotNone(__snake_case      )

@require_tf
@require_sentencepiece
@require_tokenizers
class 				a__(   unittest.TestCase      ):







    @slow
    def      lowercase_			( self					:    int      ):
      a		:   Optional[int]	  =	TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased'      )

      a		:   Union[str, Any]	  =	tf.convert_to_tensor(
          [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]]		,     dtype=tf.intaa		,     )  # "J'aime flaubert !"

      a		:   List[Any]	  =	model(__snake_case      )[0]
      a		:   List[Any]	  =	tf.TensorShape((1, 8, 5_12)      )
      self.assertEqual(output.shape		,     __snake_case      )
      # compare the actual values for a slice.
      a		:   Union[str, Any]	  =	tf.convert_to_tensor(
          [
              [
                  [-1.8768773, -1.566555, 0.27072418],
                  [-1.6920038, -0.5873505, 1.9329599],
                  [-2.9563985, -1.6993835, 1.7972052],
              ]
          ]		,     dtype=tf.floataa		,     )

      self.assertTrue(np.allclose(output[:, :3, :3].numpy()		,     expected_slice.numpy()		,     atol=1e-4      )      )




'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase:   Tuple					=		logging.get_logger(__name__)

lowerCAmelCase:   Any					=		{
    'google/pegasus-large': 'https://huggingface.co/google/pegasus-large/resolve/main/config.json',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}

class 				a__(   lowerCamelCase__      ):
    lowercase__					=       """pegasus"""
    lowercase__					=       ["""past_key_values"""]
    lowercase__					=       {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







    def __init__( self					:    Union[str, Any]		,     __snake_case					:    Tuple=5_02_65		,     __snake_case					:    List[Any]=10_24		,     __snake_case					:    Optional[Any]=12		,     __snake_case					:    Tuple=40_96		,     __snake_case					:    str=16		,     __snake_case					:    int=12		,     __snake_case					:    Tuple=40_96		,     __snake_case					:    int=16		,     __snake_case					:    Tuple=0.0		,     __snake_case					:    Any=0.0		,     __snake_case					:    Any=True		,     __snake_case					:    Optional[int]=True		,     __snake_case					:    Any="gelu"		,     __snake_case					:    str=10_24		,     __snake_case					:    Dict=0.1		,     __snake_case					:    List[Any]=0.0		,     __snake_case					:    str=0.0		,     __snake_case					:    str=0.02		,     __snake_case					:    Union[str, Any]=0		,     __snake_case					:    List[Any]=False		,     __snake_case					:    Optional[Any]=0		,     __snake_case					:    str=1		,     __snake_case					:    Any=1		,     **__snake_case					:    int		,     ):
      a		:   Optional[int]	  =	vocab_size
      a		:   List[str]	  =	max_position_embeddings
      a		:   List[Any]	  =	d_model
      a		:   Tuple	  =	encoder_ffn_dim
      a		:   List[str]	  =	encoder_layers
      a		:   str	  =	encoder_attention_heads
      a		:   str	  =	decoder_ffn_dim
      a		:   Optional[int]	  =	decoder_layers
      a		:   Optional[int]	  =	decoder_attention_heads
      a		:   Optional[int]	  =	dropout
      a		:   Any	  =	attention_dropout
      a		:   str	  =	activation_dropout
      a		:   List[Any]	  =	activation_function
      a		:   Union[str, Any]	  =	init_std
      a		:   List[Any]	  =	encoder_layerdrop
      a		:   Union[str, Any]	  =	decoder_layerdrop
      a		:   List[Any]	  =	use_cache
      a		:   Any	  =	encoder_layers
      a		:   Optional[int]	  =	scale_embedding  # scale factor will be sqrt(d_model) if True
      super().__init__(
          pad_token_id=__snake_case		,     eos_token_id=__snake_case		,     is_encoder_decoder=__snake_case		,     decoder_start_token_id=__snake_case		,     forced_eos_token_id=__snake_case		,     **__snake_case		,     )







    @property
    def      lowercase_			( self					:    int      ):
      return self.encoder_attention_heads







    @property
    def      lowercase_			( self					:    int      ):
      return self.d_model