






'''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 							__SCREAMING_SNAKE_CASE       :




						def __init__(  self ,   lowerCamelCase ,   )							->List[Any]:



									'''simple docstring'''
									__a				     =    parent
									__a				     =    13
									__a				     =    7
									__a				     =    True
									__a				     =    True
									__a				     =    True
									__a				     =    True
									__a				     =    True
									__a				     =    False
									__a				     =    False
									__a				     =    False
									__a				     =    2
									__a				     =    99
									__a				     =    0
									__a				     =    32
									__a				     =    2
									__a				     =    4
									__a				     =    0.1
									__a				     =    0.1
									__a				     =    512
									__a				     =    16
									__a				     =    2
									__a				     =    0.02
									__a				     =    3
									__a				     =    4
									__a				     =    '''last'''
									__a				     =    True
									__a				     =    None
									__a				     =    0




						def  __UpperCamelCase							(  self      )							->str:



									'''simple docstring'''
									__a				     =    ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size      )
									__a				     =    random_attention_mask([self.batch_size, self.seq_length] ,   dtype=tf.floataa      )

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

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

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

									__a				     =    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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Optional[int]:



									'''simple docstring'''
									__a				     =    TFFlaubertModel(config=UpperCamelCase__      )
									__a				     =    {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
									__a				     =    model(UpperCamelCase__      )

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




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Dict:



									'''simple docstring'''
									__a				     =    TFFlaubertWithLMHeadModel(UpperCamelCase__      )

									__a				     =    {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
									__a				     =    model(UpperCamelCase__      )

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




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Tuple:



									'''simple docstring'''
									__a				     =    TFFlaubertForQuestionAnsweringSimple(UpperCamelCase__      )

									__a				     =    {'''input_ids''': input_ids, '''lengths''': input_lengths}

									__a				     =    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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->List[str]:



									'''simple docstring'''
									__a				     =    TFFlaubertForSequenceClassification(UpperCamelCase__      )

									__a				     =    {'''input_ids''': input_ids, '''lengths''': input_lengths}

									__a				     =    model(UpperCamelCase__      )

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




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->List[Any]:



									'''simple docstring'''
									__a				     =    self.num_labels
									__a				     =    TFFlaubertForTokenClassification(config=UpperCamelCase__      )
									__a				     =    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									__a				     =    model(UpperCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.seq_length, self.num_labels)      )




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   )							->Tuple:



									'''simple docstring'''
									__a				     =    self.num_choices
									__a				     =    TFFlaubertForMultipleChoice(config=UpperCamelCase__      )
									__a				     =    tf.tile(tf.expand_dims(UpperCamelCase__ ,   1      ) ,   (1, self.num_choices, 1)      )
									__a				     =    tf.tile(tf.expand_dims(UpperCamelCase__ ,   1      ) ,   (1, self.num_choices, 1)      )
									__a				     =    tf.tile(tf.expand_dims(UpperCamelCase__ ,   1      ) ,   (1, self.num_choices, 1)      )
									__a				     =    {
									    '''input_ids''': multiple_choice_inputs_ids,
									    '''attention_mask''': multiple_choice_input_mask,
									    '''token_type_ids''': multiple_choice_token_type_ids,
									}
									__a				     =    model(UpperCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_choices)      )






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



									'''simple docstring'''
									__a				     =    self.prepare_config_and_inputs()
									(
									    __a						
									)				     =    config_and_inputs
									__a				     =    {
									    '''input_ids''': input_ids,
									    '''token_type_ids''': token_type_ids,
									    '''langs''': token_type_ids,
									    '''lengths''': input_lengths,
									}
									return config, inputs_dict







@require_tf
class 							__SCREAMING_SNAKE_CASE       (							UpperCamelCase_      ,      UpperCamelCase_      ,      unittest.TestCase     ):
						__a  					=(
						    (
						        TFFlaubertModel,
						        TFFlaubertWithLMHeadModel,
						        TFFlaubertForSequenceClassification,
						        TFFlaubertForQuestionAnsweringSimple,
						        TFFlaubertForTokenClassification,
						        TFFlaubertForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						__a  					=(
						    (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Check other models whether language generation is also applicable
						__a  					=(
						    {
						        "feature-extraction": TFFlaubertModel,
						        "fill-mask": TFFlaubertWithLMHeadModel,
						        "question-answering": TFFlaubertForQuestionAnsweringSimple,
						        "text-classification": TFFlaubertForSequenceClassification,
						        "token-classification": TFFlaubertForTokenClassification,
						        "zero-shot": TFFlaubertForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__a  					=False
						__a  					=False




						def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Optional[int]:



									'''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  __UpperCamelCase							(  self      )							->Tuple:



									'''simple docstring'''
									__a				     =    TFFlaubertModelTester(self      )
									__a				     =    ConfigTester(self ,   config_class=UpperCamelCase__ ,   emb_dim=37      )




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



									'''simple docstring'''
									self.config_tester.run_common_tests()




						def  __UpperCamelCase							(  self      )							->Tuple:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__      )




						def  __UpperCamelCase							(  self      )							->Dict:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__      )




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



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__      )




						def  __UpperCamelCase							(  self      )							->int:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__      )




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



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCamelCase__      )




						def  __UpperCamelCase							(  self      )							->Tuple:



									'''simple docstring'''
									__a				     =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCamelCase__      )






						@slow
						def  __UpperCamelCase							(  self      )							->Any:



									'''simple docstring'''





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







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




						@slow
						def  __UpperCamelCase							(  self      )							->List[Any]:



									'''simple docstring'''
									__a				     =    TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased'      )

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

									__a				     =    model(UpperCamelCase__      )[0]
									__a				     =    tf.TensorShape((1, 8, 512)      )
									self.assertEqual(output.shape ,   UpperCamelCase__      )
									# compare the actual values for a slice.
									__a				     =    tf.convert_to_tensor(
									    [
									        [
									            [-1.8768773, -1.566555, 0.27072418],
									            [-1.6920038, -0.5873505, 1.9329599],
									            [-2.9563985, -1.6993835, 1.7972052],
									        ]
									    ] ,   dtype=tf.floataa ,   )

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







