



"""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 					a	(  unittest.TestCase       ):



  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					Dict:


     '''simple docstring'''




     with tempfile.TemporaryDirectory() as tmpdirname:
        # pipeline has Flax weights
        SCREAMING_SNAKE_CASE_: List[Any]     			=FlaxDiffusionPipeline.from_pretrained(
            """hf-internal-testing/tiny-stable-diffusion-pipe"""       ,		safety_checker=lowerCAmelCase       ,		cache_dir=lowerCAmelCase		)

        SCREAMING_SNAKE_CASE_: Tuple     			=[t[-1] for t in os.walk(os.path.join(lowerCAmelCase       ,		os.listdir(lowerCAmelCase		)[0]       ,		"""snapshots"""		)		)]
        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=[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 					a	(  unittest.TestCase       ):



  def 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """hf-internal-testing/tiny-stable-diffusion-pipe"""       ,		safety_checker=lowerCAmelCase		)

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

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: str     			=4

     SCREAMING_SNAKE_CASE_: Any     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: List[str]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=pipeline.prepare_inputs(lowerCAmelCase		)

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

     SCREAMING_SNAKE_CASE_: List[Any]     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).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_5_1_4_7_4_5		) < 1E-3
        assert np.abs(np.abs(lowerCAmelCase       ,		dtype=np.floataa		).sum() - 4_9947.875		) < 5E-1

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



  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""flax"""       ,		safety_checker=lowerCAmelCase		)

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

     SCREAMING_SNAKE_CASE_: Dict     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: List[str]     			=50

     SCREAMING_SNAKE_CASE_: str     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: str     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[str]     			=pipeline.prepare_inputs(lowerCAmelCase		)

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

     SCREAMING_SNAKE_CASE_: Optional[int]     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).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_5_6_5_2_4_0_1)		) < 1E-3
        assert np.abs((np.abs(lowerCAmelCase       ,		dtype=np.floataa		).sum() - 238_3808.2)		) < 5E-1



  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		safety_checker=lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: 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"""
     )

     SCREAMING_SNAKE_CASE_: int     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: List[Any]     			=50

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Tuple     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=pipeline.prepare_inputs(lowerCAmelCase		)

     # shard inputs and rng
     SCREAMING_SNAKE_CASE_: Dict     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: str     			=jax.random.split(lowerCAmelCase       ,		lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: int     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).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_4_0_0_3_9_0_6)		) < 1E-3
        assert np.abs((np.abs(lowerCAmelCase       ,		dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1



  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa		)

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

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

     SCREAMING_SNAKE_CASE_: List[str]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: List[Any]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: int     			=pipeline.prepare_inputs(lowerCAmelCase		)

     # shard inputs and rng
     SCREAMING_SNAKE_CASE_: Dict     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: str     			=jax.random.split(lowerCAmelCase       ,		lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Any     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).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_4_0_0_3_9_0_6)		) < 1E-3
        assert np.abs((np.abs(lowerCAmelCase       ,		dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1



  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=FlaxDDIMScheduler(
         beta_start=0.0_0_0_8_5       ,		beta_end=0.0_1_2       ,		beta_schedule="""scaled_linear"""       ,		set_alpha_to_one=lowerCAmelCase       ,		steps_offset=1       ,		)

     SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		scheduler=lowerCAmelCase       ,		safety_checker=lowerCAmelCase       ,		)
     SCREAMING_SNAKE_CASE_: str     			=scheduler.create_state()

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=scheduler_state

     SCREAMING_SNAKE_CASE_: 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"""
     )

     SCREAMING_SNAKE_CASE_: List[Any]     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Tuple     			=50

     SCREAMING_SNAKE_CASE_: Optional[int]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[Any]     			=pipeline.prepare_inputs(lowerCAmelCase		)

     # shard inputs and rng
     SCREAMING_SNAKE_CASE_: Dict     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=jax.random.split(lowerCAmelCase       ,		lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Tuple     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: List[Any]     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).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_4_5_0_4_3_9_4_5)		) < 1E-3
        assert np.abs((np.abs(lowerCAmelCase       ,		dtype=np.floataa		).sum() - 234_7693.5)		) < 5E-1





  def 	lowerCamelCase__       (						self :					int		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: 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"""
     )

     SCREAMING_SNAKE_CASE_: Tuple     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Optional[int]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[str]     			=jax.random.split(jax.random.PRNGKey(0		)       ,		lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Dict     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		safety_checker=lowerCAmelCase       ,		)

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

     # With memory efficient attention
     SCREAMING_SNAKE_CASE_: List[Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		safety_checker=lowerCAmelCase       ,		use_memory_efficient_attention=lowerCAmelCase       ,		)

     SCREAMING_SNAKE_CASE_: int     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: int     			=pipeline.prepare_inputs(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: str     			=shard(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=pipeline(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		jit=lowerCAmelCase		).images
     assert images_eff.shape == (num_samples, 1, 512, 512, 3)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=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








"""simple docstring"""
import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, 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       ):



  def 	lowerCamelCase__       (						self :					Dict		)     ->					str:


     '''simple docstring'''




     super().tearDown()
     gc.collect()



  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Dict     			=FlaxStableDiffusionPipeline.from_pretrained(
         """stabilityai/stable-diffusion-2"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		)

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			="""A painting of a squirrel eating a burger"""
     SCREAMING_SNAKE_CASE_: Any     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Dict     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[Any]     			=sd_pipe.prepare_inputs(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Dict     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.random.split(lowerCAmelCase       ,		jax.device_count()		)

     SCREAMING_SNAKE_CASE_: Tuple     			=sd_pipe(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		num_inference_steps=25       ,		jit=lowerCAmelCase		)[0]
     assert images.shape == (jax.device_count(), 1, 768, 768, 3)

     SCREAMING_SNAKE_CASE_: Dict     			=images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
     SCREAMING_SNAKE_CASE_: Dict     			=images[0, 253:256, 253:256, -1]

     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.asarray(jax.device_get(image_slice.flatten()		)		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2]		)
     print(f'''output_slice: {output_slice}'''		)
     assert jnp.abs(output_slice - expected_slice		).max() < 1E-2





  def 	lowerCamelCase__       (						self :					List[str]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			="""stabilityai/stable-diffusion-2"""
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCAmelCase       ,		subfolder="""scheduler"""		)
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[int]     			=FlaxStableDiffusionPipeline.from_pretrained(
         lowerCAmelCase       ,		scheduler=lowerCAmelCase       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=scheduler_params

     SCREAMING_SNAKE_CASE_: Tuple     			="""A painting of a squirrel eating a burger"""
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: List[Any]     			=sd_pipe.prepare_inputs(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Tuple     			=replicate(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=shard(lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: Any     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Any     			=jax.random.split(lowerCAmelCase       ,		jax.device_count()		)

     SCREAMING_SNAKE_CASE_: Tuple     			=sd_pipe(lowerCAmelCase       ,		lowerCAmelCase       ,		lowerCAmelCase       ,		num_inference_steps=25       ,		jit=lowerCAmelCase		)[0]
     assert images.shape == (jax.device_count(), 1, 768, 768, 3)

     SCREAMING_SNAKE_CASE_: str     			=images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]		)
     SCREAMING_SNAKE_CASE_: Any     			=images[0, 253:256, 253:256, -1]

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=jnp.asarray(jax.device_get(image_slice.flatten()		)		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7]		)
     print(f'''output_slice: {output_slice}'''		)
     assert jnp.abs(output_slice - expected_slice		).max() < 1E-2



