





'''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     lowerCAmelCase__ :

       '''simple docstring'''

       def __init__( self					:				Union[str, Any]       ,     a__					:				str       ,     ):
              UpperCAmelCase  =						parent
              UpperCAmelCase  =						13
              UpperCAmelCase  =						7
              UpperCAmelCase  =						True
              UpperCAmelCase  =						True
              UpperCAmelCase  =						True
              UpperCAmelCase  =						True
              UpperCAmelCase  =						True
              UpperCAmelCase  =						False
              UpperCAmelCase  =						False
              UpperCAmelCase  =						False
              UpperCAmelCase  =						2
              UpperCAmelCase  =						99
              UpperCAmelCase  =						0
              UpperCAmelCase  =						32
              UpperCAmelCase  =						2
              UpperCAmelCase  =						4
              UpperCAmelCase  =						0.1
              UpperCAmelCase  =						0.1
              UpperCAmelCase  =						512
              UpperCAmelCase  =						16
              UpperCAmelCase  =						2
              UpperCAmelCase  =						0.02
              UpperCAmelCase  =						3
              UpperCAmelCase  =						4
              UpperCAmelCase  =						'''last'''
              UpperCAmelCase  =						True
              UpperCAmelCase  =						None
              UpperCAmelCase  =						0

       def 			__snake_case      ( self					:				str						):
              UpperCAmelCase  =						ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size						)
              UpperCAmelCase  =						random_attention_mask([self.batch_size, self.seq_length]       ,     dtype=tf.floataa						)

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

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

              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  =						ids_tensor([self.batch_size]       ,     2       ,     dtype=tf.floataa						)
                     UpperCAmelCase  =						ids_tensor([self.batch_size]       ,     self.num_choices						)

              UpperCAmelCase  =						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 			__snake_case      ( self					:				Dict       ,     a__					:				Optional[Any]       ,     a__					:				Union[str, Any]       ,     a__					:				Tuple       ,     a__					:				Union[str, Any]       ,     a__					:				Optional[int]       ,     a__					:				int       ,     a__					:				Tuple       ,     a__					:				Optional[Any]       ,     a__					:				Union[str, Any]       ,     ):
              UpperCAmelCase  =						TFFlaubertModel(config=a__						)
              UpperCAmelCase  =						{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
              UpperCAmelCase  =						model(a__						)

              UpperCAmelCase  =						[input_ids, input_mask]
              UpperCAmelCase  =						model(a__						)
              self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size)						)

       def 			__snake_case      ( self					:				str       ,     a__					:				Tuple       ,     a__					:				Tuple       ,     a__					:				Optional[int]       ,     a__					:				str       ,     a__					:				Any       ,     a__					:				List[str]       ,     a__					:				str       ,     a__					:				List[str]       ,     a__					:				int       ,     ):
              UpperCAmelCase  =						TFFlaubertWithLMHeadModel(a__						)

              UpperCAmelCase  =						{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
              UpperCAmelCase  =						model(a__						)

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

       def 			__snake_case      ( self					:				List[Any]       ,     a__					:				Optional[int]       ,     a__					:				List[Any]       ,     a__					:				Union[str, Any]       ,     a__					:				Optional[Any]       ,     a__					:				List[Any]       ,     a__					:				Union[str, Any]       ,     a__					:				Union[str, Any]       ,     a__					:				int       ,     a__					:				int       ,     ):
              UpperCAmelCase  =						TFFlaubertForQuestionAnsweringSimple(a__						)

              UpperCAmelCase  =						{'''input_ids''': input_ids, '''lengths''': input_lengths}

              UpperCAmelCase  =						model(a__						)

              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 			__snake_case      ( self					:				Union[str, Any]       ,     a__					:				Optional[int]       ,     a__					:				Optional[Any]       ,     a__					:				Tuple       ,     a__					:				Dict       ,     a__					:				Dict       ,     a__					:				List[Any]       ,     a__					:				List[Any]       ,     a__					:				List[str]       ,     a__					:				Union[str, Any]       ,     ):
              UpperCAmelCase  =						TFFlaubertForSequenceClassification(a__						)

              UpperCAmelCase  =						{'''input_ids''': input_ids, '''lengths''': input_lengths}

              UpperCAmelCase  =						model(a__						)

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

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

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



       def 			__snake_case      ( self					:				int						):
              UpperCAmelCase  =						self.prepare_config_and_inputs()
              (
                  (
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					(
                  UpperCAmelCase
              ),					
              )  =						config_and_inputs
              UpperCAmelCase  =						{
                  '''input_ids''': input_ids,
                  '''token_type_ids''': token_type_ids,
                  '''langs''': token_type_ids,
                  '''lengths''': input_lengths,
              }
              return config, inputs_dict






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

       '''simple docstring'''
       _lowerCamelCase								=(
           (
               TFFlaubertModel,
               TFFlaubertWithLMHeadModel,
               TFFlaubertForSequenceClassification,
               TFFlaubertForQuestionAnsweringSimple,
               TFFlaubertForTokenClassification,
               TFFlaubertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       _lowerCamelCase								=(
           (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Check other models whether language generation is also applicable
       _lowerCamelCase								=(
           {
               "feature-extraction": TFFlaubertModel,
               "fill-mask": TFFlaubertWithLMHeadModel,
               "question-answering": TFFlaubertForQuestionAnsweringSimple,
               "text-classification": TFFlaubertForSequenceClassification,
               "token-classification": TFFlaubertForTokenClassification,
               "zero-shot": TFFlaubertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       _lowerCamelCase								=False
       _lowerCamelCase								=False

       def 			__snake_case      ( self					:				Union[str, Any]       ,     a__					:				Optional[Any]       ,     a__					:				Union[str, Any]       ,     a__					:				Optional[Any]       ,     a__					:				str       ,     a__					:				Optional[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 			__snake_case      ( self					:				int						):
              UpperCAmelCase  =						TFFlaubertModelTester(self						)
              UpperCAmelCase  =						ConfigTester(self       ,     config_class=a__       ,     emb_dim=37						)

       def 			__snake_case      ( self					:				Optional[int]						):
              self.config_tester.run_common_tests()

       def 			__snake_case      ( self					:				List[str]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_model(*a__						)

       def 			__snake_case      ( self					:				List[Any]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_lm_head(*a__						)

       def 			__snake_case      ( self					:				List[Any]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_qa(*a__						)

       def 			__snake_case      ( self					:				Tuple						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_sequence_classif(*a__						)

       def 			__snake_case      ( self					:				List[str]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_for_token_classification(*a__						)

       def 			__snake_case      ( self					:				Union[str, Any]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_flaubert_for_multiple_choice(*a__						)



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






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

       '''simple docstring'''

       @slow
       def 			__snake_case      ( self					:				Union[str, Any]						):
              UpperCAmelCase  =						TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased'''						)

              UpperCAmelCase  =						tf.convert_to_tensor(
                  [[0, 158, 735, 2592, 1424, 6727, 82, 1]]       ,     dtype=tf.intaa       ,     )  # "J'aime flaubert !"

              UpperCAmelCase  =						model(a__						)[0]
              UpperCAmelCase  =						tf.TensorShape((1, 8, 512)						)
              self.assertEqual(output.shape       ,     a__						)
              # compare the actual values for a slice.
              UpperCAmelCase  =						tf.convert_to_tensor(
                  [
                      [
                          [-1.8_768_773, -1.566_555, 0.27_072_418],
                          [-1.6_920_038, -0.5_873_505, 1.9_329_599],
                          [-2.9_563_985, -1.6_993_835, 1.7_972_052],
                      ]
                  ]       ,     dtype=tf.floataa       ,     )

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