'''simple docstring'''



__UpperCamelCase      :       Tuple      			=      """0.18.2"""

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
 if not is_onnx_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_onnx_objects import *  # noqa F403
else:
 from .pipelines import OnnxRuntimeModel

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_pt_objects import *  # noqa F403
else:
 from .models import (
     AutoencoderKL,
     ControlNetModel,
     ModelMixin,
     PriorTransformer,
     TaFilmDecoder,
     TransformeraDModel,
     UNetaDModel,
     UNetaDConditionModel,
     UNetaDModel,
     UNetaDConditionModel,
     VQModel,
 )
 from .optimization import (
     get_constant_schedule,
     get_constant_schedule_with_warmup,
     get_cosine_schedule_with_warmup,
     get_cosine_with_hard_restarts_schedule_with_warmup,
     get_linear_schedule_with_warmup,
     get_polynomial_decay_schedule_with_warmup,
     get_scheduler,
 )
 from .pipelines import (
     AudioPipelineOutput,
     ConsistencyModelPipeline,
     DanceDiffusionPipeline,
     DDIMPipeline,
     DDPMPipeline,
     DiffusionPipeline,
     DiTPipeline,
     ImagePipelineOutput,
     KarrasVePipeline,
     LDMPipeline,
     LDMSuperResolutionPipeline,
     PNDMPipeline,
     RePaintPipeline,
     ScoreSdeVePipeline,
 )
 from .schedulers import (
     CMStochasticIterativeScheduler,
     DDIMInverseScheduler,
     DDIMParallelScheduler,
     DDIMScheduler,
     DDPMParallelScheduler,
     DDPMScheduler,
     DEISMultistepScheduler,
     DPMSolverMultistepInverseScheduler,
     DPMSolverMultistepScheduler,
     DPMSolverSinglestepScheduler,
     EulerAncestralDiscreteScheduler,
     EulerDiscreteScheduler,
     HeunDiscreteScheduler,
     IPNDMScheduler,
     KarrasVeScheduler,
     KDPMaAncestralDiscreteScheduler,
     KDPMaDiscreteScheduler,
     PNDMScheduler,
     RePaintScheduler,
     SchedulerMixin,
     ScoreSdeVeScheduler,
     UnCLIPScheduler,
     UniPCMultistepScheduler,
     VQDiffusionScheduler,
 )
 from .training_utils import EMAModel

