"""simple docstring"""







import pickle
import unittest

import torch

from accelerate import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils import require_cpu







@require_cpu
class  UpperCamelCase	(    unittest.TestCase						):
   def     _UpperCAmelCase     (    self    )			->   Any:


        '''simple docstring'''


        lowercase_			:       int									=			torch.nn.Linear(10  ,10    )
        lowercase_			:       int									=			torch.optim.SGD(model.parameters()  ,0.1    )
        lowercase_			:       int									=			Accelerator()
        lowercase_			:       Union[str, Any]									=			accelerator.prepare(__UpperCamelCase    )
        try:
             pickle.loads(pickle.dumps(__UpperCamelCase    )    )
        except Exception as e:
             self.fail(f'''Accelerated optimizer pickling failed with {e}'''    )
        AcceleratorState._reset_state()


"""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						):
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


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

             lowercase_			:       Dict									=			[t[-1] for t in os.walk(os.path.join(__UpperCamelCase  ,os.listdir(__UpperCamelCase    )[0]  ,'snapshots'    )    )]
             lowercase_			:       Tuple									=			[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						):
   def     _UpperCAmelCase     (    self    )			->   Optional[int]:


        '''simple docstring'''


        lowercase_							,						lowercase_			:       List[Any]									=			FlaxStableDiffusionPipeline.from_pretrained(
            'hf-internal-testing/tiny-stable-diffusion-pipe'  ,safety_checker=__UpperCamelCase    )

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

        lowercase_			:       Optional[Any]									=			jax.random.PRNGKey(0    )
        lowercase_			:       Optional[int]									=			4

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

        # shard inputs and rng
        lowercase_			:       Dict									=			replicate(__UpperCamelCase    )
        lowercase_			:       Optional[Any]									=			jax.random.split(__UpperCamelCase  ,__UpperCamelCase    )
        lowercase_			:       Dict									=			shard(__UpperCamelCase    )

        lowercase_			:       List[Any]									=			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.151_4745    ) < 1e-3
             assert np.abs(np.abs(__UpperCamelCase  ,dtype=np.floataa    ).sum() - 4_9947.875    ) < 5e-1

        lowercase_			:       List[Any]									=			pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]    )    )    )
        assert len(__UpperCamelCase    ) == num_samples
   def     _UpperCAmelCase     (    self    )			->   Optional[Any]:


        '''simple docstring'''


        lowercase_							,						lowercase_			:       Any									=			FlaxStableDiffusionPipeline.from_pretrained(
            'CompVis/stable-diffusion-v1-4'  ,revision='flax'  ,safety_checker=__UpperCamelCase    )

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

        lowercase_			:       List[str]									=			jax.random.PRNGKey(0    )
        lowercase_			:       Tuple									=			50

        lowercase_			:       Optional[Any]									=			jax.device_count()
        lowercase_			:       List[Any]									=			num_samples * [prompt]
        lowercase_			:       Union[str, Any]									=			pipeline.prepare_inputs(__UpperCamelCase    )

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

        lowercase_			:       Optional[int]									=			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.0565_2401)    ) < 1e-3
             assert np.abs((np.abs(__UpperCamelCase  ,dtype=np.floataa    ).sum() - 238_3808.2)    ) < 5e-1
   def     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''simple docstring'''


        lowercase_							,						lowercase_			:       Tuple									=			FlaxStableDiffusionPipeline.from_pretrained(
            'CompVis/stable-diffusion-v1-4'  ,revision='bf16'  ,dtype=jnp.bfloataa  ,safety_checker=__UpperCamelCase    )

        lowercase_			:       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'
        )

        lowercase_			:       Dict									=			jax.random.PRNGKey(0    )
        lowercase_			:       Optional[int]									=			50

        lowercase_			:       Tuple									=			jax.device_count()
        lowercase_			:       Dict									=			num_samples * [prompt]
        lowercase_			:       Any									=			pipeline.prepare_inputs(__UpperCamelCase    )

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

        lowercase_			:       List[str]									=			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.0400_3906)    ) < 1e-3
             assert np.abs((np.abs(__UpperCamelCase  ,dtype=np.floataa    ).sum() - 237_3516.75)    ) < 5e-1
   def     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''simple docstring'''


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

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

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

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

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

        lowercase_			:       Optional[Any]									=			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.0400_3906)    ) < 1e-3
             assert np.abs((np.abs(__UpperCamelCase  ,dtype=np.floataa    ).sum() - 237_3516.75)    ) < 5e-1
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


        lowercase_			:       Tuple									=			FlaxDDIMScheduler(
            beta_start=0.0_0085  ,beta_end=0.012  ,beta_schedule='scaled_linear'  ,set_alpha_to_one=__UpperCamelCase  ,steps_offset=1  ,)

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

        lowercase_			:       List[Any]									=			scheduler_state

        lowercase_			:       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'
        )

        lowercase_			:       str									=			jax.random.PRNGKey(0    )
        lowercase_			:       str									=			50

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

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

        lowercase_			:       str									=			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.0_4504_3945)    ) < 1e-3
             assert np.abs((np.abs(__UpperCamelCase  ,dtype=np.floataa    ).sum() - 234_7693.5)    ) < 5e-1







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


        '''simple docstring'''


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

        lowercase_			:       Union[str, Any]									=			jax.device_count()
        lowercase_			:       List[str]									=			num_samples * [prompt]
        lowercase_			:       int									=			jax.random.split(jax.random.PRNGKey(0    )  ,__UpperCamelCase    )

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

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

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

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

