


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





      def        _lowercase				(		self						:					List[str]       )    ->   Optional[int]:






         """simple docstring"""







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





      def        _lowercase				(		self						:					Union[str, Any]       )    ->   List[str]:






         """simple docstring"""







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

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

         UpperCAmelCase							=     replicate(__lowerCamelCase       )
         UpperCAmelCase							=     shard(__lowerCamelCase       )

         UpperCAmelCase							=     jax.random.PRNGKey(0       )
         UpperCAmelCase							=     jax.random.split(__lowerCamelCase					,       jax.device_count()       )

         UpperCAmelCase							=     sd_pipe(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       num_inference_steps=2_5					,       jit=__lowerCamelCase       )[0]
         assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

         UpperCAmelCase							=     images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]       )
         UpperCAmelCase							=     images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         UpperCAmelCase							=     jnp.asarray(jax.device_get(image_slice.flatten()       )       )
         UpperCAmelCase							=     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        _lowercase				(		self						:					int       )    ->   str:






         """simple docstring"""







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

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

         UpperCAmelCase							=     replicate(__lowerCamelCase       )
         UpperCAmelCase							=     shard(__lowerCamelCase       )

         UpperCAmelCase							=     jax.random.PRNGKey(0       )
         UpperCAmelCase							=     jax.random.split(__lowerCamelCase					,       jax.device_count()       )

         UpperCAmelCase							=     sd_pipe(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       num_inference_steps=2_5					,       jit=__lowerCamelCase       )[0]
         assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

         UpperCAmelCase							=     images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]       )
         UpperCAmelCase							=     images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

         UpperCAmelCase							=     jnp.asarray(jax.device_get(image_slice.flatten()       )       )
         UpperCAmelCase							=     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







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


__a     =			{"""tokenization_herbert""": ["""HerbertTokenizer"""]}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __a     =			["""HerbertTokenizerFast"""]


if TYPE_CHECKING:
 from .tokenization_herbert import HerbertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_herbert_fast import HerbertTokenizerFast

else:
 import sys

 __a     =			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



