






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    _lowerCAmelCase			(		lowercase_     ,      lowercase_     ,      unittest.TestCase      ):



				"""simple docstring"""



				_lowercase				:   Optional[int]      =  AutoencoderKL
				_lowercase				:   Dict      =  '''sample'''
				_lowercase				:   List[str]      =  1e-2



				@property
				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											snake_case__	  =       4
											snake_case__	  =       3
											snake_case__	  =       (3_2, 3_2)

											snake_case__	  =       floats_tensor((batch_size, num_channels) + sizes).to(UpperCamelCase__)

											return {"sample": image}



				@property
				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											return (3, 3_2, 3_2)



				@property
				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											return (3, 3_2, 3_2)



				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







											snake_case__	  =       {
											    """block_out_channels""": [3_2, 6_4],
											    """in_channels""": 3,
											    """out_channels""": 3,
											    """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""],
											    """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""],
											    """latent_channels""": 4,
											}
											snake_case__	  =       self.dummy_input
											return init_dict, inputs_dict



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											pass



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







											pass



				@unittest.skipIf(torch_device == """mps"""			,		"""Gradient checkpointing skipped on MPS""")
				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__  , snake_case__	  =       self.prepare_init_args_and_inputs_for_common()
											snake_case__	  =       self.model_class(**UpperCamelCase__)
											model.to(UpperCamelCase__)

											assert not model.is_gradient_checkpointing and model.training

											snake_case__	  =       model(**UpperCamelCase__).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()

											snake_case__	  =       torch.randn_like(UpperCamelCase__)
											snake_case__	  =       (out - labels).mean()
											loss.backward()

											# re-instantiate the model now enabling gradient checkpointing
											snake_case__	  =       self.model_class(**UpperCamelCase__)
											# clone model
											model_a.load_state_dict(model.state_dict())
											model_a.to(UpperCamelCase__)
											model_a.enable_gradient_checkpointing()

											assert model_a.is_gradient_checkpointing and model_a.training

											snake_case__	  =       model_a(**UpperCamelCase__).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()
											snake_case__	  =       (out_a - labels).mean()
											loss_a.backward()

											# compare the output and parameters gradients
											self.assertTrue((loss - loss_a).abs() < 1E-5)
											snake_case__	  =       dict(model.named_parameters())
											snake_case__	  =       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 					__magic_name__							(      self    :    Union[str, Any]):






											'''simple docstring'''







											snake_case__  , snake_case__	  =       AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy"""			,		output_loading_info=UpperCamelCase__)
											self.assertIsNotNone(UpperCamelCase__)
											self.assertEqual(len(loading_info["""missing_keys"""])			,		0)

											model.to(UpperCamelCase__)
											snake_case__	  =       model(**self.dummy_input)

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



				def 					__magic_name__							(      self    :    int):






											'''simple docstring'''







											snake_case__	  =       AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy""")
											snake_case__	  =       model.to(UpperCamelCase__)
											model.eval()

											if torch_device == "mps":
																		snake_case__	  =       torch.manual_seed(0)
											else:
																		snake_case__	  =       torch.Generator(device=UpperCamelCase__).manual_seed(0)

											snake_case__	  =       torch.randn(
											    1			,		model.config.in_channels			,		model.config.sample_size			,		model.config.sample_size			,		generator=torch.manual_seed(0)			,		)
											snake_case__	  =       image.to(UpperCamelCase__)
											with torch.no_grad():
																		snake_case__	  =       model(UpperCamelCase__			,		sample_posterior=UpperCamelCase__			,		generator=UpperCamelCase__).sample

											snake_case__	  =       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":
																		snake_case__	  =       torch.tensor(
																		    [
																		        -4.0_078E-01,
																		        -3.8_323E-04,
																		        -1.2_681E-01,
																		        -1.1_462E-01,
																		        2.0_095E-01,
																		        1.0_893E-01,
																		        -8.8_247E-02,
																		        -3.0_361E-01,
																		        -9.8_644E-03,
																		    ])
											elif torch_device == "cpu":
																		snake_case__	  =       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:
																		snake_case__	  =       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(UpperCamelCase__			,		UpperCamelCase__			,		rtol=1E-2))


