





'''simple docstring'''





import argparse
import struct
import unittest



class    _a     :






							"""simple docstring"""

							def __init__(      self				:     List[str]							,	a				:     int    )	->Dict:
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					data

													# Initialize hash values
													SCREAMING_SNAKE_CASE__     :				Any   =					[
													    0X6A09_E667,
													    0XBB67_AE85,
													    0X3C6E_F372,
													    0XA54F_F53A,
													    0X510E_527F,
													    0X9B05_688C,
													    0X1F83_D9AB,
													    0X5BE0_CD19,
													]

													# Initialize round constants
													SCREAMING_SNAKE_CASE__     :				str   =					[
													    0X428A_2F98,
													    0X7137_4491,
													    0XB5C0_FBCF,
													    0XE9B5_DBA5,
													    0X3956_C25B,
													    0X59F1_11F1,
													    0X923F_82A4,
													    0XAB1C_5ED5,
													    0XD807_AA98,
													    0X1283_5B01,
													    0X2431_85BE,
													    0X550C_7DC3,
													    0X72BE_5D74,
													    0X80DE_B1FE,
													    0X9BDC_06A7,
													    0XC19B_F174,
													    0XE49B_69C1,
													    0XEFBE_4786,
													    0X0FC1_9DC6,
													    0X240C_A1CC,
													    0X2DE9_2C6F,
													    0X4A74_84AA,
													    0X5CB0_A9DC,
													    0X76F9_88DA,
													    0X983E_5152,
													    0XA831_C66D,
													    0XB003_27C8,
													    0XBF59_7FC7,
													    0XC6E0_0BF3,
													    0XD5A7_9147,
													    0X06CA_6351,
													    0X1429_2967,
													    0X27B7_0A85,
													    0X2E1B_2138,
													    0X4D2C_6DFC,
													    0X5338_0D13,
													    0X650A_7354,
													    0X766A_0ABB,
													    0X81C2_C92E,
													    0X9272_2C85,
													    0XA2BF_E8A1,
													    0XA81A_664B,
													    0XC24B_8B70,
													    0XC76C_51A3,
													    0XD192_E819,
													    0XD699_0624,
													    0XF40E_3585,
													    0X106A_A070,
													    0X19A4_C116,
													    0X1E37_6C08,
													    0X2748_774C,
													    0X34B0_BCB5,
													    0X391C_0CB3,
													    0X4ED8_AA4A,
													    0X5B9C_CA4F,
													    0X682E_6FF3,
													    0X748F_82EE,
													    0X78A5_636F,
													    0X84C8_7814,
													    0X8CC7_0208,
													    0X90BE_FFFA,
													    0XA450_6CEB,
													    0XBEF9_A3F7,
													    0XC671_78F2,
													]

													SCREAMING_SNAKE_CASE__     :				Any   =					self.preprocessing(self.data    )
													self.final_hash()

							@staticmethod
							def      A_  (      a				:     Tuple    )	->int:
													SCREAMING_SNAKE_CASE__     :				int   =					b'\x80' + (b'\x00' * (63 - (len(_A    ) + 8) % 64))
													SCREAMING_SNAKE_CASE__     :				Any   =					struct.pack(">Q"							,	(len(_A    ) * 8)    )
													return data + padding + big_endian_integer

							def      A_  (      self				:     List[Any]    )	->List[str]:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					[
													    self.preprocessed_data[x : x + 64]
													    for x in range(0							,	len(self.preprocessed_data    )							,	64    )
													]

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

																			SCREAMING_SNAKE_CASE__     :				Tuple   =					self.hashes

																			for index in range(0							,	64    ):
																									if index > 15:
																															# modify the zero-ed indexes at the end of the array
																															SCREAMING_SNAKE_CASE__     :				List[str]   =					(
																															    self.ror(words[index - 15]							,	7    )
																															    ^ self.ror(words[index - 15]							,	18    )
																															    ^ (words[index - 15] >> 3)
																															)
																															SCREAMING_SNAKE_CASE__     :				Tuple   =					(
																															    self.ror(words[index - 2]							,	17    )
																															    ^ self.ror(words[index - 2]							,	19    )
																															    ^ (words[index - 2] >> 10)
																															)

																															SCREAMING_SNAKE_CASE__     :				str   =					(
																															    words[index - 16] + sa + words[index - 7] + sa
																															) % 0X1_0000_0000

																									# Compression
																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.ror(_A							,	6    ) ^ self.ror(_A							,	11    ) ^ self.ror(_A							,	25    )
																									SCREAMING_SNAKE_CASE__     :				int   =					(e & f) ^ ((~e & 0XFFFF_FFFF) & g)
																									SCREAMING_SNAKE_CASE__     :				int   =					(
																									    h + sa + ch + self.round_constants[index] + words[index]
																									) % 0X1_0000_0000
																									SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.ror(_A							,	2    ) ^ self.ror(_A							,	13    ) ^ self.ror(_A							,	22    )
																									SCREAMING_SNAKE_CASE__     :				Any   =					(a & b) ^ (a & c) ^ (b & c)
																									SCREAMING_SNAKE_CASE__     :				Any   =					(sa + maj) % 0X1_0000_0000

																									SCREAMING_SNAKE_CASE__     :				Tuple   =					(
																									    g,
																									    f,
																									    e,
																									    ((d + tempa) % 0X1_0000_0000),
																									    c,
																									    b,
																									    a,
																									    ((tempa + tempa) % 0X1_0000_0000),
																									)

																			SCREAMING_SNAKE_CASE__     :				Any   =					[a, b, c, d, e, f, g, h]

																			# Modify final values
																			SCREAMING_SNAKE_CASE__     :				int   =					[
																			    ((element + mutated_hash_values[index]) % 0X1_0000_0000)
																			    for index, element in enumerate(self.hashes    )
																			]

													SCREAMING_SNAKE_CASE__     :				List[str]   =					''.join([hex(_A    )[2:].zfill(8    ) for value in self.hashes]    )






							def      A_  (      self				:     Union[str, Any]							,	a				:     str							,	a				:     Tuple    )	->int:
													return 0XFFFF_FFFF & (value << (32 - rotations)) | (value >> rotations)







