





"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
  import torch

  from transformers.activations import gelu_new, gelu_python, get_activation




@require_torch
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] )
        __UpperCAmelCase    :						Any       =						get_activation('''gelu''' )
        self.assertTrue(torch.allclose(gelu_python(__snake_case )						,				torch_builtin(__snake_case ) ) )
        self.assertFalse(torch.allclose(gelu_python(__snake_case )						,				gelu_new(__snake_case ) ) )

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						int       =						torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] )
        __UpperCAmelCase    :						List[Any]       =						get_activation('''gelu''' )
        __UpperCAmelCase    :						Any       =						get_activation('''gelu_10''' )

        __UpperCAmelCase    :						int       =						torch_builtin(__snake_case )
        __UpperCAmelCase    :						Any       =						geluaa(__snake_case )

        __UpperCAmelCase    :						Dict       =						torch.where(y_gelu_aa < 10.0						,				1						,				0 )

        self.assertTrue(torch.max(__snake_case ).item() == 10.0 )
        self.assertTrue(torch.allclose(y_gelu * clipped_mask						,				y_gelu_aa * clipped_mask ) )

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        get_activation('''gelu''' )
        get_activation('''gelu_10''' )
        get_activation('''gelu_fast''' )
        get_activation('''gelu_new''' )
        get_activation('''gelu_python''' )
        get_activation('''gelu_pytorch_tanh''' )
        get_activation('''linear''' )
        get_activation('''mish''' )
        get_activation('''quick_gelu''' )
        get_activation('''relu''' )
        get_activation('''sigmoid''' )
        get_activation('''silu''' )
        get_activation('''swish''' )
        get_activation('''tanh''' )
        with self.assertRaises(__snake_case ):
          get_activation('''bogus''' )
        with self.assertRaises(__snake_case ):
          get_activation(__snake_case )







      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						get_activation('''gelu''' )
        __UpperCAmelCase    :						Optional[Any]       =						1
        __UpperCAmelCase    :						List[str]       =						get_activation('''gelu''' )
        self.assertEqual(acta.a						,				1 )
        with self.assertRaises(__snake_case ):
          __UpperCAmelCase    :						Dict       =						acta.a






"""simple docstring"""





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

      '''simple docstring'''



      def       A_					(							self       :      str ):


        '''simple docstring'''




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

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase       ,     __UpperCAmelCase    :						List[Any]       =						FlaxStableDiffusionPipeline.from_pretrained(
            '''stabilityai/stable-diffusion-2'''						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)

        __UpperCAmelCase    :						Dict       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						Any       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						List[Any]       =						replicate(__lowercase )
        __UpperCAmelCase    :						Optional[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						str       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						Dict       =						jax.random.split(__lowercase						,				jax.device_count() )

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

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

        __UpperCAmelCase    :						str       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						List[Any]       =						jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2







      def       A_					(							self       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						'''stabilityai/stable-diffusion-2'''
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Any       =						FlaxDPMSolverMultistepScheduler.from_pretrained(__lowercase						,				subfolder='''scheduler''' )
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Optional[int]       =						FlaxStableDiffusionPipeline.from_pretrained(
            __lowercase						,				scheduler=__lowercase						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)
        __UpperCAmelCase    :						Dict       =						scheduler_params

        __UpperCAmelCase    :						int       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						List[Any]       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						Optional[int]       =						replicate(__lowercase )
        __UpperCAmelCase    :						List[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						Union[str, Any]       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						List[Any]       =						jax.random.split(__lowercase						,				jax.device_count() )

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

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

        __UpperCAmelCase    :						List[str]       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						Any       =						jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2