

"""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 			_UpperCAmelCase	(						unittest.TestCase):
			def 							__snake_case       (						self   )			->			Tuple:






				'''simple docstring'''







				super().tearDown()
				gc.collect()
			def 							__snake_case       (						self   )			->			Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase			:							Union[str, Any]	  =    FlaxStableDiffusionPipeline.from_pretrained(
				    """stabilityai/stable-diffusion-2"""					,						revision="""bf16"""					,						dtype=jnp.bfloataa					,						)

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

				_UpperCAmelCase			:							Tuple	  =    replicate(__UpperCAmelCase   )
				_UpperCAmelCase			:							Dict	  =    shard(__UpperCAmelCase   )

				_UpperCAmelCase			:							Optional[int]	  =    jax.random.PRNGKey(0   )
				_UpperCAmelCase			:							Optional[Any]	  =    jax.random.split(__UpperCAmelCase					,						jax.device_count()   )

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

				_UpperCAmelCase			:							str	  =    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
				_UpperCAmelCase			:							Any	  =    images[0, 2_53:2_56, 2_53:2_56, -1]

				_UpperCAmelCase			:							Tuple	  =    jnp.asarray(jax.device_get(image_slice.flatten()   )   )
				_UpperCAmelCase			:							Any	  =    jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512]   )
				print(f'''output_slice: {output_slice}'''   )
				assert jnp.abs(output_slice - expected_slice   ).max() < 1e-2
			def 							__snake_case       (						self   )			->			Dict:






				'''simple docstring'''







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

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

				_UpperCAmelCase			:							List[Any]	  =    replicate(__UpperCAmelCase   )
				_UpperCAmelCase			:							List[Any]	  =    shard(__UpperCAmelCase   )

				_UpperCAmelCase			:							Dict	  =    jax.random.PRNGKey(0   )
				_UpperCAmelCase			:							Optional[int]	  =    jax.random.split(__UpperCAmelCase					,						jax.device_count()   )

				_UpperCAmelCase			:							str	  =    sd_pipe(__UpperCAmelCase					,						__UpperCAmelCase					,						__UpperCAmelCase					,						num_inference_steps=25					,						jit=__UpperCAmelCase   )[0]
				assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

				_UpperCAmelCase			:							Union[str, Any]	  =    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
				_UpperCAmelCase			:							Dict	  =    images[0, 2_53:2_56, 2_53:2_56, -1]

				_UpperCAmelCase			:							List[Any]	  =    jnp.asarray(jax.device_get(image_slice.flatten()   )   )
				_UpperCAmelCase			:							Optional[int]	  =    jnp.array([0.4336, 0.42969, 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












"""simple docstring"""





from __future__ import annotations

__A											=		1.6_021e-19  # units = C
def  __A   (_SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE     ,   )					->tuple[str, float]:





  """simple docstring"""


  if (conductivity, electron_conc, mobility).count(0			) != 1:
    raise ValueError('You cannot supply more or less than 2 values'			)
  elif conductivity < 0:
    raise ValueError('Conductivity cannot be negative'			)
  elif electron_conc < 0:
    raise ValueError('Electron concentration cannot be negative'			)
  elif mobility < 0:
    raise ValueError('mobility cannot be negative'			)
  elif conductivity == 0:
    return (
        "conductivity",
        mobility * electron_conc * ELECTRON_CHARGE,
    )
  elif electron_conc == 0:
    return (
        "electron_conc",
        conductivity / (mobility * ELECTRON_CHARGE),
    )
  else:
    return (
        "mobility",
        conductivity / (electron_conc * ELECTRON_CHARGE),
    )


if __name__ == "__main__":
  import doctest

  doctest.testmod()