class    _a     (				unittest.TestCase							):






							"""simple docstring"""

							def      A_  (      self				:     Any    )	->Union[str, Any]:
													import hashlib

													SCREAMING_SNAKE_CASE__     :				Any   =					bytes("Test String"							,	"utf-8"    )
													self.assertEqual(SHAaaa(_A    ).hash							,	hashlib.shaaaa(_A    ).hexdigest()    )







def       UpperCAmelCase							(     ):





						'''simple docstring'''


						import doctest

						doctest.testmod()

						SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					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"     )

						SCREAMING_SNAKE_CASE__     :				Tuple   =					parser.parse_args()

						SCREAMING_SNAKE_CASE__     :				List[str]   =					args.input_string

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

						print(SHAaaa(_lowerCamelCase     ).hash     )


if __name__ == "__main__":
				main()






import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
				import jax
				import jax.numpy as jnp
				from flax.jax_utils import replicate
				from flax.training.common_utils import shard



@slow
@require_flax
class    _a     (				unittest.TestCase							):






							"""simple docstring"""

							def      A_  (      self				:     Dict    )	->List[Any]:
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()

							def      A_  (      self				:     Dict    )	->Tuple:
													SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Dict   =					FlaxControlNetModel.from_pretrained(
													    "lllyasviel/sd-controlnet-canny"							,	from_pt=a							,	dtype=jnp.bfloataa    )
													SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Dict   =					FlaxStableDiffusionControlNetPipeline.from_pretrained(
													    "runwayml/stable-diffusion-v1-5"							,	controlnet=a							,	from_pt=a							,	dtype=jnp.bfloataa    )
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					controlnet_params

													SCREAMING_SNAKE_CASE__     :				Dict   =					"bird"
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					jax.device_count()
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					pipe.prepare_text_inputs([prompts] * num_samples    )

													SCREAMING_SNAKE_CASE__     :				Dict   =					load_image(
													    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"    )
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					pipe.prepare_image_inputs([canny_image] * num_samples    )

													SCREAMING_SNAKE_CASE__     :				List[Any]   =					jax.random.PRNGKey(0    )
													SCREAMING_SNAKE_CASE__     :				int   =					jax.random.split(a							,	jax.device_count()    )

													SCREAMING_SNAKE_CASE__     :				List[Any]   =					replicate(a    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					shard(a    )
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					shard(a    )

													SCREAMING_SNAKE_CASE__     :				Dict   =					pipe(
													    prompt_ids=a							,	image=a							,	params=a							,	prng_seed=a							,	num_inference_steps=50							,	jit=a							,	).images
													assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3)

													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]    )
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					images[0, 2_53:2_56, 2_53:2_56, -1]

													SCREAMING_SNAKE_CASE__     :				Tuple   =					jnp.asarray(jax.device_get(image_slice.flatten()    )    )
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					jnp.array(
													    [0.16_7969, 0.11_6699, 0.08_1543, 0.15_4297, 0.13_2812, 0.10_8887, 0.16_9922, 0.16_9922, 0.20_5078]    )
													print(f"""output_slice: {output_slice}"""    )
													assert jnp.abs(output_slice - expected_slice    ).max() < 1E-2






							def      A_  (      self				:     List[Any]    )	->Optional[Any]:
													SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				int   =					FlaxControlNetModel.from_pretrained(
													    "lllyasviel/sd-controlnet-openpose"							,	from_pt=a							,	dtype=jnp.bfloataa    )
													SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Optional[int]   =					FlaxStableDiffusionControlNetPipeline.from_pretrained(
													    "runwayml/stable-diffusion-v1-5"							,	controlnet=a							,	from_pt=a							,	dtype=jnp.bfloataa    )
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					controlnet_params

													SCREAMING_SNAKE_CASE__     :				Any   =					"Chef in the kitchen"
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					jax.device_count()
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					pipe.prepare_text_inputs([prompts] * num_samples    )

													SCREAMING_SNAKE_CASE__     :				Dict   =					load_image(
													    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png"    )
													SCREAMING_SNAKE_CASE__     :				str   =					pipe.prepare_image_inputs([pose_image] * num_samples    )

													SCREAMING_SNAKE_CASE__     :				Any   =					jax.random.PRNGKey(0    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					jax.random.split(a							,	jax.device_count()    )

													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					replicate(a    )
													SCREAMING_SNAKE_CASE__     :				Tuple   =					shard(a    )
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					shard(a    )

													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					pipe(
													    prompt_ids=a							,	image=a							,	params=a							,	prng_seed=a							,	num_inference_steps=50							,	jit=a							,	).images
													assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3)

													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]    )
													SCREAMING_SNAKE_CASE__     :				str   =					images[0, 2_53:2_56, 2_53:2_56, -1]

													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					jnp.asarray(jax.device_get(image_slice.flatten()    )    )
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					jnp.array(
													    [[0.27_1484, 0.26_1719, 0.27_5391, 0.27_7344, 0.27_9297, 0.29_1016, 0.29_4922, 0.30_2734, 0.30_2734]]    )
													print(f"""output_slice: {output_slice}"""    )
													assert jnp.abs(output_slice - expected_slice    ).max() < 1E-2