@slow
class    _lowerCAmelCase			(		unittest.TestCase      ):



				"""simple docstring"""



				def 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    Union[str, Any]):






											'''simple docstring'''







											return F'''gaussian_noise_s={seed}_shape={'_'.join([str(UpperCamelCase__) for s in shape])}.npy'''



				def 					__magic_name__							(      self    :    Tuple):






											'''simple docstring'''







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



				def 					__magic_name__							(      self    :    str			,		UpperCamelCase__    :    int=0			,		UpperCamelCase__    :    Dict=(4, 3, 5_1_2, 5_1_2)			,		UpperCamelCase__    :    str=False):






											'''simple docstring'''







											snake_case__	  =       torch.floataa if fpaa else torch.floataa
											snake_case__	  =       torch.from_numpy(load_hf_numpy(self.get_file_format(UpperCamelCase__			,		UpperCamelCase__))).to(UpperCamelCase__).to(UpperCamelCase__)
											return image



				def 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    Optional[int]="CompVis/stable-diffusion-v1-4"			,		UpperCamelCase__    :    Tuple=False):






											'''simple docstring'''







											snake_case__	  =       """fp16""" if fpaa else None
											snake_case__	  =       torch.floataa if fpaa else torch.floataa

											snake_case__	  =       AutoencoderKL.from_pretrained(
											    UpperCamelCase__			,		subfolder="""vae"""			,		torch_dtype=UpperCamelCase__			,		revision=UpperCamelCase__			,		)
											model.to(UpperCamelCase__).eval()

											return model



				def 					__magic_name__							(      self    :    Union[str, Any]			,		UpperCamelCase__    :    List[str]=0):






											'''simple docstring'''







											if torch_device == "mps":
																		return torch.manual_seed(UpperCamelCase__)
											return torch.Generator(device=UpperCamelCase__).manual_seed(UpperCamelCase__)



				@parameterized.expand(
				    [
				        # fmt: off
				        [3_3, [-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]],
				        [4_7, [-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 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    str			,		UpperCamelCase__    :    Tuple):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model()
											snake_case__	  =       self.get_sd_image(UpperCamelCase__)
											snake_case__	  =       self.get_generator(UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model(UpperCamelCase__			,		generator=UpperCamelCase__			,		sample_posterior=UpperCamelCase__).sample

											assert sample.shape == image.shape

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

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



				@parameterized.expand(
				    [
				        # fmt: off
				        [3_3, [-0.05_13, 0.02_89, 1.37_99, 0.21_66, -0.25_73, -0.08_71, 0.51_03, -0.09_99]],
				        [4_7, [-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 					__magic_name__							(      self    :    str			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Any):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model(fpaa=UpperCamelCase__)
											snake_case__	  =       self.get_sd_image(UpperCamelCase__			,		fpaa=UpperCamelCase__)
											snake_case__	  =       self.get_generator(UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model(UpperCamelCase__			,		generator=UpperCamelCase__			,		sample_posterior=UpperCamelCase__).sample

											assert sample.shape == image.shape

											snake_case__	  =       sample[-1, -2:, :2, -2:].flatten().float().cpu()
											snake_case__	  =       torch.tensor(UpperCamelCase__)

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



				@parameterized.expand(
				    [
				        # fmt: off
				        [3_3, [-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]],
				        [4_7, [-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 					__magic_name__							(      self    :    str			,		UpperCamelCase__    :    str			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model()
											snake_case__	  =       self.get_sd_image(UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model(UpperCamelCase__).sample

											assert sample.shape == image.shape

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

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



				@parameterized.expand(
				    [
				        # fmt: off
				        [1_3, [-0.20_51, -0.18_03, -0.23_11, -0.21_14, -0.32_92, -0.35_74, -0.29_53, -0.33_23]],
				        [3_7, [-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 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Optional[int]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model()
											snake_case__	  =       self.get_sd_image(UpperCamelCase__			,		shape=(3, 4, 6_4, 6_4))

											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

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

											snake_case__	  =       sample[-1, -2:, :2, -2:].flatten().cpu()
											snake_case__	  =       torch.tensor(UpperCamelCase__)

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



				@parameterized.expand(
				    [
				        # fmt: off
				        [2_7, [-0.03_69, 0.02_07, -0.07_76, -0.06_82, -0.17_47, -0.19_30, -0.14_65, -0.20_39]],
				        [1_6, [-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 					__magic_name__							(      self    :    Tuple			,		UpperCamelCase__    :    Optional[Any]			,		UpperCamelCase__    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model(fpaa=UpperCamelCase__)
											snake_case__	  =       self.get_sd_image(UpperCamelCase__			,		shape=(3, 4, 6_4, 6_4)			,		fpaa=UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

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

											snake_case__	  =       sample[-1, -2:, :2, -2:].flatten().float().cpu()
											snake_case__	  =       torch.tensor(UpperCamelCase__)

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



				@parameterized.expand([(1_3,), (1_6,), (2_7,)])
				@require_torch_gpu
				@unittest.skipIf(not is_xformers_available()			,		reason="""xformers is not required when using PyTorch 2.0.""")
				def 					__magic_name__							(      self    :    int			,		UpperCamelCase__    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model(fpaa=UpperCamelCase__)
											snake_case__	  =       self.get_sd_image(UpperCamelCase__			,		shape=(3, 4, 6_4, 6_4)			,		fpaa=UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

											model.enable_xformers_memory_efficient_attention()
											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

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

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



				@parameterized.expand([(1_3,), (1_6,), (3_7,)])
				@require_torch_gpu
				@unittest.skipIf(not is_xformers_available()			,		reason="""xformers is not required when using PyTorch 2.0.""")
				def 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model()
											snake_case__	  =       self.get_sd_image(UpperCamelCase__			,		shape=(3, 4, 6_4, 6_4))

											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

											model.enable_xformers_memory_efficient_attention()
											with torch.no_grad():
																		snake_case__	  =       model.decode(UpperCamelCase__).sample

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

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



				@parameterized.expand(
				    [
				        # fmt: off
				        [3_3, [-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]],
				        [4_7, [-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 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    List[Any]):






											'''simple docstring'''







											snake_case__	  =       self.get_sd_vae_model()
											snake_case__	  =       self.get_sd_image(UpperCamelCase__)
											snake_case__	  =       self.get_generator(UpperCamelCase__)

											with torch.no_grad():
																		snake_case__	  =       model.encode(UpperCamelCase__).latent_dist
																		snake_case__	  =       dist.sample(generator=UpperCamelCase__)

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

											snake_case__	  =       sample[0, -1, -3:, -3:].flatten().cpu()
											snake_case__	  =       torch.tensor(UpperCamelCase__)

											snake_case__	  =       3E-3 if torch_device != """mps""" else 1E-2
											assert torch_all_close(UpperCamelCase__			,		UpperCamelCase__			,		atol=UpperCamelCase__)








import requests
from bsa import BeautifulSoup

def _UpperCAmelCase   (	a						:	str  ,					a						:	dict ):
							snake_case__	  =       BeautifulSoup(requests.get(a  ,					params=a ).content  ,					"""html.parser""" )
							snake_case__	  =       soup.find("""div"""  ,					attrs={"""class""": """gs_ri"""} )
							snake_case__	  =       div.find("""div"""  ,					attrs={"""class""": """gs_fl"""} ).find_all("""a""" )
							return anchors[2].get_text()


if __name__ == "__main__":
	a__       =     {
	    """title""": (
	        """Precisely geometry controlled microsupercapacitors for ultrahigh areal """
	        """capacitance, volumetric capacitance, and energy density"""
	    ),
	    """journal""": """Chem. Mater.""",
	    """volume""": 3_0,
	    """pages""": """3979-3990""",
	    """year""": 2_0_1_8,
	    """hl""": """en""",
	}
	print(get_citation("""https://scholar.google.com/scholar_lookup""", params=params))
