


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection

from diffusers import (
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    PNDMScheduler,
    StableDiffusionImageVariationPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class A						(     UpperCamelCase_					,							UpperCamelCase_					,							UpperCamelCase_					,							unittest.TestCase  ):
						UpperCamelCase__							:						Dict   =StableDiffusionImageVariationPipeline
						UpperCamelCase__							:						Any   =IMAGE_VARIATION_PARAMS
						UpperCamelCase__							:						Any   =IMAGE_VARIATION_BATCH_PARAMS
						UpperCamelCase__							:						int   =frozenset([]  )
						# TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
						UpperCamelCase__							:						Union[str, Any]   =frozenset([]  )






						def  lowerCamelCase (      self		:  Any				)      ->		Any:
													"""simple docstring"""




													torch.manual_seed(0				)
													_lowerCamelCase :	Optional[Any]						=UNetaDConditionModel(
													    block_out_channels=(32, 64)    ,     layers_per_block=2    ,     sample_size=32    ,     in_channels=4    ,     out_channels=4    ,     down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')    ,     up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')    ,     cross_attention_dim=32    ,     )
													_lowerCamelCase :	Union[str, Any]						=PNDMScheduler(skip_prk_steps=lowercase_				)
													torch.manual_seed(0				)
													_lowerCamelCase :	List[Any]						=AutoencoderKL(
													    block_out_channels=[32, 64]    ,     in_channels=3    ,     out_channels=3    ,     down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']    ,     up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']    ,     latent_channels=4    ,     )
													torch.manual_seed(0				)
													_lowerCamelCase :	int						=CLIPVisionConfig(
													    hidden_size=32    ,     projection_dim=32    ,     intermediate_size=37    ,     layer_norm_eps=1E-05    ,     num_attention_heads=4    ,     num_hidden_layers=5    ,     image_size=32    ,     patch_size=4    ,     )
													_lowerCamelCase :	int						=CLIPVisionModelWithProjection(lowercase_				)
													_lowerCamelCase :	str						=CLIPImageProcessor(crop_size=32    ,     size=32				)

													_lowerCamelCase :	Optional[Any]						={
													    'unet': unet,
													    'scheduler': scheduler,
													    'vae': vae,
													    'image_encoder': image_encoder,
													    'feature_extractor': feature_extractor,
													    'safety_checker': None,
													}
													return components






						def  lowerCamelCase (      self		:  Tuple    ,     lowercase_		:  Any    ,     lowercase_		:  List[str]=0				)      ->		Union[str, Any]:
													"""simple docstring"""




													_lowerCamelCase :	Optional[Any]						=floats_tensor((1, 3, 32, 32)    ,     rng=random.Random(lowercase_				)				)
													_lowerCamelCase :	str						=image.cpu().permute(0    ,     2    ,     3    ,     1				)[0]
													_lowerCamelCase :	Any						=Image.fromarray(np.uinta(lowercase_				)				).convert('RGB'				).resize((32, 32)				)
													if str(lowercase_				).startswith('mps'				):
																				_lowerCamelCase :	List[str]						=torch.manual_seed(lowercase_				)
													else:
																				_lowerCamelCase :	Union[str, Any]						=torch.Generator(device=lowercase_				).manual_seed(lowercase_				)
													_lowerCamelCase :	int						={
													    'image': image,
													    'generator': generator,
													    'num_inference_steps': 2,
													    'guidance_scale': 6.0,
													    'output_type': 'numpy',
													}
													return inputs






						def  lowerCamelCase (      self		:  Any				)      ->		int:
													"""simple docstring"""




													_lowerCamelCase :	Any						='cpu'  # ensure determinism for the device-dependent torch.Generator
													_lowerCamelCase :	Optional[int]						=self.get_dummy_components()
													_lowerCamelCase :	str						=StableDiffusionImageVariationPipeline(**lowercase_				)
													_lowerCamelCase :	str						=sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_lowerCamelCase :	Union[str, Any]						=self.get_dummy_inputs(lowercase_				)
													_lowerCamelCase :	str						=sd_pipe(**lowercase_				).images
													_lowerCamelCase :	Any						=image[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													_lowerCamelCase :	Optional[Any]						=np.array([0.5239, 0.5723, 0.4796, 0.5049, 0.5550, 0.4685, 0.5329, 0.4891, 0.4921]				)

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






						def  lowerCamelCase (      self		:  Tuple				)      ->		int:
													"""simple docstring"""




													_lowerCamelCase :	Optional[int]						='cpu'  # ensure determinism for the device-dependent torch.Generator
													_lowerCamelCase :	Optional[int]						=self.get_dummy_components()
													_lowerCamelCase :	Optional[Any]						=StableDiffusionImageVariationPipeline(**lowercase_				)
													_lowerCamelCase :	List[Any]						=sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_lowerCamelCase :	str						=self.get_dummy_inputs(lowercase_				)
													_lowerCamelCase :	List[Any]						=2 * [inputs['image']]
													_lowerCamelCase :	Dict						=sd_pipe(**lowercase_				)

													_lowerCamelCase :	Tuple						=output.images

													_lowerCamelCase :	int						=image[-1, -3:, -3:, -1]

													assert image.shape == (2, 64, 64, 3)
													_lowerCamelCase :	Optional[Any]						=np.array([0.6892, 0.5637, 0.5836, 0.5771, 0.6254, 0.6409, 0.5580, 0.5569, 0.5289]				)

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






						def  lowerCamelCase (      self		:  Dict				)      ->		Optional[Any]:
													"""simple docstring"""




													super().test_inference_batch_single_identical(expected_max_diff=3E-3				)




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






						def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		Dict:
													"""simple docstring"""




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






						def  lowerCamelCase (      self		:  Any    ,     lowercase_		:  List[str]    ,     lowercase_		:  str="cpu"    ,     lowercase_		:  List[Any]=torch.floataa    ,     lowercase_		:  str=0				)      ->		List[Any]:
													"""simple docstring"""




													_lowerCamelCase :	Dict						=torch.Generator(device=lowercase_				).manual_seed(lowercase_				)
													_lowerCamelCase :	Any						=load_image(
													    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
													    '/stable_diffusion_imgvar/input_image_vermeer.png'				)
													_lowerCamelCase :	str						=np.random.RandomState(lowercase_				).standard_normal((1, 4, 64, 64)				)
													_lowerCamelCase :	str						=torch.from_numpy(lowercase_				).to(device=lowercase_    ,     dtype=lowercase_				)
													_lowerCamelCase :	List[str]						={
													    'image': init_image,
													    'latents': latents,
													    'generator': generator,
													    'num_inference_steps': 3,
													    'guidance_scale': 7.5,
													    'output_type': 'numpy',
													}
													return inputs






						def  lowerCamelCase (      self		:  Dict				)      ->		str:
													"""simple docstring"""




													_lowerCamelCase :	Dict						=StableDiffusionImageVariationPipeline.from_pretrained(
													    'lambdalabs/sd-image-variations-diffusers'    ,     safety_checker=lowercase_				)
													_lowerCamelCase :	Optional[Any]						=sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_lowerCamelCase :	int						=self.get_inputs(lowercase_				)
													_lowerCamelCase :	str						=sd_pipe(**lowercase_				).images
													_lowerCamelCase :	List[Any]						=image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 512, 512, 3)
													_lowerCamelCase :	str						=np.array([0.84491, 0.90789, 0.75708, 0.78734, 0.83485, 0.70099, 0.66938, 0.68727, 0.61379]				)
													assert np.abs(image_slice - expected_slice				).max() < 6E-3






						def  lowerCamelCase (      self		:  List[Any]				)      ->		Optional[Any]:
													"""simple docstring"""




													_lowerCamelCase :	Any						=0

													def callback_fn(lowercase_		:  int    ,     lowercase_		:  int    ,     lowercase_		:  torch.FloatTensor				) -> None:
																				_lowerCamelCase :	Optional[int]						=True
																				nonlocal number_of_steps
																				number_of_steps += 1
																				if step == 1:
																											_lowerCamelCase :	str						=latents.detach().cpu().numpy()
																											assert latents.shape == (1, 4, 64, 64)
																											_lowerCamelCase :	Union[str, Any]						=latents[0, -3:, -3:, -1]
																											_lowerCamelCase :	List[str]						=np.array(
																											    [-0.1621, 0.2837, -0.7979, -0.1221, -1.3057, 0.7681, -2.1191, 0.0464, 1.6309]				)

																											assert np.abs(latents_slice.flatten() - expected_slice				).max() < 5E-2
																				elif step == 2:
																											_lowerCamelCase :	Dict						=latents.detach().cpu().numpy()
																											assert latents.shape == (1, 4, 64, 64)
																											_lowerCamelCase :	Tuple						=latents[0, -3:, -3:, -1]
																											_lowerCamelCase :	int						=np.array([0.6299, 1.7500, 1.1992, -2.1582, -1.8994, 0.7334, -0.7090, 1.0137, 1.5273]				)

																											assert np.abs(latents_slice.flatten() - expected_slice				).max() < 5E-2

													_lowerCamelCase :	Optional[Any]						=False

													_lowerCamelCase :	Union[str, Any]						=StableDiffusionImageVariationPipeline.from_pretrained(
													    'fusing/sd-image-variations-diffusers'    ,     safety_checker=lowercase_    ,     torch_dtype=torch.floataa    ,     )
													pipe.to(lowercase_				)
													pipe.set_progress_bar_config(disable=lowercase_				)
													pipe.enable_attention_slicing()

													_lowerCamelCase :	Dict						=self.get_inputs(lowercase_    ,     dtype=torch.floataa				)
													pipe(**lowercase_    ,     callback=lowercase_    ,     callback_steps=1				)
													assert callback_fn.has_been_called
													assert number_of_steps == inputs["num_inference_steps"]






						def  lowerCamelCase (      self		:  str				)      ->		Optional[int]:
													"""simple docstring"""




													torch.cuda.empty_cache()
													torch.cuda.reset_max_memory_allocated()
													torch.cuda.reset_peak_memory_stats()

													_lowerCamelCase :	Union[str, Any]						='fusing/sd-image-variations-diffusers'
													_lowerCamelCase :	Optional[Any]						=StableDiffusionImageVariationPipeline.from_pretrained(
													    lowercase_    ,     safety_checker=lowercase_    ,     torch_dtype=torch.floataa				)
													_lowerCamelCase :	int						=pipe.to(lowercase_				)
													pipe.set_progress_bar_config(disable=lowercase_				)
													pipe.enable_attention_slicing(1				)
													pipe.enable_sequential_cpu_offload()

													_lowerCamelCase :	Any						=self.get_inputs(lowercase_    ,     dtype=torch.floataa				)
													_lowerCamelCase :	List[Any]						=pipe(**lowercase_				)

													_lowerCamelCase :	List[Any]						=torch.cuda.max_memory_allocated()
													# make sure that less than 2.6 GB is allocated
													assert mem_bytes < 2.6 * 10**9




