




"""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      :    Union[str, Any]      ,  UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					parent
						__UpperCAmelCase	: Optional[Any]		=					13
						__UpperCAmelCase	: Dict		=					7
						__UpperCAmelCase	: Any		=					True
						__UpperCAmelCase	: Dict		=					True
						__UpperCAmelCase	: str		=					True
						__UpperCAmelCase	: List[Any]		=					True
						__UpperCAmelCase	: Optional[int]		=					True
						__UpperCAmelCase	: Optional[Any]		=					False
						__UpperCAmelCase	: List[Any]		=					False
						__UpperCAmelCase	: str		=					False
						__UpperCAmelCase	: Dict		=					2
						__UpperCAmelCase	: Optional[Any]		=					99
						__UpperCAmelCase	: str		=					0
						__UpperCAmelCase	: int		=					32
						__UpperCAmelCase	: int		=					2
						__UpperCAmelCase	: Tuple		=					4
						__UpperCAmelCase	: Optional[Any]		=					0.1
						__UpperCAmelCase	: int		=					0.1
						__UpperCAmelCase	: Any		=					512
						__UpperCAmelCase	: Tuple		=					16
						__UpperCAmelCase	: Tuple		=					2
						__UpperCAmelCase	: Any		=					0.02
						__UpperCAmelCase	: Optional[Any]		=					3
						__UpperCAmelCase	: Tuple		=					4
						__UpperCAmelCase	: Dict		=					"last"
						__UpperCAmelCase	: Dict		=					True
						__UpperCAmelCase	: Optional[Any]		=					None
						__UpperCAmelCase	: Tuple		=					0


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size)
						__UpperCAmelCase	: List[Any]		=					random_attention_mask([self.batch_size, self.seq_length]      ,  dtype=tf.floataa)

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

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

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

						__UpperCAmelCase	: Union[str, Any]		=					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       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Optional[Any]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					TFFlaubertModel(config=UpperCamelCase_)
						__UpperCAmelCase	: Optional[Any]		=					{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
						__UpperCAmelCase	: Any		=					model(UpperCamelCase_)

						__UpperCAmelCase	: List[Any]		=					[input_ids, input_mask]
						__UpperCAmelCase	: List[Any]		=					model(UpperCamelCase_)
						self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size))


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


						"""simple docstring"""




						__UpperCAmelCase	: int		=					TFFlaubertWithLMHeadModel(UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
						__UpperCAmelCase	: Any		=					model(UpperCamelCase_)

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


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					TFFlaubertForQuestionAnsweringSimple(UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					{"input_ids": input_ids, "lengths": input_lengths}

						__UpperCAmelCase	: str		=					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       a_     (      self      :    Any      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    Dict      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					TFFlaubertForSequenceClassification(UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					{"input_ids": input_ids, "lengths": input_lengths}

						__UpperCAmelCase	: Optional[Any]		=					model(UpperCamelCase_)

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


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    int      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					self.num_labels
						__UpperCAmelCase	: Optional[int]		=					TFFlaubertForTokenClassification(config=UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__UpperCAmelCase	: int		=					model(UpperCamelCase_)
						self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.num_labels))


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Dict      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					self.num_choices
						__UpperCAmelCase	: str		=					TFFlaubertForMultipleChoice(config=UpperCamelCase_)
						__UpperCAmelCase	: Union[str, Any]		=					tf.tile(tf.expand_dims(UpperCamelCase_      ,  1)      ,  (1, self.num_choices, 1))
						__UpperCAmelCase	: Optional[Any]		=					tf.tile(tf.expand_dims(UpperCamelCase_      ,  1)      ,  (1, self.num_choices, 1))
						__UpperCAmelCase	: Optional[int]		=					tf.tile(tf.expand_dims(UpperCamelCase_      ,  1)      ,  (1, self.num_choices, 1))
						__UpperCAmelCase	: Optional[int]		=					{
						    "input_ids": multiple_choice_inputs_ids,
						    "attention_mask": multiple_choice_input_mask,
						    "token_type_ids": multiple_choice_token_type_ids,
						}
						__UpperCAmelCase	: Dict		=					model(UpperCamelCase_)
						self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.num_choices))


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					self.prepare_config_and_inputs()
						(
						    (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     (
						    __UpperCAmelCase
						)	,     
						)	: Tuple		=					config_and_inputs
						__UpperCAmelCase	: Optional[int]		=					{
						    "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__				(	__magic_name__							,    __magic_name__							,    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       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    int):


						"""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       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					TFFlaubertModelTester(self)
						__UpperCAmelCase	: Optional[int]		=					ConfigTester(self      ,  config_class=UpperCamelCase_      ,  emb_dim=37)


			def       a_     (      self      :    int):


						"""simple docstring"""




						self.config_tester.run_common_tests()


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_model(*UpperCamelCase_)


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase_)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase_)


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase_)


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCamelCase_)


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCamelCase_)


			@slow
			def       a_     (      self      :    Dict):


						"""simple docstring"""




						for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase	: Dict		=					TFFlaubertModel.from_pretrained(UpperCamelCase_)
									self.assertIsNotNone(UpperCamelCase_)






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


			@slow
			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased")

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

						__UpperCAmelCase	: Dict		=					model(UpperCamelCase_)[0]
						__UpperCAmelCase	: Any		=					tf.TensorShape((1, 8, 512))
						self.assertEqual(output.shape      ,  UpperCamelCase_)
						# compare the actual values for a slice.
						__UpperCAmelCase	: int		=					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 ...processing_utils import ProcessorMixin



class      a__				(	__magic_name__   ):
			lowercase_    =	["image_processor", "feature_extractor"]
			lowercase_    =	"TvltImageProcessor"
			lowercase_    =	"TvltFeatureExtractor"


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict):


						"""simple docstring"""




						super().__init__(image_processor=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					image_processor
						__UpperCAmelCase	: Dict		=					feature_extractor


			def __call__(      self      :    Tuple      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    List[str]=False      ,  UpperCamelCase_      :    Optional[int]=False      ,  *UpperCamelCase_      :    List[str]      ,  **UpperCamelCase_      :    str      ,  ):


						"""simple docstring"""




						if images is None and audio is None:
									raise ValueError("You need to specify either an `images` or `audio` input to process.")

						__UpperCAmelCase	: Optional[Any]		=					None
						if images is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  mask_pixel=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if images_mixed is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  is_mixed=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if audio is not None:
									__UpperCAmelCase	: List[Any]		=					self.feature_extractor(
									    UpperCamelCase_      ,  *UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  mask_audio=UpperCamelCase_      ,  **UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					{}
						if audio is not None:
									output_dict.update(UpperCamelCase_)
						if images is not None:
									output_dict.update(UpperCamelCase_)
						if images_mixed_dict is not None:
									output_dict.update(UpperCamelCase_)
						return output_dict


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.image_processor.model_input_names
						__UpperCAmelCase	: List[Any]		=					self.feature_extractor.model_input_names
						return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names))


