



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



  def 	lowerCamelCase__       (						self :					Optional[Any]		)     ->					List[Any]:


     '''simple docstring'''




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

        SCREAMING_SNAKE_CASE_: Any     			=[t[-1] for t in os.walk(os.path.join(UpperCamelCase__       ,		os.listdir(UpperCamelCase__		)[0]       ,		"""snapshots"""		)		)]
        SCREAMING_SNAKE_CASE_: List[str]     			=[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 :					Optional[Any]		)     ->					Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Dict     			=FlaxStableDiffusionPipeline.from_pretrained(
         """hf-internal-testing/tiny-stable-diffusion-pipe"""       ,		safety_checker=UpperCamelCase__		)

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

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: List[str]     			=4

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

     # shard inputs and rng
     SCREAMING_SNAKE_CASE_: Any     			=replicate(UpperCamelCase__		)
     SCREAMING_SNAKE_CASE_: str     			=jax.random.split(UpperCamelCase__       ,		UpperCamelCase__		)
     SCREAMING_SNAKE_CASE_: str     			=shard(UpperCamelCase__		)

     SCREAMING_SNAKE_CASE_: str     			=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.1_5_1_4_7_4_5		) < 1E-3
        assert np.abs(np.abs(UpperCamelCase__       ,		dtype=np.floataa		).sum() - 4_9947.875		) < 5E-1

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



  def 	lowerCamelCase__       (						self :					Dict		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""flax"""       ,		safety_checker=UpperCamelCase__		)

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

     SCREAMING_SNAKE_CASE_: str     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: List[Any]     			=50

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

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

     SCREAMING_SNAKE_CASE_: 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.0_5_6_5_2_4_0_1)		) < 1E-3
        assert np.abs((np.abs(UpperCamelCase__       ,		dtype=np.floataa		).sum() - 238_3808.2)		) < 5E-1



  def 	lowerCamelCase__       (						self :					Optional[int]		)     ->					Dict:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_: 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"""
     )

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: List[str]     			=50

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

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

     SCREAMING_SNAKE_CASE_: 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.0_4_0_0_3_9_0_6)		) < 1E-3
        assert np.abs((np.abs(UpperCamelCase__       ,		dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1



  def 	lowerCamelCase__       (						self :					Dict		)     ->					Dict:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_: 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"""
     )

     SCREAMING_SNAKE_CASE_: Tuple     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=50

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

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

     SCREAMING_SNAKE_CASE_: 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.0_4_0_0_3_9_0_6)		) < 1E-3
        assert np.abs((np.abs(UpperCamelCase__       ,		dtype=np.floataa		).sum() - 237_3516.75)		) < 5E-1



  def 	lowerCamelCase__       (						self :					Any		)     ->					Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[Any]     			=FlaxDDIMScheduler(
         beta_start=0.0_0_0_8_5       ,		beta_end=0.0_1_2       ,		beta_schedule="""scaled_linear"""       ,		set_alpha_to_one=UpperCamelCase__       ,		steps_offset=1       ,		)

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

     SCREAMING_SNAKE_CASE_: Dict     			=scheduler_state

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

     SCREAMING_SNAKE_CASE_: int     			=jax.random.PRNGKey(0		)
     SCREAMING_SNAKE_CASE_: Tuple     			=50

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

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

     SCREAMING_SNAKE_CASE_: Dict     			=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_4_5_0_4_3_9_4_5)		) < 1E-3
        assert np.abs((np.abs(UpperCamelCase__       ,		dtype=np.floataa		).sum() - 234_7693.5)		) < 5E-1





  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					Optional[int]:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_: List[Any]     			=jax.device_count()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=num_samples * [prompt]
     SCREAMING_SNAKE_CASE_: str     			=jax.random.split(jax.random.PRNGKey(0		)       ,		UpperCamelCase__		)

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

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

     # With memory efficient attention
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[Any]     			=FlaxStableDiffusionPipeline.from_pretrained(
         """CompVis/stable-diffusion-v1-4"""       ,		revision="""bf16"""       ,		dtype=jnp.bfloataa       ,		safety_checker=UpperCamelCase__       ,		use_memory_efficient_attention=UpperCamelCase__       ,		)

     SCREAMING_SNAKE_CASE_: Any     			=replicate(UpperCamelCase__		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=pipeline.prepare_inputs(UpperCamelCase__		)
     SCREAMING_SNAKE_CASE_: Dict     			=shard(UpperCamelCase__		)
     SCREAMING_SNAKE_CASE_: List[str]     			=pipeline(UpperCamelCase__       ,		UpperCamelCase__       ,		UpperCamelCase__       ,		jit=UpperCamelCase__		).images
     assert images_eff.shape == (num_samples, 1, 512, 512, 3)
     SCREAMING_SNAKE_CASE_: Tuple     			=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 collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


_UpperCAmelCase     =			{
    """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/config.json""",
    """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/config.json""",
    """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/config.json""",
    """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json""",
    """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/config.json""",
    """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/config.json""",
    """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/config.json""",
    """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json""",
}
class 					a	(  UpperCAmelCase__       ):
  UpperCamelCase	:				Any     						=					'albert'



  def __init__(						self :					Dict       ,		lowerCAmelCase :					List[str]=3_0000       ,		lowerCAmelCase :					List[Any]=128       ,		lowerCAmelCase :					List[str]=4096       ,		lowerCAmelCase :					str=12       ,		lowerCAmelCase :					str=1       ,		lowerCAmelCase :					Tuple=64       ,		lowerCAmelCase :					Dict=1_6384       ,		lowerCAmelCase :					int=1       ,		lowerCAmelCase :					str="gelu_new"       ,		lowerCAmelCase :					Dict=0       ,		lowerCAmelCase :					Optional[Any]=0       ,		lowerCAmelCase :					str=512       ,		lowerCAmelCase :					Optional[int]=2       ,		lowerCAmelCase :					List[Any]=0.0_2       ,		lowerCAmelCase :					Union[str, Any]=1E-12       ,		lowerCAmelCase :					Tuple=0.1       ,		lowerCAmelCase :					List[Any]="absolute"       ,		lowerCAmelCase :					List[Any]=0       ,		lowerCAmelCase :					int=2       ,		lowerCAmelCase :					Optional[int]=3       ,		**lowerCAmelCase :					int       ,		)     ->					Tuple:


     '''simple docstring'''




     super().__init__(pad_token_id=lowerCAmelCase       ,		bos_token_id=lowerCAmelCase       ,		eos_token_id=lowerCAmelCase       ,		**lowerCAmelCase		)

     SCREAMING_SNAKE_CASE_: List[str]     			=vocab_size
     SCREAMING_SNAKE_CASE_: Optional[int]     			=embedding_size
     SCREAMING_SNAKE_CASE_: Optional[int]     			=hidden_size
     SCREAMING_SNAKE_CASE_: Tuple     			=num_hidden_layers
     SCREAMING_SNAKE_CASE_: Any     			=num_hidden_groups
     SCREAMING_SNAKE_CASE_: List[Any]     			=num_attention_heads
     SCREAMING_SNAKE_CASE_: List[Any]     			=inner_group_num
     SCREAMING_SNAKE_CASE_: Optional[int]     			=hidden_act
     SCREAMING_SNAKE_CASE_: int     			=intermediate_size
     SCREAMING_SNAKE_CASE_: Any     			=hidden_dropout_prob
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=attention_probs_dropout_prob
     SCREAMING_SNAKE_CASE_: int     			=max_position_embeddings
     SCREAMING_SNAKE_CASE_: Any     			=type_vocab_size
     SCREAMING_SNAKE_CASE_: int     			=initializer_range
     SCREAMING_SNAKE_CASE_: List[Any]     			=layer_norm_eps
     SCREAMING_SNAKE_CASE_: Dict     			=classifier_dropout_prob
     SCREAMING_SNAKE_CASE_: int     			=position_embedding_type







class 					a	(  UpperCAmelCase__       ):



  @property
  def 	lowerCamelCase__       (						self :					Optional[int]		)     ->					Mapping[str, Mapping[int, str]]:


     '''simple docstring'''




     if self.task == "multiple-choice":
        SCREAMING_SNAKE_CASE_: str     			={0: """batch""", 1: """choice""", 2: """sequence"""}
     else:
        SCREAMING_SNAKE_CASE_: Dict     			={0: """batch""", 1: """sequence"""}
     return OrderedDict(
         [
             ("""input_ids""", dynamic_axis),
             ("""attention_mask""", dynamic_axis),
             ("""token_type_ids""", dynamic_axis),
         ]		)



