





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

		"""simple docstring"""


		def     lowercase		(    self			:		List[Any]					)							->						Union[str, Any]:
					# clean up the VRAM after each test
					super().tearDown()
					gc.collect()






		def     lowercase		(    self			:		Any					)							->						List[Any]:
					__lowerCAmelCase     ,     __lowerCAmelCase							     =     FlaxStableDiffusionPipeline.from_pretrained(
					    'stabilityai/stable-diffusion-2' ,  revision='bf16' ,  dtype=jnp.bfloataa ,  )

					__lowerCAmelCase							     =     'A painting of a squirrel eating a burger'
					__lowerCAmelCase							     =     jax.device_count()
					__lowerCAmelCase							     =     num_samples * [prompt]
					__lowerCAmelCase							     =     sd_pipe.prepare_inputs(lowerCAmelCase_					)

					__lowerCAmelCase							     =     replicate(lowerCAmelCase_					)
					__lowerCAmelCase							     =     shard(lowerCAmelCase_					)

					__lowerCAmelCase							     =     jax.random.PRNGKey(0					)
					__lowerCAmelCase							     =     jax.random.split(lowerCAmelCase_ ,  jax.device_count()					)

					__lowerCAmelCase							     =     sd_pipe(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  num_inference_steps=2_5 ,  jit=lowerCAmelCase_					)[0]
					assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

					__lowerCAmelCase							     =     images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]					)
					__lowerCAmelCase							     =     images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__lowerCAmelCase							     =     jnp.asarray(jax.device_get(image_slice.flatten()					)					)
					__lowerCAmelCase							     =     jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12]					)
					print(f"""output_slice: {output_slice}"""					)
					assert jnp.abs(output_slice - expected_slice					).max() < 1e-2






		def     lowercase		(    self			:		List[str]					)							->						Union[str, Any]:
					__lowerCAmelCase							     =     'stabilityai/stable-diffusion-2'
					__lowerCAmelCase     ,     __lowerCAmelCase							     =     FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCAmelCase_ ,  subfolder='scheduler'					)
					__lowerCAmelCase     ,     __lowerCAmelCase							     =     FlaxStableDiffusionPipeline.from_pretrained(
					    lowerCAmelCase_ ,  scheduler=lowerCAmelCase_ ,  revision='bf16' ,  dtype=jnp.bfloataa ,  )
					__lowerCAmelCase							     =     scheduler_params

					__lowerCAmelCase							     =     'A painting of a squirrel eating a burger'
					__lowerCAmelCase							     =     jax.device_count()
					__lowerCAmelCase							     =     num_samples * [prompt]
					__lowerCAmelCase							     =     sd_pipe.prepare_inputs(lowerCAmelCase_					)

					__lowerCAmelCase							     =     replicate(lowerCAmelCase_					)
					__lowerCAmelCase							     =     shard(lowerCAmelCase_					)

					__lowerCAmelCase							     =     jax.random.PRNGKey(0					)
					__lowerCAmelCase							     =     jax.random.split(lowerCAmelCase_ ,  jax.device_count()					)

					__lowerCAmelCase							     =     sd_pipe(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  num_inference_steps=2_5 ,  jit=lowerCAmelCase_					)[0]
					assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

					__lowerCAmelCase							     =     images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]					)
					__lowerCAmelCase							     =     images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

					__lowerCAmelCase							     =     jnp.asarray(jax.device_get(image_slice.flatten()					)					)
					__lowerCAmelCase							     =     jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97]					)
					print(f"""output_slice: {output_slice}"""					)
					assert jnp.abs(output_slice - expected_slice					).max() < 1e-2








from functools import lru_cache

@lru_cache
def   a_   (			lowerCAmelCase_				:      int     ):
   if num < 0:
      raise ValueError('Number should not be negative.'     )

   return 1 if num in (0, 1) else num * factorial(num - 1     )


if __name__ == "__main__":
     import doctest

     doctest.testmod()

