




'''simple docstring'''
import importlib
import os
import platform
from pathlib import Path

from packaging import version

from .utils.logging import get_logger


A							:      int 	=       get_logger(__name__)

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

# Metrics
A							:      Optional[int] 	=       """https://s3.amazonaws.com/datasets.huggingface.co/datasets/metrics"""
A							:      Optional[int] 	=       """https://cdn-datasets.huggingface.co/datasets/metric"""
A							:      Dict 	=       """https://raw.githubusercontent.com/huggingface/datasets/{revision}/metrics/{path}/{name}"""

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

A							:      Optional[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
A							:      Tuple 	=       {"""1""", """ON""", """YES""", """TRUE"""}
A							:      List[str] 	=       ENV_VARS_TRUE_VALUES.union({"""AUTO"""})


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

A							:      Any 	=       os.environ.get("""USE_TF""", """AUTO""").upper()
A							:      Dict 	=       os.environ.get("""USE_TORCH""", """AUTO""").upper()
A							:      Dict 	=       os.environ.get("""USE_JAX""", """AUTO""").upper()

A							:      List[Any] 	=       """N/A"""
A							:      Dict 	=       False

if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:
  A							:      Optional[Any] 	=       importlib.util.find_spec("""torch""") is not None
  if TORCH_AVAILABLE:
    try:
      A							:      Optional[Any] 	=       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""")

A							:      List[str] 	=       """N/A"""
A							:      Optional[int] 	=       False

if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:
  A							:      Dict 	=       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:
        A							:      Optional[Any] 	=       version.parse(importlib_metadata.version(package))
      except importlib_metadata.PackageNotFoundError:
        continue
      else:
        break
    else:
      A							:      Optional[int] 	=       False
  if TF_AVAILABLE:
    if TF_VERSION.major < 2:
      logger.info(F"""TensorFlow found but with version {TF_VERSION}. `datasets` requires version 2 minimum.""")
      A							:      Any 	=       False
    else:
      logger.info(F"""TensorFlow version {TF_VERSION} available.""")
else:
  logger.info("""Disabling Tensorflow because USE_TORCH is set""")


A							:      List[Any] 	=       """N/A"""
A							:      Any 	=       False

if USE_JAX in ENV_VARS_TRUE_AND_AUTO_VALUES:
  A							:      List[str] 	=       importlib.util.find_spec("""jax""") is not None and importlib.util.find_spec("""jaxlib""") is not None
  if JAX_AVAILABLE:
    try:
      A							:      Tuple 	=       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""")


A							:      Dict 	=       os.environ.get("""USE_BEAM""", """AUTO""").upper()
A							:      Dict 	=       """N/A"""
A							:      List[Any] 	=       False
if USE_BEAM in ENV_VARS_TRUE_AND_AUTO_VALUES:
  try:
    A							:      int 	=       version.parse(importlib_metadata.version("""apache_beam"""))
    A							:      Any 	=       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
A							:      List[str] 	=       importlib.util.find_spec("""sqlalchemy""") is not None

# Optional tools for feature decoding
A							:      List[str] 	=       importlib.util.find_spec("""PIL""") is not None
A							:      Optional[int] 	=       importlib.util.find_spec("""soundfile""") is not None and version.parse(
    importlib.import_module("""soundfile""").__libsndfile_version__
) >= version.parse("""1.0.31""")
A							:      Tuple 	=       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
A							:      Tuple 	=       importlib.util.find_spec("""rarfile""") is not None
A							:      Optional[Any] 	=       importlib.util.find_spec("""zstandard""") is not None
A							:      Optional[int] 	=       importlib.util.find_spec("""lz4""") is not None
A							:      Optional[int] 	=       importlib.util.find_spec("""py7zr""") is not None


# Cache location
A							:      Dict 	=       """~/.cache"""
A							:      Tuple 	=       os.getenv("""XDG_CACHE_HOME""", DEFAULT_XDG_CACHE_HOME)
A							:      Dict 	=       os.path.join(XDG_CACHE_HOME, """huggingface""")
A							:      Optional[int] 	=       os.path.expanduser(os.getenv("""HF_HOME""", DEFAULT_HF_CACHE_HOME))

