






'''simple docstring'''
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    __SCREAMING_SNAKE_CASE					(	_UpperCAmelCase		, unittest.TestCase ):
 snake_case_     							=							ConsistencyModelPipeline
 snake_case_     							=							UNCONDITIONAL_IMAGE_GENERATION_PARAMS
 snake_case_     							=							UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

 # Override required_optional_params to remove num_images_per_prompt
 snake_case_     							=							frozenset(
     [
         """num_inference_steps""",
         """generator""",
         """latents""",
         """output_type""",
         """return_dict""",
         """callback""",
         """callback_steps""",
     ] )




 @property
 def    __magic_name__		(					self		:							Union[str, Any]					)		->  str:
       SCREAMING_SNAKE_CASE__   : Optional[int]													=UNetaDModel.from_pretrained(
           '''diffusers/consistency-models-test'''    ,					subfolder='''test_unet'''    ,					)
       return unet




 @property
 def    __magic_name__		(					self		:							str					)		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__   : List[str]													=UNetaDModel.from_pretrained(
           '''diffusers/consistency-models-test'''    ,					subfolder='''test_unet_class_cond'''    ,					)
       return unet




 def    __magic_name__		(					self		:							Any    ,					__lowercase		:							List[Any]=False					)		->  List[str]:
       if class_cond:
             SCREAMING_SNAKE_CASE__   : List[str]													=self.dummy_cond_unet
       else:
             SCREAMING_SNAKE_CASE__   : int													=self.dummy_uncond_unet

       # Default to CM multistep sampler
       SCREAMING_SNAKE_CASE__   : str													=CMStochasticIterativeScheduler(
           num_train_timesteps=40    ,					sigma_min=0.002    ,					sigma_max=80.0    ,					)

       SCREAMING_SNAKE_CASE__   : List[Any]													={
           '''unet''': unet,
           '''scheduler''': scheduler,
       }

       return components




 def    __magic_name__		(					self		:							str    ,					__lowercase		:							List[Any]    ,					__lowercase		:							List[Any]=0					)		->  Optional[int]:
       if str(UpperCAmelCase_					).startswith('''mps'''					):
             SCREAMING_SNAKE_CASE__   : Any													=torch.manual_seed(UpperCAmelCase_					)
       else:
             SCREAMING_SNAKE_CASE__   : Optional[Any]													=torch.Generator(device=UpperCAmelCase_					).manual_seed(UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : Dict													={
           '''batch_size''': 1,
           '''num_inference_steps''': None,
           '''timesteps''': [22, 0],
           '''generator''': generator,
           '''output_type''': '''np''',
       }

       return inputs




 def    __magic_name__		(					self		:							int					)		->  List[Any]:
       SCREAMING_SNAKE_CASE__   : str													='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE__   : int													=self.get_dummy_components()
       SCREAMING_SNAKE_CASE__   : str													=ConsistencyModelPipeline(**UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : List[Any]													=pipe.to(UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : Tuple													=self.get_dummy_inputs(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : Any													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 32, 32, 3)

       SCREAMING_SNAKE_CASE__   : int													=image[0, -3:, -3:, -1]
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004]					)

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




 def    __magic_name__		(					self		:							Optional[int]					)		->  str:
       SCREAMING_SNAKE_CASE__   : Optional[int]													='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE__   : str													=self.get_dummy_components(class_cond=UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : Optional[int]													=ConsistencyModelPipeline(**UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : str													=pipe.to(UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

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

       SCREAMING_SNAKE_CASE__   : Optional[Any]													=image[0, -3:, -3:, -1]
       SCREAMING_SNAKE_CASE__   : List[str]													=np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004]					)

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




 def    __magic_name__		(					self		:							str					)		->  List[Any]:
       SCREAMING_SNAKE_CASE__   : Dict													='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE__   : Tuple													=self.get_dummy_components()
       SCREAMING_SNAKE_CASE__   : int													=ConsistencyModelPipeline(**UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=pipe.to(UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : str													=self.get_dummy_inputs(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=1
       SCREAMING_SNAKE_CASE__   : Any													=None
       SCREAMING_SNAKE_CASE__   : List[str]													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 32, 32, 3)

       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=image[0, -3:, -3:, -1]
       SCREAMING_SNAKE_CASE__   : Dict													=np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987]					)

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




 def    __magic_name__		(					self		:							Tuple					)		->  Optional[int]:
       SCREAMING_SNAKE_CASE__   : int													='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE__   : Tuple													=self.get_dummy_components(class_cond=UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : List[str]													=ConsistencyModelPipeline(**UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : List[Any]													=pipe.to(UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : Tuple													=self.get_dummy_inputs(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : Optional[int]													=1
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=None
       SCREAMING_SNAKE_CASE__   : Optional[int]													=0
       SCREAMING_SNAKE_CASE__   : Dict													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 32, 32, 3)

       SCREAMING_SNAKE_CASE__   : str													=image[0, -3:, -3:, -1]
       SCREAMING_SNAKE_CASE__   : List[str]													=np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987]					)

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






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




 def    __magic_name__		(					self		:							List[str]					)		->  Dict:
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def    __magic_name__		(					self		:							Optional[int]    ,					__lowercase		:							str=0    ,					__lowercase		:							Optional[Any]=False    ,					__lowercase		:							Union[str, Any]="cpu"    ,					__lowercase		:							Tuple=torch.floataa    ,					__lowercase		:							List[Any]=(1, 3, 64, 64)					)		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__   : str													=torch.manual_seed(UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : Any													={
           '''num_inference_steps''': None,
           '''timesteps''': [22, 0],
           '''class_labels''': 0,
           '''generator''': generator,
           '''output_type''': '''np''',
       }

       if get_fixed_latents:
             SCREAMING_SNAKE_CASE__   : List[Any]													=self.get_fixed_latents(seed=UpperCAmelCase_    ,					device=UpperCAmelCase_    ,					dtype=UpperCAmelCase_    ,					shape=UpperCAmelCase_					)
             SCREAMING_SNAKE_CASE__   : Dict													=latents

       return inputs




 def    __magic_name__		(					self		:							int    ,					__lowercase		:							Optional[Any]=0    ,					__lowercase		:							str="cpu"    ,					__lowercase		:							List[Any]=torch.floataa    ,					__lowercase		:							Dict=(1, 3, 64, 64)					)		->  List[str]:
       if type(UpperCAmelCase_					) == str:
             SCREAMING_SNAKE_CASE__   : Dict													=torch.device(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : str													=torch.Generator(device=UpperCAmelCase_					).manual_seed(UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : int													=randn_tensor(UpperCAmelCase_    ,					generator=UpperCAmelCase_    ,					device=UpperCAmelCase_    ,					dtype=UpperCAmelCase_					)
       return latents




 def    __magic_name__		(					self		:							Any					)		->  Dict:
       SCREAMING_SNAKE_CASE__   : Any													=UNetaDModel.from_pretrained('''diffusers/consistency_models'''    ,					subfolder='''diffusers_cd_imagenet64_l2'''					)
       SCREAMING_SNAKE_CASE__   : Dict													=CMStochasticIterativeScheduler(
           num_train_timesteps=40    ,					sigma_min=0.002    ,					sigma_max=80.0    ,					)
       SCREAMING_SNAKE_CASE__   : str													=ConsistencyModelPipeline(unet=UpperCAmelCase_    ,					scheduler=UpperCAmelCase_					)
       pipe.to(torch_device=UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : Any													=self.get_inputs()
       SCREAMING_SNAKE_CASE__   : Dict													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 64, 64, 3)

       SCREAMING_SNAKE_CASE__   : Dict													=image[0, -3:, -3:, -1]

       SCREAMING_SNAKE_CASE__   : List[Any]													=np.array([0.0888, 0.0881, 0.0666, 0.0479, 0.0292, 0.0195, 0.0201, 0.0163, 0.0254]					)

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




 def    __magic_name__		(					self		:							Tuple					)		->  Tuple:
       SCREAMING_SNAKE_CASE__   : int													=UNetaDModel.from_pretrained('''diffusers/consistency_models'''    ,					subfolder='''diffusers_cd_imagenet64_l2'''					)
       SCREAMING_SNAKE_CASE__   : str													=CMStochasticIterativeScheduler(
           num_train_timesteps=40    ,					sigma_min=0.002    ,					sigma_max=80.0    ,					)
       SCREAMING_SNAKE_CASE__   : Any													=ConsistencyModelPipeline(unet=UpperCAmelCase_    ,					scheduler=UpperCAmelCase_					)
       pipe.to(torch_device=UpperCAmelCase_					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : List[str]													=self.get_inputs()
       SCREAMING_SNAKE_CASE__   : Any													=1
       SCREAMING_SNAKE_CASE__   : int													=None
       SCREAMING_SNAKE_CASE__   : Any													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 64, 64, 3)

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

       SCREAMING_SNAKE_CASE__   : Tuple													=np.array([0.0340, 0.0152, 0.0063, 0.0267, 0.0221, 0.0107, 0.0416, 0.0186, 0.0217]					)

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




 @require_torch_a
 def    __magic_name__		(					self		:							List[Any]					)		->  str:
       SCREAMING_SNAKE_CASE__   : Dict													=UNetaDModel.from_pretrained('''diffusers/consistency_models'''    ,					subfolder='''diffusers_cd_imagenet64_l2'''					)
       SCREAMING_SNAKE_CASE__   : Tuple													=CMStochasticIterativeScheduler(
           num_train_timesteps=40    ,					sigma_min=0.002    ,					sigma_max=80.0    ,					)
       SCREAMING_SNAKE_CASE__   : List[str]													=ConsistencyModelPipeline(unet=UpperCAmelCase_    ,					scheduler=UpperCAmelCase_					)
       pipe.to(torch_device=UpperCAmelCase_    ,					torch_dtype=torch.floataa					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : str													=self.get_inputs(get_fixed_latents=UpperCAmelCase_    ,					device=UpperCAmelCase_					)
       # Ensure usage of flash attention in torch 2.0
       with sdp_kernel(enable_flash=UpperCAmelCase_    ,					enable_math=UpperCAmelCase_    ,					enable_mem_efficient=UpperCAmelCase_					):
             SCREAMING_SNAKE_CASE__   : Optional[int]													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 64, 64, 3)

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

       SCREAMING_SNAKE_CASE__   : Optional[int]													=np.array([0.1875, 0.1428, 0.1289, 0.2151, 0.2092, 0.1477, 0.1877, 0.1641, 0.1353]					)

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




 @require_torch_a
 def    __magic_name__		(					self		:							Tuple					)		->  str:
       SCREAMING_SNAKE_CASE__   : Dict													=UNetaDModel.from_pretrained('''diffusers/consistency_models'''    ,					subfolder='''diffusers_cd_imagenet64_l2'''					)
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=CMStochasticIterativeScheduler(
           num_train_timesteps=40    ,					sigma_min=0.002    ,					sigma_max=80.0    ,					)
       SCREAMING_SNAKE_CASE__   : Tuple													=ConsistencyModelPipeline(unet=UpperCAmelCase_    ,					scheduler=UpperCAmelCase_					)
       pipe.to(torch_device=UpperCAmelCase_    ,					torch_dtype=torch.floataa					)
       pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

       SCREAMING_SNAKE_CASE__   : str													=self.get_inputs(get_fixed_latents=UpperCAmelCase_    ,					device=UpperCAmelCase_					)
       SCREAMING_SNAKE_CASE__   : str													=1
       SCREAMING_SNAKE_CASE__   : Dict													=None
       # Ensure usage of flash attention in torch 2.0
       with sdp_kernel(enable_flash=UpperCAmelCase_    ,					enable_math=UpperCAmelCase_    ,					enable_mem_efficient=UpperCAmelCase_					):
             SCREAMING_SNAKE_CASE__   : Dict													=pipe(**UpperCAmelCase_					).images
       assert image.shape == (1, 64, 64, 3)

       SCREAMING_SNAKE_CASE__   : str													=image[0, -3:, -3:, -1]

       SCREAMING_SNAKE_CASE__   : Tuple													=np.array([0.1663, 0.1948, 0.2275, 0.1680, 0.1204, 0.1245, 0.1858, 0.1338, 0.2095]					)

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

import os






def 							_lowercase							(     )		->		List[str]:
					'''simple docstring'''


					with open(os.path.dirname(UpperCamelCase_		) + '/p022_names.txt'		) as file:
										SCREAMING_SNAKE_CASE__													= str(file.readlines()[0]		)
										SCREAMING_SNAKE_CASE__													= names.replace('"'	,	''		).split(','		)

					names.sort()

					SCREAMING_SNAKE_CASE__													= 0
					SCREAMING_SNAKE_CASE__													= 0

					for i, name in enumerate(UpperCamelCase_		):
										for letter in name:
															name_score += ord(UpperCamelCase_		) - 64

										total_score += (i + 1) * name_score
										SCREAMING_SNAKE_CASE__													= 0
					return total_score


if __name__ == "__main__":
				print(solution())





