




from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


lowerCamelCase__			:       Dict         =							logging.get_logger(__name__)


lowerCamelCase__			:       Optional[int]         =							TypeVar('DatasetType', Dataset, IterableDataset)

def  UpperCAmelCase_							(				__UpperCAmelCase						:     List[DatasetType]      ,      __UpperCAmelCase						:     Optional[List[float]] = None      ,      __UpperCAmelCase						:     Optional[int] = None      ,      __UpperCAmelCase						:     Optional[DatasetInfo] = None      ,      __UpperCAmelCase						:     Optional[NamedSplit] = None      ,      __UpperCAmelCase						:     Literal["first_exhausted", "all_exhausted"] = "first_exhausted"      ,      ) ->			DatasetType:
      from .arrow_dataset import Dataset
      from .iterable_dataset import IterableDataset

      if not datasets:
            raise ValueError('Unable to interleave an empty list of datasets.'							)
      for i, dataset in enumerate(__UpperCAmelCase							):
            if not isinstance(__UpperCAmelCase      ,      (Dataset, IterableDataset)							):
                  if isinstance(__UpperCAmelCase      ,      (DatasetDict, IterableDatasetDict)							):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'							)
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCAmelCase							)}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCAmelCase							)							)}']"							)
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCAmelCase							).__name__}."							)
            if i == 0:
                  SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_     						=      (
                      (Dataset, IterableDataset) if isinstance(__UpperCAmelCase      ,      __UpperCAmelCase							) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCAmelCase      ,      __UpperCAmelCase							):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."							)
      if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
            raise ValueError(f"{stopping_strategy} is not supported. Please enter a valid stopping_strategy."							)
      if dataset_type is Dataset:
            return _interleave_map_style_datasets(
                __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      info=__UpperCAmelCase      ,      split=__UpperCAmelCase      ,      stopping_strategy=__UpperCAmelCase							)
      else:
            return _interleave_iterable_datasets(
                __UpperCAmelCase      ,      __UpperCAmelCase      ,      __UpperCAmelCase      ,      info=__UpperCAmelCase      ,      split=__UpperCAmelCase      ,      stopping_strategy=__UpperCAmelCase							)





def  UpperCAmelCase_							(				__UpperCAmelCase						:     List[DatasetType]      ,      __UpperCAmelCase						:     Optional[DatasetInfo] = None      ,      __UpperCAmelCase						:     Optional[NamedSplit] = None      ,      __UpperCAmelCase						:     int = 0      ,      ) ->			DatasetType:

      if not dsets:
            raise ValueError('Unable to concatenate an empty list of datasets.'							)
      for i, dataset in enumerate(__UpperCAmelCase							):
            if not isinstance(__UpperCAmelCase      ,      (Dataset, IterableDataset)							):
                  if isinstance(__UpperCAmelCase      ,      (DatasetDict, IterableDatasetDict)							):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'							)
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCAmelCase							)}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCAmelCase							)							)}']"							)
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCAmelCase							).__name__}."							)
            if i == 0:
                  SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_     						=      (
                      (Dataset, IterableDataset) if isinstance(__UpperCAmelCase      ,      __UpperCAmelCase							) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCAmelCase      ,      __UpperCAmelCase							):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."							)
      if dataset_type is Dataset:
            return _concatenate_map_style_datasets(__UpperCAmelCase      ,      info=__UpperCAmelCase      ,      split=__UpperCAmelCase      ,      axis=__UpperCAmelCase							)
      else:
            return _concatenate_iterable_datasets(__UpperCAmelCase      ,      info=__UpperCAmelCase      ,      split=__UpperCAmelCase      ,      axis=__UpperCAmelCase							)





from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
   import accelerate

def  UpperCAmelCase_							(				__UpperCAmelCase						:     Optional[int]							) ->			int:
      if not is_accelerate_available():
            return method
      SCREAMING_SNAKE_CASE_     						=      version.parse(accelerate.__version__							).base_version
      if version.parse(__UpperCAmelCase							) < version.parse('0.17.0'							):
            return method

      def wrapper(self						:     Optional[int]      ,      *__UpperCAmelCase						:     Optional[Any]      ,      **__UpperCAmelCase						:     Optional[Any]							):
            if hasattr(self      ,      '_hf_hook'							) and hasattr(self._hf_hook      ,      'pre_forward'							):
                  self._hf_hook.pre_forward(self							)
            return method(self      ,      *__UpperCAmelCase      ,      **__UpperCAmelCase							)

      return wrapper