




'''simple docstring'''


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							):







  '''simple docstring'''

  @property
  def 					_snake_case     ( self				:							Union[str, Any]	)			->		int:







       '''simple docstring'''



       A:		str          =		1
       A:		str          =		3
       A:		Dict          =		(32, 32)

       A:		int          =		floats_tensor((batch_size, num_channels) + sizes					,  rng=random.Random(0	)	).to(SCREAMING_SNAKE_CASE_	)
       return image



  @property
  def 					_snake_case     ( self				:							str	)			->		Optional[Any]:







       '''simple docstring'''



       torch.manual_seed(0	)
       A:		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 					_snake_case     ( self				:							Union[str, Any]	)			->		Optional[int]:







       '''simple docstring'''



       torch.manual_seed(0	)
       A:		Optional[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 					_snake_case     ( self				:							Dict	)			->		int:







       '''simple docstring'''



       A:		Optional[int]          =		'''cpu'''
       A:		List[Any]          =		self.dummy_uncond_unet
       A:		Tuple          =		DDIMScheduler()
       A:		List[str]          =		self.dummy_vq_model

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

       A:		int          =		self.dummy_image.to(SCREAMING_SNAKE_CASE_	)

       A:		int          =		torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(0	)
       A:		int          =		ldm(image=SCREAMING_SNAKE_CASE_					,  generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''	).images

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

       assert image.shape == (1, 64, 64, 3)
       A:		Dict          =		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 					_snake_case     ( self				:							Union[str, Any]	)			->		List[Any]:







       '''simple docstring'''



       A:		List[str]          =		self.dummy_uncond_unet
       A:		Dict          =		DDIMScheduler()
       A:		Union[str, Any]          =		self.dummy_vq_model

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

       A:		List[Any]          =		LDMSuperResolutionPipeline(unet=SCREAMING_SNAKE_CASE_					,  vqvae=SCREAMING_SNAKE_CASE_					,  scheduler=SCREAMING_SNAKE_CASE_	)
       ldm.to(SCREAMING_SNAKE_CASE_	)
       ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		List[Any]          =		self.dummy_image.to(SCREAMING_SNAKE_CASE_	)

       A:		str          =		ldm(SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''	).images

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






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







  '''simple docstring'''

  def 					_snake_case     ( self				:							List[Any]	)			->		Optional[int]:







       '''simple docstring'''



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

       A:		Union[str, Any]          =		LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''					,  device_map='''auto'''	)
       ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		Dict          =		torch.manual_seed(0	)
       A:		Optional[int]          =		ldm(image=SCREAMING_SNAKE_CASE_					,  generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=20					,  output_type='''numpy'''	).images

       A:		Any          =		image[0, -3:, -3:, -1]

       assert image.shape == (1, 2_56, 2_56, 3)
       A:		str          =		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












'''simple docstring'''


from __future__ import annotations

import random






class 						lowerCAmelCase_	:







  '''simple docstring'''

  UpperCamelCase_			:      Optional[int]										=				6



  def __init__( self				:							Dict	)			->		Dict:







       '''simple docstring'''



       A:		Union[str, Any]          =		list(range(1					,  Dice.NUM_SIDES + 1	)	)




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







       '''simple docstring'''



       return random.choice(self.sides	)






def 					SCREAMING_SNAKE_CASE(   __lowercase		,				__lowercase = 2	)			-> list[float]:
     A:		Union[str, Any]          =		[Dice() for i in range(__lowercase	)]
     A:		Union[str, Any]          =		[0] * (len(__lowercase	) * Dice.NUM_SIDES + 1)
     for _ in range(__lowercase	):
          count_of_sum[sum(dice.roll() for dice in dices	)] += 1
     A:		Dict          =		[round((count * 1_0_0) / num_throws		,				2	) for count in count_of_sum]
     return probability[num_dice:]  # remove probability of sums that never appear


if __name__ == "__main__":
     import doctest

     doctest.testmod()






