


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





							'''simple docstring'''

							def __init__(			self							: Union[str, Any]						,    UpperCAmelCase_							: str						,    )   ->str:



														"""simple docstring"""



														snake_case_          =						parent
														snake_case_          =						13
														snake_case_          =						7
														snake_case_          =						True
														snake_case_          =						True
														snake_case_          =						True
														snake_case_          =						True
														snake_case_          =						True
														snake_case_          =						False
														snake_case_          =						False
														snake_case_          =						False
														snake_case_          =						2
														snake_case_          =						99
														snake_case_          =						0
														snake_case_          =						32
														snake_case_          =						2
														snake_case_          =						4
														snake_case_          =						0.1
														snake_case_          =						0.1
														snake_case_          =						512
														snake_case_          =						16
														snake_case_          =						2
														snake_case_          =						0.02
														snake_case_          =						3
														snake_case_          =						4
														snake_case_          =						"""last"""
														snake_case_          =						True
														snake_case_          =						None
														snake_case_          =						0



							def 						lowerCAmelCase    (			self							: List[str]    )   ->str:



														"""simple docstring"""



														snake_case_          =						ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size    )
														snake_case_          =						random_attention_mask([self.batch_size, self.seq_length]						,    dtype=tf.floataa    )

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

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

														snake_case_          =						None
														snake_case_          =						None
														snake_case_          =						None
														if self.use_labels:
																					snake_case_          =						ids_tensor([self.batch_size]						,    self.type_sequence_label_size    )
																					snake_case_          =						ids_tensor([self.batch_size, self.seq_length]						,    self.num_labels    )
																					snake_case_          =						ids_tensor([self.batch_size]						,    2						,    dtype=tf.floataa    )
																					snake_case_          =						ids_tensor([self.batch_size]						,    self.num_choices    )

														snake_case_          =						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							: Optional[int]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Optional[int]						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: Optional[int]						,    UpperCAmelCase_							: Any						,    )   ->List[Any]:



														"""simple docstring"""



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

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



							def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: List[Any]						,    )   ->Any:



														"""simple docstring"""



														snake_case_          =						TFFlaubertWithLMHeadModel(UpperCAmelCase_    )

														snake_case_          =						{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
														snake_case_          =						model(UpperCAmelCase_    )

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



							def 						lowerCAmelCase    (			self							: List[str]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: str						,    )   ->Optional[Any]:



														"""simple docstring"""



														snake_case_          =						TFFlaubertForQuestionAnsweringSimple(UpperCAmelCase_    )

														snake_case_          =						{"""input_ids""": input_ids, """lengths""": input_lengths}

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



														"""simple docstring"""



														snake_case_          =						TFFlaubertForSequenceClassification(UpperCAmelCase_    )

														snake_case_          =						{"""input_ids""": input_ids, """lengths""": input_lengths}

														snake_case_          =						model(UpperCAmelCase_    )

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



							def 						lowerCAmelCase    (			self							: List[str]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Optional[Any]						,    )   ->Dict:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: int						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: int						,    )   ->Any:



														"""simple docstring"""



														snake_case_          =						self.num_choices
														snake_case_          =						TFFlaubertForMultipleChoice(config=UpperCAmelCase_    )
														snake_case_          =						tf.tile(tf.expand_dims(UpperCAmelCase_						,    1    )						,    (1, self.num_choices, 1)    )
														snake_case_          =						tf.tile(tf.expand_dims(UpperCAmelCase_						,    1    )						,    (1, self.num_choices, 1)    )
														snake_case_          =						tf.tile(tf.expand_dims(UpperCAmelCase_						,    1    )						,    (1, self.num_choices, 1)    )
														snake_case_          =						{
														    """input_ids""": multiple_choice_inputs_ids,
														    """attention_mask""": multiple_choice_input_mask,
														    """token_type_ids""": multiple_choice_token_type_ids,
														}
														snake_case_          =						model(UpperCAmelCase_    )
														self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_choices)    )



							def 						lowerCAmelCase    (			self							: Any    )   ->Optional[Any]:



														"""simple docstring"""



														snake_case_          =						self.prepare_config_and_inputs()
														(
														    (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,  (
														    snake_case_  
														)    ,    
														)          =						config_and_inputs
														snake_case_          =						{
														    """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       (snake_case__  ,						snake_case__  ,						unittest.TestCase):





							'''simple docstring'''

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



							def 						lowerCAmelCase    (			self							: str						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: Dict						,    UpperCAmelCase_							: List[Any]    )   ->str:



														"""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							: Optional[int]    )   ->Optional[int]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: str    )   ->List[Any]:



														"""simple docstring"""



														self.config_tester.run_common_tests()



							def 						lowerCAmelCase    (			self							: Tuple    )   ->Optional[int]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: int    )   ->Dict:



														"""simple docstring"""



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



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



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Dict    )   ->List[str]:



														"""simple docstring"""



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



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



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->List[Any]:



														"""simple docstring"""



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



							@slow
							def 						lowerCAmelCase    (			self							: Dict    )   ->Union[str, Any]:



														"""simple docstring"""



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





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





							'''simple docstring'''

							@slow
							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Union[str, Any]:



														"""simple docstring"""



														snake_case_          =						TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""    )

														snake_case_          =						tf.convert_to_tensor(
														    [[0, 158, 735, 2_592, 1_424, 6_727, 82, 1]]						,    dtype=tf.intaa						,    )  # "J'aime flaubert !"

														snake_case_          =						model(UpperCAmelCase_    )[0]
														snake_case_          =						tf.TensorShape((1, 8, 512)    )
														self.assertEqual(output.shape						,    UpperCAmelCase_    )
														# compare the actual values for a slice.
														snake_case_          =						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"""
import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin





class        __A       (snake_case__):





							'''simple docstring'''

							__lowercase:						Any							 =      ["""image_processor""", """tokenizer"""]
							__lowercase:						Dict							 =      """AutoImageProcessor"""
							__lowercase:						List[Any]							 =      """AutoTokenizer"""



							def __init__(			self							: Tuple						,    UpperCAmelCase_							: int=None						,    UpperCAmelCase_							: Tuple=None						,    **UpperCAmelCase_							: str    )   ->Optional[int]:



														"""simple docstring"""



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

														snake_case_          =						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__(UpperCAmelCase_						,    UpperCAmelCase_    )
														snake_case_          =						self.image_processor
														snake_case_          =						False



							def __call__(			self							: Any						,    *UpperCAmelCase_							: Optional[int]						,    **UpperCAmelCase_							: str    )   ->List[str]:



														"""simple docstring"""



														if self._in_target_context_manager:
																					return self.current_processor(*UpperCAmelCase_						,    **UpperCAmelCase_    )

														snake_case_          =						kwargs.pop("""images"""						,    UpperCAmelCase_    )
														snake_case_          =						kwargs.pop("""text"""						,    UpperCAmelCase_    )
														if len(UpperCAmelCase_    ) > 0:
																					snake_case_          =						args[0]
																					snake_case_          =						args[1:]

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

														if images is not None:
																					snake_case_          =						self.image_processor(UpperCAmelCase_						,    *UpperCAmelCase_						,    **UpperCAmelCase_    )
														if text is not None:
																					snake_case_          =						self.tokenizer(UpperCAmelCase_						,    **UpperCAmelCase_    )

														if text is None:
																					return inputs
														elif images is None:
																					return encodings
														else:
																					snake_case_          =						encodings["""input_ids"""]
																					return inputs



							def 						lowerCAmelCase    (			self							: Optional[Any]						,    *UpperCAmelCase_							: Optional[Any]						,    **UpperCAmelCase_							: Tuple    )   ->Optional[int]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Optional[int]						,    *UpperCAmelCase_							: Union[str, Any]						,    **UpperCAmelCase_							: List[Any]    )   ->str:



														"""simple docstring"""



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



							@contextmanager
							def 						lowerCAmelCase    (			self							: List[str]    )   ->Dict:



														"""simple docstring"""



														warnings.warn(
														    """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
														    """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
														    """your images inputs, or in a separate call."""    )
														snake_case_          =						True
														snake_case_          =						self.tokenizer
														yield
														snake_case_          =						self.image_processor
														snake_case_          =						False



							def 						lowerCAmelCase    (			self							: int						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: str=False						,    UpperCAmelCase_							: str=None    )   ->Tuple:



														"""simple docstring"""



														if added_vocab is None:
																					snake_case_          =						self.tokenizer.get_added_vocab()

														snake_case_          =						{}

														while tokens:
																					snake_case_          =						re.search(R"""<s_(.*?)>"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																					if start_token is None:
																												break
																					snake_case_          =						start_token.group(1    )
																					snake_case_          =						re.search(RF"""</s_{key}>"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																					snake_case_          =						start_token.group()
																					if end_token is None:
																												snake_case_          =						tokens.replace(UpperCAmelCase_						,    """"""    )
																					else:
																												snake_case_          =						end_token.group()
																												snake_case_          =						re.escape(UpperCAmelCase_    )
																												snake_case_          =						re.escape(UpperCAmelCase_    )
																												snake_case_          =						re.search(F"""{start_token_escaped}(.*?){end_token_escaped}"""						,    UpperCAmelCase_						,    re.IGNORECASE    )
																												if content is not None:
																																			snake_case_          =						content.group(1    ).strip()
																																			if r"<s_" in content and r"</s_" in content:  # non-leaf node
																																										snake_case_          =						self.tokenajson(UpperCAmelCase_						,    is_inner_value=UpperCAmelCase_						,    added_vocab=UpperCAmelCase_    )
																																										if value:
																																																	if len(UpperCAmelCase_    ) == 1:
																																																								snake_case_          =						value[0]
																																																	snake_case_          =						value
																																			else:  # leaf nodes
																																										snake_case_          =						[]
																																										for leaf in content.split(R"""<sep/>"""    ):
																																																	snake_case_          =						leaf.strip()
																																																	if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
																																																								snake_case_          =						leaf[1:-2]  # for categorical special tokens
																																																	output[key].append(UpperCAmelCase_    )
																																										if len(output[key]    ) == 1:
																																																	snake_case_          =						output[key][0]

																												snake_case_          =						tokens[tokens.find(UpperCAmelCase_    ) + len(UpperCAmelCase_    ) :].strip()
																												if tokens[:6] == r"<sep/>":  # non-leaf nodes
																																			return [output] + self.tokenajson(tokens[6:]						,    is_inner_value=UpperCAmelCase_						,    added_vocab=UpperCAmelCase_    )

														if len(UpperCAmelCase_    ):
																					return [output] if is_inner_value else output
														else:
																					return [] if is_inner_value else {"text_sequence": tokens}



							@property
							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



														"""simple docstring"""



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



							@property
							def 						lowerCAmelCase    (			self							: List[str]    )   ->str:



														"""simple docstring"""



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



