






'''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    _UpperCAmelCase			(     unittest.TestCase						):




   """simple docstring"""

   def 						lowerCAmelCase (		self :	int		):





         '''simple docstring'''





         with tempfile.TemporaryDirectory() as tmpdirname:
               # pipeline has Flax weights
               _A			     =	FlaxDiffusionPipeline.from_pretrained(
                   "hf-internal-testing/tiny-stable-diffusion-pipe"      ,      safety_checker=__UpperCAmelCase      ,      cache_dir=__UpperCAmelCase		)

               _A			     =	[t[-1] for t in os.walk(os.path.join(__UpperCAmelCase      ,      os.listdir(__UpperCAmelCase		)[0]      ,      "snapshots"		)		)]
               _A			     =	[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    _UpperCAmelCase			(     unittest.TestCase						):




   """simple docstring"""

   def 						lowerCAmelCase (		self :	Tuple		):





         '''simple docstring'''





         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "hf-internal-testing/tiny-stable-diffusion-pipe"      ,      safety_checker=__UpperCAmelCase		)

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

         _A			     =	jax.random.PRNGKey(0		)
         _A			     =	4

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)

         # shard inputs and rng
         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	jax.random.split(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)

         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).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.1514745		) < 1E-3
               assert np.abs(np.abs(__UpperCAmelCase      ,      dtype=np.floataa		).sum() - 49947.875		) < 5E-1

         _A			     =	pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
         assert len(__UpperCAmelCase		) == num_samples

   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="flax"      ,      safety_checker=__UpperCAmelCase		)

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

         _A			     =	jax.random.PRNGKey(0		)
         _A			     =	50

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)

         # shard inputs and rng
         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	jax.random.split(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)

         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).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.05652401)		) < 1E-3
               assert np.abs((np.abs(__UpperCAmelCase      ,      dtype=np.floataa		).sum() - 2383808.2)		) < 5E-1

   def 						lowerCAmelCase (		self :	str		):





         '''simple docstring'''





         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="bf16"      ,      dtype=jnp.bfloataa      ,      safety_checker=__UpperCAmelCase		)

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

         _A			     =	jax.random.PRNGKey(0		)
         _A			     =	50

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)

         # shard inputs and rng
         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	jax.random.split(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)

         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).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.04003906)		) < 1E-3
               assert np.abs((np.abs(__UpperCAmelCase      ,      dtype=np.floataa		).sum() - 2373516.75)		) < 5E-1

   def 						lowerCAmelCase (		self :	Dict		):





         '''simple docstring'''





         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="bf16"      ,      dtype=jnp.bfloataa		)

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

         _A			     =	jax.random.PRNGKey(0		)
         _A			     =	50

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)

         # shard inputs and rng
         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	jax.random.split(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)

         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).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.04003906)		) < 1E-3
               assert np.abs((np.abs(__UpperCAmelCase      ,      dtype=np.floataa		).sum() - 2373516.75)		) < 5E-1

   def 						lowerCAmelCase (		self :	Any		):





         '''simple docstring'''





         _A			     =	FlaxDDIMScheduler(
             beta_start=0.00085      ,      beta_end=0.012      ,      beta_schedule="scaled_linear"      ,      set_alpha_to_one=__UpperCAmelCase      ,      steps_offset=1      ,      )

         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="bf16"      ,      dtype=jnp.bfloataa      ,      scheduler=__UpperCAmelCase      ,      safety_checker=__UpperCAmelCase      ,      )
         _A			     =	scheduler.create_state()

         _A			     =	scheduler_state

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

         _A			     =	jax.random.PRNGKey(0		)
         _A			     =	50

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)

         # shard inputs and rng
         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	jax.random.split(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)

         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).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.045043945)		) < 1E-3
               assert np.abs((np.abs(__UpperCAmelCase      ,      dtype=np.floataa		).sum() - 2347693.5)		) < 5E-1

   def 						lowerCAmelCase (		self :	Tuple		):





         '''simple docstring'''





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

         _A			     =	jax.device_count()
         _A			     =	num_samples * [prompt]
         _A			     =	jax.random.split(jax.random.PRNGKey(0		)      ,      __UpperCAmelCase		)

         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="bf16"      ,      dtype=jnp.bfloataa      ,      safety_checker=__UpperCAmelCase      ,      )

         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)
         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).images
         assert images.shape == (num_samples, 1, 512, 512, 3)
         _A			     =	images[2, 0, 256, 10:17, 1]

         # With memory efficient attention
         _A     ,	_A			     =	FlaxStableDiffusionPipeline.from_pretrained(
             "CompVis/stable-diffusion-v1-4"      ,      revision="bf16"      ,      dtype=jnp.bfloataa      ,      safety_checker=__UpperCAmelCase      ,      use_memory_efficient_attention=__UpperCAmelCase      ,      )

         _A			     =	replicate(__UpperCAmelCase		)
         _A			     =	pipeline.prepare_inputs(__UpperCAmelCase		)
         _A			     =	shard(__UpperCAmelCase		)
         _A			     =	pipeline(__UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      jit=__UpperCAmelCase		).images
         assert images_eff.shape == (num_samples, 1, 512, 512, 3)
         _A			     =	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'''







def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: int							):



						"""simple docstring"""





						lowercase_	:     Optional[int]													=       int(__SCREAMING_SNAKE_CASE							)
						if decimal in (0, 1):  # Exit cases for the recursion
												return str(__SCREAMING_SNAKE_CASE							)
						lowercase_						, lowercase_	:     List[str]													=       divmod(__SCREAMING_SNAKE_CASE      ,      2							)
						return binary_recursive(__SCREAMING_SNAKE_CASE							) + str(__SCREAMING_SNAKE_CASE							)
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: str							):



						"""simple docstring"""





						lowercase_	:     str													=       str(__SCREAMING_SNAKE_CASE							).strip()
						if not number:
												raise ValueError('''No input value was provided'''							)
						lowercase_	:     Optional[int]													=       '''-''' if number.startswith('''-'''							) else ''''''
						lowercase_	:     Union[str, Any]													=       number.lstrip('''-'''							)
						if not number.isnumeric():
												raise ValueError('''Input value is not an integer'''							)
						return F'''{negative}0b{binary_recursive(int(__SCREAMING_SNAKE_CASE							)							)}'''


if __name__ == "__main__":
							from doctest import testmod

							testmod()



