'''simple docstring'''




import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()





class 					__magic_name__		(					lowercase__,  lowercase__,  unittest.TestCase):
			UpperCamelCase__       = AutoencoderKL
			UpperCamelCase__       = """sample"""
			UpperCamelCase__       = 1e-2







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    Optional[int]       		= 4
										lowercase_      :    Optional[Any]       		= 3
										lowercase_      :    Any       		= (32, 32)

										lowercase_      :    str       		= floats_tensor((batch_size, num_channels) + sizes					).to(__lowercase					)

										return {"sample": image}







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										return (3, 32, 32)







			@property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										return (3, 32, 32)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int					):
										lowercase_      :    List[Any]       		= {
										    """block_out_channels""": [32, 64],
										    """in_channels""": 3,
										    """out_channels""": 3,
										    """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""],
										    """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""],
										    """latent_channels""": 4,
										}
										lowercase_      :    Tuple       		= self.dummy_input
										return init_dict, inputs_dict







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										pass







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										pass







			@unittest.skipIf(torch_device == """mps"""       ,    """Gradient checkpointing skipped on MPS"""					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[Any]					):
										# enable deterministic behavior for gradient checkpointing
										lowercase_		,		lowercase_      :    Optional[Any]       		= self.prepare_init_args_and_inputs_for_common()
										lowercase_      :    List[str]       		= self.model_class(**__lowercase					)
										model.to(__lowercase					)

										assert not model.is_gradient_checkpointing and model.training

										lowercase_      :    str       		= model(**__lowercase					).sample
										# run the backwards pass on the model. For backwards pass, for simplicity purpose,
										# we won't calculate the loss and rather backprop on out.sum()
										model.zero_grad()

										lowercase_      :    Dict       		= torch.randn_like(__lowercase					)
										lowercase_      :    Dict       		= (out - labels).mean()
										loss.backward()

										# re-instantiate the model now enabling gradient checkpointing
										lowercase_      :    List[str]       		= self.model_class(**__lowercase					)
										# clone model
										model_a.load_state_dict(model.state_dict()					)
										model_a.to(__lowercase					)
										model_a.enable_gradient_checkpointing()

										assert model_a.is_gradient_checkpointing and model_a.training

										lowercase_      :    Optional[int]       		= model_a(**__lowercase					).sample
										# run the backwards pass on the model. For backwards pass, for simplicity purpose,
										# we won't calculate the loss and rather backprop on out.sum()
										model_a.zero_grad()
										lowercase_      :    str       		= (out_a - labels).mean()
										loss_a.backward()

										# compare the output and parameters gradients
										self.assertTrue((loss - loss_a).abs() < 1E-5					)
										lowercase_      :    Dict       		= dict(model.named_parameters()					)
										lowercase_      :    List[str]       		= dict(model_a.named_parameters()					)
										for name, param in named_params.items():
																	self.assertTrue(torch_all_close(param.grad.data       ,    named_params_a[name].grad.data       ,    atol=5E-5					)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										lowercase_		,		lowercase_      :    Optional[int]       		= AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy"""       ,    output_loading_info=__lowercase					)
										self.assertIsNotNone(__lowercase					)
										self.assertEqual(len(loading_info["""missing_keys"""]					)       ,    0					)

										model.to(__lowercase					)
										lowercase_      :    str       		= model(**self.dummy_input					)

										assert image is not None, "Make sure output is not None"







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Optional[int]       		= AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy"""					)
										lowercase_      :    str       		= model.to(__lowercase					)
										model.eval()

										if torch_device == "mps":
																	lowercase_      :    Any       		= torch.manual_seed(0					)
										else:
																	lowercase_      :    str       		= torch.Generator(device=__lowercase					).manual_seed(0					)

										lowercase_      :    Dict       		= torch.randn(
										    1       ,    model.config.in_channels       ,    model.config.sample_size       ,    model.config.sample_size       ,    generator=torch.manual_seed(0					)       ,    )
										lowercase_      :    Optional[Any]       		= image.to(__lowercase					)
										with torch.no_grad():
																	lowercase_      :    str       		= model(__lowercase       ,    sample_posterior=__lowercase       ,    generator=__lowercase					).sample

										lowercase_      :    Optional[int]       		= output[0, -1, -3:, -3:].flatten().cpu()

										# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
										# the expected output slices are not the same for CPU and GPU.
										if torch_device == "mps":
																	lowercase_      :    Tuple       		= torch.tensor(
																	    [
																	        -4.0078E-01,
																	        -3.8323E-04,
																	        -1.2681E-01,
																	        -1.1462E-01,
																	        2.0095E-01,
																	        1.0893E-01,
																	        -8.8247E-02,
																	        -3.0361E-01,
																	        -9.8644E-03,
																	    ]					)
										elif torch_device == "cpu":
																	lowercase_      :    Tuple       		= torch.tensor(
																	    [-0.13_52, 0.08_78, 0.04_19, -0.08_18, -0.10_69, 0.06_88, -0.14_58, -0.44_46, -0.00_26]					)
										else:
																	lowercase_      :    Union[str, Any]       		= torch.tensor(
																	    [-0.24_21, 0.46_42, 0.25_07, -0.04_38, 0.06_82, 0.31_60, -0.20_18, -0.07_27, 0.24_85]					)

										self.assertTrue(torch_all_close(__lowercase       ,    __lowercase       ,    rtol=1E-2					)					)





