
'''simple docstring'''




import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
      from transformers import SpeechaTextFeatureExtractor

lowercase     :							Union[str, Any]       						=     random.Random()







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__=1.0						,							snake_case__=None						,							snake_case__=None		):




    '''simple docstring'''



    if rng is None:
        A			:  Any   =		global_rng

    A			:  Tuple   =		[]
    for batch_idx in range(shape[0]		):
        values.append([]		)
        for _ in range(shape[1]		):
            values[-1].append(rng.random() * scale		)

    return values





@require_torch
@require_torchaudio
class 					A							(				unittest.TestCase		):





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=7		,     SCREAMING_SNAKE_CASE=400		,     SCREAMING_SNAKE_CASE=2000		,     SCREAMING_SNAKE_CASE=24		,     SCREAMING_SNAKE_CASE=24		,     SCREAMING_SNAKE_CASE=0.0		,     SCREAMING_SNAKE_CASE=16000		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=True		,     )					->							Optional[int]:






         """simple docstring"""






         A			:  List[Any]   =		parent
         A			:  int   =		batch_size
         A			:  List[str]   =		min_seq_length
         A			:  Optional[Any]   =		max_seq_length
         A			:  Tuple   =		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
         A			:  Any   =		feature_size
         A			:  Optional[int]   =		num_mel_bins
         A			:  Tuple   =		padding_value
         A			:  Optional[int]   =		sampling_rate
         A			:  List[str]   =		return_attention_mask
         A			:  Dict   =		do_normalize





     def 							__lowerCAmelCase				(				self      )					->							Tuple:






         """simple docstring"""






         return {
             "feature_size": self.feature_size,
             "num_mel_bins": self.num_mel_bins,
             "padding_value": self.padding_value,
             "sampling_rate": self.sampling_rate,
             "return_attention_mask": self.return_attention_mask,
             "do_normalize": self.do_normalize,
         }





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE=False		,     SCREAMING_SNAKE_CASE=False      )					->							Tuple:






         """simple docstring"""






         def _flatten(SCREAMING_SNAKE_CASE      ):
             return list(itertools.chain(*SCREAMING_SNAKE_CASE      )      )

         if equal_length:
             A			:  List[str]   =		[floats_list((self.max_seq_length, self.feature_size)      ) for _ in range(self.batch_size      )]
         else:
             # make sure that inputs increase in size
             A			:  Optional[int]   =		[
                 floats_list((x, self.feature_size)      )
                 for x in range(self.min_seq_length		,     self.max_seq_length		,     self.seq_length_diff      )
             ]
         if numpify:
             A			:  int   =		[np.asarray(SCREAMING_SNAKE_CASE      ) for x in speech_inputs]
         return speech_inputs





