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







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







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

																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    [t[-1] for t in os.walk(os.path.join(_A      ,    os.listdir(_A)[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  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







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

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

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    jax.random.PRNGKey(0)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    4

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    jax.device_count()
											_SCREAMING_SNAKE_CASE		:						int			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						str			  =    pipeline.prepare_inputs(_A)

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

											_SCREAMING_SNAKE_CASE		:						str			  =    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

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
											assert len(_A) == num_samples





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[Any]			  =    FlaxStableDiffusionPipeline.from_pretrained(
											    """CompVis/stable-diffusion-v1-4"""      ,    revision="""flax"""      ,    safety_checker=_A)

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

											_SCREAMING_SNAKE_CASE		:						str			  =    jax.device_count()
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    pipeline.prepare_inputs(_A)

											# shard inputs and rng
											_SCREAMING_SNAKE_CASE		:						Dict			  =    replicate(_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    jax.random.split(_A      ,    _A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    shard(_A)

											_SCREAMING_SNAKE_CASE		:						int			  =    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 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







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

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

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    jax.random.PRNGKey(0)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    5_0

											_SCREAMING_SNAKE_CASE		:						Any			  =    jax.device_count()
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    pipeline.prepare_inputs(_A)

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

											_SCREAMING_SNAKE_CASE		:						int			  =    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 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







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

											_SCREAMING_SNAKE_CASE		:						Dict			  =    jax.device_count()
											_SCREAMING_SNAKE_CASE		:						int			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    pipeline.prepare_inputs(_A)

											# shard inputs and rng
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    replicate(_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    jax.random.split(_A      ,    _A)
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    shard(_A)

											_SCREAMING_SNAKE_CASE		:						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 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    FlaxDDIMScheduler(
											    beta_start=0.00_085      ,    beta_end=0.012      ,    beta_schedule="""scaled_linear"""      ,    set_alpha_to_one=_A      ,    steps_offset=1      ,    )

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

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    scheduler_state

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

											_SCREAMING_SNAKE_CASE		:						int			  =    jax.random.PRNGKey(0)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    5_0

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    jax.device_count()
											_SCREAMING_SNAKE_CASE		:						Any			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						Any			  =    pipeline.prepare_inputs(_A)

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

											_SCREAMING_SNAKE_CASE		:						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 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_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.device_count()
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    num_samples * [prompt]
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    jax.random.split(jax.random.PRNGKey(0)      ,    _A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[str]			  =    FlaxStableDiffusionPipeline.from_pretrained(
											    """CompVis/stable-diffusion-v1-4"""      ,    revision="""bf16"""      ,    dtype=jnp.bfloataa      ,    safety_checker=_A      ,    )

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

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

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    replicate(_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    pipeline.prepare_inputs(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    shard(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    pipeline(_A      ,    _A      ,    _A      ,    jit=_A).images
											assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    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



"""simple docstring"""





import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						"M-CLIP"





				def __init__(      self							:							Optional[Any]      ,    _A							:							List[str]=1_0_2_4      ,    _A							:							Union[str, Any]=7_6_8      ,    **_A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    transformerDimSize
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    imageDimSize
											super().__init__(**_A)




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						MCLIPConfig





				def __init__(      self							:							Dict      ,    _A							:							Optional[Any]      ,    *_A							:							Any      ,    **_A							:							Dict):



											"""simple docstring"""







											super().__init__(_A      ,    *_A      ,    **_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    XLMRobertaModel(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    torch.nn.Linear(
											    in_features=config.transformerDimensions      ,    out_features=config.numDims)





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]      ,    _A							:							str      ,    _A							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.transformer(input_ids=_A      ,    attention_mask=_A)[0]
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    (embs * attention_mask.unsqueeze(2)).sum(dim=1) / attention_mask.sum(dim=1)[:, None]
											return self.LinearTransformation(_A), embs


