


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







     def 							lowercase__       (						self :						Any      ):
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()







     def 							lowercase__       (						self :						List[Any]      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		FlaxStableDiffusionPipeline.from_pretrained(
               '''stabilityai/stable-diffusion-2'''   ,   revision='''bf16'''   ,   dtype=jnp.bfloataa   ,   )

           SCREAMING_SNAKE_CASE__       :  Dict						     =		'''A painting of a squirrel eating a burger'''
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		jax.device_count()
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		num_samples * [prompt]
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		sd_pipe.prepare_inputs(_lowercase      )

           SCREAMING_SNAKE_CASE__       :  int						     =		replicate(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  str						     =		shard(_lowercase      )

           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		jax.random.PRNGKey(0      )
           SCREAMING_SNAKE_CASE__       :  Any						     =		jax.random.split(_lowercase   ,   jax.device_count()      )

           SCREAMING_SNAKE_CASE__       :  List[str]						     =		sd_pipe(_lowercase   ,   _lowercase   ,   _lowercase   ,   num_inference_steps=25   ,   jit=_lowercase      )[0]
           assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		images[0, 2_53:2_56, 2_53:2_56, -1]

           SCREAMING_SNAKE_CASE__       :  Tuple						     =		jnp.asarray(jax.device_get(image_slice.flatten()      )      )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512]      )
           print(f"""output_slice: {output_slice}"""      )
           assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2







     def 							lowercase__       (						self :						Optional[int]      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		'''stabilityai/stable-diffusion-2'''
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		FlaxDPMSolverMultistepScheduler.from_pretrained(_lowercase   ,   subfolder='''scheduler'''      )
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		FlaxStableDiffusionPipeline.from_pretrained(
               _lowercase   ,   scheduler=_lowercase   ,   revision='''bf16'''   ,   dtype=jnp.bfloataa   ,   )
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		scheduler_params

           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		'''A painting of a squirrel eating a burger'''
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		jax.device_count()
           SCREAMING_SNAKE_CASE__       :  str						     =		num_samples * [prompt]
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		sd_pipe.prepare_inputs(_lowercase      )

           SCREAMING_SNAKE_CASE__       :  str						     =		replicate(_lowercase      )
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		shard(_lowercase      )

           SCREAMING_SNAKE_CASE__       :  Any						     =		jax.random.PRNGKey(0      )
           SCREAMING_SNAKE_CASE__       :  int						     =		jax.random.split(_lowercase   ,   jax.device_count()      )

           SCREAMING_SNAKE_CASE__       :  int						     =		sd_pipe(_lowercase   ,   _lowercase   ,   _lowercase   ,   num_inference_steps=25   ,   jit=_lowercase      )[0]
           assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

           SCREAMING_SNAKE_CASE__       :  str						     =		images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
           SCREAMING_SNAKE_CASE__       :  str						     =		images[0, 2_53:2_56, 2_53:2_56, -1]

           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		jnp.asarray(jax.device_get(image_slice.flatten()      )      )
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297]      )
           print(f"""output_slice: {output_slice}"""      )
           assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2











'''simple docstring'''





def lowerCAmelCase    (__A):



     """simple docstring"""



     if not isinstance(__A    ,      __A):
          raise ValueError('''multiplicative_persistence() only accepts integral values''')
     if num < 0:
          raise ValueError('''multiplicative_persistence() does not accept negative values''')

     _a           =    0
     _a           =    str(__A)

     while len(__A) != 1:
          _a           =    [int(__A) for i in num_string]

          _a           =    1
          for i in range(0    ,      len(__A)):
               total *= numbers[i]

          _a           =    str(__A)

          steps += 1
     return steps




def lowerCAmelCase    (__A):



     """simple docstring"""



     if not isinstance(__A    ,      __A):
          raise ValueError('''additive_persistence() only accepts integral values''')
     if num < 0:
          raise ValueError('''additive_persistence() does not accept negative values''')

     _a           =    0
     _a           =    str(__A)

     while len(__A) != 1:
          _a           =    [int(__A) for i in num_string]

          _a           =    0
          for i in range(0    ,      len(__A)):
               total += numbers[i]

          _a           =    str(__A)

          steps += 1
     return steps


if __name__ == "__main__":
  import doctest

  doctest.testmod()



