
from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, 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 tensorflow as tf

				from transformers.models.mpnet.modeling_tf_mpnet import (
				    TFMPNetForMaskedLM,
				    TFMPNetForMultipleChoice,
				    TFMPNetForQuestionAnswering,
				    TFMPNetForSequenceClassification,
				    TFMPNetForTokenClassification,
				    TFMPNetModel,
				)







class 							UpperCAmelCase	:

		def __init__(			self      :					List[Any] ,      lowerCAmelCase      :					Optional[int] ,      lowerCAmelCase      :					Dict=13 ,      lowerCAmelCase      :					Optional[int]=7 ,      lowerCAmelCase      :					str=True ,      lowerCAmelCase      :					str=True ,      lowerCAmelCase      :					Dict=False ,      lowerCAmelCase      :					int=True ,      lowerCAmelCase      :					List[Any]=99 ,      lowerCAmelCase      :					List[Any]=64 ,      lowerCAmelCase      :					List[str]=2 ,      lowerCAmelCase      :					int=4 ,      lowerCAmelCase      :					Tuple=64 ,      lowerCAmelCase      :					Any="gelu" ,      lowerCAmelCase      :					Tuple=0.1 ,      lowerCAmelCase      :					List[Any]=0.1 ,      lowerCAmelCase      :					Optional[Any]=512 ,      lowerCAmelCase      :					Tuple=16 ,      lowerCAmelCase      :					Tuple=2 ,      lowerCAmelCase      :					Any=0.02 ,      lowerCAmelCase      :					str=3 ,      lowerCAmelCase      :					Optional[Any]=4 ,      lowerCAmelCase      :					List[str]=None ,      ):
								lowercase       :    Union[str, Any]											=  parent
								lowercase       :    Any											=  batch_size
								lowercase       :    List[Any]											=  seq_length
								lowercase       :    int											=  is_training
								lowercase       :    Optional[Any]											=  use_input_mask
								lowercase       :    Union[str, Any]											=  use_token_type_ids
								lowercase       :    Dict											=  use_labels
								lowercase       :    List[str]											=  vocab_size
								lowercase       :    int											=  hidden_size
								lowercase       :    List[str]											=  num_hidden_layers
								lowercase       :    int											=  num_attention_heads
								lowercase       :    Any											=  intermediate_size
								lowercase       :    Tuple											=  hidden_act
								lowercase       :    int											=  hidden_dropout_prob
								lowercase       :    Optional[int]											=  attention_probs_dropout_prob
								lowercase       :    Optional[int]											=  max_position_embeddings
								lowercase       :    List[str]											=  type_vocab_size
								lowercase       :    Optional[Any]											=  type_sequence_label_size
								lowercase       :    Tuple											=  initializer_range
								lowercase       :    int											=  num_labels
								lowercase       :    int											=  num_choices
								lowercase       :    List[str]											=  scope

		def        _lowerCAmelCase	(			self      :					List[str]					):
								lowercase       :    int											=  ids_tensor([self.batch_size, self.seq_length] ,      self.vocab_size					)

								lowercase       :    List[str]											=  None
								if self.use_input_mask:
														lowercase       :    Union[str, Any]											=  random_attention_mask([self.batch_size, self.seq_length]					)

								lowercase       :    List[Any]											=  None
								lowercase       :    List[str]											=  None
								lowercase       :    Any											=  None
								if self.use_labels:
														lowercase       :    Any											=  ids_tensor([self.batch_size] ,      self.type_sequence_label_size					)
														lowercase       :    List[Any]											=  ids_tensor([self.batch_size, self.seq_length] ,      self.num_labels					)
														lowercase       :    Tuple											=  ids_tensor([self.batch_size] ,      self.num_choices					)

								lowercase       :    Optional[int]											=  MPNetConfig(
								    vocab_size=self.vocab_size ,      hidden_size=self.hidden_size ,      num_hidden_layers=self.num_hidden_layers ,      num_attention_heads=self.num_attention_heads ,      intermediate_size=self.intermediate_size ,      hidden_act=self.hidden_act ,      hidden_dropout_prob=self.hidden_dropout_prob ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob ,      max_position_embeddings=self.max_position_embeddings ,      initializer_range=self.initializer_range ,      )
								return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

		def        _lowerCAmelCase	(			self      :					Dict ,      lowerCAmelCase      :					Union[str, Any] ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					List[str]					):
								lowercase       :    Dict											=  TFMPNetModel(config=lowerCAmelCase					)
								lowercase       :    Union[str, Any]											=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase       :    str											=  model(lowerCAmelCase					)
								lowercase       :    Optional[int]											=  [input_ids, input_mask]
								lowercase       :    int											=  model(lowerCAmelCase					)
								self.parent.assertEqual(result.last_hidden_state.shape ,      (self.batch_size, self.seq_length, self.hidden_size)					)

		def        _lowerCAmelCase	(			self      :					Tuple ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					Optional[int] ,      lowerCAmelCase      :					List[Any] ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					str					):
								lowercase       :    Optional[int]											=  TFMPNetForMaskedLM(config=lowerCAmelCase					)
								lowercase       :    Dict											=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase       :    Optional[Any]											=  model(lowerCAmelCase					)
								self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.seq_length, self.vocab_size)					)

		def        _lowerCAmelCase	(			self      :					str ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					List[str] ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					Tuple					):
								lowercase       :    Tuple											=  TFMPNetForQuestionAnswering(config=lowerCAmelCase					)
								lowercase       :    List[str]											=  {
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								}
								lowercase       :    Dict											=  model(lowerCAmelCase					)
								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 ,      lowerCAmelCase      :					str ,      lowerCAmelCase      :					int ,      lowerCAmelCase      :					Optional[int] ,      lowerCAmelCase      :					Union[str, Any] ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					str					):
								lowercase       :    int											=  self.num_labels
								lowercase       :    List[str]											=  TFMPNetForSequenceClassification(lowerCAmelCase					)
								lowercase       :    List[Any]											=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase       :    List[str]											=  model(lowerCAmelCase					)
								self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.num_labels)					)

		def        _lowerCAmelCase	(			self      :					int ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					Union[str, Any] ,      lowerCAmelCase      :					Optional[Any] ,      lowerCAmelCase      :					str ,      lowerCAmelCase      :					Tuple ,      lowerCAmelCase      :					Tuple					):
								lowercase       :    Optional[int]											=  self.num_choices
								lowercase       :    List[Any]											=  TFMPNetForMultipleChoice(lowerCAmelCase					)
								lowercase       :    str											=  tf.tile(tf.expand_dims(lowerCAmelCase ,      1					) ,      (1, self.num_choices, 1)					)
								lowercase       :    Tuple											=  tf.tile(tf.expand_dims(lowerCAmelCase ,      1					) ,      (1, self.num_choices, 1)					)
								lowercase       :    Any											=  {
								    '''input_ids''': multiple_choice_inputs_ids,
								    '''attention_mask''': multiple_choice_input_mask,
								}
								lowercase       :    List[Any]											=  model(lowerCAmelCase					)
								self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.num_choices)					)

		def        _lowerCAmelCase	(			self      :					List[str] ,      lowerCAmelCase      :					Optional[int] ,      lowerCAmelCase      :					Any ,      lowerCAmelCase      :					List[str] ,      lowerCAmelCase      :					Optional[int] ,      lowerCAmelCase      :					int ,      lowerCAmelCase      :					Any					):
								lowercase       :    List[str]											=  self.num_labels
								lowercase       :    Union[str, Any]											=  TFMPNetForTokenClassification(lowerCAmelCase					)
								lowercase       :    List[str]											=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								lowercase       :    Tuple											=  model(lowerCAmelCase					)
								self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.seq_length, self.num_labels)					)




		def        _lowerCAmelCase	(			self      :					List[str]					):
								lowercase       :    int											=  self.prepare_config_and_inputs()
								(lowercase)       :    int											=  config_and_inputs
								lowercase       :    List[str]											=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict







