


import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class    UpperCAmelCase__	( lowerCamelCase__       ,  unittest.TestCase		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[Any]              =  DDIMPipeline
    UpperCAmelCase__						:		Tuple              =  UNCONDITIONAL_IMAGE_GENERATION_PARAMS
    UpperCAmelCase__						:		Dict              =  PipelineTesterMixin.required_optional_params - {
        'num_images_per_prompt',
        'latents',
        'callback',
        'callback_steps',
    }
    UpperCAmelCase__						:		str              =  UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
    UpperCAmelCase__						:		Optional[Any]              =  False






    def _a (      self   )  ->	Union[str, Any]:
           torch.manual_seed(0   )
           __UpperCamelCase   							=UNetaDModel(
               block_out_channels=(32, 64) ,       layers_per_block=2 ,       sample_size=32 ,       in_channels=3 ,       out_channels=3 ,       down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,       up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,       )
           __UpperCamelCase   							=DDIMScheduler()
           __UpperCamelCase   							={'unet': unet, 'scheduler': scheduler}
           return components






    def _a (      self ,       A_ ,       A_=0   )  ->	Tuple:







           if str(A_   ).startswith('mps'   ):
                  __UpperCamelCase   							=torch.manual_seed(A_   )
           else:
                  __UpperCamelCase   							=torch.Generator(device=A_   ).manual_seed(A_   )
           __UpperCamelCase   							={
               'batch_size': 1,
               'generator': generator,
               'num_inference_steps': 2,
               'output_type': 'numpy',
           }
           return inputs






    def _a (      self   )  ->	Union[str, Any]:
           __UpperCamelCase   							='cpu'

           __UpperCamelCase   							=self.get_dummy_components()
           __UpperCamelCase   							=self.pipeline_class(**A_   )
           pipe.to(A_   )
           pipe.set_progress_bar_config(disable=A_   )

           __UpperCamelCase   							=self.get_dummy_inputs(A_   )
           __UpperCamelCase   							=pipe(**A_   ).images
           __UpperCamelCase   							=image[0, -3:, -3:, -1]

           self.assertEqual(image.shape ,       (1, 32, 32, 3)   )
           __UpperCamelCase   							=np.array(
               [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04]   )
           __UpperCamelCase   							=np.abs(image_slice.flatten() - expected_slice   ).max()
           self.assertLessEqual(A_ ,       1E-3   )






    def _a (      self   )  ->	Dict:
           super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3   )






    def _a (      self   )  ->	Dict:
           super().test_save_load_local(expected_max_difference=3E-3   )






    def _a (      self   )  ->	Optional[Any]:
           super().test_save_load_optional_components(expected_max_difference=3E-3   )






    def _a (      self   )  ->	str:
           super().test_inference_batch_single_identical(expected_max_diff=3E-3   )






@slow
@require_torch_gpu
class    UpperCAmelCase__	( unittest.TestCase		):



    """simple docstring"""






    def _a (      self   )  ->	List[str]:
           __UpperCamelCase   							='google/ddpm-cifar10-32'

           __UpperCamelCase   							=UNetaDModel.from_pretrained(A_   )
           __UpperCamelCase   							=DDIMScheduler()

           __UpperCamelCase   							=DDIMPipeline(unet=A_ ,       scheduler=A_   )
           ddim.to(A_   )
           ddim.set_progress_bar_config(disable=A_   )

           __UpperCamelCase   							=torch.manual_seed(0   )
           __UpperCamelCase   							=ddim(generator=A_ ,       eta=0.0 ,       output_type='numpy'   ).images

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

           assert image.shape == (1, 32, 32, 3)
           __UpperCamelCase   							=np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]   )

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






    def _a (      self   )  ->	Optional[int]:
           __UpperCamelCase   							='google/ddpm-ema-bedroom-256'

           __UpperCamelCase   							=UNetaDModel.from_pretrained(A_   )
           __UpperCamelCase   							=DDIMScheduler.from_pretrained(A_   )

           __UpperCamelCase   							=DDIMPipeline(unet=A_ ,       scheduler=A_   )
           ddpm.to(A_   )
           ddpm.set_progress_bar_config(disable=A_   )

           __UpperCamelCase   							=torch.manual_seed(0   )
           __UpperCamelCase   							=ddpm(generator=A_ ,       output_type='numpy'   ).images

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

           assert image.shape == (1, 256, 256, 3)
           __UpperCamelCase   							=np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]   )

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









import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812

def 						_UpperCAmelCase    (      ):
     print('Making key files...'    )
     make_key_files('rsa'      ,					10_24    )
     print('Key files generation successful.'    )

def 						_UpperCAmelCase    (      SCREAMING_SNAKE_CASE__      : int    ):
     print('Generating prime p...'    )
     __UpperCamelCase   							=rabinMiller.generate_large_prime(SCREAMING_SNAKE_CASE__    )
     print('Generating prime q...'    )
     __UpperCamelCase   							=rabinMiller.generate_large_prime(SCREAMING_SNAKE_CASE__    )
     __UpperCamelCase   							=p * q

     print('Generating e that is relatively prime to (p - 1) * (q - 1)...'    )
     while True:
          __UpperCamelCase   							=random.randrange(2 ** (key_size - 1)      ,					2 ** (key_size)    )
          if cryptoMath.gcd(SCREAMING_SNAKE_CASE__      ,					(p - 1) * (q - 1)    ) == 1:
               break

     print('Calculating d that is mod inverse of e...'    )
     __UpperCamelCase   							=cryptoMath.find_mod_inverse(SCREAMING_SNAKE_CASE__      ,					(p - 1) * (q - 1)    )

     __UpperCamelCase   							=(n, e)
     __UpperCamelCase   							=(n, d)
     return (public_key, private_key)

def 						_UpperCAmelCase    (      SCREAMING_SNAKE_CASE__      : str      ,					SCREAMING_SNAKE_CASE__      : int    ):
     if os.path.exists(F'{name}_pubkey.txt'    ) or os.path.exists(F'{name}_privkey.txt'    ):
          print('\nWARNING:'    )
          print(
              F'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n'
              'Use a different name or delete these files and re-run this program.'    )
          sys.exit()

     __UpperCamelCase							,				__UpperCamelCase   							=generate_key(SCREAMING_SNAKE_CASE__    )
     print(F'\nWriting public key to file {name}_pubkey.txt...'    )
     with open(F'{name}_pubkey.txt'      ,					'w'    ) as out_file:
          out_file.write(F'{key_size},{public_key[0]},{public_key[1]}'    )

     print(F'Writing private key to file {name}_privkey.txt...'    )
     with open(F'{name}_privkey.txt'      ,					'w'    ) as out_file:
          out_file.write(F'{key_size},{private_key[0]},{private_key[1]}'    )


if __name__ == "__main__":
   main()





