



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






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

							__lowerCAmelCase												=  [t[-1] for t in os.walk(os.path.join(lowerCamelCase__			,				os.listdir(lowerCamelCase__     )[0]			,				"snapshots"     )     )]
							__lowerCAmelCase												=  [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]:
						__lowerCAmelCase												=  FlaxStableDiffusionPipeline.from_pretrained(
						    "hf-internal-testing/tiny-stable-diffusion-pipe"			,				safety_checker=lowerCamelCase__     )

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

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

						__lowerCAmelCase												=  jax.device_count()
						__lowerCAmelCase												=  num_samples * [prompt]
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )

						# shard inputs and rng
						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  jax.random.split(lowerCamelCase__			,				lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )

						__lowerCAmelCase												=  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

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



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  FlaxStableDiffusionPipeline.from_pretrained(
						    "CompVis/stable-diffusion-v1-4"			,				revision="flax"			,				safety_checker=lowerCamelCase__     )

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

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

						__lowerCAmelCase												=  jax.device_count()
						__lowerCAmelCase												=  num_samples * [prompt]
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )

						# shard inputs and rng
						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  jax.random.split(lowerCamelCase__			,				lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )

						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  FlaxStableDiffusionPipeline.from_pretrained(
						    "CompVis/stable-diffusion-v1-4"			,				revision="bf16"			,				dtype=jnp.bfloataa			,				safety_checker=lowerCamelCase__     )

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

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

						__lowerCAmelCase												=  jax.device_count()
						__lowerCAmelCase												=  num_samples * [prompt]
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )

						# shard inputs and rng
						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  jax.random.split(lowerCamelCase__			,				lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )

						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self     )     ->			int:
						__lowerCAmelCase												=  FlaxStableDiffusionPipeline.from_pretrained(
						    "CompVis/stable-diffusion-v1-4"			,				revision="bf16"			,				dtype=jnp.bfloataa     )

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

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

						__lowerCAmelCase												=  jax.device_count()
						__lowerCAmelCase												=  num_samples * [prompt]
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )

						# shard inputs and rng
						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  jax.random.split(lowerCamelCase__			,				lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )

						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  FlaxDDIMScheduler(
						    beta_start=0.0_00_85			,				beta_end=0.0_12			,				beta_schedule="scaled_linear"			,				set_alpha_to_one=lowerCamelCase__			,				steps_offset=1			,				)

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

						__lowerCAmelCase												=  scheduler_state

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

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

						__lowerCAmelCase												=  jax.device_count()
						__lowerCAmelCase												=  num_samples * [prompt]
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )

						# shard inputs and rng
						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  jax.random.split(lowerCamelCase__			,				lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )

						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  (
						    '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
						    ''' field, close up, split lighting, cinematic'''
						)

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

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

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

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

						__lowerCAmelCase												=  replicate(lowerCamelCase__     )
						__lowerCAmelCase												=  pipeline.prepare_inputs(lowerCamelCase__     )
						__lowerCAmelCase												=  shard(lowerCamelCase__     )
						__lowerCAmelCase												=  pipeline(lowerCamelCase__			,				lowerCamelCase__			,				lowerCamelCase__			,				jit=lowerCamelCase__     ).images
						assert images_eff.shape == (num_samples, 1, 512, 512, 3)
						__lowerCAmelCase												=  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




'''simple docstring'''



from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class 					UpperCAmelCase__	(		UpperCamelCase__ ):
					a      :     torch.FloatTensor






class 					UpperCAmelCase__	(		UpperCamelCase__							,       UpperCamelCase__ ):



					@register_to_config
					def __init__(    self			,				UpperCamelCase = 16			,				UpperCamelCase = 88			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = 1			,				UpperCamelCase = 0.0			,				UpperCamelCase = 32			,				UpperCamelCase = None			,				UpperCamelCase = False			,				UpperCamelCase = None			,				UpperCamelCase = "geglu"			,				UpperCamelCase = True			,				UpperCamelCase = True			,				)     ->			List[str]:
						super().__init__()
						__lowerCAmelCase												=  num_attention_heads
						__lowerCAmelCase												=  attention_head_dim
						__lowerCAmelCase												=  num_attention_heads * attention_head_dim

						__lowerCAmelCase												=  in_channels

						__lowerCAmelCase												=  torch.nn.GroupNorm(num_groups=UpperCamelCase			,				num_channels=UpperCamelCase			,				eps=1E-6			,				affine=UpperCamelCase     )
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )

						# 3. Define transformers blocks
						__lowerCAmelCase												=  nn.ModuleList(
						    [
						        BasicTransformerBlock(
						            UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				dropout=UpperCamelCase			,				cross_attention_dim=UpperCamelCase			,				activation_fn=UpperCamelCase			,				attention_bias=UpperCamelCase			,				double_self_attention=UpperCamelCase			,				norm_elementwise_affine=UpperCamelCase			,				)
						        for d in range(UpperCamelCase     )
						    ]     )

						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase=None			,				UpperCamelCase=None			,				UpperCamelCase=None			,				UpperCamelCase=1			,				UpperCamelCase=None			,				UpperCamelCase = True			,				)     ->			List[str]:
						__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  hidden_states.shape
						__lowerCAmelCase												=  batch_frames // num_frames

						__lowerCAmelCase												=  hidden_states

						__lowerCAmelCase												=  hidden_states[None, :].reshape(UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  hidden_states.permute(0			,				2			,				1			,				3			,				4     )

						__lowerCAmelCase												=  self.norm(UpperCamelCase     )
						__lowerCAmelCase												=  hidden_states.permute(0			,				3			,				4			,				2			,				1     ).reshape(batch_size * height * width			,				UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  self.proj_in(UpperCamelCase     )

						# 2. Blocks
						for block in self.transformer_blocks:
							__lowerCAmelCase												=  block(
							    UpperCamelCase			,				encoder_hidden_states=UpperCamelCase			,				timestep=UpperCamelCase			,				cross_attention_kwargs=UpperCamelCase			,				class_labels=UpperCamelCase			,				)

						# 3. Output
						__lowerCAmelCase												=  self.proj_out(UpperCamelCase     )
						__lowerCAmelCase												=  (
						    hidden_states[None, None, :]
						    .reshape(UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )
						    .permute(0			,				3			,				4			,				1			,				2     )
						    .contiguous()
						)
						__lowerCAmelCase												=  hidden_states.reshape(UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  hidden_states + residual

						if not return_dict:
							return (output,)

						return TransformerTemporalModelOutput(sample=UpperCamelCase     )