






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

													__magic_name__      :       Any       						= [t[-1] for t in os.walk(os.path.join(lowerCAmelCase__      ,       os.listdir(lowerCAmelCase__			)[0]      ,       """snapshots"""			)			)]
													__magic_name__      :       Optional[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 	snake_case__ (							unittest.TestCase				):
	def       __magic_name__    (    self			)							->				Any:
							__magic_name__      :       Optional[Any]       						= FlaxStableDiffusionPipeline.from_pretrained(
							    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,       safety_checker=lowerCAmelCase__			)

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

							__magic_name__      :       Optional[Any]       						= jax.random.PRNGKey(0			)
							__magic_name__      :       Dict       						= 4

							__magic_name__      :       int       						= jax.device_count()
							__magic_name__      :       Tuple       						= num_samples * [prompt]
							__magic_name__      :       str       						= pipeline.prepare_inputs(lowerCAmelCase__			)

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

							__magic_name__      :       str       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).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(lowerCAmelCase__      ,       dtype=np.floataa			).sum() - 4_99_47.8_75			) < 5e-1

							__magic_name__      :       Union[str, Any]       						= pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]			)			)			)
							assert len(lowerCAmelCase__			) == num_samples
	def       __magic_name__    (    self			)							->				Tuple:
							__magic_name__      :       Dict       						= FlaxStableDiffusionPipeline.from_pretrained(
							    """CompVis/stable-diffusion-v1-4"""      ,       revision="""flax"""      ,       safety_checker=lowerCAmelCase__			)

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

							__magic_name__      :       Dict       						= jax.random.PRNGKey(0			)
							__magic_name__      :       List[Any]       						= 50

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

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

							__magic_name__      :       List[str]       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).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.0_5_6_5_2_4_0_1)			) < 1e-3
													assert np.abs((np.abs(lowerCAmelCase__      ,       dtype=np.floataa			).sum() - 2_38_38_08.2)			) < 5e-1
	def       __magic_name__    (    self			)							->				int:
							__magic_name__      :       List[str]       						= FlaxStableDiffusionPipeline.from_pretrained(
							    """CompVis/stable-diffusion-v1-4"""      ,       revision="""bf16"""      ,       dtype=jnp.bfloataa      ,       safety_checker=lowerCAmelCase__			)

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

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

							__magic_name__      :       Optional[Any]       						= jax.device_count()
							__magic_name__      :       Optional[int]       						= num_samples * [prompt]
							__magic_name__      :       Optional[Any]       						= pipeline.prepare_inputs(lowerCAmelCase__			)

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

							__magic_name__      :       Optional[int]       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).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.0_4_0_0_3_9_0_6)			) < 1e-3
													assert np.abs((np.abs(lowerCAmelCase__      ,       dtype=np.floataa			).sum() - 2_37_35_16.75)			) < 5e-1
	def       __magic_name__    (    self			)							->				Optional[int]:
							__magic_name__      :       List[str]       						= FlaxStableDiffusionPipeline.from_pretrained(
							    """CompVis/stable-diffusion-v1-4"""      ,       revision="""bf16"""      ,       dtype=jnp.bfloataa			)

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

							__magic_name__      :       Any       						= jax.random.PRNGKey(0			)
							__magic_name__      :       Dict       						= 50

							__magic_name__      :       Tuple       						= jax.device_count()
							__magic_name__      :       Optional[Any]       						= num_samples * [prompt]
							__magic_name__      :       List[str]       						= pipeline.prepare_inputs(lowerCAmelCase__			)

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

							__magic_name__      :       List[str]       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).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.0_4_0_0_3_9_0_6)			) < 1e-3
													assert np.abs((np.abs(lowerCAmelCase__      ,       dtype=np.floataa			).sum() - 2_37_35_16.75)			) < 5e-1
	def       __magic_name__    (    self			)							->				Optional[int]:
							__magic_name__      :       Any       						= FlaxDDIMScheduler(
							    beta_start=0.0_0_0_8_5      ,       beta_end=0.0_1_2      ,       beta_schedule="""scaled_linear"""      ,       set_alpha_to_one=lowerCAmelCase__      ,       steps_offset=1      ,       )

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

							__magic_name__      :       Any       						= scheduler_state

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

							__magic_name__      :       Union[str, Any]       						= jax.random.PRNGKey(0			)
							__magic_name__      :       List[str]       						= 50

							__magic_name__      :       Tuple       						= jax.device_count()
							__magic_name__      :       Any       						= num_samples * [prompt]
							__magic_name__      :       Union[str, Any]       						= pipeline.prepare_inputs(lowerCAmelCase__			)

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

							__magic_name__      :       Optional[int]       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).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.0_4_5_0_4_3_9_4_5)			) < 1e-3
													assert np.abs((np.abs(lowerCAmelCase__      ,       dtype=np.floataa			).sum() - 2_34_76_93.5)			) < 5e-1







	def       __magic_name__    (    self			)							->				Union[str, Any]:
							__magic_name__      :       Tuple       						= (
							    """A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"""
							    """ field, close up, split lighting, cinematic"""
							)

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

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

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

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

							__magic_name__      :       int       						= replicate(lowerCAmelCase__			)
							__magic_name__      :       int       						= pipeline.prepare_inputs(lowerCAmelCase__			)
							__magic_name__      :       Optional[Any]       						= shard(lowerCAmelCase__			)
							__magic_name__      :       Dict       						= pipeline(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       jit=lowerCAmelCase__			).images
							assert images_eff.shape == (num_samples, 1, 5_12, 5_12, 3)
							__magic_name__      :       Any       						= 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








__magic_name__:					List[Any]							=     "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

__magic_name__:					Dict							=     [{"type": "code", "content": INSTALL_CONTENT}]
__magic_name__:					Optional[Any]							=     {
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}