@slow
class 					__magic_name__		(					unittest.TestCase):







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict       ,    lowercase_       :						int       ,    lowercase_       :						str					):
										return f'''gaussian_noise_s={seed}_shape={'_'.join([str(__lowercase					) for s in shape]					)}.npy'''







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]       ,    lowercase_       :						Optional[Any]=0       ,    lowercase_       :						List[str]=(4, 3, 512, 512)       ,    lowercase_       :						List[Any]=False					):
										lowercase_      :    Dict       		= torch.floataa if fpaa else torch.floataa
										lowercase_      :    Optional[int]       		= torch.from_numpy(load_hf_numpy(self.get_file_format(__lowercase       ,    __lowercase					)					)					).to(__lowercase					).to(__lowercase					)
										return image







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]       ,    lowercase_       :						Any="CompVis/stable-diffusion-v1-4"       ,    lowercase_       :						List[Any]=False					):
										lowercase_      :    Optional[int]       		= """fp16""" if fpaa else None
										lowercase_      :    Dict       		= torch.floataa if fpaa else torch.floataa

										lowercase_      :    int       		= AutoencoderKL.from_pretrained(
										    __lowercase       ,    subfolder="""vae"""       ,    torch_dtype=__lowercase       ,    revision=__lowercase       ,    )
										model.to(__lowercase					).eval()

										return model







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str       ,    lowercase_       :						Optional[int]=0					):
										if torch_device == "mps":
																	return torch.manual_seed(__lowercase					)
										return torch.Generator(device=__lowercase					).manual_seed(__lowercase					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.16_03, 0.98_78, -0.04_95, -0.07_90, -0.27_09, 0.83_75, -0.20_60, -0.08_24], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
			        [47, [-0.23_76, 0.11_68, 0.13_32, -0.48_40, -0.25_08, -0.07_91, -0.04_93, -0.40_89], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
			        # fmt: on
			    ]					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						str       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						List[Any]					):
										lowercase_      :    Optional[Any]       		= self.get_sd_vae_model()
										lowercase_      :    List[Any]       		= self.get_sd_image(__lowercase					)
										lowercase_      :    Union[str, Any]       		= self.get_generator(__lowercase					)

										with torch.no_grad():
																	lowercase_      :    Dict       		= model(__lowercase       ,    generator=__lowercase       ,    sample_posterior=__lowercase					).sample

										assert sample.shape == image.shape

										lowercase_      :    Optional[Any]       		= sample[-1, -2:, -2:, :2].flatten().float().cpu()
										lowercase_      :    Optional[int]       		= torch.tensor(expected_slice_mps if torch_device == """mps""" else expected_slice					)

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=3E-3					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.05_13, 0.02_89, 1.37_99, 0.21_66, -0.25_73, -0.08_71, 0.51_03, -0.09_99]],
			        [47, [-0.41_28, -0.13_20, -0.37_04, 0.19_65, -0.41_16, -0.23_32, -0.33_40, 0.22_47]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Tuple					):
										lowercase_      :    int       		= self.get_sd_vae_model(fpaa=__lowercase					)
										lowercase_      :    Optional[int]       		= self.get_sd_image(__lowercase       ,    fpaa=__lowercase					)
										lowercase_      :    Union[str, Any]       		= self.get_generator(__lowercase					)

										with torch.no_grad():
																	lowercase_      :    List[Any]       		= model(__lowercase       ,    generator=__lowercase       ,    sample_posterior=__lowercase					).sample

										assert sample.shape == image.shape

										lowercase_      :    Tuple       		= sample[-1, -2:, :2, -2:].flatten().float().cpu()
										lowercase_      :    Tuple       		= torch.tensor(__lowercase					)

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=1E-2					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.16_09, 0.98_66, -0.04_87, -0.07_77, -0.27_16, 0.83_68, -0.20_55, -0.08_14], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
			        [47, [-0.23_77, 0.11_47, 0.13_33, -0.48_41, -0.25_06, -0.08_05, -0.04_91, -0.40_85], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
			        # fmt: on
			    ]					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						Tuple       ,    lowercase_       :						List[Any]					):
										lowercase_      :    int       		= self.get_sd_vae_model()
										lowercase_      :    Optional[Any]       		= self.get_sd_image(__lowercase					)

										with torch.no_grad():
																	lowercase_      :    Tuple       		= model(__lowercase					).sample

										assert sample.shape == image.shape

										lowercase_      :    Any       		= sample[-1, -2:, -2:, :2].flatten().float().cpu()
										lowercase_      :    Any       		= torch.tensor(expected_slice_mps if torch_device == """mps""" else expected_slice					)

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=3E-3					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [13, [-0.20_51, -0.18_03, -0.23_11, -0.21_14, -0.32_92, -0.35_74, -0.29_53, -0.33_23]],
			        [37, [-0.26_32, -0.26_25, -0.21_99, -0.27_41, -0.45_39, -0.49_90, -0.37_20, -0.49_25]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						Tuple					):
										lowercase_      :    List[Any]       		= self.get_sd_vae_model()
										lowercase_      :    List[str]       		= self.get_sd_image(__lowercase       ,    shape=(3, 4, 64, 64)					)

										with torch.no_grad():
																	lowercase_      :    str       		= model.decode(__lowercase					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										lowercase_      :    Optional[Any]       		= sample[-1, -2:, :2, -2:].flatten().cpu()
										lowercase_      :    Union[str, Any]       		= torch.tensor(__lowercase					)

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=1E-3					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [27, [-0.03_69, 0.02_07, -0.07_76, -0.06_82, -0.17_47, -0.19_30, -0.14_65, -0.20_39]],
			        [16, [-0.16_28, -0.21_34, -0.27_47, -0.26_42, -0.37_74, -0.44_04, -0.36_87, -0.42_77]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]       ,    lowercase_       :						List[Any]       ,    lowercase_       :						Any					):
										lowercase_      :    Optional[Any]       		= self.get_sd_vae_model(fpaa=__lowercase					)
										lowercase_      :    int       		= self.get_sd_image(__lowercase       ,    shape=(3, 4, 64, 64)       ,    fpaa=__lowercase					)

										with torch.no_grad():
																	lowercase_      :    Optional[int]       		= model.decode(__lowercase					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										lowercase_      :    Dict       		= sample[-1, -2:, :2, -2:].flatten().float().cpu()
										lowercase_      :    int       		= torch.tensor(__lowercase					)

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=5E-3					)







			@parameterized.expand([(13,), (16,), (27,)]					)
			@require_torch_gpu
			@unittest.skipIf(not is_xformers_available()       ,    reason="""xformers is not required when using PyTorch 2.0."""					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple       ,    lowercase_       :						Union[str, Any]					):
										lowercase_      :    Union[str, Any]       		= self.get_sd_vae_model(fpaa=__lowercase					)
										lowercase_      :    Any       		= self.get_sd_image(__lowercase       ,    shape=(3, 4, 64, 64)       ,    fpaa=__lowercase					)

										with torch.no_grad():
																	lowercase_      :    Optional[Any]       		= model.decode(__lowercase					).sample

										model.enable_xformers_memory_efficient_attention()
										with torch.no_grad():
																	lowercase_      :    int       		= model.decode(__lowercase					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=1E-1					)







			@parameterized.expand([(13,), (16,), (37,)]					)
			@require_torch_gpu
			@unittest.skipIf(not is_xformers_available()       ,    reason="""xformers is not required when using PyTorch 2.0."""					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Any					):
										lowercase_      :    Union[str, Any]       		= self.get_sd_vae_model()
										lowercase_      :    Optional[Any]       		= self.get_sd_image(__lowercase       ,    shape=(3, 4, 64, 64)					)

										with torch.no_grad():
																	lowercase_      :    Any       		= model.decode(__lowercase					).sample

										model.enable_xformers_memory_efficient_attention()
										with torch.no_grad():
																	lowercase_      :    Any       		= model.decode(__lowercase					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=1E-2					)







			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.30_01, 0.09_18, -2.69_84, -3.97_20, -3.20_99, -5.03_53, 1.73_38, -0.20_65, 3.42_67]],
			        [47, [-1.50_30, -4.38_71, -6.03_55, -9.11_57, -1.66_61, -2.78_53, 2.16_07, -5.08_23, 2.56_33]],
			        # fmt: on
			    ]					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]       ,    lowercase_       :						Union[str, Any]       ,    lowercase_       :						Optional[Any]					):
										lowercase_      :    Tuple       		= self.get_sd_vae_model()
										lowercase_      :    Dict       		= self.get_sd_image(__lowercase					)
										lowercase_      :    Any       		= self.get_generator(__lowercase					)

										with torch.no_grad():
																	lowercase_      :    Dict       		= model.encode(__lowercase					).latent_dist
																	lowercase_      :    Any       		= dist.sample(generator=__lowercase					)

										assert list(sample.shape					) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

										lowercase_      :    Dict       		= sample[0, -1, -3:, -3:].flatten().cpu()
										lowercase_      :    int       		= torch.tensor(__lowercase					)

										lowercase_      :    Tuple       		= 3E-3 if torch_device != """mps""" else 1E-2
										assert torch_all_close(__lowercase       ,    __lowercase       ,    atol=__lowercase					)