A							:      Optional[int] 	=       os.path.join(HF_CACHE_HOME, """datasets""")
A							:      Any 	=       Path(os.getenv("""HF_DATASETS_CACHE""", DEFAULT_HF_DATASETS_CACHE))

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

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

A							:      List[str] 	=       """downloads"""
A							:      Optional[int] 	=       os.path.join(HF_DATASETS_CACHE, DOWNLOADED_DATASETS_DIR)
A							:      Union[str, Any] 	=       Path(os.getenv("""HF_DATASETS_DOWNLOADED_DATASETS_PATH""", DEFAULT_DOWNLOADED_DATASETS_PATH))

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

# Download count for the website
A							:      List[Any] 	=       (
    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)
A							:      Any 	=       1_000

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

# Pickling tables works only for small tables (<4GiB)
# For big tables, we write them on disk instead
A							:      Any 	=       4 << 30

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

# Parquet configuration
A							:      Optional[Any] 	=       100
A							:      Dict 	=       100
A							:      int 	=       100

# Offline mode
A							:      Union[str, Any] 	=       os.environ.get("""HF_DATASETS_OFFLINE""", """AUTO""").upper() in ENV_VARS_TRUE_VALUES

# In-memory
A							:      Union[str, Any] 	=       0  # Disabled
A							:      Any 	=       float(os.environ.get("""HF_DATASETS_IN_MEMORY_MAX_SIZE""", DEFAULT_IN_MEMORY_MAX_SIZE))

# File names
A							:      str 	=       """dataset.arrow"""
A							:      Dict 	=       """indices.arrow"""
A							:      Any 	=       """state.json"""
A							:      str 	=       """dataset_info.json"""
A							:      List[str] 	=       """dataset_infos.json"""
A							:      List[str] 	=       """LICENSE"""
A							:      int 	=       """metric_info.json"""
A							:      Tuple 	=       """dataset_dict.json"""

A							:      Union[str, Any] 	=       """datasets_modules"""

A							:      List[Any] 	=       255

# Streaming
A							:      int 	=       20
A							:      int 	=       5

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

# Progress bars
A							:      Optional[Any] 	=       0.05  # 20 progress updates per sec










