



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




    '''simple docstring'''






    def 			_lowerCAmelCase					(       self		):
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()






    def 			_lowerCAmelCase					(       self		):
        A     ,							A :       Optional[Any]          =   FlaxStableDiffusionPipeline.from_pretrained(
            """stabilityai/stable-diffusion-2""",		revision="""bf16""",		dtype=jnp.bfloataa,		)

        A :       int          =   """A painting of a squirrel eating a burger"""
        A :       int          =   jax.device_count()
        A :       Dict          =   num_samples * [prompt]
        A :       Tuple          =   sd_pipe.prepare_inputs(lowerCamelCase__		)

        A :       List[str]          =   replicate(lowerCamelCase__		)
        A :       Optional[int]          =   shard(lowerCamelCase__		)

        A :       Tuple          =   jax.random.PRNGKey(0		)
        A :       Optional[int]          =   jax.random.split(lowerCamelCase__,		jax.device_count()		)

        A :       Optional[Any]          =   sd_pipe(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		num_inference_steps=25,		jit=lowerCamelCase__		)[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

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

        A :       Optional[int]          =   jnp.asarray(jax.device_get(image_slice.flatten()		)		)
        A :       Dict          =   jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.4_5508, 0.4512]		)
        print(f'''output_slice: {output_slice}'''		)
        assert jnp.abs(output_slice - expected_slice		).max() < 1e-2






    def 			_lowerCAmelCase					(       self		):
        A :       List[Any]          =   """stabilityai/stable-diffusion-2"""
        A     ,							A :       List[Any]          =   FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCamelCase__,		subfolder="""scheduler"""		)
        A     ,							A :       Dict          =   FlaxStableDiffusionPipeline.from_pretrained(
            lowerCamelCase__,		scheduler=lowerCamelCase__,		revision="""bf16""",		dtype=jnp.bfloataa,		)
        A :       Any          =   scheduler_params

        A :       int          =   """A painting of a squirrel eating a burger"""
        A :       List[Any]          =   jax.device_count()
        A :       str          =   num_samples * [prompt]
        A :       Optional[Any]          =   sd_pipe.prepare_inputs(lowerCamelCase__		)

        A :       str          =   replicate(lowerCamelCase__		)
        A :       int          =   shard(lowerCamelCase__		)

        A :       Optional[Any]          =   jax.random.PRNGKey(0		)
        A :       str          =   jax.random.split(lowerCamelCase__,		jax.device_count()		)

        A :       Optional[int]          =   sd_pipe(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		num_inference_steps=25,		jit=lowerCamelCase__		)[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

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

        A :       str          =   jnp.asarray(jax.device_get(image_slice.flatten()		)		)
        A :       Optional[int]          =   jnp.array([0.4336, 0.4_2969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297]		)
        print(f'''output_slice: {output_slice}'''		)
        assert jnp.abs(output_slice - expected_slice		).max() < 1e-2







from ..utils import DummyObject, requires_backends






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: List[str]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Dict					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Any					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)


