


"""simple docstring"""




from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    AutoTokenizer,
				    TFAutoModelForCausalLM,
				    TFAutoModelForSeqaSeqLM,
				    TFAutoModelForSpeechSeqaSeq,
				    TFAutoModelForVisionaSeq,
				    TFBartForConditionalGeneration,
				    TFLogitsProcessorList,
				    TFMinLengthLogitsProcessor,
				    tf_top_k_top_p_filtering,
				)

if is_tensorflow_text_available():
				import tensorflow_text as text
@require_tf
class    __lowerCAmelCase   (  unittest.TestCase			):


		'''simple docstring'''



		def      __UpperCAmelCase			(    self ):
				__a	 =     tf.convert_to_tensor(
				    [
				        [
				            8.222_0991,  # 3rd highest value; idx. 0
				            -0.562_0044,
				            5.2322_9752,
				            4.038_6393,
				            -6.879_8378,
				            -0.5478_5802,
				            -3.201_2153,
				            2.9277_7176,
				            1.8817_1953,
				            7.3534_1276,  # 5th highest value; idx. 9
				            8.4320_7833,  # 2nd highest value; idx. 10
				            -9.8571_1836,
				            -5.9620_9236,
				            -1.1303_9161,
				            -7.111_5294,
				            -0.836_9633,
				            -5.318_6408,
				            7.0642_7407,
				            0.8136_9344,
				            -0.8202_3817,
				            -5.917_9796,
				            0.5881_3443,
				            -6.9977_8438,
				            4.7155_1189,
				            -0.1877_1637,
				            7.4402_0759,  # 4th highest value; idx. 25
				            9.3845_0987,  # 1st highest value; idx. 26
				            2.1266_2941,
				            -9.3256_2038,
				            2.3565_2522,
				        ],  # cummulative prob of 5 highest values <= 0.6
				        [
				            0.5842_5518,
				            4.5313_9238,
				            -5.5751_0464,
				            -6.2803_0699,
				            -7.1952_9503,
				            -4.0212_2551,
				            1.3933_7037,
				            -6.0670_7057,
				            1.5948_0517,
				            -9.64_3119,
				            0.0390_7799,
				            0.6723_1762,
				            -8.8820_6726,
				            6.2711_5922,  # 4th highest value; idx. 13
				            2.2852_0723,
				            4.8276_7506,
				            4.3042_1368,
				            8.827_5313,  # 2nd highest value; idx. 17
				            5.4402_9958,  # 5th highest value; idx. 18
				            -4.473_5794,
				            7.3857_9536,  # 3rd highest value; idx. 20
				            -2.9105_1663,
				            2.6194_6077,
				            -2.567_4762,
				            -9.4895_9302,
				            -4.0292_2645,
				            -1.3541_6918,
				            9.6770_2323,  # 1st highest value; idx. 27
				            -5.8947_8553,
				            1.8537_0467,
				        ],  # cummulative prob of 5 highest values <= 0.6
				    ]				,					dtype=tf.floataa				,					)

				__a	 =     tf.convert_to_tensor(
				    [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]]				,					dtype=tf.intaa				,					)  # expected non filtered idx as noted above

				__a	 =     tf.convert_to_tensor(
				    [8.22_2099, 7.353_4126, 8.43_2078, 7.440_2075, 9.3_8451, 6.27_1159, 8.82_7531, 5.440_2995, 7.385_7956, 9.67_7023]				,					dtype=tf.floataa				,					)  # expected non filtered values as noted above

				__a	 =     tf_top_k_top_p_filtering(snake_case_				,					top_k=10				,					top_p=0.6				,					min_tokens_to_keep=4 )

				__a	 =     output[output != -float('''inf''' )]
				__a	 =     tf.cast(
				    tf.where(tf.not_equal(snake_case_				,					tf.constant(-float('''inf''' )				,					dtype=tf.floataa ) ) )				,					dtype=tf.intaa				,					)

				tf.debugging.assert_near(snake_case_				,					snake_case_				,					rtol=1E-12 )
				tf.debugging.assert_equal(snake_case_				,					snake_case_ )







