import gc
import random
import unittest

import numpy as np
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModel,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    floats_tensor,
    load_image,
    load_numpy,
    require_torch_gpu,
    skip_mps,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
    assert_mean_pixel_difference,
)


enable_full_determinism()







class __snake_case				(						A__   ,		A__   ,		A__   ,		unittest.TestCase		):
   lowerCAmelCase_														=							StableUnCLIPImgaImgPipeline
   lowerCAmelCase_														=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS
   lowerCAmelCase_														=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
   lowerCAmelCase_														=							frozenset(
       []		)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
   lowerCAmelCase_														=							frozenset([]		)




   def 	__a						(    self			:				int      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      32
     SCREAMING_SNAKE_CASE__										=      embedder_hidden_size

     # image encoding components

     SCREAMING_SNAKE_CASE__										=      CLIPImageProcessor(crop_size=32       ,	size=32      )

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      CLIPVisionModelWithProjection(
         CLIPVisionConfig(
             hidden_size=__snake_case       ,	projection_dim=__snake_case       ,	num_hidden_layers=5       ,	num_attention_heads=4       ,	image_size=32       ,	intermediate_size=37       ,	patch_size=1       ,	)      )

     # regular denoising components

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      StableUnCLIPImageNormalizer(embedding_dim=__snake_case      )
     SCREAMING_SNAKE_CASE__										=      DDPMScheduler(beta_schedule="""squaredcos_cap_v2"""      )

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""      )

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      CLIPTextModel(
         CLIPTextConfig(
             bos_token_id=0       ,	eos_token_id=2       ,	hidden_size=__snake_case       ,	projection_dim=32       ,	intermediate_size=37       ,	layer_norm_eps=1E-05       ,	num_attention_heads=4       ,	num_hidden_layers=5       ,	pad_token_id=1       ,	vocab_size=10_00       ,	)      )

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      UNetaDConditionModel(
         sample_size=32       ,	in_channels=4       ,	out_channels=4       ,	down_block_types=("""CrossAttnDownBlock2D""", """DownBlock2D""")       ,	up_block_types=("""UpBlock2D""", """CrossAttnUpBlock2D""")       ,	block_out_channels=(32, 64)       ,	attention_head_dim=(2, 4)       ,	class_embed_type="""projection"""       ,	projection_class_embeddings_input_dim=embedder_projection_dim * 2       ,	cross_attention_dim=__snake_case       ,	layers_per_block=1       ,	upcast_attention=__snake_case       ,	use_linear_projection=__snake_case       ,	)

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      DDIMScheduler(
         beta_schedule="""scaled_linear"""       ,	beta_start=0.0_00_85       ,	beta_end=0.0_12       ,	prediction_type="""v_prediction"""       ,	set_alpha_to_one=__snake_case       ,	steps_offset=1       ,	)

     torch.manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      AutoencoderKL()

     SCREAMING_SNAKE_CASE__										=      {
         # image encoding components
         '''feature_extractor''': feature_extractor,
         '''image_encoder''': image_encoder.eval(),
         # image noising components
         '''image_normalizer''': image_normalizer.eval(),
         '''image_noising_scheduler''': image_noising_scheduler,
         # regular denoising components
         '''tokenizer''': tokenizer,
         '''text_encoder''': text_encoder.eval(),
         '''unet''': unet.eval(),
         '''scheduler''': scheduler,
         '''vae''': vae.eval(),
     }

     return components




   def 	__a						(    self			:				Dict       ,	_lowercase			:				int       ,	_lowercase			:				Union[str, Any]=0       ,	_lowercase			:				Union[str, Any]=True      ):



     """simple docstring"""






     if str(__snake_case      ).startswith("""mps"""      ):
       SCREAMING_SNAKE_CASE__										=      torch.manual_seed(__snake_case      )
     else:
       SCREAMING_SNAKE_CASE__										=      torch.Generator(device=__snake_case      ).manual_seed(__snake_case      )

     SCREAMING_SNAKE_CASE__										=      floats_tensor((1, 3, 32, 32)       ,	rng=random.Random(__snake_case      )      ).to(__snake_case      )

     if pil_image:
       SCREAMING_SNAKE_CASE__										=      input_image * 0.5 + 0.5
       SCREAMING_SNAKE_CASE__										=      input_image.clamp(0       ,	1      )
       SCREAMING_SNAKE_CASE__										=      input_image.cpu().permute(0       ,	2       ,	3       ,	1      ).float().numpy()
       SCREAMING_SNAKE_CASE__										=      DiffusionPipeline.numpy_to_pil(__snake_case      )[0]

     return {
         "prompt": "An anime racoon running a marathon",
         "image": input_image,
         "generator": generator,
         "num_inference_steps": 2,
         "output_type": "np",
     }




   @skip_mps
   def 	__a						(    self			:				List[str]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     SCREAMING_SNAKE_CASE__										=      self.get_dummy_components()
     SCREAMING_SNAKE_CASE__										=      StableUnCLIPImgaImgPipeline(**__snake_case      )
     SCREAMING_SNAKE_CASE__										=      sd_pipe.to(__snake_case      )
     sd_pipe.set_progress_bar_config(disable=__snake_case      )

     SCREAMING_SNAKE_CASE__										=      self.get_dummy_inputs(__snake_case      )
     inputs.update({"""image_embeds""": None}      )
     SCREAMING_SNAKE_CASE__										=      sd_pipe(**__snake_case      ).images
     SCREAMING_SNAKE_CASE__										=      image[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     SCREAMING_SNAKE_CASE__										=      np.array([0.38_72, 0.72_24, 0.56_01, 0.47_41, 0.68_72, 0.58_14, 0.46_36, 0.38_67, 0.50_78]      )

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




   def 	__a						(    self			:				Optional[int]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      torch_device in ['''cpu''', '''mps''']

     self._test_attention_slicing_forward_pass(test_max_difference=__snake_case      )




   def 	__a						(    self			:				Any      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      torch_device in ['''cpu''', '''mps''']

     self._test_inference_batch_single_identical(test_max_difference=__snake_case      )




   @unittest.skipIf(
       torch_device != """cuda""" or not is_xformers_available()       ,	reason="""XFormers attention is only available with CUDA and `xformers` installed"""       ,	)
   def 	__a						(    self			:				Optional[Any]      ):



     """simple docstring"""






     self._test_xformers_attention_forwardGenerator_pass(test_max_difference=__snake_case      )







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




   def 	__a						(    self			:				int      ):



     """simple docstring"""






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




   def 	__a						(    self			:				List[str]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      load_image(
         """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"""      )

     SCREAMING_SNAKE_CASE__										=      load_numpy(
         """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy"""      )

     SCREAMING_SNAKE_CASE__										=      StableUnCLIPImgaImgPipeline.from_pretrained(
         """fusing/stable-unclip-2-1-l-img2img"""       ,	torch_dtype=torch.floataa      )
     pipe.to(__snake_case      )
     pipe.set_progress_bar_config(disable=__snake_case      )
     # stable unclip will oom when integration tests are run on a V100,
     # so turn on memory savings
     pipe.enable_attention_slicing()
     pipe.enable_sequential_cpu_offload()

     SCREAMING_SNAKE_CASE__										=      torch.Generator(device="""cpu"""      ).manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      pipe(__snake_case       ,	"""anime turle"""       ,	generator=__snake_case       ,	output_type="""np"""      )

     SCREAMING_SNAKE_CASE__										=      output.images[0]

     assert image.shape == (7_68, 7_68, 3)

     assert_mean_pixel_difference(__snake_case       ,	__snake_case      )




   def 	__a						(    self			:				Optional[int]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      load_image(
         """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"""      )

     SCREAMING_SNAKE_CASE__										=      load_numpy(
         """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy"""      )

     SCREAMING_SNAKE_CASE__										=      StableUnCLIPImgaImgPipeline.from_pretrained(
         """fusing/stable-unclip-2-1-h-img2img"""       ,	torch_dtype=torch.floataa      )
     pipe.to(__snake_case      )
     pipe.set_progress_bar_config(disable=__snake_case      )
     # stable unclip will oom when integration tests are run on a V100,
     # so turn on memory savings
     pipe.enable_attention_slicing()
     pipe.enable_sequential_cpu_offload()

     SCREAMING_SNAKE_CASE__										=      torch.Generator(device="""cpu"""      ).manual_seed(0      )
     SCREAMING_SNAKE_CASE__										=      pipe(__snake_case       ,	"""anime turle"""       ,	generator=__snake_case       ,	output_type="""np"""      )

     SCREAMING_SNAKE_CASE__										=      output.images[0]

     assert image.shape == (7_68, 7_68, 3)

     assert_mean_pixel_difference(__snake_case       ,	__snake_case      )




   def 	__a						(    self			:				Union[str, Any]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      load_image(
         """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"""      )

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

     SCREAMING_SNAKE_CASE__										=      StableUnCLIPImgaImgPipeline.from_pretrained(
         """fusing/stable-unclip-2-1-h-img2img"""       ,	torch_dtype=torch.floataa      )
     SCREAMING_SNAKE_CASE__										=      pipe.to(__snake_case      )
     pipe.set_progress_bar_config(disable=__snake_case      )
     pipe.enable_attention_slicing()
     pipe.enable_sequential_cpu_offload()

     SCREAMING_SNAKE_CASE__										=      pipe(
         __snake_case       ,	"""anime turtle"""       ,	num_inference_steps=2       ,	output_type="""np"""       ,	)

     SCREAMING_SNAKE_CASE__										=      torch.cuda.max_memory_allocated()
     # make sure that less than 7 GB is allocated
     assert mem_bytes < 7 * 10**9





'''simple docstring'''






import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


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

						from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline

@require_flax
class 				SCREAMING_SNAKE_CASE(							unittest.TestCase			):







	"""simple docstring"""






	def      A	(						self			:     str		)			->		int:
								with tempfile.TemporaryDirectory() as tmpdirname:
															# pipeline has Flax weights
															UpperCAmelCase :       Optional[Any] 							=		FlaxDiffusionPipeline.from_pretrained(
															    '''hf-internal-testing/tiny-stable-diffusion-pipe'''		,			safety_checker=__snake_case		,			cache_dir=__snake_case		)

															UpperCAmelCase :       str 							=		[t[-1] for t in os.walk(os.path.join(__snake_case		,			os.listdir(__snake_case		)[0]		,			'''snapshots'''		)		)]
															UpperCAmelCase :       str 							=		[item for sublist in all_root_files for item in sublist]

															# None of the downloaded files should be a PyTorch file even if we have some here:
															# https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
															assert not any(f.endswith('''.bin'''		) for f in files		)

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







	"""simple docstring"""






	def      A	(						self			:     List[str]		)			->		Dict:
								UpperCAmelCase							,		UpperCAmelCase :       str 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''hf-internal-testing/tiny-stable-diffusion-pipe'''		,			safety_checker=__snake_case		)

								UpperCAmelCase :       List[Any] 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       List[str] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Optional[Any] 							=		4

								UpperCAmelCase :       Optional[Any] 							=		jax.device_count()
								UpperCAmelCase :       Tuple 							=		num_samples * [prompt]
								UpperCAmelCase :       int 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       Any 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[int] 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images

								assert images.shape == (num_samples, 1, 64, 64, 3)
								if jax.device_count() == 8:
															assert np.abs(np.abs(images[0, 0, :2, :2, -2:]		,			dtype=np.floataa		).sum() - 4.1_51_47_45		) < 1E-3
															assert np.abs(np.abs(__snake_case		,			dtype=np.floataa		).sum() - 4_99_47.8_75		) < 5E-1

								UpperCAmelCase :       Union[str, Any] 							=		pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
								assert len(__snake_case		) == num_samples







	def      A	(						self			:     List[Any]		)			->		List[str]:
								UpperCAmelCase							,		UpperCAmelCase :       Union[str, Any] 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''flax'''		,			safety_checker=__snake_case		)

								UpperCAmelCase :       Dict 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       Optional[Any] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Any 							=		50

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       int 							=		num_samples * [prompt]
								UpperCAmelCase :       Union[str, Any] 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Dict 							=		replicate(__snake_case		)
								UpperCAmelCase :       int 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Tuple 							=		shard(__snake_case		)

								UpperCAmelCase :       Tuple 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images

								assert images.shape == (num_samples, 1, 512, 512, 3)
								if jax.device_count() == 8:
															assert np.abs((np.abs(images[0, 0, :2, :2, -2:]		,			dtype=np.floataa		).sum() - 0.05_65_24_01)		) < 1E-3
															assert np.abs((np.abs(__snake_case		,			dtype=np.floataa		).sum() - 2_38_38_08.2)		) < 5E-1







	def      A	(						self			:     int		)			->		Dict:
								UpperCAmelCase							,		UpperCAmelCase :       List[str] 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			safety_checker=__snake_case		)

								UpperCAmelCase :       Dict 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       Union[str, Any] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       List[str] 							=		50

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       List[Any] 							=		num_samples * [prompt]
								UpperCAmelCase :       int 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       List[Any] 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Optional[int] 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[Any] 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images

								assert images.shape == (num_samples, 1, 512, 512, 3)
								if jax.device_count() == 8:
															assert np.abs((np.abs(images[0, 0, :2, :2, -2:]		,			dtype=np.floataa		).sum() - 0.04_00_39_06)		) < 1E-3
															assert np.abs((np.abs(__snake_case		,			dtype=np.floataa		).sum() - 2_37_35_16.75)		) < 5E-1







	def      A	(						self			:     int		)			->		Any:
								UpperCAmelCase							,		UpperCAmelCase :       Dict 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		)

								UpperCAmelCase :       List[str] 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       List[str] 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Union[str, Any] 							=		50

								UpperCAmelCase :       Optional[int] 							=		jax.device_count()
								UpperCAmelCase :       List[str] 							=		num_samples * [prompt]
								UpperCAmelCase :       Dict 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       Tuple 							=		replicate(__snake_case		)
								UpperCAmelCase :       Any 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       str 							=		shard(__snake_case		)

								UpperCAmelCase :       Optional[int] 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images

								assert images.shape == (num_samples, 1, 512, 512, 3)
								if jax.device_count() == 8:
															assert np.abs((np.abs(images[0, 0, :2, :2, -2:]		,			dtype=np.floataa		).sum() - 0.04_00_39_06)		) < 1E-3
															assert np.abs((np.abs(__snake_case		,			dtype=np.floataa		).sum() - 2_37_35_16.75)		) < 5E-1







	def      A	(						self			:     Tuple		)			->		Optional[Any]:
								UpperCAmelCase :       int 							=		FlaxDDIMScheduler(
								    beta_start=0.0_00_85		,			beta_end=0.0_12		,			beta_schedule='''scaled_linear'''		,			set_alpha_to_one=__snake_case		,			steps_offset=1		,			)

								UpperCAmelCase							,		UpperCAmelCase :       Tuple 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			scheduler=__snake_case		,			safety_checker=__snake_case		,			)
								UpperCAmelCase :       Tuple 							=		scheduler.create_state()

								UpperCAmelCase :       Dict 							=		scheduler_state

								UpperCAmelCase :       str 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       int 							=		jax.random.PRNGKey(0		)
								UpperCAmelCase :       Union[str, Any] 							=		50

								UpperCAmelCase :       Optional[Any] 							=		jax.device_count()
								UpperCAmelCase :       Any 							=		num_samples * [prompt]
								UpperCAmelCase :       Dict 							=		pipeline.prepare_inputs(__snake_case		)

								# shard inputs and rng
								UpperCAmelCase :       str 							=		replicate(__snake_case		)
								UpperCAmelCase :       List[str] 							=		jax.random.split(__snake_case		,			__snake_case		)
								UpperCAmelCase :       Optional[int] 							=		shard(__snake_case		)

								UpperCAmelCase :       Dict 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images

								assert images.shape == (num_samples, 1, 512, 512, 3)
								if jax.device_count() == 8:
															assert np.abs((np.abs(images[0, 0, :2, :2, -2:]		,			dtype=np.floataa		).sum() - 0.0_45_04_39_45)		) < 1E-3
															assert np.abs((np.abs(__snake_case		,			dtype=np.floataa		).sum() - 2_34_76_93.5)		) < 5E-1







	def      A	(						self			:     Any		)			->		Tuple:
								UpperCAmelCase :       List[Any] 							=		(
								    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
								    ''' field, close up, split lighting, cinematic'''
								)

								UpperCAmelCase :       Union[str, Any] 							=		jax.device_count()
								UpperCAmelCase :       List[Any] 							=		num_samples * [prompt]
								UpperCAmelCase :       str 							=		jax.random.split(jax.random.PRNGKey(0		)		,			__snake_case		)

								UpperCAmelCase							,		UpperCAmelCase :       Tuple 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			safety_checker=__snake_case		,			)

								UpperCAmelCase :       Dict 							=		replicate(__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		pipeline.prepare_inputs(__snake_case		)
								UpperCAmelCase :       List[str] 							=		shard(__snake_case		)
								UpperCAmelCase :       Any 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images
								assert images.shape == (num_samples, 1, 512, 512, 3)
								UpperCAmelCase :       Optional[int] 							=		images[2, 0, 256, 10:17, 1]

								# With memory efficient attention
								UpperCAmelCase							,		UpperCAmelCase :       Any 							=		FlaxStableDiffusionPipeline.from_pretrained(
								    '''CompVis/stable-diffusion-v1-4'''		,			revision='''bf16'''		,			dtype=jnp.bfloataa		,			safety_checker=__snake_case		,			use_memory_efficient_attention=__snake_case		,			)

								UpperCAmelCase :       int 							=		replicate(__snake_case		)
								UpperCAmelCase :       int 							=		pipeline.prepare_inputs(__snake_case		)
								UpperCAmelCase :       List[Any] 							=		shard(__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		pipeline(__snake_case		,			__snake_case		,			__snake_case		,			jit=__snake_case		).images
								assert images_eff.shape == (num_samples, 1, 512, 512, 3)
								UpperCAmelCase :       int 							=		images[2, 0, 256, 10:17, 1]

								# I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
								# over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
								assert abs(slice_eff - slice		).max() < 1E-2





