



"""simple docstring"""




from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings






@maybe_allow_in_graph
class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Tuple	,      A	:      int	,      A	:      int	,      A	:      int	,      A	:      Dict=0.0	,      A	:      Optional[int] = None	,      A	:      str = "geglu"	,      A	:      Optional[int] = None	,      A	:      bool = False	,      A	:      bool = False	,      A	:      bool = False	,      A	:      bool = False	,      A	:      bool = True	,      A	:      str = "layer_norm"	,      A	:      bool = False	,      ):
           super().__init__()
           _UpperCAmelCase     :      List[Any]														=       only_cross_attention

           _UpperCAmelCase     :      List[str]														=       (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
           _UpperCAmelCase     :      Union[str, Any]														=       (num_embeds_ada_norm is not None) and norm_type == "ada_norm"

           if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
                raise ValueError(
                    f'`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to'
                    f' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.'		)

           # Define 3 blocks. Each block has its own normalization layer.
           # 1. Self-Attn
           if self.use_ada_layer_norm:
                _UpperCAmelCase     :      Optional[Any]														=       AdaLayerNorm(A	,      A		)
           elif self.use_ada_layer_norm_zero:
                _UpperCAmelCase     :      Any														=       AdaLayerNormZero(A	,      A		)
           else:
                _UpperCAmelCase     :      List[Any]														=       nn.LayerNorm(A	,      elementwise_affine=A		)
           _UpperCAmelCase     :      Dict														=       Attention(
               query_dim=A	,      heads=A	,      dim_head=A	,      dropout=A	,      bias=A	,      cross_attention_dim=cross_attention_dim if only_cross_attention else None	,      upcast_attention=A	,      )

           # 2. Cross-Attn
           if cross_attention_dim is not None or double_self_attention:
                # We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
                # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
                # the second cross attention block.
                _UpperCAmelCase     :      Any														=       (
                    AdaLayerNorm(A	,      A		)
                    if self.use_ada_layer_norm
                    else nn.LayerNorm(A	,      elementwise_affine=A		)
                )
                _UpperCAmelCase     :      Optional[int]														=       Attention(
                    query_dim=A	,      cross_attention_dim=cross_attention_dim if not double_self_attention else None	,      heads=A	,      dim_head=A	,      dropout=A	,      bias=A	,      upcast_attention=A	,      )  # is self-attn if encoder_hidden_states is none
           else:
                _UpperCAmelCase     :      Dict														=       None
                _UpperCAmelCase     :      Tuple														=       None

           # 3. Feed-forward
           _UpperCAmelCase     :      Tuple														=       nn.LayerNorm(A	,      elementwise_affine=A		)
           _UpperCAmelCase     :      Any														=       FeedForward(A	,      dropout=A	,      activation_fn=A	,      final_dropout=A		)

           # let chunk size default to None
           _UpperCAmelCase     :      List[str]														=       None
           _UpperCAmelCase     :      str														=       0



      def 		snake_case_   (	self	:      Dict	,      A	:      Optional[int]	,      A	:      int		):
           # Sets chunk feed-forward
           _UpperCAmelCase     :      Optional[Any]														=       chunk_size
           _UpperCAmelCase     :      str														=       dim



      def 		snake_case_   (	self	:      List[Any]	,      A	:      torch.FloatTensor	,      A	:      Optional[torch.FloatTensor] = None	,      A	:      Optional[torch.FloatTensor] = None	,      A	:      Optional[torch.FloatTensor] = None	,      A	:      Optional[torch.LongTensor] = None	,      A	:      Dict[str, Any] = None	,      A	:      Optional[torch.LongTensor] = None	,      ):
           # Notice that normalization is always applied before the real computation in the following blocks.
           # 1. Self-Attention
           if self.use_ada_layer_norm:
                _UpperCAmelCase     :      List[Any]														=       self.norma(A	,      A		)
           elif self.use_ada_layer_norm_zero:
                _UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase     :      Optional[Any]														=       self.norma(
                    A	,      A	,      A	,      hidden_dtype=hidden_states.dtype		)
           else:
                _UpperCAmelCase     :      Dict														=       self.norma(A		)

           _UpperCAmelCase     :      Any														=       cross_attention_kwargs if cross_attention_kwargs is not None else {}

           _UpperCAmelCase     :      Any														=       self.attna(
               A	,      encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None	,      attention_mask=A	,      **A	,      )
           if self.use_ada_layer_norm_zero:
                _UpperCAmelCase     :      List[str]														=       gate_msa.unsqueeze(1		) * attn_output
           _UpperCAmelCase     :      Dict														=       attn_output + hidden_states

           # 2. Cross-Attention
           if self.attna is not None:
                _UpperCAmelCase     :      List[Any]														=       (
                    self.norma(A	,      A		) if self.use_ada_layer_norm else self.norma(A		)
                )

                _UpperCAmelCase     :      Any														=       self.attna(
                    A	,      encoder_hidden_states=A	,      attention_mask=A	,      **A	,      )
                _UpperCAmelCase     :      Optional[int]														=       attn_output + hidden_states

           # 3. Feed-forward
           _UpperCAmelCase     :      Dict														=       self.norma(A		)

           if self.use_ada_layer_norm_zero:
                _UpperCAmelCase     :      Optional[Any]														=       norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

           if self._chunk_size is not None:
                # "feed_forward_chunk_size" can be used to save memory
                if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
                     raise ValueError(
                         f'`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.'		)

                _UpperCAmelCase     :      Optional[Any]														=       norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
                _UpperCAmelCase     :      List[Any]														=       torch.cat(
                    [self.ff(A		) for hid_slice in norm_hidden_states.chunk(A	,      dim=self._chunk_dim		)]	,      dim=self._chunk_dim	,      )
           else:
                _UpperCAmelCase     :      List[Any]														=       self.ff(A		)

           if self.use_ada_layer_norm_zero:
                _UpperCAmelCase     :      str														=       gate_mlp.unsqueeze(1		) * ff_output

           _UpperCAmelCase     :      Any														=       ff_output + hidden_states

           return hidden_states







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Optional[Any]	,      A	:      int	,      A	:      Optional[int] = None	,      A	:      int = 4	,      A	:      float = 0.0	,      A	:      str = "geglu"	,      A	:      bool = False	,      ):
           super().__init__()
           _UpperCAmelCase     :      int														=       int(dim * mult		)
           _UpperCAmelCase     :      str														=       dim_out if dim_out is not None else dim

           if activation_fn == "gelu":
                _UpperCAmelCase     :      Union[str, Any]														=       GELU(A	,      A		)
           if activation_fn == "gelu-approximate":
                _UpperCAmelCase     :      List[str]														=       GELU(A	,      A	,      approximate="tanh"		)
           elif activation_fn == "geglu":
                _UpperCAmelCase     :      Any														=       GEGLU(A	,      A		)
           elif activation_fn == "geglu-approximate":
                _UpperCAmelCase     :      Dict														=       ApproximateGELU(A	,      A		)

           _UpperCAmelCase     :      Any														=       nn.ModuleList([]		)
           # project in
           self.net.append(A		)
           # project dropout
           self.net.append(nn.Dropout(A		)		)
           # project out
           self.net.append(nn.Linear(A	,      A		)		)
           # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
           if final_dropout:
                self.net.append(nn.Dropout(A		)		)



      def 		snake_case_   (	self	:      Dict	,      A	:      int		):
           for module in self.net:
                _UpperCAmelCase     :      Optional[Any]														=       module(A		)
           return hidden_states







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      List[Any]	,      A	:      int	,      A	:      int	,      A	:      str = "none"		):
           super().__init__()
           _UpperCAmelCase     :      Union[str, Any]														=       nn.Linear(A	,      A		)
           _UpperCAmelCase     :      Dict														=       approximate



      def 		snake_case_   (	self	:      Optional[int]	,      A	:      Union[str, Any]		):
           if gate.device.type != "mps":
                return F.gelu(A	,      approximate=self.approximate		)
           # mps: gelu is not implemented for float16
           return F.gelu(gate.to(dtype=torch.floataa		)	,      approximate=self.approximate		).to(dtype=gate.dtype		)



      def 		snake_case_   (	self	:      int	,      A	:      str		):
           _UpperCAmelCase     :      List[str]														=       self.proj(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       self.gelu(A		)
           return hidden_states







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Union[str, Any]	,      A	:      int	,      A	:      int		):
           super().__init__()
           _UpperCAmelCase     :      Union[str, Any]														=       nn.Linear(A	,      dim_out * 2		)



      def 		snake_case_   (	self	:      int	,      A	:      int		):
           if gate.device.type != "mps":
                return F.gelu(A		)
           # mps: gelu is not implemented for float16
           return F.gelu(gate.to(dtype=torch.floataa		)		).to(dtype=gate.dtype		)



      def 		snake_case_   (	self	:      Tuple	,      A	:      Dict		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Union[str, Any]														=       self.proj(A		).chunk(2	,      dim=-1		)
           return hidden_states * self.gelu(A		)







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Optional[int]	,      A	:      int	,      A	:      int		):
           super().__init__()
           _UpperCAmelCase     :      Optional[int]														=       nn.Linear(A	,      A		)



      def 		snake_case_   (	self	:      int	,      A	:      str		):
           _UpperCAmelCase     :      Tuple														=       self.proj(A		)
           return x * torch.sigmoid(1.702 * x		)







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      str	,      A	:      str	,      A	:      Tuple		):
           super().__init__()
           _UpperCAmelCase     :      Union[str, Any]														=       nn.Embedding(A	,      A		)
           _UpperCAmelCase     :      Optional[int]														=       nn.SiLU()
           _UpperCAmelCase     :      Any														=       nn.Linear(A	,      embedding_dim * 2		)
           _UpperCAmelCase     :      Tuple														=       nn.LayerNorm(A	,      elementwise_affine=A		)



      def 		snake_case_   (	self	:      str	,      A	:      Any	,      A	:      Tuple		):
           _UpperCAmelCase     :      Optional[int]														=       self.linear(self.silu(self.emb(A		)		)		)
           _UpperCAmelCase							,			_UpperCAmelCase     :      Any														=       torch.chunk(A	,      2		)
           _UpperCAmelCase     :      Tuple														=       self.norm(A		) * (1 + scale) + shift
           return x







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Union[str, Any]	,      A	:      Optional[Any]	,      A	:      List[Any]		):
           super().__init__()

           _UpperCAmelCase     :      Any														=       CombinedTimestepLabelEmbeddings(A	,      A		)

           _UpperCAmelCase     :      Optional[int]														=       nn.SiLU()
           _UpperCAmelCase     :      Dict														=       nn.Linear(A	,      6 * embedding_dim	,      bias=A		)
           _UpperCAmelCase     :      List[str]														=       nn.LayerNorm(A	,      elementwise_affine=A	,      eps=1e-6		)



      def 		snake_case_   (	self	:      List[Any]	,      A	:      Dict	,      A	:      Optional[Any]	,      A	:      int	,      A	:      Dict=None		):
           _UpperCAmelCase     :      str														=       self.linear(self.silu(self.emb(A	,      A	,      hidden_dtype=A		)		)		)
           _UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase							,			_UpperCAmelCase     :      Union[str, Any]														=       emb.chunk(6	,      dim=1		)
           _UpperCAmelCase     :      Any														=       self.norm(A		) * (1 + scale_msa[:, None]) + shift_msa[:, None]
           return x, gate_msa, shift_mlp, scale_mlp, gate_mlp







