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

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

					__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'
					)

					__snake_case    :							Dict   	=				jax.random.PRNGKey(0)
					__snake_case    :							Tuple   	=				4

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

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

					__snake_case    :							Any   	=				pipeline(_A			,						_A			,						_A			,						_A			,						jit=_A).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_514_745) < 1E-3
						assert np.abs(np.abs(_A			,						dtype=np.floataa).sum() - 49_947.875) < 5E-1

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



				def       _lowercase  (self       :   Tuple)					->			Dict:
					__snake_case	,       __snake_case    :							Union[str, Any]   	=				FlaxStableDiffusionPipeline.from_pretrained(
					    'CompVis/stable-diffusion-v1-4'			,						revision='flax'			,						safety_checker=_A)

					__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'
					)

					__snake_case    :							Tuple   	=				jax.random.PRNGKey(0)
					__snake_case    :							List[str]   	=				50

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

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

					__snake_case    :							str   	=				pipeline(_A			,						_A			,						_A			,						_A			,						jit=_A).images

					assert images.shape == (num_samples, 1, 5_12, 5_12, 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       _lowercase  (self       :   str)					->			List[Any]:
					__snake_case	,       __snake_case    :							Any   	=				FlaxStableDiffusionPipeline.from_pretrained(
					    'CompVis/stable-diffusion-v1-4'			,						revision='bf16'			,						dtype=jnp.bfloataa			,						safety_checker=_A)

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

					__snake_case    :							Tuple   	=				jax.random.PRNGKey(0)
					__snake_case    :							List[Any]   	=				50

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

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

					__snake_case    :							List[str]   	=				pipeline(_A			,						_A			,						_A			,						_A			,						jit=_A).images

					assert images.shape == (num_samples, 1, 5_12, 5_12, 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       _lowercase  (self       :   Optional[int])					->			Union[str, Any]:
					__snake_case	,       __snake_case    :							int   	=				FlaxStableDiffusionPipeline.from_pretrained(
					    'CompVis/stable-diffusion-v1-4'			,						revision='bf16'			,						dtype=jnp.bfloataa)

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

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

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

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

					__snake_case    :							List[str]   	=				pipeline(_A			,						_A			,						_A			,						_A			,						jit=_A).images

					assert images.shape == (num_samples, 1, 5_12, 5_12, 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       _lowercase  (self       :   List[str])					->			Union[str, Any]:
					__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			,						)

					__snake_case	,       __snake_case    :							Tuple   	=				FlaxStableDiffusionPipeline.from_pretrained(
					    'CompVis/stable-diffusion-v1-4'			,						revision='bf16'			,						dtype=jnp.bfloataa			,						scheduler=_A			,						safety_checker=_A			,						)
					__snake_case    :							int   	=				scheduler.create_state()

					__snake_case    :							str   	=				scheduler_state

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

					__snake_case    :							Optional[int]   	=				jax.random.PRNGKey(0)
					__snake_case    :							List[Any]   	=				50

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

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

					__snake_case    :							Union[str, Any]   	=				pipeline(_A			,						_A			,						_A			,						_A			,						jit=_A).images

					assert images.shape == (num_samples, 1, 5_12, 5_12, 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       _lowercase  (self       :   Tuple)					->			Optional[Any]:
					__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'
					)

					__snake_case    :							Tuple   	=				jax.device_count()
					__snake_case    :							Union[str, Any]   	=				num_samples * [prompt]
					__snake_case    :							str   	=				jax.random.split(jax.random.PRNGKey(0)			,						_A)

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

					__snake_case    :							Any   	=				replicate(_A)
					__snake_case    :							Optional[Any]   	=				pipeline.prepare_inputs(_A)
					__snake_case    :							List[str]   	=				shard(_A)
					__snake_case    :							Union[str, Any]   	=				pipeline(_A			,						_A			,						_A			,						jit=_A).images
					assert images.shape == (num_samples, 1, 5_12, 5_12, 3)
					__snake_case    :							int   	=				images[2, 0, 2_56, 10:17, 1]

					# With memory efficient attention
					__snake_case	,       __snake_case    :							Optional[int]   	=				FlaxStableDiffusionPipeline.from_pretrained(
					    'CompVis/stable-diffusion-v1-4'			,						revision='bf16'			,						dtype=jnp.bfloataa			,						safety_checker=_A			,						use_memory_efficient_attention=_A			,						)

					__snake_case    :							int   	=				replicate(_A)
					__snake_case    :							str   	=				pipeline.prepare_inputs(_A)
					__snake_case    :							Dict   	=				shard(_A)
					__snake_case    :							int   	=				pipeline(_A			,						_A			,						_A			,						jit=_A).images
					assert images_eff.shape == (num_samples, 1, 5_12, 5_12, 3)
					__snake_case    :							Tuple   	=				images[2, 0, 2_56, 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"""







import math

import tensorflow as tf
from packaging import version
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]						)							->    Any:

	'''simple docstring'''





	__snake_case    :							List[str]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							List[str]   	=				0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)    ,							x.dtype						)						))

	return x * cdf
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    Optional[Any]:

	'''simple docstring'''





	__snake_case    :							Optional[int]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Any   	=				tf.cast(math.pi    ,							x.dtype						)
	__snake_case    :							List[Any]   	=				tf.cast(0.044_715    ,							x.dtype						)
	__snake_case    :							Union[str, Any]   	=				0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(UpperCAmelCase_    ,							3						))						))

	return x * cdf
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    List[Any]:

	'''simple docstring'''





	__snake_case    :							Optional[int]   	=				tf.convert_to_tensor(UpperCAmelCase_						)

	return x * tf.tanh(tf.math.softplus(UpperCAmelCase_						)						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    List[str]:

	'''simple docstring'''





	__snake_case    :							Union[str, Any]   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Dict   	=				tf.cast(0.044_715    ,							x.dtype						)
	__snake_case    :							int   	=				tf.cast(0.7_978_845_608    ,							x.dtype						)

	return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       int						)							->    Dict:

	'''simple docstring'''





	__snake_case    :							Any   	=				tf.convert_to_tensor(UpperCAmelCase_						)
	__snake_case    :							Dict   	=				tf.cast(1.702    ,							x.dtype						)
	return x * tf.math.sigmoid(coeff * x						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]						)							->    List[Any]:

	'''simple docstring'''





	return tf.clip_by_value(_gelu(UpperCAmelCase_						)    ,							-10    ,							10						)
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Union[str, Any]    ,							UpperCAmelCase_					:       Any=-1						)							->    int:

	'''simple docstring'''





	__snake_case	,       __snake_case    :							str   	=				tf.split(UpperCAmelCase_    ,							2    ,							axis=UpperCAmelCase_						)
	return a * tf.math.sigmoid(UpperCAmelCase_						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):
	def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Tuple						)							->    int:

		'''simple docstring'''





		return tf.keras.activations.gelu(UpperCAmelCase_    ,							approximate=UpperCAmelCase_						)

	_a  :  Tuple=				tf.keras.activations.gelu

	_a  :  Tuple=				approximate_gelu_wrap
else:
	_a  :  Any=				_gelu
	_a  :  str=				_gelu_new


_a  :  List[str]=				{
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}

def 						__UpperCAmelCase       (					UpperCAmelCase_					:       Optional[Any]						)							->    List[Any]:

	'''simple docstring'''





	if activation_string in ACTaFN:
		return ACTaFN[activation_string]
	else:
		raise KeyError(F"function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"						)