'''simple docstring'''



from typing import Dict

from .base import GenericTensor, Pipeline






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       def        UpperCamelCase_							(      self,							A=None,							A=None,							A=None,							**A  ):







             '''simple docstring'''







             if tokenize_kwargs is None:
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						{}

             if truncation is not None:
                   if "truncation" in tokenize_kwargs:
                         raise ValueError(
                             'truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)'  )
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						truncation

             SCREAMING_SNAKE_CASE   :      int    					=						tokenize_kwargs

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						{}
             if return_tensors is not None:
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						return_tensors

             return preprocess_params, {}, postprocess_params







       def        UpperCamelCase_							(      self,							A,							**A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						self.framework
             SCREAMING_SNAKE_CASE   :      Tuple    					=						self.tokenizer(A,							return_tensors=A,							**A  )
             return model_inputs







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						self.model(**A  )
             return model_outputs







       def        UpperCamelCase_							(      self,							A,							A=False  ):







             '''simple docstring'''







             if return_tensors:
                   return model_outputs[0]
             if self.framework == "pt":
                   return model_outputs[0].tolist()
             elif self.framework == "tf":
                   return model_outputs[0].numpy().tolist()







       def __call__(      self,							*A,							**A  ):







             '''simple docstring'''







             return super().__call__(*A,							**A  )