try:
 if not (is_torch_available() and is_scipy_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
 from .schedulers import LMSDiscreteScheduler

try:
 if not (is_torch_available() and is_torchsde_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
 from .schedulers import DPMSolverSDEScheduler

try:
 if not (is_torch_available() and is_transformers_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
 from .pipelines import (
     AltDiffusionImgaImgPipeline,
     AltDiffusionPipeline,
     AudioLDMPipeline,
     CycleDiffusionPipeline,
     IFImgaImgPipeline,
     IFImgaImgSuperResolutionPipeline,
     IFInpaintingPipeline,
     IFInpaintingSuperResolutionPipeline,
     IFPipeline,
     IFSuperResolutionPipeline,
     ImageTextPipelineOutput,
     KandinskyImgaImgPipeline,
     KandinskyInpaintPipeline,
     KandinskyPipeline,
     KandinskyPriorPipeline,
     KandinskyVaaControlnetImgaImgPipeline,
     KandinskyVaaControlnetPipeline,
     KandinskyVaaImgaImgPipeline,
     KandinskyVaaInpaintPipeline,
     KandinskyVaaPipeline,
     KandinskyVaaPriorEmbaEmbPipeline,
     KandinskyVaaPriorPipeline,
     LDMTextToImagePipeline,
     PaintByExamplePipeline,
     SemanticStableDiffusionPipeline,
     ShapEImgaImgPipeline,
     ShapEPipeline,
     StableDiffusionAttendAndExcitePipeline,
     StableDiffusionControlNetImgaImgPipeline,
     StableDiffusionControlNetInpaintPipeline,
     StableDiffusionControlNetPipeline,
     StableDiffusionDepthaImgPipeline,
     StableDiffusionDiffEditPipeline,
     StableDiffusionImageVariationPipeline,
     StableDiffusionImgaImgPipeline,
     StableDiffusionInpaintPipeline,
     StableDiffusionInpaintPipelineLegacy,
     StableDiffusionInstructPixaPixPipeline,
     StableDiffusionLatentUpscalePipeline,
     StableDiffusionLDMaDPipeline,
     StableDiffusionModelEditingPipeline,
     StableDiffusionPanoramaPipeline,
     StableDiffusionParadigmsPipeline,
     StableDiffusionPipeline,
     StableDiffusionPipelineSafe,
     StableDiffusionPixaPixZeroPipeline,
     StableDiffusionSAGPipeline,
     StableDiffusionUpscalePipeline,
     StableUnCLIPImgaImgPipeline,
     StableUnCLIPPipeline,
     TextToVideoSDPipeline,
     TextToVideoZeroPipeline,
     UnCLIPImageVariationPipeline,
     UnCLIPPipeline,
     UniDiffuserModel,
     UniDiffuserPipeline,
     UniDiffuserTextDecoder,
     VersatileDiffusionDualGuidedPipeline,
     VersatileDiffusionImageVariationPipeline,
     VersatileDiffusionPipeline,
     VersatileDiffusionTextToImagePipeline,
     VideoToVideoSDPipeline,
     VQDiffusionPipeline,
 )

try:
 if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
 from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
 if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
 from .pipelines import StableDiffusionKDiffusionPipeline

try:
 if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
 from .pipelines import (
     OnnxStableDiffusionImgaImgPipeline,
     OnnxStableDiffusionInpaintPipeline,
     OnnxStableDiffusionInpaintPipelineLegacy,
     OnnxStableDiffusionPipeline,
     OnnxStableDiffusionUpscalePipeline,
     StableDiffusionOnnxPipeline,
 )

try:
 if not (is_torch_available() and is_librosa_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
 from .pipelines import AudioDiffusionPipeline, Mel

try:
 if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
 from .pipelines import SpectrogramDiffusionPipeline

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_flax_objects import *  # noqa F403
else:
 from .models.controlnet_flax import FlaxControlNetModel
 from .models.modeling_flax_utils import FlaxModelMixin
 from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
 from .models.vae_flax import FlaxAutoencoderKL
 from .pipelines import FlaxDiffusionPipeline
 from .schedulers import (
     FlaxDDIMScheduler,
     FlaxDDPMScheduler,
     FlaxDPMSolverMultistepScheduler,
     FlaxKarrasVeScheduler,
     FlaxLMSDiscreteScheduler,
     FlaxPNDMScheduler,
     FlaxSchedulerMixin,
     FlaxScoreSdeVeScheduler,
 )


try:
 if not (is_flax_available() and is_transformers_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
 from .pipelines import (
     FlaxStableDiffusionControlNetPipeline,
     FlaxStableDiffusionImgaImgPipeline,
     FlaxStableDiffusionInpaintPipeline,
     FlaxStableDiffusionPipeline,
 )

try:
 if not (is_note_seq_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from .utils.dummy_note_seq_objects import *  # noqa F403
else:
 from .pipelines import MidiProcessor