






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



				"""simple docstring"""



				def        __magic_name__				(   self     :     List[str]):






											'''simple docstring'''







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



				def        __magic_name__				(   self     :     Tuple):






											'''simple docstring'''







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

											snake_case__							=							'A painting of a squirrel eating a burger'
											snake_case__							=							jax.device_count()
											snake_case__							=							num_samples * [prompt]
											snake_case__							=							sd_pipe.prepare_inputs(UpperCamelCase__)

											snake_case__							=							replicate(UpperCamelCase__)
											snake_case__							=							shard(UpperCamelCase__)

											snake_case__							=							jax.random.PRNGKey(0)
											snake_case__							=							jax.random.split(UpperCamelCase__      ,		jax.device_count())

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

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

											snake_case__							=							jnp.asarray(jax.device_get(image_slice.flatten()))
											snake_case__							=							jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12])
											print(F'''output_slice: {output_slice}''')
											assert jnp.abs(output_slice - expected_slice).max() < 1E-2



				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







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

											snake_case__							=							'A painting of a squirrel eating a burger'
											snake_case__							=							jax.device_count()
											snake_case__							=							num_samples * [prompt]
											snake_case__							=							sd_pipe.prepare_inputs(UpperCamelCase__)

											snake_case__							=							replicate(UpperCamelCase__)
											snake_case__							=							shard(UpperCamelCase__)

											snake_case__							=							jax.random.PRNGKey(0)
											snake_case__							=							jax.random.split(UpperCamelCase__      ,		jax.device_count())

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

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

											snake_case__							=							jnp.asarray(jax.device_get(image_slice.flatten()))
											snake_case__							=							jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97])
											print(F'''output_slice: {output_slice}''')
											assert jnp.abs(output_slice - expected_slice).max() < 1E-2




'''simple docstring'''






import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


lowercase				=pytest.mark.integration





@pytest.mark.parametrize('path'			,       ['paws', 'csv']				)
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]				):







   '''simple docstring'''




   inspect_dataset(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[str]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.filterwarnings('ignore:inspect_metric is deprecated:FutureWarning'				)
@pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning'				)
@pytest.mark.parametrize('path'			,       ['accuracy']				)
def lowerCamelCase__     (		__lowerCamelCase			:				Dict			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   inspect_metric(__lowerCamelCase			,       __lowerCamelCase				)
   _UpperCAmelCase  :  List[Any]				       =path + '.py'
   assert script_name in os.listdir(__lowerCamelCase				)
   assert "__pycache__" not in os.listdir(__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, config_name, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Any			,       __lowerCamelCase			:				Optional[int]			,       __lowerCamelCase			:				str				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)
   assert info.config_name == config_name
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_config_info(__lowerCamelCase			,       config_name=__lowerCamelCase				)





@pytest.mark.parametrize(
    'path, expected'			,       [
        ('squad', 'plain_text'),
        ('acronym_identification', 'default'),
        ('lhoestq/squad', 'plain_text'),
        ('lhoestq/test', 'default'),
        ('lhoestq/demo1', 'lhoestq--demo1'),
        ('dalle-mini/wit', 'dalle-mini--wit'),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[str]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Dict				       =get_dataset_config_names(__lowerCamelCase				)
   assert expected in config_names





@pytest.mark.parametrize(
    'path, expected_configs, expected_splits_in_first_config'			,       [
        ('squad', ['plain_text'], ['train', 'validation']),
        ('dalle-mini/wit', ['dalle-mini--wit'], ['train']),
        ('paws', ['labeled_final', 'labeled_swap', 'unlabeled_final'], ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				List[Any]			,       __lowerCamelCase			:				List[Any]				):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =get_dataset_infos(__lowerCamelCase				)
   assert list(infos.keys()				) == expected_configs
   _UpperCAmelCase  :  Optional[int]				       =expected_configs[0]
   assert expected_config in infos
   _UpperCAmelCase  :  Optional[int]				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits_in_first_config





@pytest.mark.parametrize(
    'path, expected_config, expected_splits'			,       [
        ('squad', 'plain_text', ['train', 'validation']),
        ('dalle-mini/wit', 'dalle-mini--wit', ['train']),
        ('paws', 'labeled_final', ['train', 'test', 'validation']),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				Tuple			,       __lowerCamelCase			:				int				):







   '''simple docstring'''




   _UpperCAmelCase  :  str				       =get_dataset_infos(__lowerCamelCase				)
   assert expected_config in infos
   _UpperCAmelCase  :  Dict				       =infos[expected_config]
   assert info.config_name == expected_config
   assert list(info.splits.keys()				) == expected_splits





@pytest.mark.parametrize(
    'path, config_name, expected_exception'			,       [
        ('paws', None, ValueError),
    ]			,       )
def lowerCamelCase__     (		__lowerCamelCase			:				Union[str, Any]			,       __lowerCamelCase			:				Optional[Any]			,       __lowerCamelCase			:				Any				):







   '''simple docstring'''




   with pytest.raises(__lowerCamelCase				):
      get_dataset_split_names(__lowerCamelCase			,       config_name=__lowerCamelCase				)

