





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



      def 	lowerCAmelCase__    (	self:						str       )						->    Union[str, Any]:
         '''simple docstring'''

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

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Tuple:
         '''simple docstring'''

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

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

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

         UpperCAmelCase_											=jax.device_count()
         UpperCAmelCase_											=num_samples * [prompt]
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )

         # shard inputs and rng
         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=jax.random.split(_lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )

         UpperCAmelCase_											=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.1_51_47_45       ) < 1e-3
            assert np.abs(np.abs(_lowerCAmelCase		,  dtype=np.floataa       ).sum() - 4_9947.875       ) < 5e-1

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



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Optional[Any]:
         '''simple docstring'''

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

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

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

         UpperCAmelCase_											=jax.device_count()
         UpperCAmelCase_											=num_samples * [prompt]
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )

         # shard inputs and rng
         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=jax.random.split(_lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )

         UpperCAmelCase_											=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.05_65_24_01)       ) < 1e-3
            assert np.abs((np.abs(_lowerCAmelCase		,  dtype=np.floataa       ).sum() - 238_3808.2)       ) < 5e-1



      def 	lowerCAmelCase__    (	self:						int       )						->    Union[str, Any]:
         '''simple docstring'''

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

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

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

         UpperCAmelCase_											=jax.device_count()
         UpperCAmelCase_											=num_samples * [prompt]
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )

         # shard inputs and rng
         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=jax.random.split(_lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )

         UpperCAmelCase_											=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.04_00_39_06)       ) < 1e-3
            assert np.abs((np.abs(_lowerCAmelCase		,  dtype=np.floataa       ).sum() - 237_3516.75)       ) < 5e-1



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Tuple:
         '''simple docstring'''

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

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

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

         UpperCAmelCase_											=jax.device_count()
         UpperCAmelCase_											=num_samples * [prompt]
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )

         # shard inputs and rng
         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=jax.random.split(_lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )

         UpperCAmelCase_											=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.04_00_39_06)       ) < 1e-3
            assert np.abs((np.abs(_lowerCAmelCase		,  dtype=np.floataa       ).sum() - 237_3516.75)       ) < 5e-1



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=FlaxDDIMScheduler(
             beta_start=0.0_00_85		,  beta_end=0.0_12		,  beta_schedule="scaled_linear"		,  set_alpha_to_one=_lowerCAmelCase		,  steps_offset=1		,  )

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

         UpperCAmelCase_											=scheduler_state

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

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

         UpperCAmelCase_											=jax.device_count()
         UpperCAmelCase_											=num_samples * [prompt]
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )

         # shard inputs and rng
         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=jax.random.split(_lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )

         UpperCAmelCase_											=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_45_04_39_45)       ) < 1e-3
            assert np.abs((np.abs(_lowerCAmelCase		,  dtype=np.floataa       ).sum() - 234_7693.5)       ) < 5e-1






      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    int:
         '''simple docstring'''

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

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

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

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

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

         UpperCAmelCase_											=replicate(_lowerCAmelCase       )
         UpperCAmelCase_											=pipeline.prepare_inputs(_lowerCAmelCase       )
         UpperCAmelCase_											=shard(_lowerCAmelCase       )
         UpperCAmelCase_											=pipeline(_lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  jit=_lowerCAmelCase       ).images
         assert images_eff.shape == (num_samples, 1, 512, 512, 3)
         UpperCAmelCase_											=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 gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()

class      A       (	__lowercase		,      unittest.TestCase						):
      _snake_case							     =DanceDiffusionPipeline
      _snake_case							     =UNCONDITIONAL_AUDIO_GENERATION_PARAMS
      _snake_case							     =PipelineTesterMixin.required_optional_params - {
          '''callback''',
          '''latents''',
          '''callback_steps''',
          '''output_type''',
          '''num_images_per_prompt''',
      }
      _snake_case							     =UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Tuple:
         '''simple docstring'''

         torch.manual_seed(0       )
         UpperCAmelCase_											=UNetaDModel(
             block_out_channels=(32, 32, 64)		,  extra_in_channels=16		,  sample_size=512		,  sample_rate=1_6000		,  in_channels=2		,  out_channels=2		,  flip_sin_to_cos=_lowerCAmelCase		,  use_timestep_embedding=_lowerCAmelCase		,  time_embedding_type="fourier"		,  mid_block_type="UNetMidBlock1D"		,  down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")		,  up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")		,  )
         UpperCAmelCase_											=IPNDMScheduler()

         UpperCAmelCase_											={
             "unet": unet,
             "scheduler": scheduler,
         }
         return components



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						List[str]=0       )						->    str:
         '''simple docstring'''

         if str(_lowerCAmelCase       ).startswith("mps"       ):
            UpperCAmelCase_											=torch.manual_seed(_lowerCAmelCase       )
         else:
            UpperCAmelCase_											=torch.Generator(device=_lowerCAmelCase       ).manual_seed(_lowerCAmelCase       )
         UpperCAmelCase_											={
             "batch_size": 1,
             "generator": generator,
             "num_inference_steps": 4,
         }
         return inputs



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											="cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCAmelCase_											=self.get_dummy_components()
         UpperCAmelCase_											=DanceDiffusionPipeline(**_lowerCAmelCase       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=self.get_dummy_inputs(_lowerCAmelCase       )
         UpperCAmelCase_											=pipe(**_lowerCAmelCase       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, components["unet"].sample_size)
         UpperCAmelCase_											=np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00]       )
         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Any:
         '''simple docstring'''

         return super().test_save_load_local()



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    int:
         '''simple docstring'''

         return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3       )



      @skip_mps
      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Optional[int]:
         '''simple docstring'''

         return super().test_save_load_optional_components()



      @skip_mps
      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         return super().test_attention_slicing_forward_pass()






      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Optional[Any]:
         '''simple docstring'''

         super().test_inference_batch_single_identical(expected_max_diff=3e-3       )

@slow
@require_torch_gpu
class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[Any]:
         '''simple docstring'''

         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=torch_device

         UpperCAmelCase_											=DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=torch.manual_seed(0       )
         UpperCAmelCase_											=pipe(generator=_lowerCAmelCase		,  num_inference_steps=100		,  audio_length_in_s=4.0_96       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, pipe.unet.sample_size)
         UpperCAmelCase_											=np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20]       )

         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2






      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=torch_device

         UpperCAmelCase_											=DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"		,  torch_dtype=torch.floataa       )
         UpperCAmelCase_											=pipe.to(_lowerCAmelCase       )
         pipe.set_progress_bar_config(disable=_lowerCAmelCase       )

         UpperCAmelCase_											=torch.manual_seed(0       )
         UpperCAmelCase_											=pipe(generator=_lowerCAmelCase		,  num_inference_steps=100		,  audio_length_in_s=4.0_96       )
         UpperCAmelCase_											=output.audios

         UpperCAmelCase_											=audio[0, -3:, -3:]

         assert audio.shape == (1, 2, pipe.unet.sample_size)
         UpperCAmelCase_											=np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41]       )

         assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2




