


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


     def      UpperCAmelCase__	(      self							:     Any			)  ->      int:





      '''simple docstring'''






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

       UpperCamelCase__					:    List[str]          =   [t[-1] for t in os.walk(os.path.join(lowerCamelCase__							,	os.listdir(lowerCamelCase__			)[0]							,	'''snapshots'''			)			)]
       UpperCamelCase__					:    Optional[int]          =   [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       __magic_name__	(     unittest.TestCase):


     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[Any]          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''hf-internal-testing/tiny-stable-diffusion-pipe'''							,	safety_checker=lowerCamelCase__			)

      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__					:    str          =   jax.random.PRNGKey(0			)
      UpperCamelCase__					:    List[str]          =   4

      UpperCamelCase__					:    Tuple          =   jax.device_count()
      UpperCamelCase__					:    Union[str, Any]          =   num_samples * [prompt]
      UpperCamelCase__					:    Dict          =   pipeline.prepare_inputs(lowerCamelCase__			)

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

      UpperCamelCase__					:    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.151_4745			) < 1E-3
       assert np.abs(np.abs(lowerCamelCase__							,	dtype=np.floataa			).sum() - 49947.875			) < 5E-1

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


     def      UpperCAmelCase__	(      self							:     Dict			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''CompVis/stable-diffusion-v1-4'''							,	revision='''flax'''							,	safety_checker=lowerCamelCase__			)

      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__					:    str          =   jax.random.PRNGKey(0			)
      UpperCamelCase__					:    Any          =   50

      UpperCamelCase__					:    Optional[int]          =   jax.device_count()
      UpperCamelCase__					:    Any          =   num_samples * [prompt]
      UpperCamelCase__					:    str          =   pipeline.prepare_inputs(lowerCamelCase__			)

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

      UpperCamelCase__					:    List[str]          =   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.0565_2401)			) < 1E-3
       assert np.abs((np.abs(lowerCamelCase__							,	dtype=np.floataa			).sum() - 2383808.2)			) < 5E-1


     def      UpperCAmelCase__	(      self							:     Dict			)  ->      Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''CompVis/stable-diffusion-v1-4'''							,	revision='''bf16'''							,	dtype=jnp.bfloataa							,	safety_checker=lowerCamelCase__			)

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

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

      UpperCamelCase__					:    List[str]          =   jax.device_count()
      UpperCamelCase__					:    List[str]          =   num_samples * [prompt]
      UpperCamelCase__					:    List[Any]          =   pipeline.prepare_inputs(lowerCamelCase__			)

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

      UpperCamelCase__					:    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.0400_3906)			) < 1E-3
       assert np.abs((np.abs(lowerCamelCase__							,	dtype=np.floataa			).sum() - 2373516.75)			) < 5E-1


     def      UpperCAmelCase__	(      self							:     int			)  ->      int:





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''CompVis/stable-diffusion-v1-4'''							,	revision='''bf16'''							,	dtype=jnp.bfloataa			)

      UpperCamelCase__					:    Tuple          =   (
          '''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[Any]          =   jax.random.PRNGKey(0			)
      UpperCamelCase__					:    List[str]          =   50

      UpperCamelCase__					:    Optional[Any]          =   jax.device_count()
      UpperCamelCase__					:    Optional[int]          =   num_samples * [prompt]
      UpperCamelCase__					:    Dict          =   pipeline.prepare_inputs(lowerCamelCase__			)

      # shard inputs and rng
      UpperCamelCase__					:    Optional[int]          =   replicate(lowerCamelCase__			)
      UpperCamelCase__					:    str          =   jax.random.split(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    Union[str, Any]          =   shard(lowerCamelCase__			)

      UpperCamelCase__					:    Union[str, 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.0400_3906)			) < 1E-3
       assert np.abs((np.abs(lowerCamelCase__							,	dtype=np.floataa			).sum() - 2373516.75)			) < 5E-1


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





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   FlaxDDIMScheduler(
          beta_start=0.0_0085							,	beta_end=0.012							,	beta_schedule='''scaled_linear'''							,	set_alpha_to_one=lowerCamelCase__							,	steps_offset=1							,	)

      UpperCamelCase__					:    Tuple          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''CompVis/stable-diffusion-v1-4'''							,	revision='''bf16'''							,	dtype=jnp.bfloataa							,	scheduler=lowerCamelCase__							,	safety_checker=lowerCamelCase__							,	)
      UpperCamelCase__					:    List[Any]          =   scheduler.create_state()

      UpperCamelCase__					:    Union[str, Any]          =   scheduler_state

      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__					:    Tuple          =   jax.random.PRNGKey(0			)
      UpperCamelCase__					:    Optional[int]          =   50

      UpperCamelCase__					:    int          =   jax.device_count()
      UpperCamelCase__					:    Dict          =   num_samples * [prompt]
      UpperCamelCase__					:    str          =   pipeline.prepare_inputs(lowerCamelCase__			)

      # shard inputs and rng
      UpperCamelCase__					:    List[Any]          =   replicate(lowerCamelCase__			)
      UpperCamelCase__					:    Optional[Any]          =   jax.random.split(lowerCamelCase__							,	lowerCamelCase__			)
      UpperCamelCase__					:    Union[str, Any]          =   shard(lowerCamelCase__			)

      UpperCamelCase__					:    Dict          =   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_4504_3945)			) < 1E-3
       assert np.abs((np.abs(lowerCamelCase__							,	dtype=np.floataa			).sum() - 2347693.5)			) < 5E-1


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





      '''simple docstring'''






      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__					:    str          =   jax.device_count()
      UpperCamelCase__					:    Union[str, Any]          =   num_samples * [prompt]
      UpperCamelCase__					:    str          =   jax.random.split(jax.random.PRNGKey(0			)							,	lowerCamelCase__			)

      UpperCamelCase__					:    Dict          =   FlaxStableDiffusionPipeline.from_pretrained(
          '''CompVis/stable-diffusion-v1-4'''							,	revision='''bf16'''							,	dtype=jnp.bfloataa							,	safety_checker=lowerCamelCase__							,	)

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

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

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






import functools

def 			_a  (				SCREAMING_SNAKE_CASE	:				str    , SCREAMING_SNAKE_CASE	:				str     ):







 """simple docstring"""



 UpperCamelCase__					:    str          =   len(SCREAMING_SNAKE_CASE     )
 UpperCamelCase__					:    Optional[Any]          =   len(SCREAMING_SNAKE_CASE     )

 @functools.cache
 def min_distance(SCREAMING_SNAKE_CASE	:				int    , SCREAMING_SNAKE_CASE	:				int     ) -> int:
  # if first word index is overflow - delete all from the second word
  if indexa >= len_worda:
   return len_worda - indexa
  # if second word index is overflow - delete all from the first word
  if indexa >= len_worda:
   return len_worda - indexa
  UpperCamelCase__					:    Tuple          =   int(worda[indexa] != worda[indexa]     )  # current letters not identical
  return min(
      1 + min_distance(indexa + 1    , SCREAMING_SNAKE_CASE     )    , 1 + min_distance(SCREAMING_SNAKE_CASE    , indexa + 1     )    , diff + min_distance(indexa + 1    , indexa + 1     )    , )

 return min_distance(0    , 0     )


if __name__ == "__main__":
       import doctest

       doctest.testmod()


