

'''simple docstring'''





import argparse
import struct
import unittest
class        A  :





						def __init__(	self	,    snake_case_			)			->							None:
										_a        =   data

										# Initialize hash values
										_a        =   [
										    0X6a_09_e6_67,
										    0Xbb_67_ae_85,
										    0X3c_6e_f3_72,
										    0Xa5_4f_f5_3a,
										    0X51_0e_52_7f,
										    0X9b_05_68_8c,
										    0X1f_83_d9_ab,
										    0X5b_e0_cd_19,
										]

										# Initialize round constants
										_a        =   [
										    0X42_8a_2f_98,
										    0X71_37_44_91,
										    0Xb5_c0_fb_cf,
										    0Xe9_b5_db_a5,
										    0X39_56_c2_5b,
										    0X59_f1_11_f1,
										    0X92_3f_82_a4,
										    0Xab_1c_5e_d5,
										    0Xd8_07_aa_98,
										    0X12_83_5b_01,
										    0X24_31_85_be,
										    0X55_0c_7d_c3,
										    0X72_be_5d_74,
										    0X80_de_b1_fe,
										    0X9b_dc_06_a7,
										    0Xc1_9b_f1_74,
										    0Xe4_9b_69_c1,
										    0Xef_be_47_86,
										    0X0f_c1_9d_c6,
										    0X24_0c_a1_cc,
										    0X2d_e9_2c_6f,
										    0X4a_74_84_aa,
										    0X5c_b0_a9_dc,
										    0X76_f9_88_da,
										    0X98_3e_51_52,
										    0Xa8_31_c6_6d,
										    0Xb0_03_27_c8,
										    0Xbf_59_7f_c7,
										    0Xc6_e0_0b_f3,
										    0Xd5_a7_91_47,
										    0X06_ca_63_51,
										    0X14_29_29_67,
										    0X27_b7_0a_85,
										    0X2e_1b_21_38,
										    0X4d_2c_6d_fc,
										    0X53_38_0d_13,
										    0X65_0a_73_54,
										    0X76_6a_0a_bb,
										    0X81_c2_c9_2e,
										    0X92_72_2c_85,
										    0Xa2_bf_e8_a1,
										    0Xa8_1a_66_4b,
										    0Xc2_4b_8b_70,
										    0Xc7_6c_51_a3,
										    0Xd1_92_e8_19,
										    0Xd6_99_06_24,
										    0Xf4_0e_35_85,
										    0X10_6a_a0_70,
										    0X19_a4_c1_16,
										    0X1e_37_6c_08,
										    0X27_48_77_4c,
										    0X34_b0_bc_b5,
										    0X39_1c_0c_b3,
										    0X4e_d8_aa_4a,
										    0X5b_9c_ca_4f,
										    0X68_2e_6f_f3,
										    0X74_8f_82_ee,
										    0X78_a5_63_6f,
										    0X84_c8_78_14,
										    0X8c_c7_02_08,
										    0X90_be_ff_fa,
										    0Xa4_50_6c_eb,
										    0Xbe_f9_a3_f7,
										    0Xc6_71_78_f2,
										]

										_a        =   self.preprocessing(self.data			)
										self.final_hash()





						@staticmethod
						def __lowerCAmelCase   (	snake_case_			)			->							bytes:
										_a        =   B"\x80" + (B"\x00" * (6_3 - (len(snake_case_			) + 8) % 6_4))
										_a        =   struct.pack(">Q"	,    (len(snake_case_			) * 8)			)
										return data + padding + big_endian_integer





						def __lowerCAmelCase   (	self			)			->							None:
										_a        =   [
										    self.preprocessed_data[x : x + 6_4]
										    for x in range(0	,    len(self.preprocessed_data			)	,    6_4			)
										]

										for block in self.blocks:
														# Convert the given block into a list of 4 byte integers
														_a        =   list(struct.unpack(">16L"	,    snake_case_			)			)
														# add 48 0-ed integers
														words += [0] * 4_8

														_a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a        =   self.hashes

														for index in range(0	,    6_4			):
																		if index > 1_5:
																						# modify the zero-ed indexes at the end of the array
																						_a        =   (
																						    self.ror(words[index - 1_5]	,    7			)
																						    ^ self.ror(words[index - 1_5]	,    1_8			)
																						    ^ (words[index - 1_5] >> 3)
																						)
																						_a        =   (
																						    self.ror(words[index - 2]	,    1_7			)
																						    ^ self.ror(words[index - 2]	,    1_9			)
																						    ^ (words[index - 2] >> 1_0)
																						)

																						_a        =   (
																						    words[index - 1_6] + sa + words[index - 7] + sa
																						) % 0X1_00_00_00_00

																		# Compression
																		_a        =   self.ror(snake_case_	,    6			) ^ self.ror(snake_case_	,    1_1			) ^ self.ror(snake_case_	,    2_5			)
																		_a        =   (e & f) ^ ((~e & 0Xff_ff_ff_ff) & g)
																		_a        =   (
																		    h + sa + ch + self.round_constants[index] + words[index]
																		) % 0X1_00_00_00_00
																		_a        =   self.ror(snake_case_	,    2			) ^ self.ror(snake_case_	,    1_3			) ^ self.ror(snake_case_	,    2_2			)
																		_a        =   (a & b) ^ (a & c) ^ (b & c)
																		_a        =   (sa + maj) % 0X1_00_00_00_00

																		_a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a        =   (
																		    g,
																		    f,
																		    e,
																		    ((d + tempa) % 0X1_00_00_00_00),
																		    c,
																		    b,
																		    a,
																		    ((tempa + tempa) % 0X1_00_00_00_00),
																		)

														_a        =   [a, b, c, d, e, f, g, h]

														# Modify final values
														_a        =   [
														    ((element + mutated_hash_values[index]) % 0X1_00_00_00_00)
														    for index, element in enumerate(self.hashes			)
														]

										_a        =   "".join([hex(snake_case_			)[2:].zfill(8			) for value in self.hashes]			)





						def __lowerCAmelCase   (	self	,    snake_case_	,    snake_case_			)			->							int:
										return 0Xff_ff_ff_ff & (value << (3_2 - rotations)) | (value >> rotations)