@require_torch
@require_torchaudio
class 					A							(				__snake_case       ,     unittest.TestCase		):
     __magic_name__          =						SpeechaTextFeatureExtractor if is_speech_available() else None





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Union[str, Any]   =		SpeechaTextFeatureExtractionTester(self      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							Tuple:






         """simple docstring"""






         self.assertTrue(np.all(np.mean(SCREAMING_SNAKE_CASE		,     axis=0      ) < 1e-3      )      )
         self.assertTrue(np.all(np.abs(np.var(SCREAMING_SNAKE_CASE		,     axis=0      ) - 1      ) < 1e-3      )      )





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






         """simple docstring"""






         A			:  List[str]   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         # create three inputs of length 800, 1000, and 1200
         A			:  Union[str, Any]   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]
         A			:  Optional[int]   =		[np.asarray(SCREAMING_SNAKE_CASE      ) for speech_input in speech_inputs]

         # Test feature size
         A			:  int   =		feature_extractor(SCREAMING_SNAKE_CASE		,     padding=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      ).input_features
         self.assertTrue(input_features.ndim == 3      )
         self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size      )

         # Test not batched input
         A			:  Union[str, Any]   =		feature_extractor(speech_inputs[0]		,     return_tensors='''np'''      ).input_features
         A			:  Dict   =		feature_extractor(np_speech_inputs[0]		,     return_tensors='''np'''      ).input_features
         self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     atol=1e-3      )      )

         # Test batched
         A			:  Dict   =		feature_extractor(SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      ).input_features
         A			:  Union[str, Any]   =		feature_extractor(SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      ).input_features
         for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     atol=1e-3      )      )

         # Test 2-D numpy arrays are batched.
         A			:  int   =		[floats_list((1, x)      )[0] for x in (800, 800, 800)]
         A			:  List[Any]   =		np.asarray(SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		feature_extractor(SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      ).input_features
         A			:  Optional[Any]   =		feature_extractor(SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      ).input_features
         for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     atol=1e-3      )      )





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






         """simple docstring"""






         A			:  List[Any]   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Dict   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]

         A			:  int   =		['''longest''', '''max_length''', '''do_not_pad''']
         A			:  Optional[Any]   =		[None, 16, None]
         for max_length, padding in zip(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             A			:  Dict   =		feature_extractor(
                 SCREAMING_SNAKE_CASE		,     padding=SCREAMING_SNAKE_CASE		,     max_length=SCREAMING_SNAKE_CASE		,     return_attention_mask=SCREAMING_SNAKE_CASE      )
             A			:  List[Any]   =		inputs.input_features
             A			:  Optional[int]   =		inputs.attention_mask
             A			:  Dict   =		[np.sum(SCREAMING_SNAKE_CASE      ) for x in attention_mask]

             self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]      )
             self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]      )
             self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]      )





     def 							__lowerCAmelCase				(				self      )					->							List[Any]:






         """simple docstring"""






         A			:  List[str]   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Tuple   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]

         A			:  List[Any]   =		['''longest''', '''max_length''', '''do_not_pad''']
         A			:  Optional[int]   =		[None, 16, None]
         for max_length, padding in zip(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             A			:  Union[str, Any]   =		feature_extractor(
                 SCREAMING_SNAKE_CASE		,     max_length=SCREAMING_SNAKE_CASE		,     padding=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''		,     return_attention_mask=SCREAMING_SNAKE_CASE      )
             A			:  Tuple   =		inputs.input_features
             A			:  List[str]   =		inputs.attention_mask
             A			:  Any   =		[np.sum(SCREAMING_SNAKE_CASE      ) for x in attention_mask]

             self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]      )
             self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6      )
             self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]      )
             self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6      )
             self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]      )





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






         """simple docstring"""






         A			:  Dict   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Optional[Any]   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]
         A			:  Optional[int]   =		feature_extractor(
             SCREAMING_SNAKE_CASE		,     padding='''max_length'''		,     max_length=4		,     truncation=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''		,     return_attention_mask=SCREAMING_SNAKE_CASE		,     )
         A			:  List[str]   =		inputs.input_features
         A			:  int   =		inputs.attention_mask
         A			:  List[Any]   =		np.sum(attention_mask == 1		,     axis=1      )

         self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
         self._check_zero_mean_unit_variance(input_features[1]      )
         self._check_zero_mean_unit_variance(input_features[2]      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Optional[int]   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]
         A			:  List[str]   =		feature_extractor(
             SCREAMING_SNAKE_CASE		,     padding='''longest'''		,     max_length=4		,     truncation=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''		,     return_attention_mask=SCREAMING_SNAKE_CASE		,     )
         A			:  List[str]   =		inputs.input_features
         A			:  List[str]   =		inputs.attention_mask
         A			:  Dict   =		np.sum(attention_mask == 1		,     axis=1      )

         self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
         self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]      )
         self._check_zero_mean_unit_variance(input_features[2]      )

         # make sure that if max_length < longest -> then pad to max_length
         self.assertEqual(input_features.shape		,     (3, 4, 24)      )

         A			:  Any   =		[floats_list((1, x)      )[0] for x in range(800		,     1400		,     200      )]
         A			:  Optional[Any]   =		feature_extractor(
             SCREAMING_SNAKE_CASE		,     padding='''longest'''		,     max_length=16		,     truncation=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''		,     return_attention_mask=SCREAMING_SNAKE_CASE		,     )
         A			:  Optional[int]   =		inputs.input_features
         A			:  List[Any]   =		inputs.attention_mask
         A			:  List[str]   =		np.sum(attention_mask == 1		,     axis=1      )

         self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
         self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]      )
         self._check_zero_mean_unit_variance(input_features[2]      )

         # make sure that if max_length < longest -> then pad to max_length
         self.assertEqual(input_features.shape		,     (3, 6, 24)      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         import torch

         A			:  Any   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Dict   =		np.random.rand(100		,     32      ).astype(np.floataa      )
         A			:  List[str]   =		np_speech_inputs.tolist()

         for inputs in [py_speech_inputs, np_speech_inputs]:
             A			:  Optional[int]   =		feature_extractor.pad([{'''input_features''': inputs}]		,     return_tensors='''np'''      )
             self.assertTrue(np_processed.input_features.dtype == np.floataa      )
             A			:  Dict   =		feature_extractor.pad([{'''input_features''': inputs}]		,     return_tensors='''pt'''      )
             self.assertTrue(pt_processed.input_features.dtype == torch.floataa      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							List[str]:






         """simple docstring"""






         from datasets import load_dataset

         A			:  Union[str, Any]   =		load_dataset('''hf-internal-testing/librispeech_asr_dummy'''		,     '''clean'''		,     split='''validation'''      )
         # automatic decoding with librispeech
         A			:  Dict   =		ds.sort('''id'''      ).select(range(SCREAMING_SNAKE_CASE      )      )[:num_samples]['''audio''']

         return [x["array"] for x in speech_samples]





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






         """simple docstring"""






         A			:  Tuple   =		np.array([
             -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
             -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
             -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
         ]      )
         # fmt: on

         A			:  List[Any]   =		self._load_datasamples(1      )
         A			:  List[str]   =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         A			:  Optional[Any]   =		feature_extractor(SCREAMING_SNAKE_CASE		,     return_tensors='''pt'''      ).input_features
         self.assertEquals(input_features.shape		,     (1, 584, 24)      )
         self.assertTrue(np.allclose(input_features[0, 0, :30]		,     SCREAMING_SNAKE_CASE		,     atol=1e-4      )      )






'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import (
          TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          FlaubertConfig,
          TFFlaubertForMultipleChoice,
          TFFlaubertForQuestionAnsweringSimple,
          TFFlaubertForSequenceClassification,
          TFFlaubertForTokenClassification,
          TFFlaubertModel,
          TFFlaubertWithLMHeadModel,
      )





class 					A							:





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     )					->							Optional[Any]:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self      )					->							List[Any]:






         """simple docstring"""






         A			:  int   =		ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )
         A			:  Any   =		random_attention_mask([self.batch_size, self.seq_length]		,     dtype=tf.floataa      )

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

         A			:  Tuple   =		None
         if self.use_token_type_ids:
             A			:  Union[str, Any]   =		ids_tensor([self.batch_size, self.seq_length]		,     self.n_langs      )

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

         A			:  Dict   =		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		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							List[str]:






         """simple docstring"""






         A			:  int   =		TFFlaubertModel(config=SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		[input_ids, input_mask]
         A			:  str   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							List[str]:






         """simple docstring"""






         A			:  List[str]   =		TFFlaubertWithLMHeadModel(SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE      )

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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Optional[Any]:






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForQuestionAnsweringSimple(SCREAMING_SNAKE_CASE      )

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

         A			:  Tuple   =		model(SCREAMING_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 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForSequenceClassification(SCREAMING_SNAKE_CASE      )

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

         A			:  Optional[Any]   =		model(SCREAMING_SNAKE_CASE      )

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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							str:






         """simple docstring"""






         A			:  Optional[Any]   =		self.num_labels
         A			:  str   =		TFFlaubertForTokenClassification(config=SCREAMING_SNAKE_CASE      )
         A			:  List[str]   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  str   =		self.num_choices
         A			:  int   =		TFFlaubertForMultipleChoice(config=SCREAMING_SNAKE_CASE      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  Optional[Any]   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  List[str]   =		{
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
             '''token_type_ids''': multiple_choice_token_type_ids,
         }
         A			:  List[Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.prepare_config_and_inputs()
         (
             (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    
         )			:  Any   =		config_and_inputs
         A			:  List[str]   =		{
             '''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		):
     __magic_name__          =						(
         (
             TFFlaubertModel,
             TFFlaubertWithLMHeadModel,
             TFFlaubertForSequenceClassification,
             TFFlaubertForQuestionAnsweringSimple,
             TFFlaubertForTokenClassification,
             TFFlaubertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     __magic_name__          =						(
         (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     __magic_name__          =						(
         {
             '''feature-extraction''': TFFlaubertModel,
             '''fill-mask''': TFFlaubertWithLMHeadModel,
             '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
             '''text-classification''': TFFlaubertForSequenceClassification,
             '''token-classification''': TFFlaubertForTokenClassification,
             '''zero-shot''': TFFlaubertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __magic_name__          =						False
     __magic_name__          =						False





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






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






         """simple docstring"""






         A			:  int   =		TFFlaubertModelTester(self      )
         A			:  Dict   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     emb_dim=37      )





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






         """simple docstring"""






         self.config_tester.run_common_tests()





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  List[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  int   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_lm_head(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							List[Any]:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_qa(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         A			:  Optional[int]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_sequence_classif(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_token_classification(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_multiple_choice(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






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





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





     @slow
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Tuple   =		TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased'''      )

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

         A			:  int   =		model(SCREAMING_SNAKE_CASE      )[0]
         A			:  str   =		tf.TensorShape((1, 8, 512)      )
         self.assertEqual(output.shape		,     SCREAMING_SNAKE_CASE      )
         # compare the actual values for a slice.
         A			:  Union[str, Any]   =		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      )      )




