


"""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"""
def   _a			( _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE = 0			)      ->			list:
							snake_case_          =						length or len(_SCREAMING_SNAKE_CASE			)
							snake_case_          =						False
							for i in range(length - 1			):
														if list_data[i] > list_data[i + 1]:
																					snake_case_    ,  snake_case_          =						list_data[i + 1], list_data[i]
																					snake_case_          =						True

							return list_data if not swapped else bubble_sort(_SCREAMING_SNAKE_CASE			,	length - 1			)


if __name__ == "__main__":
				import doctest

				doctest.testmod()