@nightly
@require_torch_gpu
class A						(     unittest.TestCase  ):






						def  lowerCamelCase (      self		:  int				)      ->		Optional[int]:
													"""simple docstring"""




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






						def  lowerCamelCase (      self		:  int    ,     lowercase_		:  Tuple    ,     lowercase_		:  int="cpu"    ,     lowercase_		:  Any=torch.floataa    ,     lowercase_		:  List[Any]=0				)      ->		Dict:
													"""simple docstring"""




													_lowerCamelCase :	Dict						=torch.Generator(device=lowercase_				).manual_seed(lowercase_				)
													_lowerCamelCase :	Union[str, Any]						=load_image(
													    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
													    '/stable_diffusion_imgvar/input_image_vermeer.png'				)
													_lowerCamelCase :	int						=np.random.RandomState(lowercase_				).standard_normal((1, 4, 64, 64)				)
													_lowerCamelCase :	Dict						=torch.from_numpy(lowercase_				).to(device=lowercase_    ,     dtype=lowercase_				)
													_lowerCamelCase :	str						={
													    'image': init_image,
													    'latents': latents,
													    'generator': generator,
													    'num_inference_steps': 50,
													    'guidance_scale': 7.5,
													    'output_type': 'numpy',
													}
													return inputs






						def  lowerCamelCase (      self		:  str				)      ->		List[str]:
													"""simple docstring"""




													_lowerCamelCase :	List[str]						=StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'				)
													sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_lowerCamelCase :	int						=self.get_inputs(lowercase_				)
													_lowerCamelCase :	int						=sd_pipe(**lowercase_				).images[0]

													_lowerCamelCase :	Tuple						=load_numpy(
													    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
													    '/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy'				)
													_lowerCamelCase :	str						=np.abs(expected_image - image				).max()
													assert max_diff < 1E-3






						def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		List[Any]:
													"""simple docstring"""




													_lowerCamelCase :	int						=StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'				)
													_lowerCamelCase :	List[str]						=DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config				)
													sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_lowerCamelCase :	Optional[Any]						=self.get_inputs(lowercase_				)
													_lowerCamelCase :	Any						=25
													_lowerCamelCase :	Optional[Any]						=sd_pipe(**lowercase_				).images[0]

													_lowerCamelCase :	List[str]						=load_numpy(
													    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
													    '/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy'				)
													_lowerCamelCase :	List[str]						=np.abs(expected_image - image				).max()
													assert max_diff < 1E-3






