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






			'''simple docstring'''

			def __init__(       self :       List[str]						,	snake_case_ :       List[str]						,	):
							UpperCamelCase_:							List[str]	=						parent
							UpperCamelCase_:							Optional[Any]	=						13
							UpperCamelCase_:							Any	=						7
							UpperCamelCase_:							int	=						True
							UpperCamelCase_:							Union[str, Any]	=						True
							UpperCamelCase_:							List[Any]	=						True
							UpperCamelCase_:							Any	=						True
							UpperCamelCase_:							int	=						True
							UpperCamelCase_:							str	=						False
							UpperCamelCase_:							Optional[int]	=						False
							UpperCamelCase_:							Tuple	=						False
							UpperCamelCase_:							int	=						2
							UpperCamelCase_:							Any	=						99
							UpperCamelCase_:							Optional[Any]	=						0
							UpperCamelCase_:							Optional[Any]	=						32
							UpperCamelCase_:							List[Any]	=						2
							UpperCamelCase_:							Any	=						4
							UpperCamelCase_:							Dict	=						0.1
							UpperCamelCase_:							Tuple	=						0.1
							UpperCamelCase_:							Dict	=						512
							UpperCamelCase_:							Tuple	=						16
							UpperCamelCase_:							Any	=						2
							UpperCamelCase_:							int	=						0.02
							UpperCamelCase_:							Tuple	=						3
							UpperCamelCase_:							List[Any]	=						4
							UpperCamelCase_:							int	=						'''last'''
							UpperCamelCase_:							int	=						True
							UpperCamelCase_:							Optional[Any]	=						None
							UpperCamelCase_:							Optional[int]	=						0

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							UpperCamelCase_:							Tuple	=						ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size      )
							UpperCamelCase_:							int	=						random_attention_mask([self.batch_size, self.seq_length]						,	dtype=tf.floataa      )

							UpperCamelCase_:							int	=						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_:							int	=						None
							if self.use_token_type_ids:
											UpperCamelCase_:							str	=						ids_tensor([self.batch_size, self.seq_length]						,	self.n_langs      )

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

							UpperCamelCase_:							List[str]	=						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 :       int						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       List[Any]						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       List[Any]						,	snake_case_ :       int						,	snake_case_ :       List[str]						,	snake_case_ :       Dict						,	snake_case_ :       Optional[int]						,	snake_case_ :       int						,	):
							UpperCamelCase_:							Dict	=						TFFlaubertModel(config=UpperCamelCase__      )
							UpperCamelCase_:							List[str]	=						{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
							UpperCamelCase_:							Optional[Any]	=						model(UpperCamelCase__      )

							UpperCamelCase_:							List[str]	=						[input_ids, input_mask]
							UpperCamelCase_:							Optional[int]	=						model(UpperCamelCase__      )
							self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)      )

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       List[Any]						,	snake_case_ :       Optional[Any]						,	snake_case_ :       Tuple						,	snake_case_ :       Any						,	snake_case_ :       int						,	snake_case_ :       List[Any]						,	snake_case_ :       List[str]						,	snake_case_ :       Dict						,	snake_case_ :       int						,	):
							UpperCamelCase_:							List[Any]	=						TFFlaubertWithLMHeadModel(UpperCamelCase__      )

							UpperCamelCase_:							Optional[int]	=						{'''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       lowerCAmelCase__	(       self :       List[str]						,	snake_case_ :       List[Any]						,	snake_case_ :       int						,	snake_case_ :       Tuple						,	snake_case_ :       Optional[Any]						,	snake_case_ :       int						,	snake_case_ :       str						,	snake_case_ :       str						,	snake_case_ :       int						,	snake_case_ :       Dict						,	):
							UpperCamelCase_:							Union[str, Any]	=						TFFlaubertForQuestionAnsweringSimple(UpperCamelCase__      )

							UpperCamelCase_:							Dict	=						{'''input_ids''': input_ids, '''lengths''': input_lengths}

							UpperCamelCase_:							Optional[Any]	=						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 :       str						,	snake_case_ :       Any						,	snake_case_ :       Dict						,	snake_case_ :       List[Any]						,	snake_case_ :       Any						,	snake_case_ :       List[str]						,	snake_case_ :       Tuple						,	snake_case_ :       Tuple						,	snake_case_ :       Optional[int]						,	snake_case_ :       Tuple						,	):
							UpperCamelCase_:							Any	=						TFFlaubertForSequenceClassification(UpperCamelCase__      )

							UpperCamelCase_:							List[str]	=						{'''input_ids''': input_ids, '''lengths''': input_lengths}

							UpperCamelCase_:							Any	=						model(UpperCamelCase__      )

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

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

			def       lowerCAmelCase__	(       self :       str						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       Optional[Any]						,	snake_case_ :       Dict						,	snake_case_ :       List[str]						,	snake_case_ :       Optional[int]						,	snake_case_ :       int						,	snake_case_ :       Any						,	snake_case_ :       Tuple						,	snake_case_ :       Any						,	):
							UpperCamelCase_:							List[Any]	=						self.num_choices
							UpperCamelCase_:							int	=						TFFlaubertForMultipleChoice(config=UpperCamelCase__      )
							UpperCamelCase_:							Optional[Any]	=						tf.tile(tf.expand_dims(UpperCamelCase__						,	1      )						,	(1, self.num_choices, 1)      )
							UpperCamelCase_:							Dict	=						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_:							int	=						{
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							    '''token_type_ids''': multiple_choice_token_type_ids,
							}
							UpperCamelCase_:							Optional[int]	=						model(UpperCamelCase__      )
							self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_choices)      )




			def       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							Optional[Any]	=						self.prepare_config_and_inputs()
							(
							    UpperCamelCase_						
							):							Optional[int]	=						config_and_inputs
							UpperCamelCase_:							Optional[Any]	=						{
							    '''input_ids''': input_ids,
							    '''token_type_ids''': token_type_ids,
							    '''langs''': token_type_ids,
							    '''lengths''': input_lengths,
							}
							return config, inputs_dict




@require_tf
class        _UpperCamelCase				(					UpperCamelCase_     ,       UpperCamelCase_     ,       unittest.TestCase  ):






			'''simple docstring'''

			__UpperCamelCase      :						Tuple					      = (
			    (
			        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      :						Union[str, Any]					      = (
			    {
			        """feature-extraction""": TFFlaubertModel,
			        """fill-mask""": TFFlaubertWithLMHeadModel,
			        """question-answering""": TFFlaubertForQuestionAnsweringSimple,
			        """text-classification""": TFFlaubertForSequenceClassification,
			        """token-classification""": TFFlaubertForTokenClassification,
			        """zero-shot""": TFFlaubertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			__UpperCamelCase      :						Any					      = False
			__UpperCamelCase      :						Dict					      = False

			def       lowerCAmelCase__	(       self :       int						,	snake_case_ :       Optional[Any]						,	snake_case_ :       str						,	snake_case_ :       str						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       str      ):





							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[Any]      ):
							UpperCamelCase_:							Optional[Any]	=						TFFlaubertModelTester(self      )
							UpperCamelCase_:							Union[str, Any]	=						ConfigTester(self						,	config_class=UpperCamelCase__						,	emb_dim=37      )

			def       lowerCAmelCase__	(       self :       int      ):
							self.config_tester.run_common_tests()

			def       lowerCAmelCase__	(       self :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__      )

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							UpperCamelCase_:							Tuple	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__      )

			def       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							Optional[int]	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__      )

			def       lowerCAmelCase__	(       self :       Union[str, Any]      ):
							UpperCamelCase_:							List[str]	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__      )

			def       lowerCAmelCase__	(       self :       str      ):
							UpperCamelCase_:							Tuple	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCamelCase__      )

			def       lowerCAmelCase__	(       self :       str      ):
							UpperCamelCase_:							str	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCamelCase__      )




			@slow
			def       lowerCAmelCase__	(       self :       Optional[int]      ):





							for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCamelCase_:							Union[str, Any]	=						TFFlaubertModel.from_pretrained(UpperCamelCase__      )
											self.assertIsNotNone(UpperCamelCase__      )




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






			'''simple docstring'''

			@slow
			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							Optional[Any]	=						TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""      )

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

							UpperCamelCase_:							Tuple	=						model(UpperCamelCase__      )[0]
							UpperCamelCase_:							str	=						tf.TensorShape((1, 8, 512)      )
							self.assertEqual(output.shape						,	UpperCamelCase__      )
							# compare the actual values for a slice.
							UpperCamelCase_:							Optional[Any]	=						tf.convert_to_tensor(
							    [
							        [
							            [-1.876_8773, -1.56_6555, 0.2707_2418],
							            [-1.692_0038, -0.587_3505, 1.932_9599],
							            [-2.956_3985, -1.699_3835, 1.797_2052],
							        ]
							    ]						,	dtype=tf.floataa						,	)

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








class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       List[str]						,	snake_case_ :       int						,	snake_case_ :       Optional[Any]=None						,	snake_case_ :       List[str]=None      ):
							UpperCamelCase_:							List[Any]	=						data
							UpperCamelCase_:							List[Any]	=						previous
							UpperCamelCase_:							Tuple	=						next_node

			def __str__(       self :       Dict      ):
							return f'''{self.data}'''

			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.data

			def       lowerCAmelCase__	(       self :       Any      ):
							return self.next




			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.previous




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       Optional[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						head

			def __iter__(       self :       Union[str, Any]      ):
							return self




			def       lowerCAmelCase__	(       self :       Union[str, Any]      ):
							if not self.current:
											raise StopIteration
							else:
											UpperCamelCase_:							Dict	=						self.current.get_data()
											UpperCamelCase_:							Tuple	=						self.current.get_next()
											return value




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       int      ):
							UpperCamelCase_:							Optional[int]	=						None  # First node in list
							UpperCamelCase_:							Dict	=						None  # Last node in list

			def __str__(       self :       Tuple      ):
							UpperCamelCase_:							int	=						self.head
							UpperCamelCase_:							Tuple	=						[]
							while current is not None:
											nodes.append(current.get_data()      )
											UpperCamelCase_:							List[str]	=						current.get_next()
							return " ".join(str(snake_case_      ) for node in nodes      )

			def __contains__(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Optional[Any]	=						self.head
							while current:
											if current.get_data() == value:
															return True
											UpperCamelCase_:							Any	=						current.get_next()
							return False

			def __iter__(       self :       Any      ):
							return LinkedListIterator(self.head      )

			def       lowerCAmelCase__	(       self :       Tuple      ):
							if self.head:
											return self.head.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							if self.tail:
											return self.tail.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       Node      ):
							if self.head is None:
											UpperCamelCase_:							Tuple	=						node
											UpperCamelCase_:							Optional[int]	=						node
							else:
											self.insert_before_node(self.head						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node      ):
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Any	=						Node(snake_case_      )
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.set_tail(snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							str	=						node
							UpperCamelCase_:							int	=						node.previous

							if node.get_previous() is None:
											UpperCamelCase_:							int	=						node_to_insert
							else:
											UpperCamelCase_:							Dict	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							Tuple	=						node
							UpperCamelCase_:							Dict	=						node.next

							if node.get_next() is None:
											UpperCamelCase_:							Union[str, Any]	=						node_to_insert
							else:
											UpperCamelCase_:							str	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Tuple						,	snake_case_ :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						1
							UpperCamelCase_:							List[str]	=						Node(snake_case_      )
							UpperCamelCase_:							Optional[Any]	=						self.head
							while node:
											if current_position == position:
															self.insert_before_node(snake_case_						,	snake_case_      )
															return
											current_position += 1
											UpperCamelCase_:							Dict	=						node.next
							self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						self.head
							while node:
											if node.get_data() == item:
															return node
											UpperCamelCase_:							List[Any]	=						node.get_next()
							raise Exception("""Node not found"""      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       List[str]      ):
							if (node := self.get_node(snake_case_      )) is not None:
											if node == self.head:
															UpperCamelCase_:							Optional[int]	=						self.head.get_next()

											if node == self.tail:
															UpperCamelCase_:							Union[str, Any]	=						self.tail.get_previous()

											self.remove_node_pointers(snake_case_      )

			@staticmethod
			def       lowerCAmelCase__	(       snake_case_ :       Node      ):
							if node.get_next():
											UpperCamelCase_:							str	=						node.previous

							if node.get_previous():
											UpperCamelCase_:							int	=						node.next

							UpperCamelCase_:							List[str]	=						None
							UpperCamelCase_:							int	=						None




			def       lowerCAmelCase__	(       self :       str      ):
							return self.head is None







def        A__					(						)  ->	None:
				pass


if __name__ == "__main__":
							import doctest

							doctest.testmod()