@require_tf
class    __lowerCAmelCase   (  unittest.TestCase	,     __SCREAMING_SNAKE_CASE			):


		'''simple docstring'''



		if is_tf_available():
				__UpperCAmelCase						:     Optional[int]									=							{
				    'AutoModelForCausalLM': TFAutoModelForCausalLM,
				    'AutoModelForSpeechSeq2Seq': TFAutoModelForSpeechSeqaSeq,
				    'AutoModelForSeq2SeqLM': TFAutoModelForSeqaSeqLM,
				    'AutoModelForVision2Seq': TFAutoModelForVisionaSeq,
				    'LogitsProcessorList': TFLogitsProcessorList,
				    'MinLengthLogitsProcessor': TFMinLengthLogitsProcessor,
				    'create_tensor_fn': tf.convert_to_tensor,
				    'floats_tensor': floats_tensor,
				    'return_tensors': 'tf',
				}

		@slow
		def      __UpperCAmelCase			(    self ):
				# TF-only test: tf.saved_model export
				__a	 =     TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' )
				__a	 =     2
				__a	 =     2
				class    __lowerCAmelCase   (  tf.Module			):


						'''simple docstring'''



						def __init__(    self				,					_a ):
								super(snake_case_				,					self ).__init__()
								__a	 =     model





						@tf.function(
						    input_signature=(
						        tf.TensorSpec((None, input_length)				,					tf.intaa				,					name='''input_ids''' ),
						        tf.TensorSpec((None, input_length)				,					tf.intaa				,					name='''attention_mask''' ),
						    )				,					jit_compile=snake_case_				,					)
						def      __UpperCAmelCase			(    self				,					_a				,					_a ):
								__a	 =     self.model.generate(
								    input_ids=snake_case_				,					attention_mask=snake_case_				,					max_new_tokens=snake_case_				,					return_dict_in_generate=snake_case_				,					)
								return {"sequences": outputs["sequences"]}







				__a	 =     [[2, 0], [102, 103]]
				__a	 =     [[1, 0], [1, 1]]
				__a	 =     DummyModel(model=snake_case_ )
				with tempfile.TemporaryDirectory() as tmp_dir:
						tf.saved_model.save(snake_case_				,					snake_case_				,					signatures={'''serving_default''': dummy_model.serving} )
						__a	 =     tf.saved_model.load(snake_case_ ).signatures['''serving_default''']
						for batch_size in range(1				,					len(snake_case_ ) + 1 ):
								__a	 =     {
								    '''input_ids''': tf.constant(dummy_input_ids[:batch_size] ),
								    '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size] ),
								}
								__a	 =     serving_func(**snake_case_ )['''sequences''']
								__a	 =     test_model.generate(**snake_case_				,					max_new_tokens=snake_case_ )
								tf.debugging.assert_equal(snake_case_				,					snake_case_ )

		@slow
		def      __UpperCAmelCase			(    self ):
				# TF-only test: tf.saved_model export
				__a	 =     TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' )
				__a	 =     1
				__a	 =     2
				class    __lowerCAmelCase   (  tf.Module			):


						'''simple docstring'''



						def __init__(    self				,					_a ):
								super(snake_case_				,					self ).__init__()
								__a	 =     model





						@tf.function(
						    input_signature=(
						        tf.TensorSpec((batch_size, None)				,					tf.intaa				,					name='''input_ids''' ),
						        tf.TensorSpec((batch_size, None)				,					tf.intaa				,					name='''attention_mask''' ),
						    )				,					jit_compile=snake_case_				,					)
						def      __UpperCAmelCase			(    self				,					_a				,					_a ):
								__a	 =     self.model.generate(
								    input_ids=snake_case_				,					attention_mask=snake_case_				,					max_new_tokens=snake_case_				,					return_dict_in_generate=snake_case_				,					)
								return {"sequences": outputs["sequences"]}







				__a	 =     [[2], [102, 103]]
				__a	 =     [[1], [1, 1]]
				__a	 =     DummyModel(model=snake_case_ )
				with tempfile.TemporaryDirectory() as tmp_dir:
						tf.saved_model.save(snake_case_				,					snake_case_				,					signatures={'''serving_default''': dummy_model.serving} )
						__a	 =     tf.saved_model.load(snake_case_ ).signatures['''serving_default''']
						for input_row in range(len(snake_case_ ) ):
								__a	 =     {
								    '''input_ids''': tf.constant([dummy_input_ids[input_row]] ),
								    '''attention_mask''': tf.constant([dummy_attention_masks[input_row]] ),
								}
								__a	 =     serving_func(**snake_case_ )['''sequences''']
								__a	 =     test_model.generate(**snake_case_				,					max_new_tokens=snake_case_ )
								tf.debugging.assert_equal(snake_case_				,					snake_case_ )

		@slow
		@require_tensorflow_text
		def      __UpperCAmelCase			(    self ):
				# TF-only test: tf.saved_model export
				with tempfile.TemporaryDirectory() as tmp_dir:
						# file needed to load the TF tokenizer
						hf_hub_download(repo_id='''google/flan-t5-small'''				,					filename='''spiece.model'''				,					local_dir=snake_case_ )
						class    __lowerCAmelCase   (  tf.keras.layers.Layer			):


								'''simple docstring'''



								def __init__(    self ):
										super().__init__()
										__a	 =     text.SentencepieceTokenizer(
										    model=tf.io.gfile.GFile(os.path.join(snake_case_				,					'''spiece.model''' )				,					'''rb''' ).read() )
										__a	 =     TFAutoModelForSeqaSeqLM.from_pretrained('''hf-internal-testing/tiny-random-t5''' )





								def      __UpperCAmelCase			(    self				,					_a				,					*_a				,					**_a ):
										__a	 =     self.tokenizer.tokenize(snake_case_ )
										__a ,	__a	 =     text.pad_model_inputs(
										    snake_case_				,					max_seq_length=64				,					pad_value=self.model.config.pad_token_id )
										__a	 =     self.model.generate(input_ids=snake_case_				,					attention_mask=snake_case_ )
										return self.tokenizer.detokenize(snake_case_ )







						__a	 =     CompleteSentenceTransformer()
						__a	 =     tf.keras.layers.Input(shape=(1,)				,					dtype=tf.string				,					name='''inputs''' )
						__a	 =     complete_model(snake_case_ )
						__a	 =     tf.keras.Model(snake_case_				,					snake_case_ )
						keras_model.save(snake_case_ )

		def      __UpperCAmelCase			(    self ):
				# Has PT equivalent: this test relies on random sampling
				__a	 =     {
				    '''do_sample''': True,
				    '''num_beams''': 1,
				    '''top_p''': 0.7,
				    '''top_k''': 10,
				    '''temperature''': 0.7,
				}
				__a	 =     14

				__a	 =     AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' )
				__a	 =     '''Hello, my dog is cute and'''
				__a	 =     tokenizer(snake_case_				,					return_tensors='''tf''' )
				__a	 =     TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' )

				__a	 =     638
				# forces the generation to happen on CPU, to avoid GPU-related quirks
				with tf.device(''':/CPU:0''' ):
						tf.random.set_seed(0 )
						__a	 =     model.generate(**snake_case_				,					eos_token_id=snake_case_				,					**snake_case_ )
				self.assertTrue(expectation == len(generated_tokens[0] ) )

				__a	 =     [638, 198]
				with tf.device(''':/CPU:0''' ):
						tf.random.set_seed(0 )
						__a	 =     model.generate(**snake_case_				,					eos_token_id=snake_case_				,					**snake_case_ )
				self.assertTrue(expectation == len(generated_tokens[0] ) )





		def      __UpperCAmelCase			(    self ):
				# Has PT equivalent: ample use of framework-specific code
				__a	 =     AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bart''' )
				__a	 =     '''Hugging Face is a technology company based in New York and Paris.'''
				__a	 =     bart_tokenizer(snake_case_				,					return_tensors='''tf''' ).input_ids
				__a	 =     TFBartForConditionalGeneration.from_pretrained('''hf-internal-testing/tiny-random-bart''' )
				__a	 =     bart_model.generate(snake_case_ ).numpy()
				class    __lowerCAmelCase   (  __SCREAMING_SNAKE_CASE			):


						'''simple docstring'''



						def      __UpperCAmelCase			(    self				,					_a				,					_a=None				,					**_a ):
								return super().call(snake_case_				,					**snake_case_ )







				__a	 =     FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart''' )
				__a	 =     bart_model.generate(snake_case_				,					foo='''bar''' ).numpy()
				self.assertTrue(np.array_equal(snake_case_				,					snake_case_ ) )
				class    __lowerCAmelCase   (  bart_model.model.encoder.__class__			):


						'''simple docstring'''



						def      __UpperCAmelCase			(    self				,					_a				,					**_a ):
								return super().call(snake_case_				,					**snake_case_ )







				__a	 =     FakeEncoder(bart_model.config				,					bart_model.model.shared )
				__a	 =     fake_encoder

				# Normal generation still works (the output will be different because the encoder weights are different)
				__a	 =     bart_model.generate(snake_case_ ).numpy()
				with self.assertRaises(snake_case_ ):
						# FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
						bart_model.generate(snake_case_				,					foo='''bar''' )