from collections.abc import Callable

lowerCamelCase		      = Callable[[float], float]  # type alias for a real -> real function




def 			a_   (					SCREAMING_SNAKE_CASE__      :   RealFunc	,    SCREAMING_SNAKE_CASE__      :   RealFunc	,    SCREAMING_SNAKE_CASE__      :   int	,    ):
       '''simple docstring'''





       _lowerCamelCase :	Tuple						=float(SCREAMING_SNAKE_CASE__  )
       while True:
              try:
                     _lowerCamelCase :	int						=prev_guess - function(SCREAMING_SNAKE_CASE__  ) / derivative(SCREAMING_SNAKE_CASE__  )
              except ZeroDivisionError:
                     raise ZeroDivisionError('Could not find root'  ) from None
              if abs(prev_guess - next_guess  ) < 10**-5:
                     return next_guess
              _lowerCamelCase :	Union[str, Any]						=next_guess




def 			a_   (					SCREAMING_SNAKE_CASE__      :   float  ):
       '''simple docstring'''





       return (x**3) - (2 * x) - 5




def 			a_   (					SCREAMING_SNAKE_CASE__      :   float  ):
       '''simple docstring'''





       return 3 * (x**2) - 2


if __name__ == "__main__":
 print(newton(f, fa, 3))


