






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCamelCase      :      Union[str, Any]     						=						{}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__UpperCamelCase      :      Dict     						=						["""NllbTokenizer"""]

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__UpperCamelCase      :      Union[str, Any]     						=						["""NllbTokenizerFast"""]


if TYPE_CHECKING:
						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_nllb import NllbTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_nllb_fast import NllbTokenizerFast

else:
						import sys

						__UpperCamelCase      :      Optional[Any]     						=						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)









import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
						import jax
						import jax.numpy as jnp
						from flax.jax_utils import replicate
						from flax.training.common_utils import shard


@slow
@require_flax
class        __UpperCamelCase    (     unittest.TestCase   ):







	def  _a   (  self      :							Union[str, Any]			)		-> Any:
		"""simple docstring"""







		super().tearDown()
		gc.collect()







	def  _a   (  self      :							List[str]			)		-> int:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  FlaxStableDiffusionPipeline.from_pretrained(
		    """stabilityai/stable-diffusion-2"""       ,				revision="""bf16"""       ,				dtype=jnp.bfloataa       ,				)

		__lowercase             =  """A painting of a squirrel eating a burger"""
		__lowercase             =  jax.device_count()
		__lowercase             =  num_samples * [prompt]
		__lowercase             =  sd_pipe.prepare_inputs(_lowerCAmelCase			)

		__lowercase             =  replicate(_lowerCAmelCase			)
		__lowercase             =  shard(_lowerCAmelCase			)

		__lowercase             =  jax.random.PRNGKey(0			)
		__lowercase             =  jax.random.split(_lowerCAmelCase       ,				jax.device_count()			)

		__lowercase             =  sd_pipe(_lowerCAmelCase       ,				_lowerCAmelCase       ,				_lowerCAmelCase       ,				num_inference_steps=25       ,				jit=_lowerCAmelCase			)[0]
		assert images.shape == (jax.device_count(), 1, 768, 768, 3)

		__lowercase             =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]			)
		__lowercase             =  images[0, 253:256, 253:256, -1]

		__lowercase             =  jnp.asarray(jax.device_get(image_slice.flatten()			)			)
		__lowercase             =  jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]			)
		print(F'output_slice: {output_slice}'			)
		assert jnp.abs(output_slice - expected_slice			).max() < 1e-2







	def  _a   (  self      :							str			)		-> List[Any]:
		"""simple docstring"""







		__lowercase             =  """stabilityai/stable-diffusion-2"""
		__lowercase       ,						__lowercase             =  FlaxDPMSolverMultistepScheduler.from_pretrained(_lowerCAmelCase       ,				subfolder="""scheduler"""			)
		__lowercase       ,						__lowercase             =  FlaxStableDiffusionPipeline.from_pretrained(
		    _lowerCAmelCase       ,				scheduler=_lowerCAmelCase       ,				revision="""bf16"""       ,				dtype=jnp.bfloataa       ,				)
		__lowercase             =  scheduler_params

		__lowercase             =  """A painting of a squirrel eating a burger"""
		__lowercase             =  jax.device_count()
		__lowercase             =  num_samples * [prompt]
		__lowercase             =  sd_pipe.prepare_inputs(_lowerCAmelCase			)

		__lowercase             =  replicate(_lowerCAmelCase			)
		__lowercase             =  shard(_lowerCAmelCase			)

		__lowercase             =  jax.random.PRNGKey(0			)
		__lowercase             =  jax.random.split(_lowerCAmelCase       ,				jax.device_count()			)

		__lowercase             =  sd_pipe(_lowerCAmelCase       ,				_lowerCAmelCase       ,				_lowerCAmelCase       ,				num_inference_steps=25       ,				jit=_lowerCAmelCase			)[0]
		assert images.shape == (jax.device_count(), 1, 768, 768, 3)

		__lowercase             =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]			)
		__lowercase             =  images[0, 253:256, 253:256, -1]

		__lowercase             =  jnp.asarray(jax.device_get(image_slice.flatten()			)			)
		__lowercase             =  jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]			)
		print(F'output_slice: {output_slice}'			)
		assert jnp.abs(output_slice - expected_slice			).max() < 1e-2

