



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




      """simple docstring"""




      @property
      def 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        lowerCAmelCase_  :							int								=					1
        lowerCAmelCase_  :							Any								=					3
        lowerCAmelCase_  :							Optional[Any]								=					(3_2, 3_2)

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



      @property
      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        torch.manual_seed(0			)
        lowerCAmelCase_  :							List[str]								=					UNetaDModel(
            block_out_channels=(3_2, 6_4)       ,     layers_per_block=2       ,     sample_size=3_2       ,     in_channels=6       ,     out_channels=3       ,     down_block_types=('DownBlock2D', 'AttnDownBlock2D')       ,     up_block_types=('AttnUpBlock2D', 'UpBlock2D')       ,     )
        return model



      @property
      def 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        torch.manual_seed(0			)
        lowerCAmelCase_  :							List[Any]								=					VQModel(
            block_out_channels=[3_2, 6_4]       ,     in_channels=3       ,     out_channels=3       ,     down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']       ,     up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']       ,     latent_channels=3       ,     )
        return model



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							List[Any]								=					'cpu'
        lowerCAmelCase_  :							int								=					self.dummy_uncond_unet
        lowerCAmelCase_  :							str								=					DDIMScheduler()
        lowerCAmelCase_  :							Any								=					self.dummy_vq_model

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

        lowerCAmelCase_  :							Optional[Any]								=					self.dummy_image.to(SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							List[Any]								=					torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(0			)
        lowerCAmelCase_  :							List[Any]								=					ldm(image=SCREAMING_SNAKE_CASE_       ,     generator=SCREAMING_SNAKE_CASE_       ,     num_inference_steps=2       ,     output_type='numpy'			).images

        lowerCAmelCase_  :							List[Any]								=					image[0, -3:, -3:, -1]

        assert image.shape == (1, 6_4, 6_4, 3)
        lowerCAmelCase_  :							List[str]								=					np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]			)

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





      @unittest.skipIf(torch_device != 'cuda'       ,     'This test requires a GPU'			)
      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[int]			):
        lowerCAmelCase_  :							Optional[Any]								=					self.dummy_uncond_unet
        lowerCAmelCase_  :							List[Any]								=					DDIMScheduler()
        lowerCAmelCase_  :							int								=					self.dummy_vq_model

        # put models in fp16
        lowerCAmelCase_  :							Tuple								=					unet.half()
        lowerCAmelCase_  :							Any								=					vqvae.half()

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

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

        lowerCAmelCase_  :							Tuple								=					ldm(SCREAMING_SNAKE_CASE_       ,     num_inference_steps=2       ,     output_type='numpy'			).images

        assert image.shape == (1, 6_4, 6_4, 3)







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




      """simple docstring"""




      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							str								=					load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
            '/vq_diffusion/teddy_bear_pool.png'			)
        lowerCAmelCase_  :							Dict								=					init_image.resize((6_4, 6_4)       ,     resample=PIL_INTERPOLATION['lanczos']			)

        lowerCAmelCase_  :							int								=					LDMSuperResolutionPipeline.from_pretrained('duongna/ldm-super-resolution'       ,     device_map='auto'			)
        ldm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							List[Any]								=					torch.manual_seed(0			)
        lowerCAmelCase_  :							str								=					ldm(image=SCREAMING_SNAKE_CASE_       ,     generator=SCREAMING_SNAKE_CASE_       ,     num_inference_steps=2_0       ,     output_type='numpy'			).images

        lowerCAmelCase_  :							Tuple								=					image[0, -3:, -3:, -1]

        assert image.shape == (1, 2_5_6, 2_5_6, 3)
        lowerCAmelCase_  :							Optional[Any]								=					np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]			)

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










"""simple docstring"""







import unittest
from timeit import timeit

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  lowerCAmelCase_  :							Any								=					first_num if first_num >= second_num else second_num
  lowerCAmelCase_  :							Union[str, Any]								=					max_num
  while (common_mult % first_num > 0) or (common_mult % second_num > 0):
    common_mult += max_num
  return common_mult

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return b if a == 0 else greatest_common_divisor(b % a       ,  lowerCAmelCase__		)

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return first_num // greatest_common_divisor(lowerCAmelCase__       ,  lowerCAmelCase__		) * second_num

def 							UpperCamelCase_       (       )		->  Dict:

  """simple docstring"""




  lowerCAmelCase_  :							str								=					(
      'from __main__ import least_common_multiple_slow, least_common_multiple_fast'
  )
  print(
      'least_common_multiple_slow():'       ,  timeit('least_common_multiple_slow(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )
  print(
      'least_common_multiple_fast():'       ,  timeit('least_common_multiple_fast(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )







class  UpperCamelCase__  (      unittest.TestCase    ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE    							=	(
          (10, 20),
          (13, 15),
          (4, 31),
          (10, 42),
          (43, 34),
          (5, 12),
          (12, 25),
          (10, 25),
          (6, 9),
      )
      _SCREAMING_SNAKE_CASE    							=	(20, 195, 124, 210, 1462, 60, 300, 50, 18)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        for i, (first_num, second_num) in enumerate(self.test_inputs			):
          lowerCAmelCase_  :							Union[str, Any]								=					least_common_multiple_slow(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          lowerCAmelCase_  :							Any								=					least_common_multiple_fast(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          with self.subTest(i=SCREAMING_SNAKE_CASE_			):
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)


if __name__ == "__main__":
     benchmark()
     unittest.main()





