


import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()







class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       @property
       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    1
             lowerCamelCase__						:							Union[str, Any]       =    3
             lowerCamelCase__						:							str       =    (32, 32)

             lowerCamelCase__						:							Optional[Any]       =    floats_tensor((batch_size, num_channels) + sizes   , rng=random.Random(0			)			).to(__lowerCamelCase			)
             return image





       @property
       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             torch.manual_seed(0			)
             lowerCamelCase__						:							Tuple       =    UNetaDModel(
                 block_out_channels=(32, 64)   , layers_per_block=2   , sample_size=32   , in_channels=6   , out_channels=3   , down_block_types=("DownBlock2D", "AttnDownBlock2D")   , up_block_types=("AttnUpBlock2D", "UpBlock2D")   , )
             return model





       @property
       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             torch.manual_seed(0			)
             lowerCamelCase__						:							Union[str, Any]       =    VQModel(
                 block_out_channels=[32, 64]   , in_channels=3   , out_channels=3   , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]   , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]   , latent_channels=3   , )
             return model





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    "cpu"
             lowerCamelCase__						:							int       =    self.dummy_uncond_unet
             lowerCamelCase__						:							int       =    DDIMScheduler()
             lowerCamelCase__						:							Optional[int]       =    self.dummy_vq_model

             lowerCamelCase__						:							Optional[Any]       =    LDMSuperResolutionPipeline(unet=__lowerCamelCase   , vqvae=__lowerCamelCase   , scheduler=__lowerCamelCase			)
             ldm.to(__lowerCamelCase			)
             ldm.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							str       =    self.dummy_image.to(__lowerCamelCase			)

             lowerCamelCase__						:							Optional[Any]       =    torch.Generator(device=__lowerCamelCase			).manual_seed(0			)
             lowerCamelCase__						:							Any       =    ldm(image=__lowerCamelCase   , generator=__lowerCamelCase   , num_inference_steps=2   , output_type="numpy"			).images

             lowerCamelCase__						:							List[str]       =    image[0, -3:, -3:, -1]

             assert image.shape == (1, 64, 64, 3)
             lowerCamelCase__						:							str       =    np.array([0.8_6_7_8, 0.8_2_4_5, 0.6_3_8_1, 0.6_8_3_0, 0.4_3_8_5, 0.5_5_9_9, 0.4_6_4_1, 0.6_2_0_1, 0.5_1_5_0]			)

             assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2





       @unittest.skipIf(torch_device != "cuda"   , "This test requires a GPU"			)
       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    self.dummy_uncond_unet
             lowerCamelCase__						:							Tuple       =    DDIMScheduler()
             lowerCamelCase__						:							int       =    self.dummy_vq_model

             # put models in fp16
             lowerCamelCase__						:							Tuple       =    unet.half()
             lowerCamelCase__						:							List[str]       =    vqvae.half()

             lowerCamelCase__						:							Union[str, Any]       =    LDMSuperResolutionPipeline(unet=__lowerCamelCase   , vqvae=__lowerCamelCase   , scheduler=__lowerCamelCase			)
             ldm.to(__lowerCamelCase			)
             ldm.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							str       =    self.dummy_image.to(__lowerCamelCase			)

             lowerCamelCase__						:							str       =    ldm(__lowerCamelCase   , num_inference_steps=2   , output_type="numpy"			).images

             assert image.shape == (1, 64, 64, 3)







@slow
@require_torch
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/vq_diffusion/teddy_bear_pool.png"			)
             lowerCamelCase__						:							str       =    init_image.resize((64, 64)   , resample=PIL_INTERPOLATION["lanczos"]			)

             lowerCamelCase__						:							Optional[Any]       =    LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution"   , device_map="auto"			)
             ldm.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							str       =    torch.manual_seed(0			)
             lowerCamelCase__						:							str       =    ldm(image=__lowerCamelCase   , generator=__lowerCamelCase   , num_inference_steps=20   , output_type="numpy"			).images

             lowerCamelCase__						:							Union[str, Any]       =    image[0, -3:, -3:, -1]

             assert image.shape == (1, 256, 256, 3)
             lowerCamelCase__						:							Dict       =    np.array([0.7_6_4_4, 0.7_6_7_9, 0.7_6_4_2, 0.7_6_3_3, 0.7_6_6_6, 0.7_5_6_0, 0.7_4_2_5, 0.7_2_5_7, 0.6_9_0_7]			)

             assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A :       Optional[int]						=   {
    "configuration_mpnet": ["MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "MPNetConfig"],
    "tokenization_mpnet": ["MPNetTokenizer"],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A :       List[Any]						=   ["MPNetTokenizerFast"]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A :       List[str]						=   [
        "MPNET_PRETRAINED_MODEL_ARCHIVE_LIST",
        "MPNetForMaskedLM",
        "MPNetForMultipleChoice",
        "MPNetForQuestionAnswering",
        "MPNetForSequenceClassification",
        "MPNetForTokenClassification",
        "MPNetLayer",
        "MPNetModel",
        "MPNetPreTrainedModel",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A :       Tuple						=   [
        "TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST",
        "TFMPNetEmbeddings",
        "TFMPNetForMaskedLM",
        "TFMPNetForMultipleChoice",
        "TFMPNetForQuestionAnswering",
        "TFMPNetForSequenceClassification",
        "TFMPNetForTokenClassification",
        "TFMPNetMainLayer",
        "TFMPNetModel",
        "TFMPNetPreTrainedModel",
    ]


if TYPE_CHECKING:
    from .configuration_mpnet import MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP, MPNetConfig
    from .tokenization_mpnet import MPNetTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_mpnet_fast import MPNetTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_mpnet import (
            MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
            MPNetForMaskedLM,
            MPNetForMultipleChoice,
            MPNetForQuestionAnswering,
            MPNetForSequenceClassification,
            MPNetForTokenClassification,
            MPNetLayer,
            MPNetModel,
            MPNetPreTrainedModel,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_mpnet import (
            TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFMPNetEmbeddings,
            TFMPNetForMaskedLM,
            TFMPNetForMultipleChoice,
            TFMPNetForQuestionAnswering,
            TFMPNetForSequenceClassification,
            TFMPNetForTokenClassification,
            TFMPNetMainLayer,
            TFMPNetModel,
            TFMPNetPreTrainedModel,
        )

else:
    import sys

    A :       Union[str, Any]						=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