@require_tf
class 							UpperCAmelCase	(   __lowerCamelCase						, __lowerCamelCase						, unittest.TestCase					):
		a__: Optional[Any]			  =				(
		    (
		        TFMPNetForMaskedLM,
		        TFMPNetForMultipleChoice,
		        TFMPNetForQuestionAnswering,
		        TFMPNetForSequenceClassification,
		        TFMPNetForTokenClassification,
		        TFMPNetModel,
		    )
		    if is_tf_available()
		    else ()
		)
		a__: Tuple			  =				(
		    {
		        """feature-extraction""": TFMPNetModel,
		        """fill-mask""": TFMPNetForMaskedLM,
		        """question-answering""": TFMPNetForQuestionAnswering,
		        """text-classification""": TFMPNetForSequenceClassification,
		        """token-classification""": TFMPNetForTokenClassification,
		        """zero-shot""": TFMPNetForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		a__: Tuple			  =				False
		a__: Union[str, Any]			  =				False

		def        _lowerCAmelCase	(			self      :					str					):
								lowercase       :    Union[str, Any]											=  TFMPNetModelTester(self					)
								lowercase       :    Dict											=  ConfigTester(self ,      config_class=lowerCAmelCase ,      hidden_size=37					)

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

		def        _lowerCAmelCase	(			self      :					str					):
								lowercase       :    int											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase					)

		def        _lowerCAmelCase	(			self      :					Any					):
								lowercase       :    Tuple											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCAmelCase					)

		def        _lowerCAmelCase	(			self      :					Union[str, Any]					):
								lowercase       :    List[str]											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase					)

		def        _lowerCAmelCase	(			self      :					List[Any]					):
								lowercase       :    List[str]											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase					)

		def        _lowerCAmelCase	(			self      :					Optional[int]					):
								lowercase       :    int											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase					)

		def        _lowerCAmelCase	(			self      :					List[Any]					):
								lowercase       :    List[Any]											=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase					)




		@slow
		def        _lowerCAmelCase	(			self      :					List[str]					):
								for model_name in ["microsoft/mpnet-base"]:
														lowercase       :    Optional[Any]											=  TFMPNetModel.from_pretrained(lowerCAmelCase					)
														self.assertIsNotNone(lowerCAmelCase					)







