

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 					__lowerCamelCase							(      unittest.TestCase      ):







      @property
      def       lowerCAmelCase_       (	self  )		->			Optional[int]:
        snake_case_					= 1
        snake_case_					= 3
        snake_case_					= (32, 32)

        snake_case_					= floats_tensor((batch_size, num_channels) + sizes				,    rng=random.Random(0  )  ).to(lowerCamelCase  )
        return image







      @property
      def       lowerCAmelCase_       (	self  )		->			Dict:
        torch.manual_seed(0  )
        snake_case_					= 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  )		->			Tuple:
        torch.manual_seed(0  )
        snake_case_					= 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  )		->			Optional[Any]:
        snake_case_					= """cpu"""
        snake_case_					= self.dummy_uncond_unet
        snake_case_					= DDIMScheduler()
        snake_case_					= self.dummy_vq_model

        snake_case_					= LDMSuperResolutionPipeline(unet=lowerCamelCase				,    vqvae=lowerCamelCase				,    scheduler=lowerCamelCase  )
        ldm.to(lowerCamelCase  )
        ldm.set_progress_bar_config(disable=lowerCamelCase  )

        snake_case_					= self.dummy_image.to(lowerCamelCase  )

        snake_case_					= torch.Generator(device=lowerCamelCase  ).manual_seed(0  )
        snake_case_					= ldm(image=lowerCamelCase				,    generator=lowerCamelCase				,    num_inference_steps=2				,    output_type="""numpy"""  ).images

        snake_case_					= image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)
        snake_case_					= np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150]  )

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







      @unittest.skipIf(torch_device != """cuda"""				,    """This test requires a GPU"""  )
      def       lowerCAmelCase_       (	self  )		->			Optional[int]:
        snake_case_					= self.dummy_uncond_unet
        snake_case_					= DDIMScheduler()
        snake_case_					= self.dummy_vq_model

        # put models in fp16
        snake_case_					= unet.half()
        snake_case_					= vqvae.half()

        snake_case_					= LDMSuperResolutionPipeline(unet=lowerCamelCase				,    vqvae=lowerCamelCase				,    scheduler=lowerCamelCase  )
        ldm.to(lowerCamelCase  )
        ldm.set_progress_bar_config(disable=lowerCamelCase  )

        snake_case_					= self.dummy_image.to(lowerCamelCase  )

        snake_case_					= ldm(lowerCamelCase				,    num_inference_steps=2				,    output_type="""numpy"""  ).images

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







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







      def       lowerCAmelCase_       (	self  )		->			Optional[Any]:
        snake_case_					= load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/vq_diffusion/teddy_bear_pool.png"""  )
        snake_case_					= init_image.resize((64, 64)				,    resample=PIL_INTERPOLATION["""lanczos"""]  )

        snake_case_					= LDMSuperResolutionPipeline.from_pretrained("""duongna/ldm-super-resolution"""				,    device_map="""auto"""  )
        ldm.set_progress_bar_config(disable=lowerCamelCase  )

        snake_case_					= torch.manual_seed(0  )
        snake_case_					= ldm(image=lowerCamelCase				,    generator=lowerCamelCase				,    num_inference_steps=20				,    output_type="""numpy"""  ).images

        snake_case_					= image[0, -3:, -3:, -1]

        assert image.shape == (1, 256, 256, 3)
        snake_case_					= np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907]  )

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


import unittest

from diffusers import __version__
from diffusers.utils import deprecate







class 					__lowerCamelCase							(      unittest.TestCase      ):
      lowerCamelCase_      : Dict  			=   '.'.join([str(int(__version__.split('.'      )[0]      ) + 1      )] + __version__.split('.'      )[1:]      )
      lowerCamelCase_      : int  			=   '0.0.1'







      def       lowerCAmelCase_       (	self  )		->			Optional[int]:
        snake_case_					= {"""deprecated_arg""": 4}

        with self.assertWarns(lowerCamelCase  ) as warning:
          snake_case_					= deprecate("""deprecated_arg"""				,    self.higher_version				,    """message"""				,    take_from=lowerCamelCase  )

        assert output == 4
        assert (
            str(warning.warning  )
            == f'''The `deprecated_arg` argument is deprecated and will be removed in version {self.higher_version}.'''
            " message"
        )







      def       lowerCAmelCase_       (	self  )		->			List[Any]:
        snake_case_					= {"""deprecated_arg""": 4}

        with self.assertWarns(lowerCamelCase  ) as warning:
          snake_case_					= deprecate(("""deprecated_arg""", self.higher_version, """message""")				,    take_from=lowerCamelCase  )

        assert output == 4
        assert (
            str(warning.warning  )
            == f'''The `deprecated_arg` argument is deprecated and will be removed in version {self.higher_version}.'''
            " message"
        )







      def       lowerCAmelCase_       (	self  )		->			int:
        snake_case_					= {"""deprecated_arg_1""": 4, """deprecated_arg_2""": 8}
        with self.assertWarns(lowerCamelCase  ) as warning:
          snake_case_		,							snake_case_					= deprecate(
              ("""deprecated_arg_1""", self.higher_version, """Hey""")				,    ("""deprecated_arg_2""", self.higher_version, """Hey""")				,    take_from=lowerCamelCase				,    )
        assert output_a == 4
        assert output_a == 8
        assert (
            str(warning.warnings[0].message  )
            == "The `deprecated_arg_1` argument is deprecated and will be removed in version"
            f''' {self.higher_version}. Hey'''
        )
        assert (
            str(warning.warnings[1].message  )
            == "The `deprecated_arg_2` argument is deprecated and will be removed in version"
            f''' {self.higher_version}. Hey'''
        )







      def       lowerCAmelCase_       (	self  )		->			Any:
        snake_case_					= {"""deprecated_arg""": 4}

        with self.assertRaises(lowerCamelCase  ) as error:
          deprecate(("""wrong_arg""", self.higher_version, """message""")				,    take_from=lowerCamelCase  )

        assert "test_deprecate_function_incorrect_arg in" in str(error.exception  )
        assert "line" in str(error.exception  )
        assert "got an unexpected keyword argument `deprecated_arg`" in str(error.exception  )







      def       lowerCAmelCase_       (	self  )		->			Optional[Any]:
        with self.assertWarns(lowerCamelCase  ) as warning:
          deprecate(("""deprecated_arg""", self.higher_version, """message""")  )

        assert (
            str(warning.warning  )
            == f'''`deprecated_arg` is deprecated and will be removed in version {self.higher_version}. message'''
        )







      def       lowerCAmelCase_       (	self  )		->			Any:
        with self.assertWarns(lowerCamelCase  ) as warning:
          deprecate(
              ("""deprecated_arg_1""", self.higher_version, """Hey""")				,    ("""deprecated_arg_2""", self.higher_version, """Hey""")				,    )
        assert (
            str(warning.warnings[0].message  )
            == f'''`deprecated_arg_1` is deprecated and will be removed in version {self.higher_version}. Hey'''
        )
        assert (
            str(warning.warnings[1].message  )
            == f'''`deprecated_arg_2` is deprecated and will be removed in version {self.higher_version}. Hey'''
        )







      def       lowerCAmelCase_       (	self  )		->			Any:







        class 					__lowerCamelCase							:
              lowerCamelCase_      : Dict  			=   5

        with self.assertWarns(lowerCamelCase  ) as warning:
          snake_case_					= deprecate(("""arg""", self.higher_version, """message""")				,    take_from=Args()  )

        assert arg == 5
        assert (
            str(warning.warning  )
            == f'''The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message'''
        )







      def       lowerCAmelCase_       (	self  )		->			Any:







        class 					__lowerCamelCase							:
              lowerCamelCase_      : str  			=   5
              lowerCamelCase_      : List[str]  			=   7

        with self.assertWarns(lowerCamelCase  ) as warning:
          snake_case_		,							snake_case_					= deprecate(
              ("""arg""", self.higher_version, """message""")				,    ("""foo""", self.higher_version, """message""")				,    ("""does not exist""", self.higher_version, """message""")				,    take_from=Args()				,    )

        assert arg_a == 5
        assert arg_a == 7
        assert (
            str(warning.warning  )
            == f'''The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message'''
        )
        assert (
            str(warning.warnings[0].message  )
            == f'''The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message'''
        )
        assert (
            str(warning.warnings[1].message  )
            == f'''The `foo` attribute is deprecated and will be removed in version {self.higher_version}. message'''
        )







      def       lowerCAmelCase_       (	self  )		->			int:
        snake_case_					= {"""deprecated_arg""": 4}

        with self.assertRaises(lowerCamelCase  ) as error:
          deprecate(("""wrong_arg""", self.lower_version, """message""")				,    take_from=lowerCamelCase  )

        assert (
            str(error.exception  )
            == "The deprecation tuple ('wrong_arg', '0.0.1', 'message') should be removed since diffusers' version"
            f''' {__version__} is >= {self.lower_version}'''
        )







      def       lowerCAmelCase_       (	self  )		->			List[Any]:
        with self.assertWarns(lowerCamelCase  ) as warning:
          deprecate(("""deprecated_arg""", self.higher_version, """This message is better!!!""")				,    standard_warn=lowerCamelCase  )

        assert str(warning.warning  ) == "This message is better!!!"







      def       lowerCAmelCase_       (	self  )		->			List[Any]:
        with self.assertWarns(lowerCamelCase  ) as warning:
          deprecate(("""deprecated_arg""", self.higher_version, """This message is better!!!""")				,    standard_warn=lowerCamelCase  )
        assert str(warning.warning  ) == "This message is better!!!"
        assert "diffusers/tests/others/test_utils.py" in warning.filename