class        A  (	unittest.TestCase ):





						def __lowerCAmelCase   (	self			)			->							None:
										import hashlib

										_a        =   bytes("Test String"	,    "utf-8"			)
										self.assertEqual(SHAaaa(snake_case_			).hash	,    hashlib.shaaaa(snake_case_			).hexdigest()			)



def 					_lowercase						(   ):
				import doctest

				doctest.testmod()

				_a        =   argparse.ArgumentParser()
				parser.add_argument(
				    "-s",		"--string",		dest="input_string",		default="Hello World!! Welcome to Cryptography",		help="Hash the string",		)
				parser.add_argument(
				    "-f",		"--file",		dest="input_file",		help="Hash contents of a file"							)

				_a        =   parser.parse_args()

				_a        =   args.input_string

				# hash input should be a bytestring
				if args.input_file:
								with open(args.input_file,		"rb"							) as f:
												_a        =   f.read()
				else:
								_a        =   bytes(lowerCamelCase__,		"utf-8"							)

				print(SHAaaa(lowerCamelCase__							).hash							)


if __name__ == "__main__":
							main()









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        __lowercase       (							a_			,							unittest.TestCase  ):



		"""simple docstring"""

		UpperCamelCase     :					List[str]			       =		ConsistencyModelPipeline
		UpperCamelCase     :					int			       =		UNCONDITIONAL_IMAGE_GENERATION_PARAMS
		UpperCamelCase     :					Any			       =		UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

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







		@property
		def 					__A   ( self	)  -> Optional[int]:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained(
					    """diffusers/consistency-models-test"""		,      subfolder="""test_unet"""		,      )
					return unet







		@property
		def 					__A   ( self	)  -> str:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained(
					    """diffusers/consistency-models-test"""		,      subfolder="""test_unet_class_cond"""		,      )
					return unet







		def 					__A   ( self		,      A=False	)  -> Tuple:







					'''simple docstring'''







					if class_cond:
								lowerCamelCase    		=  self.dummy_cond_unet
					else:
								lowerCamelCase    		=  self.dummy_uncond_unet

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

					lowerCamelCase    		=  {
					    """unet""": unet,
					    """scheduler""": scheduler,
					}

					return components







		def 					__A   ( self		,      A		,      A=0	)  -> List[str]:







					'''simple docstring'''







					if str(A	).startswith("""mps"""	):
								lowerCamelCase    		=  torch.manual_seed(A	)
					else:
								lowerCamelCase    		=  torch.Generator(device=A	).manual_seed(A	)

					lowerCamelCase    		=  {
					    """batch_size""": 1,
					    """num_inference_steps""": None,
					    """timesteps""": [22, 0],
					    """generator""": generator,
					    """output_type""": """np""",
					}

					return inputs







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







					'''simple docstring'''







					lowerCamelCase    		=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase    		=  self.get_dummy_components()
					lowerCamelCase    		=  ConsistencyModelPipeline(**A	)
					lowerCamelCase    		=  pipe.to(A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_dummy_inputs(A	)
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 32, 32, 3)

					lowerCamelCase    		=  image[0, -3:, -3:, -1]
					lowerCamelCase    		=  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 					__A   ( self	)  -> Dict:







					'''simple docstring'''







					lowerCamelCase    		=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase    		=  self.get_dummy_components(class_cond=A	)
					lowerCamelCase    		=  ConsistencyModelPipeline(**A	)
					lowerCamelCase    		=  pipe.to(A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_dummy_inputs(A	)
					lowerCamelCase    		=  0
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 32, 32, 3)

					lowerCamelCase    		=  image[0, -3:, -3:, -1]
					lowerCamelCase    		=  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 					__A   ( self	)  -> List[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase    		=  self.get_dummy_components()
					lowerCamelCase    		=  ConsistencyModelPipeline(**A	)
					lowerCamelCase    		=  pipe.to(A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_dummy_inputs(A	)
					lowerCamelCase    		=  1
					lowerCamelCase    		=  None
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 32, 32, 3)

					lowerCamelCase    		=  image[0, -3:, -3:, -1]
					lowerCamelCase    		=  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 					__A   ( self	)  -> List[str]:







					'''simple docstring'''







					lowerCamelCase    		=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase    		=  self.get_dummy_components(class_cond=A	)
					lowerCamelCase    		=  ConsistencyModelPipeline(**A	)
					lowerCamelCase    		=  pipe.to(A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_dummy_inputs(A	)
					lowerCamelCase    		=  1
					lowerCamelCase    		=  None
					lowerCamelCase    		=  0
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 32, 32, 3)

					lowerCamelCase    		=  image[0, -3:, -3:, -1]
					lowerCamelCase    		=  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        __lowercase       (							unittest.TestCase  ):



		"""simple docstring"""

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







					'''simple docstring'''







					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()







		def 					__A   ( self		,      A=0		,      A=False		,      A="cpu"		,      A=torch.floataa		,      A=(1, 3, 64, 64)	)  -> Optional[int]:







					'''simple docstring'''







					lowerCamelCase    		=  torch.manual_seed(A	)

					lowerCamelCase    		=  {
					    """num_inference_steps""": None,
					    """timesteps""": [22, 0],
					    """class_labels""": 0,
					    """generator""": generator,
					    """output_type""": """np""",
					}

					if get_fixed_latents:
								lowerCamelCase    		=  self.get_fixed_latents(seed=A		,      device=A		,      dtype=A		,      shape=A	)
								lowerCamelCase    		=  latents

					return inputs







		def 					__A   ( self		,      A=0		,      A="cpu"		,      A=torch.floataa		,      A=(1, 3, 64, 64)	)  -> Union[str, Any]:







					'''simple docstring'''







					if type(A	) == str:
								lowerCamelCase    		=  torch.device(A	)
					lowerCamelCase    		=  torch.Generator(device=A	).manual_seed(A	)
					lowerCamelCase    		=  randn_tensor(A		,      generator=A		,      device=A		,      dtype=A	)
					return latents







		def 					__A   ( self	)  -> str:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained("""diffusers/consistency_models"""		,      subfolder="""diffusers_cd_imagenet64_l2"""	)
					lowerCamelCase    		=  CMStochasticIterativeScheduler(
					    num_train_timesteps=40		,      sigma_min=0.002		,      sigma_max=80.0		,      )
					lowerCamelCase    		=  ConsistencyModelPipeline(unet=A		,      scheduler=A	)
					pipe.to(torch_device=A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_inputs()
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 64, 64, 3)

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

					lowerCamelCase    		=  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 					__A   ( self	)  -> Optional[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained("""diffusers/consistency_models"""		,      subfolder="""diffusers_cd_imagenet64_l2"""	)
					lowerCamelCase    		=  CMStochasticIterativeScheduler(
					    num_train_timesteps=40		,      sigma_min=0.002		,      sigma_max=80.0		,      )
					lowerCamelCase    		=  ConsistencyModelPipeline(unet=A		,      scheduler=A	)
					pipe.to(torch_device=A	)
					pipe.set_progress_bar_config(disable=A	)

					lowerCamelCase    		=  self.get_inputs()
					lowerCamelCase    		=  1
					lowerCamelCase    		=  None
					lowerCamelCase    		=  pipe(**A	).images
					assert image.shape == (1, 64, 64, 3)

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

					lowerCamelCase    		=  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 					__A   ( self	)  -> Any:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained("""diffusers/consistency_models"""		,      subfolder="""diffusers_cd_imagenet64_l2"""	)
					lowerCamelCase    		=  CMStochasticIterativeScheduler(
					    num_train_timesteps=40		,      sigma_min=0.002		,      sigma_max=80.0		,      )
					lowerCamelCase    		=  ConsistencyModelPipeline(unet=A		,      scheduler=A	)
					pipe.to(torch_device=A		,      torch_dtype=torch.floataa	)
					pipe.set_progress_bar_config(disable=A	)

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

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

					lowerCamelCase    		=  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 					__A   ( self	)  -> List[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  UNetaDModel.from_pretrained("""diffusers/consistency_models"""		,      subfolder="""diffusers_cd_imagenet64_l2"""	)
					lowerCamelCase    		=  CMStochasticIterativeScheduler(
					    num_train_timesteps=40		,      sigma_min=0.002		,      sigma_max=80.0		,      )
					lowerCamelCase    		=  ConsistencyModelPipeline(unet=A		,      scheduler=A	)
					pipe.to(torch_device=A		,      torch_dtype=torch.floataa	)
					pipe.set_progress_bar_config(disable=A	)

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

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

					lowerCamelCase    		=  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