class       UpperCAmelCase_    (							nn.Module		):



      def __init__(	self	:      Dict	,      A	:      int	,      A	:      int	,      A	:      int	,      A	:      Optional[str] = None	,      A	:      float = 1e-5		):
           super().__init__()
           _UpperCAmelCase     :      Tuple														=       num_groups
           _UpperCAmelCase     :      List[Any]														=       eps

           if act_fn is None:
                _UpperCAmelCase     :      int														=       None
           else:
                _UpperCAmelCase     :      Tuple														=       get_activation(A		)

           _UpperCAmelCase     :      Tuple														=       nn.Linear(A	,      out_dim * 2		)



      def 		snake_case_   (	self	:      Dict	,      A	:      List[str]	,      A	:      Any		):
           if self.act:
                _UpperCAmelCase     :      Dict														=       self.act(A		)
           _UpperCAmelCase     :      Optional[int]														=       self.linear(A		)
           _UpperCAmelCase     :      Optional[Any]														=       emb[:, :, None, None]
           _UpperCAmelCase							,			_UpperCAmelCase     :      int														=       emb.chunk(2	,      dim=1		)

           _UpperCAmelCase     :      Union[str, Any]														=       F.group_norm(A	,      self.num_groups	,      eps=self.eps		)
           _UpperCAmelCase     :      Dict														=       x * (1 + scale) + shift
           return x





