

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    a__  (   unittest.TestCase     ):
			"""simple docstring"""







			@property
			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     1
							A__            =     3
							A__            =     (32, 32)

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


			@property
			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							torch.manual_seed(0    )
							A__            =     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        UpperCamelCase						(      self    )					->  Optional[int]:
							'''simple docstring'''






							torch.manual_seed(0    )
							A__            =     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        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							A__            =     "cpu"
							A__            =     self.dummy_uncond_unet
							A__            =     DDIMScheduler()
							A__            =     self.dummy_vq_model

							A__            =     LDMSuperResolutionPipeline(unet=lowercase , vqvae=lowercase , scheduler=lowercase    )
							ldm.to(lowercase    )
							ldm.set_progress_bar_config(disable=lowercase    )

							A__            =     self.dummy_image.to(lowercase    )

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

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

							assert image.shape == (1, 64, 64, 3)
							A__            =     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        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							A__            =     self.dummy_uncond_unet
							A__            =     DDIMScheduler()
							A__            =     self.dummy_vq_model

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

							A__            =     LDMSuperResolutionPipeline(unet=lowercase , vqvae=lowercase , scheduler=lowercase    )
							ldm.to(lowercase    )
							ldm.set_progress_bar_config(disable=lowercase    )

							A__            =     self.dummy_image.to(lowercase    )

							A__            =     ldm(lowercase , num_inference_steps=2 , output_type="numpy"    ).images

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

@slow
@require_torch
class    a__  (   unittest.TestCase     ):
			"""simple docstring"""







			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






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

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

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

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

							assert image.shape == (1, 256, 256, 3)
							A__            =     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




def    lowerCAmelCase__							(		SCREAMING_SNAKE_CASE_:							list		,    SCREAMING_SNAKE_CASE_:							int		,    SCREAMING_SNAKE_CASE_:							int		,    SCREAMING_SNAKE_CASE_:							int )     -> int:




				'''simple docstring'''





				A__            =     int((lower_bound + upper_bound) // 2 )
				if array[r] == value:
								return r
				if lower_bound >= upper_bound:
								return -1
				if array[r] < value:
								return binary_search(SCREAMING_SNAKE_CASE_		,    r + 1		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_ )
				else:
								return binary_search(SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    r - 1		,    SCREAMING_SNAKE_CASE_ )



def    lowerCAmelCase__							(		SCREAMING_SNAKE_CASE_:							int		,    SCREAMING_SNAKE_CASE_:							list )     -> list:




				'''simple docstring'''





				A__            =     0
				if matrix[index][0] == value:
								return [index, 0]
				while index < len(SCREAMING_SNAKE_CASE_ ) and matrix[index][0] < value:
								A__            =     binary_search(matrix[index]		,    0		,    len(matrix[index] ) - 1		,    SCREAMING_SNAKE_CASE_ )
								if r != -1:
												return [index, r]
								index += 1
				return [-1, -1]


if __name__ == "__main__":
	import doctest

	doctest.testmod()

