



import math

import tensorflow as tf
from packaging import version







def     UpperCAmelCase_		(					snake_case__					)	->     int:



				"""simple docstring"""





				lowerCAmelCase__		  =  tf.convert_to_tensor(snake_case__					)
				lowerCAmelCase__		  =  0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0					)    ,			x.dtype					)					))

				return x * cdf







def     UpperCAmelCase_		(					snake_case__					)	->     int:



				"""simple docstring"""





				lowerCAmelCase__		  =  tf.convert_to_tensor(snake_case__					)
				lowerCAmelCase__		  =  tf.cast(math.pi    ,			x.dtype					)
				lowerCAmelCase__		  =  tf.cast(0.044715    ,			x.dtype					)
				lowerCAmelCase__		  =  0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi					) * (x + coeff * tf.pow(snake_case__    ,			3					))					))

				return x * cdf







def     UpperCAmelCase_		(					snake_case__					)	->     List[Any]:



				"""simple docstring"""





				lowerCAmelCase__		  =  tf.convert_to_tensor(snake_case__					)

				return x * tf.tanh(tf.math.softplus(snake_case__					)					)







def     UpperCAmelCase_		(					snake_case__					)	->     Tuple:



				"""simple docstring"""





				lowerCAmelCase__		  =  tf.convert_to_tensor(snake_case__					)
				lowerCAmelCase__		  =  tf.cast(0.044715    ,			x.dtype					)
				lowerCAmelCase__		  =  tf.cast(0.7978845608    ,			x.dtype					)

				return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)					))







def     UpperCAmelCase_		(					snake_case__					)	->     Tuple:



				"""simple docstring"""





				lowerCAmelCase__		  =  tf.convert_to_tensor(snake_case__					)
				lowerCAmelCase__		  =  tf.cast(1.702    ,			x.dtype					)
				return x * tf.math.sigmoid(coeff * x					)







def     UpperCAmelCase_		(					snake_case__					)	->     Optional[int]:



				"""simple docstring"""





				return tf.clip_by_value(_gelu(snake_case__					)    ,			-10    ,			10					)







def     UpperCAmelCase_		(					snake_case__    ,			snake_case__=-1					)	->     str:



				"""simple docstring"""





				lowerCAmelCase__  ,   lowerCAmelCase__		  =  tf.split(snake_case__    ,			2    ,			axis=snake_case__					)
				return a * tf.math.sigmoid(snake_case__					)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):







	def     UpperCAmelCase_		(					snake_case__					)	->     str:



					"""simple docstring"""





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

	_lowerCAmelCase	:   Any    =   tf.keras.activations.gelu

	_lowerCAmelCase	:   Optional[int]    =   approximate_gelu_wrap
else:
	_lowerCAmelCase	:   List[Any]    =   _gelu
	_lowerCAmelCase	:   str    =   _gelu_new


_lowerCAmelCase	:   Optional[int]    =   {
    "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_		(					snake_case__					)	->     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()					)}'					)





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_lowerCAmelCase	:   Any    =   False


class 	__snake_case   ( unittest.TestCase ):
		pass



@nightly
@require_torch_gpu
class 	__snake_case   ( unittest.TestCase ):
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)

						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt='first prompt'    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=2    ,output_type='numpy'    ,).images

						with tempfile.TemporaryDirectory() as tmpdirname:
										pipe.save_pretrained(a_						)
										lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained(a_    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  generator.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt='first prompt'    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=2    ,output_type='numpy'    ,).images

						assert np.abs(image - new_image						).sum() < 1e-5, "Models don't have the same forward pass"



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'    ,torch_dtype=torch.floataa						)
						pipe.to(a_						)
						pipe.set_progress_bar_config(disable=a_						)

						lowerCAmelCase__		  =  'cyberpunk 2077'
						lowerCAmelCase__		  =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)
						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.dual_guided(
						    prompt=a_    ,image=a_    ,text_to_image_strength=0.75    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=50    ,output_type='numpy'    ,).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1

						lowerCAmelCase__		  =  'A painting of a squirrel eating a burger '
						lowerCAmelCase__		  =  torch.manual_seed(0						)
						lowerCAmelCase__		  =  pipe.text_to_image(
						    prompt=a_    ,generator=a_    ,guidance_scale=7.5    ,num_inference_steps=50    ,output_type='numpy'						).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1

						lowerCAmelCase__		  =  pipe.image_variation(a_    ,generator=a_    ,output_type='numpy'						).images

						lowerCAmelCase__		  =  image[0, 253:256, 253:256, -1]

						assert image.shape == (1, 512, 512, 3)
						lowerCAmelCase__		  =  np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]						)

						assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1
