




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







   def __init__(      self		:						List[str]					,						_snake_case		:						Tuple					,						):

        """simple docstring"""


        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.0_2
        UpperCAmelCase_          =							3
        UpperCAmelCase_          =							4
        UpperCAmelCase_          =							'''last'''
        UpperCAmelCase_          =							True
        UpperCAmelCase_          =							None
        UpperCAmelCase_          =							0







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        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 lowerCamelCase						(      self		:						Tuple					,						_snake_case		:						int					,						_snake_case		:						Tuple					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Any					,						_snake_case		:						List[Any]					,						_snake_case		:						Any					,						_snake_case		:						Tuple					,						):

        """simple docstring"""


        UpperCAmelCase_          =							TFFlaubertModel(config=_snake_case)
        UpperCAmelCase_          =							{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
        UpperCAmelCase_          =							model(_snake_case)

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







   def lowerCamelCase						(      self		:						Union[str, Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						Optional[int]					,						_snake_case		:						str					,						_snake_case		:						Optional[int]					,						_snake_case		:						int					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Tuple					,						_snake_case		:						Optional[int]					,						_snake_case		:						Union[str, Any]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							TFFlaubertWithLMHeadModel(_snake_case)

        UpperCAmelCase_          =							{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
        UpperCAmelCase_          =							model(_snake_case)

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







   def lowerCamelCase						(      self		:						str					,						_snake_case		:						Optional[int]					,						_snake_case		:						Optional[int]					,						_snake_case		:						Optional[int]					,						_snake_case		:						Dict					,						_snake_case		:						str					,						_snake_case		:						Any					,						_snake_case		:						List[str]					,						_snake_case		:						str					,						_snake_case		:						Optional[int]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							TFFlaubertForQuestionAnsweringSimple(_snake_case)

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

        UpperCAmelCase_          =							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 lowerCamelCase						(      self		:						List[str]					,						_snake_case		:						List[Any]					,						_snake_case		:						List[str]					,						_snake_case		:						Optional[Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						int					,						_snake_case		:						Any					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Dict					,						_snake_case		:						int					,						):

        """simple docstring"""


        UpperCAmelCase_          =							TFFlaubertForSequenceClassification(_snake_case)

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

        UpperCAmelCase_          =							model(_snake_case)

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







   def lowerCamelCase						(      self		:						int					,						_snake_case		:						Any					,						_snake_case		:						List[str]					,						_snake_case		:						Optional[int]					,						_snake_case		:						Any					,						_snake_case		:						Dict					,						_snake_case		:						List[Any]					,						_snake_case		:						Any					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						Union[str, Any]					,						):

        """simple docstring"""


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







   def lowerCamelCase						(      self		:						List[str]					,						_snake_case		:						Any					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						Tuple					,						_snake_case		:						Optional[int]					,						_snake_case		:						Dict					,						_snake_case		:						Tuple					,						_snake_case		:						int					,						_snake_case		:						List[str]					,						_snake_case		:						Tuple					,						):

        """simple docstring"""


        UpperCAmelCase_          =							self.num_choices
        UpperCAmelCase_          =							TFFlaubertForMultipleChoice(config=_snake_case)
        UpperCAmelCase_          =							tf.tile(tf.expand_dims(_snake_case					,						1)					,						(1, self.num_choices, 1))
        UpperCAmelCase_          =							tf.tile(tf.expand_dims(_snake_case					,						1)					,						(1, self.num_choices, 1))
        UpperCAmelCase_          =							tf.tile(tf.expand_dims(_snake_case					,						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(_snake_case)
        self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices))







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        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 __snake_case			(			a ,  a ,  unittest.TestCase		):
   UpperCAmelCase__  :    Optional[Any]          =  (
       (
           TFFlaubertModel,
           TFFlaubertWithLMHeadModel,
           TFFlaubertForSequenceClassification,
           TFFlaubertForQuestionAnsweringSimple,
           TFFlaubertForTokenClassification,
           TFFlaubertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   UpperCAmelCase__  :    List[Any]          =  (
       (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
   )  # TODO (PVP): Check other models whether language generation is also applicable
   UpperCAmelCase__  :    List[str]          =  (
       {
           '''feature-extraction''': TFFlaubertModel,
           '''fill-mask''': TFFlaubertWithLMHeadModel,
           '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
           '''text-classification''': TFFlaubertForSequenceClassification,
           '''token-classification''': TFFlaubertForTokenClassification,
           '''zero-shot''': TFFlaubertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   UpperCAmelCase__  :    List[str]          =  False
   UpperCAmelCase__  :    str          =  False







   def lowerCamelCase						(      self		:						Union[str, Any]					,						_snake_case		:						Optional[Any]					,						_snake_case		:						int					,						_snake_case		:						Tuple					,						_snake_case		:						Dict					,						_snake_case		:						List[Any]):

        """simple docstring"""


        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 lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        UpperCAmelCase_          =							TFFlaubertModelTester(self)
        UpperCAmelCase_          =							ConfigTester(self					,						config_class=_snake_case					,						emb_dim=37)







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        self.config_tester.run_common_tests()







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_model(*_snake_case)







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_lm_head(*_snake_case)







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_qa(*_snake_case)







   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_sequence_classif(*_snake_case)







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_for_token_classification(*_snake_case)







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_for_multiple_choice(*_snake_case)







   @slow
   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_          =							TFFlaubertModel.from_pretrained(_snake_case)
             self.assertIsNotNone(_snake_case)







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







   @slow
   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        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(_snake_case)[0]
        UpperCAmelCase_          =							tf.TensorShape((1, 8, 512))
        self.assertEqual(output.shape					,						_snake_case)
        # compare the actual values for a slice.
        UpperCAmelCase_          =							tf.convert_to_tensor(
            [
                [
                    [-1.8_7_6_8_7_7_3, -1.5_6_6_5_5_5, 0.2_7_0_7_2_4_1_8],
                    [-1.6_9_2_0_0_3_8, -0.5_8_7_3_5_0_5, 1.9_3_2_9_5_9_9],
                    [-2.9_5_6_3_9_8_5, -1.6_9_9_3_8_3_5, 1.7_9_7_2_0_5_2],
                ]
            ]					,						dtype=tf.floataa					,						)

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












import warnings
from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class __snake_case			(			a		):
   UpperCAmelCase__  :    Dict          =  ['''image_processor''', '''tokenizer''']
   UpperCAmelCase__  :    Dict          =  '''FlavaImageProcessor'''
   UpperCAmelCase__  :    Dict          =  ('''BertTokenizer''', '''BertTokenizerFast''')







   def __init__(      self		:						Union[str, Any]					,						_snake_case		:						List[str]=None					,						_snake_case		:						str=None					,						**_snake_case		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							None
        if "feature_extractor" in kwargs:
             warnings.warn(
                 '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                 ''' instead.'''					,						_snake_case					,						)
             UpperCAmelCase_          =							kwargs.pop('''feature_extractor''')

        UpperCAmelCase_          =							image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
             raise ValueError('''You need to specify an `image_processor`.''')
        if tokenizer is None:
             raise ValueError('''You need to specify a `tokenizer`.''')

        super().__init__(_snake_case					,						_snake_case)
        UpperCAmelCase_          =							self.image_processor







   def __call__(      self		:						List[Any]					,						_snake_case		:						Optional[ImageInput] = None					,						_snake_case		:						Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None					,						_snake_case		:						bool = True					,						_snake_case		:						Union[bool, str, PaddingStrategy] = False					,						_snake_case		:						Union[bool, str, TruncationStrategy] = False					,						_snake_case		:						Optional[int] = None					,						_snake_case		:						int = 0					,						_snake_case		:						Optional[int] = None					,						_snake_case		:						Optional[bool] = None					,						_snake_case		:						Optional[bool] = None					,						_snake_case		:						Optional[bool] = None					,						_snake_case		:						Optional[bool] = None					,						_snake_case		:						bool = False					,						_snake_case		:						bool = False					,						_snake_case		:						bool = False					,						_snake_case		:						bool = False					,						_snake_case		:						bool = True					,						_snake_case		:						Optional[Union[str, TensorType]] = None					,						**_snake_case		:						Any					,						):

        """simple docstring"""


        if text is None and images is None:
             raise ValueError('''You have to specify either text or images. Both cannot be none.''')

        if text is not None:
             UpperCAmelCase_          =							self.tokenizer(
                 text=_snake_case					,						add_special_tokens=_snake_case					,						padding=_snake_case					,						truncation=_snake_case					,						max_length=_snake_case					,						stride=_snake_case					,						pad_to_multiple_of=_snake_case					,						return_token_type_ids=_snake_case					,						return_attention_mask=_snake_case					,						return_overflowing_tokens=_snake_case					,						return_special_tokens_mask=_snake_case					,						return_offsets_mapping=_snake_case					,						return_length=_snake_case					,						verbose=_snake_case					,						return_tensors=_snake_case					,						**_snake_case					,						)
        if images is not None:
             UpperCAmelCase_          =							self.image_processor(
                 _snake_case					,						return_image_mask=_snake_case					,						return_codebook_pixels=_snake_case					,						return_tensors=_snake_case					,						**_snake_case					,						)

        if text is not None and images is not None:
             encoding.update(_snake_case)
             return encoding
        elif text is not None:
             return encoding
        else:
             return BatchEncoding(data=dict(**_snake_case)					,						tensor_type=_snake_case)







   def lowerCamelCase						(      self		:						Any					,						*_snake_case		:						Optional[Any]					,						**_snake_case		:						int):

        """simple docstring"""


        return self.tokenizer.batch_decode(*_snake_case					,						**_snake_case)







   def lowerCamelCase						(      self		:						Optional[int]					,						*_snake_case		:						int					,						**_snake_case		:						Dict):

        """simple docstring"""


        return self.tokenizer.decode(*_snake_case					,						**_snake_case)







   @property
   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        UpperCAmelCase_          =							self.tokenizer.model_input_names
        UpperCAmelCase_          =							self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))







   @property
   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        warnings.warn(
            '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''					,						_snake_case					,						)
        return self.image_processor_class







   @property
   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        warnings.warn(
            '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''					,						_snake_case					,						)
        return self.image_processor