'''simple docstring'''
import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class        SCREAMING_SNAKE_CASE(				__A     ):
       snake_case_ :     Any    =					(HeunDiscreteScheduler,)
       snake_case_ :     Any    =					10

       def     snake_case__			(    self					,		**lowerCamelCase__     )    ->					int:

           """simple docstring"""



           __lowercase							=						{
               """num_train_timesteps""": 1100,
               """beta_start""": 0.00_01,
               """beta_end""": 0.02,
               """beta_schedule""": """linear""",
           }

           config.update(**lowerCamelCase__     )
           return config

       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           for timesteps in [10, 50, 100, 1000]:
               self.check_over_configs(num_train_timesteps=lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]					,		[0.00_02, 0.0_02, 0.02]     ):
               self.check_over_configs(beta_start=lowerCamelCase__					,		beta_end=lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           for schedule in ["linear", "scaled_linear", "exp"]:
               self.check_over_configs(beta_schedule=lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           for clip_sample_range in [1.0, 2.0, 3.0]:
               self.check_over_configs(clip_sample_range=lowerCamelCase__					,		clip_sample=lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					Optional[int]:

           """simple docstring"""



           for prediction_type in ["epsilon", "v_prediction", "sample"]:
               self.check_over_configs(prediction_type=lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						self.scheduler_classes[0]
           __lowercase							=						self.get_scheduler_config()
           __lowercase							=						scheduler_class(**lowerCamelCase__     )

           scheduler.set_timesteps(self.num_inference_steps     )

           __lowercase							=						self.dummy_model()
           __lowercase							=						self.dummy_sample_deter * scheduler.init_noise_sigma
           __lowercase							=						sample.to(lowerCamelCase__     )

           for i, t in enumerate(scheduler.timesteps     ):
               __lowercase							=						scheduler.scale_model_input(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						model(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						scheduler.step(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )
               __lowercase							=						output.prev_sample

           __lowercase							=						torch.sum(torch.abs(lowerCamelCase__     )     )
           __lowercase							=						torch.mean(torch.abs(lowerCamelCase__     )     )

           if torch_device in ["cpu", "mps"]:
               assert abs(result_sum.item() - 0.12_33     ) < 1E-2
               assert abs(result_mean.item() - 0.00_02     ) < 1E-3
           else:
               # CUDA
               assert abs(result_sum.item() - 0.12_33     ) < 1E-2
               assert abs(result_mean.item() - 0.00_02     ) < 1E-3

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						self.scheduler_classes[0]
           __lowercase							=						self.get_scheduler_config(prediction_type="""v_prediction"""     )
           __lowercase							=						scheduler_class(**lowerCamelCase__     )

           scheduler.set_timesteps(self.num_inference_steps     )

           __lowercase							=						self.dummy_model()
           __lowercase							=						self.dummy_sample_deter * scheduler.init_noise_sigma
           __lowercase							=						sample.to(lowerCamelCase__     )

           for i, t in enumerate(scheduler.timesteps     ):
               __lowercase							=						scheduler.scale_model_input(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						model(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						scheduler.step(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )
               __lowercase							=						output.prev_sample

           __lowercase							=						torch.sum(torch.abs(lowerCamelCase__     )     )
           __lowercase							=						torch.mean(torch.abs(lowerCamelCase__     )     )

           if torch_device in ["cpu", "mps"]:
               assert abs(result_sum.item() - 4.6_9_3_4E-0_7     ) < 1E-2
               assert abs(result_mean.item() - 6.1_1_1_2E-1_0     ) < 1E-3
           else:
               # CUDA
               assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2E-0_7     ) < 1E-2
               assert abs(result_mean.item() - 0.00_02     ) < 1E-3

       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           __lowercase							=						self.scheduler_classes[0]
           __lowercase							=						self.get_scheduler_config()
           __lowercase							=						scheduler_class(**lowerCamelCase__     )

           scheduler.set_timesteps(self.num_inference_steps					,		device=lowerCamelCase__     )

           __lowercase							=						self.dummy_model()
           __lowercase							=						self.dummy_sample_deter.to(lowerCamelCase__     ) * scheduler.init_noise_sigma

           for t in scheduler.timesteps:
               __lowercase							=						scheduler.scale_model_input(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						model(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						scheduler.step(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )
               __lowercase							=						output.prev_sample

           __lowercase							=						torch.sum(torch.abs(lowerCamelCase__     )     )
           __lowercase							=						torch.mean(torch.abs(lowerCamelCase__     )     )

           if str(lowerCamelCase__     ).startswith("""cpu"""     ):
               # The following sum varies between 148 and 156 on mps. Why?
               assert abs(result_sum.item() - 0.12_33     ) < 1E-2
               assert abs(result_mean.item() - 0.00_02     ) < 1E-3
           elif str(lowerCamelCase__     ).startswith("""mps"""     ):
               # Larger tolerance on mps
               assert abs(result_mean.item() - 0.00_02     ) < 1E-2
           else:
               # CUDA
               assert abs(result_sum.item() - 0.12_33     ) < 1E-2
               assert abs(result_mean.item() - 0.00_02     ) < 1E-3




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

           """simple docstring"""



           __lowercase							=						self.scheduler_classes[0]
           __lowercase							=						self.get_scheduler_config()
           __lowercase							=						scheduler_class(**lowerCamelCase__					,		use_karras_sigmas=lowerCamelCase__     )

           scheduler.set_timesteps(self.num_inference_steps					,		device=lowerCamelCase__     )

           __lowercase							=						self.dummy_model()
           __lowercase							=						self.dummy_sample_deter.to(lowerCamelCase__     ) * scheduler.init_noise_sigma
           __lowercase							=						sample.to(lowerCamelCase__     )

           for t in scheduler.timesteps:
               __lowercase							=						scheduler.scale_model_input(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						model(lowerCamelCase__					,		lowerCamelCase__     )

               __lowercase							=						scheduler.step(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )
               __lowercase							=						output.prev_sample

           __lowercase							=						torch.sum(torch.abs(lowerCamelCase__     )     )
           __lowercase							=						torch.mean(torch.abs(lowerCamelCase__     )     )

           assert abs(result_sum.item() - 0.0_00_15     ) < 1E-2
           assert abs(result_mean.item() - 1.9_8_6_9_5_5_4_5_3_5_0_3_4_6_9_5E-0_7     ) < 1E-2