@require_tf
class 							UpperCAmelCase	(   unittest.TestCase					):

		@slow
		def        _lowerCAmelCase	(			self      :					List[str]					):
								lowercase       :    Tuple											=  TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''					)
								lowercase       :    Union[str, Any]											=  tf.constant([[0, 1, 2, 3, 4, 5]]					)
								lowercase       :    Optional[int]											=  model(lowerCAmelCase					)[0]

								lowercase       :    str											=  [1, 6, 768]
								self.assertEqual(output.shape ,      lowerCAmelCase					)

								lowercase       :    Any											=  tf.constant(
								    [
								        [
								            [-0.1_06_71_72, 0.08_21_64_73, 0.0_02_45_43],
								            [-0.03_46_58_79, 0.8_35_41_18, -0.03_25_22_88],
								            [-0.06_56_94_76, -0.12_42_41_11, -0.0_49_44_36],
								        ]
								    ]					)
								tf.debugging.assert_near(output[:, :3, :3] ,      lowerCAmelCase ,      atol=1E-4					)




from ..utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
)


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				from ..utils.dummy_pt_objects import *  # noqa F403
else:
				from .consistency_models import ConsistencyModelPipeline
				from .dance_diffusion import DanceDiffusionPipeline
				from .ddim import DDIMPipeline
				from .ddpm import DDPMPipeline
				from .dit import DiTPipeline
				from .latent_diffusion import LDMSuperResolutionPipeline
				from .latent_diffusion_uncond import LDMPipeline
				from .pipeline_utils import AudioPipelineOutput, DiffusionPipeline, ImagePipelineOutput
				from .pndm import PNDMPipeline
				from .repaint import RePaintPipeline
				from .score_sde_ve import ScoreSdeVePipeline
				from .stochastic_karras_ve import KarrasVePipeline

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 .audio_diffusion import AudioDiffusionPipeline, Mel

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 .alt_diffusion import AltDiffusionImgaImgPipeline, AltDiffusionPipeline
				from .audioldm import AudioLDMPipeline
				from .controlnet import (
				    StableDiffusionControlNetImgaImgPipeline,
				    StableDiffusionControlNetInpaintPipeline,
				    StableDiffusionControlNetPipeline,
				)
				from .deepfloyd_if import (
				    IFImgaImgPipeline,
				    IFImgaImgSuperResolutionPipeline,
				    IFInpaintingPipeline,
				    IFInpaintingSuperResolutionPipeline,
				    IFPipeline,
				    IFSuperResolutionPipeline,
				)
				from .kandinsky import (
				    KandinskyImgaImgPipeline,
				    KandinskyInpaintPipeline,
				    KandinskyPipeline,
				    KandinskyPriorPipeline,
				)
				from .kandinskya_a import (
				    KandinskyVaaControlnetImgaImgPipeline,
				    KandinskyVaaControlnetPipeline,
				    KandinskyVaaImgaImgPipeline,
				    KandinskyVaaInpaintPipeline,
				    KandinskyVaaPipeline,
				    KandinskyVaaPriorEmbaEmbPipeline,
				    KandinskyVaaPriorPipeline,
				)
				from .latent_diffusion import LDMTextToImagePipeline
				from .paint_by_example import PaintByExamplePipeline
				from .semantic_stable_diffusion import SemanticStableDiffusionPipeline
				from .shap_e import ShapEImgaImgPipeline, ShapEPipeline
				from .stable_diffusion import (
				    CycleDiffusionPipeline,
				    StableDiffusionAttendAndExcitePipeline,
				    StableDiffusionDepthaImgPipeline,
				    StableDiffusionDiffEditPipeline,
				    StableDiffusionImageVariationPipeline,
				    StableDiffusionImgaImgPipeline,
				    StableDiffusionInpaintPipeline,
				    StableDiffusionInpaintPipelineLegacy,
				    StableDiffusionInstructPixaPixPipeline,
				    StableDiffusionLatentUpscalePipeline,
				    StableDiffusionLDMaDPipeline,
				    StableDiffusionModelEditingPipeline,
				    StableDiffusionPanoramaPipeline,
				    StableDiffusionParadigmsPipeline,
				    StableDiffusionPipeline,
				    StableDiffusionPixaPixZeroPipeline,
				    StableDiffusionSAGPipeline,
				    StableDiffusionUpscalePipeline,
				    StableUnCLIPImgaImgPipeline,
				    StableUnCLIPPipeline,
				)
				from .stable_diffusion_safe import StableDiffusionPipelineSafe
				from .text_to_video_synthesis import TextToVideoSDPipeline, TextToVideoZeroPipeline, VideoToVideoSDPipeline
				from .unclip import UnCLIPImageVariationPipeline, UnCLIPPipeline
				from .unidiffuser import ImageTextPipelineOutput, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder
				from .versatile_diffusion import (
				    VersatileDiffusionDualGuidedPipeline,
				    VersatileDiffusionImageVariationPipeline,
				    VersatileDiffusionPipeline,
				    VersatileDiffusionTextToImagePipeline,
				)
				from .vq_diffusion import 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 .stable_diffusion_xl import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

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

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 .stable_diffusion import (
				    OnnxStableDiffusionImgaImgPipeline,
				    OnnxStableDiffusionInpaintPipeline,
				    OnnxStableDiffusionInpaintPipelineLegacy,
				    OnnxStableDiffusionPipeline,
				    OnnxStableDiffusionUpscalePipeline,
				    StableDiffusionOnnxPipeline,
				)

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 .stable_diffusion import StableDiffusionKDiffusionPipeline

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				from ..utils.dummy_flax_objects import *  # noqa F403
else:
				from .pipeline_flax_utils import FlaxDiffusionPipeline


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 .controlnet import FlaxStableDiffusionControlNetPipeline
				from .stable_diffusion import (
				    FlaxStableDiffusionImgaImgPipeline,
				    FlaxStableDiffusionInpaintPipeline,
				    FlaxStableDiffusionPipeline,
				)
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 .spectrogram_diffusion import MidiProcessor, SpectrogramDiffusionPipeline


