






import gc
import unittest

import numpy as np
import torch
from torch.backends.cuda import sdp_kernel

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)
from diffusers.utils import randn_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu

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


enable_full_determinism()




class 							_lowerCAmelCase(			UpperCAmelCase_				,      unittest.TestCase   ):





       """simple docstring"""







       a							:						List[str]										=ConsistencyModelPipeline
       a							:						Union[str, Any]										=UNCONDITIONAL_IMAGE_GENERATION_PARAMS
       a							:						List[str]										=UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

       # Override required_optional_params to remove num_images_per_prompt
       a							:						List[str]										=frozenset(
           [
               '''num_inference_steps''',
               '''generator''',
               '''latents''',
               '''output_type''',
               '''return_dict''',
               '''callback''',
               '''callback_steps''',
           ]   )





       @property
       def      _a       (   self				):
         UpperCamelCase_:   List[str]        =							UNetaDModel.from_pretrained(
             'diffusers/consistency-models-test'    ,     subfolder='test_unet'    ,     )
         return unet





       @property
       def      _a       (   self				):
         UpperCamelCase_:   Any        =							UNetaDModel.from_pretrained(
             'diffusers/consistency-models-test'    ,     subfolder='test_unet_class_cond'    ,     )
         return unet





       def      _a       (   self    ,     _lowerCamelCase=False				):
         if class_cond:
           UpperCamelCase_:   str        =							self.dummy_cond_unet
         else:
           UpperCamelCase_:   Union[str, Any]        =							self.dummy_uncond_unet

         # Default to CM multistep sampler
         UpperCamelCase_:   Optional[int]        =							CMStochasticIterativeScheduler(
             num_train_timesteps=4_0    ,     sigma_min=0.0_0_2    ,     sigma_max=8_0.0    ,     )

         UpperCamelCase_:   Optional[Any]        =							{
             'unet': unet,
             'scheduler': scheduler,
         }

         return components





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase=0				):
         if str(_lowerCamelCase				).startswith('mps'				):
           UpperCamelCase_:   List[Any]        =							torch.manual_seed(_lowerCamelCase				)
         else:
           UpperCamelCase_:   Union[str, Any]        =							torch.Generator(device=_lowerCamelCase				).manual_seed(_lowerCamelCase				)

         UpperCamelCase_:   Union[str, Any]        =							{
             'batch_size': 1,
             'num_inference_steps': None,
             'timesteps': [2_2, 0],
             'generator': generator,
             'output_type': 'np',
         }

         return inputs





       def      _a       (   self				):
         UpperCamelCase_:   Optional[int]        =							'cpu'  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase_:   Union[str, Any]        =							self.get_dummy_components()
         UpperCamelCase_:   List[str]        =							ConsistencyModelPipeline(**_lowerCamelCase				)
         UpperCamelCase_:   List[str]        =							pipe.to(_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Dict        =							self.get_dummy_inputs(_lowerCamelCase				)
         UpperCamelCase_:   Any        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 3_2, 3_2, 3)

         UpperCamelCase_:   int        =							image[0, -3:, -3:, -1]
         UpperCamelCase_:   Optional[int]        =							np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]				)

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





       def      _a       (   self				):
         UpperCamelCase_:   int        =							'cpu'  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase_:   List[Any]        =							self.get_dummy_components(class_cond=_lowerCamelCase				)
         UpperCamelCase_:   Tuple        =							ConsistencyModelPipeline(**_lowerCamelCase				)
         UpperCamelCase_:   Any        =							pipe.to(_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Optional[int]        =							self.get_dummy_inputs(_lowerCamelCase				)
         UpperCamelCase_:   Optional[int]        =							0
         UpperCamelCase_:   Union[str, Any]        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 3_2, 3_2, 3)

         UpperCamelCase_:   Union[str, Any]        =							image[0, -3:, -3:, -1]
         UpperCamelCase_:   int        =							np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]				)

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





       def      _a       (   self				):
         UpperCamelCase_:   Dict        =							'cpu'  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase_:   Any        =							self.get_dummy_components()
         UpperCamelCase_:   Dict        =							ConsistencyModelPipeline(**_lowerCamelCase				)
         UpperCamelCase_:   Any        =							pipe.to(_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   int        =							self.get_dummy_inputs(_lowerCamelCase				)
         UpperCamelCase_:   List[str]        =							1
         UpperCamelCase_:   List[str]        =							None
         UpperCamelCase_:   Optional[int]        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 3_2, 3_2, 3)

         UpperCamelCase_:   str        =							image[0, -3:, -3:, -1]
         UpperCamelCase_:   Optional[int]        =							np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]				)

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





       def      _a       (   self				):
         UpperCamelCase_:   Optional[int]        =							'cpu'  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase_:   Any        =							self.get_dummy_components(class_cond=_lowerCamelCase				)
         UpperCamelCase_:   Any        =							ConsistencyModelPipeline(**_lowerCamelCase				)
         UpperCamelCase_:   Optional[Any]        =							pipe.to(_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Dict        =							self.get_dummy_inputs(_lowerCamelCase				)
         UpperCamelCase_:   Tuple        =							1
         UpperCamelCase_:   List[str]        =							None
         UpperCamelCase_:   Tuple        =							0
         UpperCamelCase_:   Dict        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 3_2, 3_2, 3)

         UpperCamelCase_:   Optional[Any]        =							image[0, -3:, -3:, -1]
         UpperCamelCase_:   Dict        =							np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]				)

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







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





       """simple docstring"""







       def      _a       (   self				):
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





       def      _a       (   self    ,     _lowerCamelCase=0    ,     _lowerCamelCase=False    ,     _lowerCamelCase="cpu"    ,     _lowerCamelCase=torch.floataa    ,     _lowerCamelCase=(1, 3, 6_4, 6_4)				):
         UpperCamelCase_:   Dict        =							torch.manual_seed(_lowerCamelCase				)

         UpperCamelCase_:   Dict        =							{
             'num_inference_steps': None,
             'timesteps': [2_2, 0],
             'class_labels': 0,
             'generator': generator,
             'output_type': 'np',
         }

         if get_fixed_latents:
           UpperCamelCase_:   Any        =							self.get_fixed_latents(seed=_lowerCamelCase    ,     device=_lowerCamelCase    ,     dtype=_lowerCamelCase    ,     shape=_lowerCamelCase				)
           UpperCamelCase_:   List[Any]        =							latents

         return inputs





       def      _a       (   self    ,     _lowerCamelCase=0    ,     _lowerCamelCase="cpu"    ,     _lowerCamelCase=torch.floataa    ,     _lowerCamelCase=(1, 3, 6_4, 6_4)				):
         if type(_lowerCamelCase				) == str:
           UpperCamelCase_:   List[Any]        =							torch.device(_lowerCamelCase				)
         UpperCamelCase_:   List[Any]        =							torch.Generator(device=_lowerCamelCase				).manual_seed(_lowerCamelCase				)
         UpperCamelCase_:   Optional[int]        =							randn_tensor(_lowerCamelCase    ,     generator=_lowerCamelCase    ,     device=_lowerCamelCase    ,     dtype=_lowerCamelCase				)
         return latents





       def      _a       (   self				):
         UpperCamelCase_:   List[Any]        =							UNetaDModel.from_pretrained('diffusers/consistency_models'    ,     subfolder='diffusers_cd_imagenet64_l2'				)
         UpperCamelCase_:   List[Any]        =							CMStochasticIterativeScheduler(
             num_train_timesteps=4_0    ,     sigma_min=0.0_0_2    ,     sigma_max=8_0.0    ,     )
         UpperCamelCase_:   Optional[int]        =							ConsistencyModelPipeline(unet=_lowerCamelCase    ,     scheduler=_lowerCamelCase				)
         pipe.to(torch_device=_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Dict        =							self.get_inputs()
         UpperCamelCase_:   int        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 6_4, 6_4, 3)

         UpperCamelCase_:   int        =							image[0, -3:, -3:, -1]

         UpperCamelCase_:   Any        =							np.array([0.0_8_8_8, 0.0_8_8_1, 0.0_6_6_6, 0.0_4_7_9, 0.0_2_9_2, 0.0_1_9_5, 0.0_2_0_1, 0.0_1_6_3, 0.0_2_5_4]				)

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





       def      _a       (   self				):
         UpperCamelCase_:   str        =							UNetaDModel.from_pretrained('diffusers/consistency_models'    ,     subfolder='diffusers_cd_imagenet64_l2'				)
         UpperCamelCase_:   Union[str, Any]        =							CMStochasticIterativeScheduler(
             num_train_timesteps=4_0    ,     sigma_min=0.0_0_2    ,     sigma_max=8_0.0    ,     )
         UpperCamelCase_:   List[str]        =							ConsistencyModelPipeline(unet=_lowerCamelCase    ,     scheduler=_lowerCamelCase				)
         pipe.to(torch_device=_lowerCamelCase				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   str        =							self.get_inputs()
         UpperCamelCase_:   Union[str, Any]        =							1
         UpperCamelCase_:   Dict        =							None
         UpperCamelCase_:   Optional[Any]        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 6_4, 6_4, 3)

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

         UpperCamelCase_:   Optional[Any]        =							np.array([0.0_3_4_0, 0.0_1_5_2, 0.0_0_6_3, 0.0_2_6_7, 0.0_2_2_1, 0.0_1_0_7, 0.0_4_1_6, 0.0_1_8_6, 0.0_2_1_7]				)

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





       @require_torch_a
       def      _a       (   self				):
         UpperCamelCase_:   Tuple        =							UNetaDModel.from_pretrained('diffusers/consistency_models'    ,     subfolder='diffusers_cd_imagenet64_l2'				)
         UpperCamelCase_:   Dict        =							CMStochasticIterativeScheduler(
             num_train_timesteps=4_0    ,     sigma_min=0.0_0_2    ,     sigma_max=8_0.0    ,     )
         UpperCamelCase_:   List[str]        =							ConsistencyModelPipeline(unet=_lowerCamelCase    ,     scheduler=_lowerCamelCase				)
         pipe.to(torch_device=_lowerCamelCase    ,     torch_dtype=torch.floataa				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Optional[Any]        =							self.get_inputs(get_fixed_latents=_lowerCamelCase    ,     device=_lowerCamelCase				)
         # Ensure usage of flash attention in torch 2.0
         with sdp_kernel(enable_flash=_lowerCamelCase    ,     enable_math=_lowerCamelCase    ,     enable_mem_efficient=_lowerCamelCase				):
           UpperCamelCase_:   Union[str, Any]        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 6_4, 6_4, 3)

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

         UpperCamelCase_:   Any        =							np.array([0.1_8_7_5, 0.1_4_2_8, 0.1_2_8_9, 0.2_1_5_1, 0.2_0_9_2, 0.1_4_7_7, 0.1_8_7_7, 0.1_6_4_1, 0.1_3_5_3]				)

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





       @require_torch_a
       def      _a       (   self				):
         UpperCamelCase_:   Any        =							UNetaDModel.from_pretrained('diffusers/consistency_models'    ,     subfolder='diffusers_cd_imagenet64_l2'				)
         UpperCamelCase_:   Tuple        =							CMStochasticIterativeScheduler(
             num_train_timesteps=4_0    ,     sigma_min=0.0_0_2    ,     sigma_max=8_0.0    ,     )
         UpperCamelCase_:   List[Any]        =							ConsistencyModelPipeline(unet=_lowerCamelCase    ,     scheduler=_lowerCamelCase				)
         pipe.to(torch_device=_lowerCamelCase    ,     torch_dtype=torch.floataa				)
         pipe.set_progress_bar_config(disable=_lowerCamelCase				)

         UpperCamelCase_:   Any        =							self.get_inputs(get_fixed_latents=_lowerCamelCase    ,     device=_lowerCamelCase				)
         UpperCamelCase_:   Union[str, Any]        =							1
         UpperCamelCase_:   List[str]        =							None
         # Ensure usage of flash attention in torch 2.0
         with sdp_kernel(enable_flash=_lowerCamelCase    ,     enable_math=_lowerCamelCase    ,     enable_mem_efficient=_lowerCamelCase				):
           UpperCamelCase_:   Optional[Any]        =							pipe(**_lowerCamelCase				).images
         assert image.shape == (1, 6_4, 6_4, 3)

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

         UpperCamelCase_:   Tuple        =							np.array([0.1_6_6_3, 0.1_9_4_8, 0.2_2_7_5, 0.1_6_8_0, 0.1_2_0_4, 0.1_2_4_5, 0.1_8_5_8, 0.1_3_3_8, 0.2_0_9_5]				)

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