'''simple docstring'''




_lowercase		:				int	       =		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"




def 							lowerCamelCase			(   )    ->   None:
							lowercase_      :    List[Any]       		= input("""Enter message: """				)
							lowercase_      :    str       		= input("""Enter key [alphanumeric]: """				)
							lowercase_      :    List[Any]       		= input("""Encrypt/Decrypt [e/d]: """				)

							if mode.lower().startswith("""e"""				):
														lowercase_      :    List[str]       		= """encrypt"""
														lowercase_      :    Optional[int]       		= encrypt_message(UpperCAmelCase__   ,   UpperCAmelCase__				)
							elif mode.lower().startswith("""d"""				):
														lowercase_      :    Any       		= """decrypt"""
														lowercase_      :    Optional[Any]       		= decrypt_message(UpperCAmelCase__   ,   UpperCAmelCase__				)

							print(F'''\n{mode.title()}ed message:'''				)
							print(UpperCAmelCase__				)




def 							lowerCamelCase			(   UpperCAmelCase__ :      str   ,   UpperCAmelCase__ :      str				)    ->   str:
							return translate_message(UpperCAmelCase__   ,   UpperCAmelCase__   ,   """encrypt"""				)




def 							lowerCamelCase			(   UpperCAmelCase__ :      str   ,   UpperCAmelCase__ :      str				)    ->   str:
							return translate_message(UpperCAmelCase__   ,   UpperCAmelCase__   ,   """decrypt"""				)




def 							lowerCamelCase			(   UpperCAmelCase__ :      str   ,   UpperCAmelCase__ :      str   ,   UpperCAmelCase__ :      str				)    ->   str:
							lowercase_      :    Union[str, Any]       		= []
							lowercase_      :    List[Any]       		= 0
							lowercase_      :    str       		= key.upper()

							for symbol in message:
														lowercase_      :    Tuple       		= LETTERS.find(symbol.upper()				)
														if num != -1:
																					if mode == "encrypt":
																												num += LETTERS.find(key[key_index]				)
																					elif mode == "decrypt":
																												num -= LETTERS.find(key[key_index]				)

																					num %= len(UpperCAmelCase__				)

																					if symbol.isupper():
																												translated.append(LETTERS[num]				)
																					elif symbol.islower():
																												translated.append(LETTERS[num].lower()				)

																					key_index += 1
																					if key_index == len(UpperCAmelCase__				):
																												lowercase_      :    Any       		= 0
														else:
																					translated.append(UpperCAmelCase__				)
							return "".join(UpperCAmelCase__				)


if __name__ == "__main__":
						main()