import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    FlaubertForMultipleChoice,
				    FlaubertForQuestionAnswering,
				    FlaubertForQuestionAnsweringSimple,
				    FlaubertForSequenceClassification,
				    FlaubertForTokenClassification,
				    FlaubertModel,
				    FlaubertWithLMHeadModel,
				)
				from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST






class     __a			(  SCREAMING_SNAKE_CASE					):



	def __init__(    self				:     List[Any]					,						snake_case_				:     Tuple					,						snake_case_				:     Dict=13					,						snake_case_				:     List[Any]=7					,						snake_case_				:     Optional[int]=True					,						snake_case_				:     Tuple=True					,						snake_case_				:     Optional[int]=True					,						snake_case_				:     Optional[int]=True					,						snake_case_				:     Any=True					,						snake_case_				:     Any=False					,						snake_case_				:     Union[str, Any]=False					,						snake_case_				:     Dict=False					,						snake_case_				:     str=2					,						snake_case_				:     Optional[int]=99					,						snake_case_				:     Any=0					,						snake_case_				:     Dict=32					,						snake_case_				:     Optional[Any]=5					,						snake_case_				:     Optional[Any]=4					,						snake_case_				:     Tuple=0.1					,						snake_case_				:     Any=0.1					,						snake_case_				:     str=5_12					,						snake_case_				:     List[str]=12					,						snake_case_				:     List[Any]=2					,						snake_case_				:     Optional[int]=0.0_2					,						snake_case_				:     Dict=3					,						snake_case_				:     Dict=4					,						snake_case_				:     Any="last"					,						snake_case_				:     List[Any]=None					,						snake_case_				:     int=None					,						)->				List[Any]:
						__lowerCAmelCase						 =parent
						__lowerCAmelCase						 =batch_size
						__lowerCAmelCase						 =seq_length
						__lowerCAmelCase						 =is_training
						__lowerCAmelCase						 =use_input_lengths
						__lowerCAmelCase						 =use_token_type_ids
						__lowerCAmelCase						 =use_labels
						__lowerCAmelCase						 =gelu_activation
						__lowerCAmelCase						 =sinusoidal_embeddings
						__lowerCAmelCase						 =causal
						__lowerCAmelCase						 =asm
						__lowerCAmelCase						 =n_langs
						__lowerCAmelCase						 =vocab_size
						__lowerCAmelCase						 =n_special
						__lowerCAmelCase						 =hidden_size
						__lowerCAmelCase						 =num_hidden_layers
						__lowerCAmelCase						 =num_attention_heads
						__lowerCAmelCase						 =hidden_dropout_prob
						__lowerCAmelCase						 =attention_probs_dropout_prob
						__lowerCAmelCase						 =max_position_embeddings
						__lowerCAmelCase						 =type_vocab_size
						__lowerCAmelCase						 =type_sequence_label_size
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =num_labels
						__lowerCAmelCase						 =num_choices
						__lowerCAmelCase						 =summary_type
						__lowerCAmelCase						 =use_proj
						__lowerCAmelCase						 =scope



	def 		UpperCamelCase				(    self				:     List[Any])->				str:
						__lowerCAmelCase						 =ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size)
						__lowerCAmelCase						 =random_attention_mask([self.batch_size, self.seq_length])

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

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

						__lowerCAmelCase						 =None
						__lowerCAmelCase						 =None
						__lowerCAmelCase						 =None
						if self.use_labels:
											__lowerCAmelCase						 =ids_tensor([self.batch_size]					,						self.type_sequence_label_size)
											__lowerCAmelCase						 =ids_tensor([self.batch_size, self.seq_length]					,						self.num_labels)
											__lowerCAmelCase						 =ids_tensor([self.batch_size]					,						2).float()
											__lowerCAmelCase						 =ids_tensor([self.batch_size]					,						self.num_choices)

						__lowerCAmelCase						 =self.get_config()

						return (
						    config,
						    input_ids,
						    token_type_ids,
						    input_lengths,
						    sequence_labels,
						    token_labels,
						    is_impossible_labels,
						    choice_labels,
						    input_mask,
						)



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Union[str, Any]:
						return 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					,						)



	def 		UpperCamelCase				(    self				:     Any					,						snake_case_				:     Optional[Any]					,						snake_case_				:     Dict					,						snake_case_				:     Any					,						snake_case_				:     Optional[int]					,						snake_case_				:     Tuple					,						snake_case_				:     Optional[Any]					,						snake_case_				:     Optional[int]					,						snake_case_				:     Optional[int]					,						snake_case_				:     int					,						)->				Tuple:
						__lowerCAmelCase						 =FlaubertModel(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_					,						lengths=snake_case_					,						langs=snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						langs=snake_case_)
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))



	def 		UpperCamelCase				(    self				:     Optional[int]					,						snake_case_				:     Tuple					,						snake_case_				:     Dict					,						snake_case_				:     List[Any]					,						snake_case_				:     int					,						snake_case_				:     List[str]					,						snake_case_				:     Any					,						snake_case_				:     Optional[int]					,						snake_case_				:     List[str]					,						snake_case_				:     str					,						)->				str:
						__lowerCAmelCase						 =FlaubertWithLMHeadModel(snake_case_)
						model.to(snake_case_)
						model.eval()

						__lowerCAmelCase						 =model(snake_case_					,						token_type_ids=snake_case_					,						labels=snake_case_)
						self.parent.assertEqual(result.loss.shape					,						())
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size))



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     Dict					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Optional[int]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     List[str]					,						snake_case_				:     Dict					,						snake_case_				:     Dict					,						snake_case_				:     Any					,						)->				Tuple:
						__lowerCAmelCase						 =FlaubertForQuestionAnsweringSimple(snake_case_)
						model.to(snake_case_)
						model.eval()

						__lowerCAmelCase						 =model(snake_case_)

						__lowerCAmelCase						 =model(snake_case_					,						start_positions=snake_case_					,						end_positions=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 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     str					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     str					,						snake_case_				:     Optional[Any]					,						snake_case_				:     str					,						snake_case_				:     Optional[Any]					,						snake_case_				:     Optional[Any]					,						snake_case_				:     str					,						snake_case_				:     Optional[Any]					,						)->				Optional[int]:
						__lowerCAmelCase						 =FlaubertForQuestionAnswering(snake_case_)
						model.to(snake_case_)
						model.eval()

						__lowerCAmelCase						 =model(snake_case_)

						__lowerCAmelCase						 =model(
						    snake_case_					,						start_positions=snake_case_					,						end_positions=snake_case_					,						cls_index=snake_case_					,						is_impossible=snake_case_					,						p_mask=snake_case_					,						)

						__lowerCAmelCase						 =model(
						    snake_case_					,						start_positions=snake_case_					,						end_positions=snake_case_					,						cls_index=snake_case_					,						is_impossible=snake_case_					,						)

						((__lowerCAmelCase) ,						)						 =result_with_labels.to_tuple()

						__lowerCAmelCase						 =model(snake_case_					,						start_positions=snake_case_					,						end_positions=snake_case_)

						((__lowerCAmelCase) ,						)						 =result_with_labels.to_tuple()

						self.parent.assertEqual(result_with_labels.loss.shape					,						())
						self.parent.assertEqual(result.start_top_log_probs.shape					,						(self.batch_size, model.config.start_n_top))
						self.parent.assertEqual(result.start_top_index.shape					,						(self.batch_size, model.config.start_n_top))
						self.parent.assertEqual(
						    result.end_top_log_probs.shape					,						(self.batch_size, model.config.start_n_top * model.config.end_n_top))
						self.parent.assertEqual(
						    result.end_top_index.shape					,						(self.batch_size, model.config.start_n_top * model.config.end_n_top))
						self.parent.assertEqual(result.cls_logits.shape					,						(self.batch_size,))



	def 		UpperCamelCase				(    self				:     List[Any]					,						snake_case_				:     Any					,						snake_case_				:     int					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Dict					,						snake_case_				:     int					,						snake_case_				:     Tuple					,						snake_case_				:     int					,						snake_case_				:     str					,						snake_case_				:     Dict					,						)->				Tuple:
						__lowerCAmelCase						 =FlaubertForSequenceClassification(snake_case_)
						model.to(snake_case_)
						model.eval()

						__lowerCAmelCase						 =model(snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						labels=snake_case_)

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



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Dict					,						snake_case_				:     List[str]					,						snake_case_				:     str					,						snake_case_				:     List[Any]					,						snake_case_				:     Dict					,						snake_case_				:     List[str]					,						snake_case_				:     Any					,						snake_case_				:     Tuple					,						)->				Any:
						__lowerCAmelCase						 =self.num_labels
						__lowerCAmelCase						 =FlaubertForTokenClassification(snake_case_)
						model.to(snake_case_)
						model.eval()

						__lowerCAmelCase						 =model(snake_case_					,						attention_mask=snake_case_					,						labels=snake_case_)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels))



	def 		UpperCamelCase				(    self				:     Union[str, Any]					,						snake_case_				:     str					,						snake_case_				:     int					,						snake_case_				:     List[Any]					,						snake_case_				:     int					,						snake_case_				:     List[Any]					,						snake_case_				:     List[str]					,						snake_case_				:     str					,						snake_case_				:     Optional[int]					,						snake_case_				:     Union[str, Any]					,						)->				Optional[int]:
						__lowerCAmelCase						 =self.num_choices
						__lowerCAmelCase						 =FlaubertForMultipleChoice(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =input_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =token_type_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =input_mask.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =model(
						    snake_case_					,						attention_mask=snake_case_					,						token_type_ids=snake_case_					,						labels=snake_case_					,						)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices))



	def 		UpperCamelCase				(    self				:     int)->				Dict:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						(
						    (
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						
						)						 =config_and_inputs
						__lowerCAmelCase						 ={
						    """input_ids""": input_ids,
						    """token_type_ids""": token_type_ids,
						    """lengths""": input_lengths,
						    """attention_mask""": input_mask,
						}
						return config, inputs_dict






@require_torch
class     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE  	=							(
	    {
	        "feature-extraction": FlaubertModel,
	        "fill-mask": FlaubertWithLMHeadModel,
	        "question-answering": FlaubertForQuestionAnsweringSimple,
	        "text-classification": FlaubertForSequenceClassification,
	        "token-classification": FlaubertForTokenClassification,
	        "zero-shot": FlaubertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)



	def 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     Any					,						snake_case_				:     Dict					,						snake_case_				:     Any					,						snake_case_				:     Dict					,						snake_case_				:     Dict)->				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 		UpperCamelCase				(    self				:     int					,						snake_case_				:     List[str]					,						snake_case_				:     Any					,						snake_case_				:     int=False)->				Optional[Any]:
						__lowerCAmelCase						 =super()._prepare_for_class(snake_case_					,						snake_case_					,						return_labels=snake_case_)

						if return_labels:
											if model_class.__name__ == "FlaubertForQuestionAnswering":
																__lowerCAmelCase						 =torch.zeros(
																    self.model_tester.batch_size					,						dtype=torch.long					,						device=snake_case_)
																__lowerCAmelCase						 =torch.zeros(
																    self.model_tester.batch_size					,						dtype=torch.long					,						device=snake_case_)

						return inputs_dict



	def 		UpperCamelCase				(    self				:     Dict)->				Optional[Any]:
						__lowerCAmelCase						 =FlaubertModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						emb_dim=37)



	def 		UpperCamelCase				(    self				:     int)->				Dict:
						self.config_tester.run_common_tests()



	def 		UpperCamelCase				(    self				:     Any)->				Tuple:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				List[str]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_lm_head(*snake_case_)



	def 		UpperCamelCase				(    self				:     Any)->				Any:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_simple_qa(*snake_case_)



	def 		UpperCamelCase				(    self				:     Optional[int])->				List[str]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_qa(*snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any])->				Optional[Any]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case_)



	def 		UpperCamelCase				(    self				:     Tuple)->				Union[str, Any]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_token_classif(*snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any])->				Any:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case_)



	@slow
	def 		UpperCamelCase				(    self				:     List[str])->				Tuple:
						for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =FlaubertModel.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)



	@slow
	@require_torch_gpu
	def 		UpperCamelCase				(    self				:     Dict)->				str:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()
						for model_class in self.all_model_classes:
											# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
											if model_class == FlaubertForMultipleChoice:
																return

											__lowerCAmelCase						 =True
											__lowerCAmelCase						 =model_class(config=snake_case_)

											__lowerCAmelCase						 =self._prepare_for_class(snake_case_					,						snake_case_)
											__lowerCAmelCase						 =torch.jit.trace(
											    snake_case_					,						(inputs_dict["""input_ids"""].to("""cpu"""), inputs_dict["""attention_mask"""].to("""cpu""")))

											with tempfile.TemporaryDirectory() as tmp:
																torch.jit.save(snake_case_					,						os.path.join(snake_case_					,						"""traced_model.pt"""))
																__lowerCAmelCase						 =torch.jit.load(os.path.join(snake_case_					,						"""traced_model.pt""")					,						map_location=snake_case_)
																loaded(inputs_dict["""input_ids"""].to(snake_case_)					,						inputs_dict["""attention_mask"""].to(snake_case_))






@require_torch
class     __a			(  unittest.TestCase					):



	@slow
	def 		UpperCamelCase				(    self				:     Tuple)->				str:
						__lowerCAmelCase						 =FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased""")
						__lowerCAmelCase						 =torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
						with torch.no_grad():
											__lowerCAmelCase						 =model(snake_case_)[0]
						__lowerCAmelCase						 =torch.Size((1, 11, 7_68))
						self.assertEqual(output.shape					,						snake_case_)
						__lowerCAmelCase						 =torch.tensor(
						    [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]])

						self.assertTrue(torch.allclose(output[:, :3, :3]					,						snake_case_					,						atol=1e-4))