'''simple docstring'''

from __future__ import annotations







def a_     (    __UpperCAmelCase					)				->					list[int]:





			"""simple docstring"""







			snake_case:    Tuple      							=[True] * limit
			snake_case:    Optional[int]      							=False
			snake_case:    Union[str, Any]      							=False
			snake_case:    List[Any]      							=True

			for i in range(3				,							int(limit**0.5 + 1					)				,							2					):
						snake_case:    str      							=i * 2
						while index < limit:
									snake_case:    List[Any]      							=False
									snake_case:    List[str]      							=index + i

			snake_case:    Union[str, Any]      							=[2]

			for i in range(3				,							__UpperCAmelCase				,							2					):
						if is_prime[i]:
									primes.append(__UpperCAmelCase					)

			return primes







def a_     (    __UpperCAmelCase = 1_00_00_00					)				->					int:





			"""simple docstring"""







			snake_case:    str      							=prime_sieve(__UpperCAmelCase					)
			snake_case:    str      							=0
			snake_case:    str      							=0

			for i in range(len(__UpperCAmelCase					)					):
						for j in range(i + length				,							len(__UpperCAmelCase					)					):
									snake_case:    Tuple      							=sum(primes[i:j]					)
									if sol >= ceiling:
												break

									if sol in primes:
												snake_case:    List[str]      							=j - i
												snake_case:    Optional[int]      							=sol

			return largest


if __name__ == "__main__":
	print(F"""{solution() = }""")




