'''simple docstring'''





import importlib
import os
import platform
from pathlib import Path

from packaging import version

from .utils.logging import get_logger


UpperCamelCase    :			Union[str, Any]						     =  get_logger(__name__)

# Datasets
UpperCamelCase    :			Any						     =  '''https://s3.amazonaws.com/datasets.huggingface.co/datasets/datasets'''
UpperCamelCase    :			Union[str, Any]						     =  '''https://cdn-datasets.huggingface.co/datasets/datasets'''
UpperCamelCase    :			Optional[Any]						     =  '''https://raw.githubusercontent.com/huggingface/datasets/{revision}/datasets/{path}/{name}'''

# Metrics
UpperCamelCase    :			Tuple						     =  '''https://s3.amazonaws.com/datasets.huggingface.co/datasets/metrics'''
UpperCamelCase    :			int						     =  '''https://cdn-datasets.huggingface.co/datasets/metric'''
UpperCamelCase    :			Dict						     =  '''https://raw.githubusercontent.com/huggingface/datasets/{revision}/metrics/{path}/{name}'''

# Hub
UpperCamelCase    :			str						     =  os.environ.get("""HF_ENDPOINT""", """https://huggingface.co""")
UpperCamelCase    :			Optional[Any]						     =  HF_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}'''
UpperCamelCase    :			Union[str, Any]						     =  '''main'''

UpperCamelCase    :			Union[str, Any]						     =  version.parse(platform.python_version())

if PY_VERSION < version.parse("""3.8"""):
      import importlib_metadata
else:
      import importlib.metadata as importlib_metadata

# General environment variables accepted values for booleans
UpperCamelCase    :			Any						     =  {'''1''', '''ON''', '''YES''', '''TRUE'''}
UpperCamelCase    :			int						     =  ENV_VARS_TRUE_VALUES.union({"""AUTO"""})


# Imports
UpperCamelCase    :			Optional[int]						     =  version.parse(importlib_metadata.version("""dill"""))
UpperCamelCase    :			Optional[Any]						     =  version.parse(importlib_metadata.version("""pandas"""))
UpperCamelCase    :			Any						     =  version.parse(importlib_metadata.version("""pyarrow"""))

UpperCamelCase    :			List[Any]						     =  os.environ.get("""USE_TF""", """AUTO""").upper()
UpperCamelCase    :			Optional[int]						     =  os.environ.get("""USE_TORCH""", """AUTO""").upper()
UpperCamelCase    :			Dict						     =  os.environ.get("""USE_JAX""", """AUTO""").upper()

UpperCamelCase    :			str						     =  '''N/A'''
UpperCamelCase    :			Any						     =  False

if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:
      UpperCamelCase    :			Any						     =  importlib.util.find_spec("""torch""") is not None
      if TORCH_AVAILABLE:
            try:
                  UpperCamelCase    :			List[str]						     =  version.parse(importlib_metadata.version("""torch"""))
                  logger.info(f'''PyTorch version {TORCH_VERSION} available.''')
            except importlib_metadata.PackageNotFoundError:
                  pass
else:
      logger.info("""Disabling PyTorch because USE_TF is set""")

UpperCamelCase    :			List[str]						     =  '''N/A'''
UpperCamelCase    :			List[str]						     =  False

if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:
      UpperCamelCase    :			str						     =  importlib.util.find_spec("""tensorflow""") is not None
      if TF_AVAILABLE:
            # For the metadata, we have to look for both tensorflow and tensorflow-cpu
            for package in [
                "tensorflow",
                "tensorflow-cpu",
                "tensorflow-gpu",
                "tf-nightly",
                "tf-nightly-cpu",
                "tf-nightly-gpu",
                "intel-tensorflow",
                "tensorflow-rocm",
                "tensorflow-macos",
            ]:
                  try:
                        UpperCamelCase    :			List[Any]						     =  version.parse(importlib_metadata.version(package))
                  except importlib_metadata.PackageNotFoundError:
                        continue
                  else:
                        break
            else:
                  UpperCamelCase    :			Dict						     =  False
      if TF_AVAILABLE:
            if TF_VERSION.major < 2:
                  logger.info(f'''TensorFlow found but with version {TF_VERSION}. `datasets` requires version 2 minimum.''')
                  UpperCamelCase    :			List[str]						     =  False
            else:
                  logger.info(f'''TensorFlow version {TF_VERSION} available.''')
else:
      logger.info("""Disabling Tensorflow because USE_TORCH is set""")


UpperCamelCase    :			Any						     =  '''N/A'''
UpperCamelCase    :			Optional[int]						     =  False

if USE_JAX in ENV_VARS_TRUE_AND_AUTO_VALUES:
      UpperCamelCase    :			int						     =  importlib.util.find_spec("""jax""") is not None and importlib.util.find_spec("""jaxlib""") is not None
      if JAX_AVAILABLE:
            try:
                  UpperCamelCase    :			Dict						     =  version.parse(importlib_metadata.version("""jax"""))
                  logger.info(f'''JAX version {JAX_VERSION} available.''')
            except importlib_metadata.PackageNotFoundError:
                  pass
else:
      logger.info("""Disabling JAX because USE_JAX is set to False""")


UpperCamelCase    :			Union[str, Any]						     =  os.environ.get("""USE_BEAM""", """AUTO""").upper()
UpperCamelCase    :			Dict						     =  '''N/A'''
UpperCamelCase    :			Tuple						     =  False
if USE_BEAM in ENV_VARS_TRUE_AND_AUTO_VALUES:
      try:
            UpperCamelCase    :			Tuple						     =  version.parse(importlib_metadata.version("""apache_beam"""))
            UpperCamelCase    :			Optional[int]						     =  True
            logger.info(f'''Apache Beam version {BEAM_VERSION} available.''')
      except importlib_metadata.PackageNotFoundError:
            pass
else:
      logger.info("""Disabling Apache Beam because USE_BEAM is set to False""")


# Optional tools for data loading
UpperCamelCase    :			Union[str, Any]						     =  importlib.util.find_spec("""sqlalchemy""") is not None

# Optional tools for feature decoding
UpperCamelCase    :			int						     =  importlib.util.find_spec("""PIL""") is not None
UpperCamelCase    :			Optional[int]						     =  importlib.util.find_spec("""soundfile""") is not None and version.parse(
    importlib.import_module("""soundfile""").__libsndfile_version__
) >= version.parse("""1.0.31""")
UpperCamelCase    :			Optional[Any]						     =  importlib.util.find_spec("""soundfile""") is not None and version.parse(
    importlib.import_module("""soundfile""").__libsndfile_version__
) >= version.parse("""1.1.0""")

# Optional compression tools
UpperCamelCase    :			Union[str, Any]						     =  importlib.util.find_spec("""rarfile""") is not None
UpperCamelCase    :			List[str]						     =  importlib.util.find_spec("""zstandard""") is not None
UpperCamelCase    :			int						     =  importlib.util.find_spec("""lz4""") is not None
UpperCamelCase    :			Optional[int]						     =  importlib.util.find_spec("""py7zr""") is not None


# Cache location
UpperCamelCase    :			Tuple						     =  '''~/.cache'''
UpperCamelCase    :			Any						     =  os.getenv("""XDG_CACHE_HOME""", DEFAULT_XDG_CACHE_HOME)
UpperCamelCase    :			Dict						     =  os.path.join(XDG_CACHE_HOME, """huggingface""")
UpperCamelCase    :			Union[str, Any]						     =  os.path.expanduser(os.getenv("""HF_HOME""", DEFAULT_HF_CACHE_HOME))

UpperCamelCase    :			Dict						     =  os.path.join(HF_CACHE_HOME, """datasets""")
UpperCamelCase    :			Dict						     =  Path(os.getenv("""HF_DATASETS_CACHE""", DEFAULT_HF_DATASETS_CACHE))

UpperCamelCase    :			int						     =  os.path.join(HF_CACHE_HOME, """metrics""")
UpperCamelCase    :			Optional[Any]						     =  Path(os.getenv("""HF_METRICS_CACHE""", DEFAULT_HF_METRICS_CACHE))

UpperCamelCase    :			List[str]						     =  os.path.join(HF_CACHE_HOME, """modules""")
UpperCamelCase    :			Optional[int]						     =  Path(os.getenv("""HF_MODULES_CACHE""", DEFAULT_HF_MODULES_CACHE))

UpperCamelCase    :			Optional[Any]						     =  '''downloads'''
UpperCamelCase    :			Any						     =  os.path.join(HF_DATASETS_CACHE, DOWNLOADED_DATASETS_DIR)
UpperCamelCase    :			str						     =  Path(os.getenv("""HF_DATASETS_DOWNLOADED_DATASETS_PATH""", DEFAULT_DOWNLOADED_DATASETS_PATH))

UpperCamelCase    :			str						     =  '''extracted'''
UpperCamelCase    :			Optional[Any]						     =  os.path.join(DEFAULT_DOWNLOADED_DATASETS_PATH, EXTRACTED_DATASETS_DIR)
UpperCamelCase    :			List[Any]						     =  Path(os.getenv("""HF_DATASETS_EXTRACTED_DATASETS_PATH""", DEFAULT_EXTRACTED_DATASETS_PATH))

# Download count for the website
UpperCamelCase    :			int						     =  (
    os.environ.get("""HF_UPDATE_DOWNLOAD_COUNTS""", """AUTO""").upper() in ENV_VARS_TRUE_AND_AUTO_VALUES
)

# Batch size constants. For more info, see:
# https://github.com/apache/arrow/blob/master/docs/source/cpp/arrays.rst#size-limitations-and-recommendations)
UpperCamelCase    :			List[str]						     =  1_000

# Size of the preloaded record batch in `Dataset.__iter__`
UpperCamelCase    :			Optional[Any]						     =  10

# Pickling tables works only for small tables (<4GiB)
# For big tables, we write them on disk instead
UpperCamelCase    :			List[str]						     =  4 << 30

# Max shard size in bytes (e.g. to shard parquet datasets in push_to_hub or download_and_prepare)
UpperCamelCase    :			List[Any]						     =  '''500MB'''

# Parquet configuration
UpperCamelCase    :			int						     =  100
UpperCamelCase    :			Union[str, Any]						     =  100
UpperCamelCase    :			Optional[Any]						     =  100

# Offline mode
UpperCamelCase    :			Tuple						     =  os.environ.get("""HF_DATASETS_OFFLINE""", """AUTO""").upper() in ENV_VARS_TRUE_VALUES

# In-memory
UpperCamelCase    :			str						     =  0  # Disabled
UpperCamelCase    :			int						     =  float(os.environ.get("""HF_DATASETS_IN_MEMORY_MAX_SIZE""", DEFAULT_IN_MEMORY_MAX_SIZE))

# File names
UpperCamelCase    :			Any						     =  '''dataset.arrow'''
UpperCamelCase    :			Tuple						     =  '''indices.arrow'''
UpperCamelCase    :			Optional[int]						     =  '''state.json'''
UpperCamelCase    :			Tuple						     =  '''dataset_info.json'''
UpperCamelCase    :			Dict						     =  '''dataset_infos.json'''
UpperCamelCase    :			Optional[Any]						     =  '''LICENSE'''
UpperCamelCase    :			List[Any]						     =  '''metric_info.json'''
UpperCamelCase    :			Tuple						     =  '''dataset_dict.json'''

UpperCamelCase    :			str						     =  '''datasets_modules'''

UpperCamelCase    :			str						     =  255

# Streaming
UpperCamelCase    :			List[Any]						     =  20
UpperCamelCase    :			List[str]						     =  5

# Datasets without script
UpperCamelCase    :			List[str]						     =  200
UpperCamelCase    :			Optional[Any]						     =  10
UpperCamelCase    :			Union[str, Any]						     =  200

# Progress bars
UpperCamelCase    :			Union[str, Any]						     =  0.05  # 20 progress updates per sec


import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class   lowercase			(   a ,				a ,				unittest.TestCase   ):
							lowercase__  :				Optional[Any]									=							StableDiffusionParadigmsPipeline
							lowercase__  :				Optional[Any]									=							TEXT_TO_IMAGE_PARAMS
							lowercase__  :				Union[str, Any]									=							TEXT_TO_IMAGE_BATCH_PARAMS
							lowercase__  :				int									=							TEXT_TO_IMAGE_IMAGE_PARAMS
							lowercase__  :				List[Any]									=							TEXT_TO_IMAGE_IMAGE_PARAMS




							def 		__snake_case(  self      :    Tuple					)    -> List[Any]:



												'''simple docstring'''

												torch.manual_seed(0					)
												SCREAMING_SNAKE_CASE				=				UNetaDConditionModel(
												    block_out_channels=(32, 64)		,							layers_per_block=2		,							sample_size=32		,							in_channels=4		,							out_channels=4		,							down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")		,							up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")		,							cross_attention_dim=32		,							attention_head_dim=(2, 4)		,							use_linear_projection=_UpperCamelCase		,							)
												SCREAMING_SNAKE_CASE				=				DDIMParallelScheduler(
												    beta_start=0.0_0_0_8_5		,							beta_end=0.0_1_2		,							beta_schedule="scaled_linear"		,							clip_sample=_UpperCamelCase		,							set_alpha_to_one=_UpperCamelCase		,							)
												torch.manual_seed(0					)
												SCREAMING_SNAKE_CASE				=				AutoencoderKL(
												    block_out_channels=[32, 64]		,							in_channels=3		,							out_channels=3		,							down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]		,							up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]		,							latent_channels=4		,							sample_size=128		,							)
												torch.manual_seed(0					)
												SCREAMING_SNAKE_CASE				=				CLIPTextConfig(
												    bos_token_id=0		,							eos_token_id=2		,							hidden_size=32		,							intermediate_size=37		,							layer_norm_eps=1e-05		,							num_attention_heads=4		,							num_hidden_layers=5		,							pad_token_id=1		,							vocab_size=1_000		,							hidden_act="gelu"		,							projection_dim=512		,							)
												SCREAMING_SNAKE_CASE				=				CLIPTextModel(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

												SCREAMING_SNAKE_CASE				=				{
												    "unet": unet,
												    "scheduler": scheduler,
												    "vae": vae,
												    "text_encoder": text_encoder,
												    "tokenizer": tokenizer,
												    "safety_checker": None,
												    "feature_extractor": None,
												}
												return components




							def 		__snake_case(  self      :    Any		,							_UpperCamelCase      :    List[str]		,							_UpperCamelCase      :    Any=0					)    -> Optional[int]:



												'''simple docstring'''

												if str(_UpperCamelCase					).startswith("mps"					):
																	SCREAMING_SNAKE_CASE				=				torch.manual_seed(_UpperCamelCase					)
												else:
																	SCREAMING_SNAKE_CASE				=				torch.Generator(device=_UpperCamelCase					).manual_seed(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				{
												    "prompt": "a photograph of an astronaut riding a horse",
												    "generator": generator,
												    "num_inference_steps": 10,
												    "guidance_scale": 6.0,
												    "output_type": "numpy",
												    "parallel": 3,
												    "debug": True,
												}
												return inputs




							def 		__snake_case(  self      :    Optional[Any]					)    -> Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				"cpu"  # ensure determinism for the device-dependent torch.Generator
												SCREAMING_SNAKE_CASE				=				self.get_dummy_components()
												SCREAMING_SNAKE_CASE				=				StableDiffusionParadigmsPipeline(**_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				sd_pipe.to(_UpperCamelCase					)
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase					)

												SCREAMING_SNAKE_CASE				=				self.get_dummy_inputs(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				sd_pipe(**_UpperCamelCase					).images
												SCREAMING_SNAKE_CASE				=				image[0, -3:, -3:, -1]
												assert image.shape == (1, 64, 64, 3)

												SCREAMING_SNAKE_CASE				=				np.array([0.4_7_7_3, 0.5_4_1_7, 0.4_7_2_3, 0.4_9_2_5, 0.5_6_3_1, 0.4_7_5_2, 0.5_2_4_0, 0.4_9_3_5, 0.5_0_2_3]					)

												assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2




							def 		__snake_case(  self      :    Optional[Any]					)    -> List[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				"cpu"  # ensure determinism for the device-dependent torch.Generator
												SCREAMING_SNAKE_CASE				=				self.get_dummy_components()
												torch.manual_seed(0					)
												SCREAMING_SNAKE_CASE				=				DDPMParallelScheduler()
												torch.manual_seed(0					)
												SCREAMING_SNAKE_CASE				=				StableDiffusionParadigmsPipeline(**_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				sd_pipe.to(_UpperCamelCase					)
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase					)

												SCREAMING_SNAKE_CASE				=				self.get_dummy_inputs(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				sd_pipe(**_UpperCamelCase					).images
												SCREAMING_SNAKE_CASE				=				image[0, -3:, -3:, -1]
												assert image.shape == (1, 64, 64, 3)

												SCREAMING_SNAKE_CASE				=				np.array([0.3_5_7_3, 0.4_4_2_0, 0.4_9_6_0, 0.4_7_9_9, 0.3_7_9_6, 0.3_8_7_9, 0.4_8_1_9, 0.4_3_6_5, 0.4_4_6_8]					)

												assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2




							def 		__snake_case(  self      :    Tuple					)    -> int:



												'''simple docstring'''

												super().test_inference_batch_consistent(batch_sizes=[1, 2]					)




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



												'''simple docstring'''

												super().test_inference_batch_single_identical(batch_size=2		,							expected_max_diff=3e-3					)





							def 		__snake_case(  self      :    List[Any]					)    -> str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				"cpu"  # ensure determinism for the device-dependent torch.Generator
												SCREAMING_SNAKE_CASE				=				self.get_dummy_components()
												SCREAMING_SNAKE_CASE				=				StableDiffusionParadigmsPipeline(**_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				sd_pipe.to(_UpperCamelCase					)
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase					)

												SCREAMING_SNAKE_CASE				=				self.get_dummy_inputs(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				"french fries"
												SCREAMING_SNAKE_CASE				=				sd_pipe(**_UpperCamelCase		,							negative_prompt=_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				output.images
												SCREAMING_SNAKE_CASE				=				image[0, -3:, -3:, -1]

												assert image.shape == (1, 64, 64, 3)

												SCREAMING_SNAKE_CASE				=				np.array([0.4_7_7_1, 0.5_4_2_0, 0.4_6_8_3, 0.4_9_1_8, 0.5_6_3_6, 0.4_7_2_5, 0.5_2_3_0, 0.4_9_2_3, 0.5_0_1_5]					)

												assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2



@slow
@require_torch_gpu
class   lowercase			(   unittest.TestCase   ):




							def 		__snake_case(  self      :    List[str]					)    -> List[Any]:



												'''simple docstring'''

												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




							def 		__snake_case(  self      :    Optional[Any]		,							_UpperCamelCase      :    int=0					)    -> Tuple:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				torch.Generator(device=_UpperCamelCase					).manual_seed(_UpperCamelCase					)
												SCREAMING_SNAKE_CASE				=				{
												    "prompt": "a photograph of an astronaut riding a horse",
												    "generator": generator,
												    "num_inference_steps": 10,
												    "guidance_scale": 7.5,
												    "output_type": "numpy",
												    "parallel": 3,
												    "debug": True,
												}
												return inputs





							def 		__snake_case(  self      :    Dict					)    -> str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE				=				"stabilityai/stable-diffusion-2-base"
												SCREAMING_SNAKE_CASE				=				DDIMParallelScheduler.from_pretrained(_UpperCamelCase		,							subfolder="scheduler"					)
												SCREAMING_SNAKE_CASE				=				StableDiffusionParadigmsPipeline.from_pretrained(_UpperCamelCase		,							scheduler=_UpperCamelCase		,							safety_checker=_UpperCamelCase					)
												pipe.to(_UpperCamelCase					)
												pipe.set_progress_bar_config(disable=_UpperCamelCase					)
												pipe.enable_attention_slicing()

												SCREAMING_SNAKE_CASE				=				self.get_inputs()
												SCREAMING_SNAKE_CASE				=				pipe(**_UpperCamelCase					).images
												SCREAMING_SNAKE_CASE				=				image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 512, 512, 3)

												SCREAMING_SNAKE_CASE				=				np.array([0.9_6_2_2, 0.9_6_0_2, 0.9_7_4_8, 0.9_5_9_1, 0.9_6_3_0, 0.9_6_9_1, 0.9_6_6_1, 0.9_6_3_1, 0.9_7_4_1]					)

												assert np.abs(expected_slice - image_slice					).max() < 1e-2