"""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 		snake_case_   (	self	:      Optional[int]		):
           with tempfile.TemporaryDirectory() as tmpdirname:
                # pipeline has Flax weights
                _UpperCAmelCase     :      Union[str, Any]														=       FlaxDiffusionPipeline.from_pretrained(
                    "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A	,      cache_dir=A		)

                _UpperCAmelCase     :      List[str]														=       [t[-1] for t in os.walk(os.path.join(A	,      os.listdir(A		)[0]	,      "snapshots"		)		)]
                _UpperCAmelCase     :      Union[str, Any]														=       [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 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      int														=       FlaxStableDiffusionPipeline.from_pretrained(
               "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A		)

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

           _UpperCAmelCase     :      Any														=       jax.device_count()
           _UpperCAmelCase     :      Tuple														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

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

           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
           if jax.device_count() == 8:
                assert np.abs(np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 4.1_514_745		) < 1e-3
                assert np.abs(np.abs(A	,      dtype=np.floataa		).sum() - 49_947.875		) < 5e-1

           _UpperCAmelCase     :      Optional[int]														=       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
           assert len(A		) == num_samples



      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Optional[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="flax"	,      safety_checker=A		)

           _UpperCAmelCase     :      Dict														=       (
               "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     :      str														=       5_0

           _UpperCAmelCase     :      List[str]														=       jax.device_count()
           _UpperCAmelCase     :      Any														=       num_samples * [prompt]
           _UpperCAmelCase     :      Dict														=       pipeline.prepare_inputs(A		)

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

           _UpperCAmelCase     :      List[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.05_652_401)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_383_808.2)		) < 5e-1



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      List[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A		)

           _UpperCAmelCase     :      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     :      List[Any]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[Any]														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      Optional[int]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Optional[int]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      str														=       shard(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa		)

           _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     :      Optional[int]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      Dict														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      str														=       num_samples * [prompt]
           _UpperCAmelCase     :      int														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Optional[int]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Dict														=       shard(A		)

           _UpperCAmelCase     :      Optional[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Dict														=       FlaxDDIMScheduler(
               beta_start=0.00_085	,      beta_end=0.012	,      beta_schedule="scaled_linear"	,      set_alpha_to_one=A	,      steps_offset=1	,      )

           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      scheduler=A	,      safety_checker=A	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       scheduler.create_state()

           _UpperCAmelCase     :      str														=       scheduler_state

           _UpperCAmelCase     :      Union[str, 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]														=       5_0

           _UpperCAmelCase     :      Optional[int]														=       jax.device_count()
           _UpperCAmelCase     :      int														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      List[Any]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Union[str, Any]														=       shard(A		)

           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.045_043_945)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_347_693.5)		) < 5e-1



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

           _UpperCAmelCase     :      Optional[Any]														=       jax.device_count()
           _UpperCAmelCase     :      Union[str, Any]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[Any]														=       jax.random.split(jax.random.PRNGKey(0		)	,      A		)

           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      )

           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      int														=       shard(A		)
           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      List[str]														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

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

           _UpperCAmelCase     :      Any														=       replicate(A		)
           _UpperCAmelCase     :      str														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      Tuple														=       shard(A		)
           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      str														=       images[2, 0, 2_5_6, 1_0:1_7, 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
