




# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
		import torch




class 					snake_case__							(TensorFormatter[Mapping, """torch.Tensor""", Mapping]     ):
				"""simple docstring"""



				def __init__(   self       :  str      ,   __lowerCamelCase       :  List[Any]=None      ,   **__lowerCamelCase       :  Union[str, Any]							)      ->  int:
							super().__init__(features=__lowerCamelCase							)
							a					=    torch_tensor_kwargs
							import torch  # noqa import torch at initialization





				def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  Union[str, Any]							)      ->  str:
							import torch

							if isinstance(__lowerCamelCase      ,   __lowerCamelCase							) and column:
										if all(
										    isinstance(__lowerCamelCase      ,   torch.Tensor							) and x.shape == column[0].shape and x.dtype == column[0].dtype
										    for x in column							):
													return torch.stack(__lowerCamelCase							)
							return column





				def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  Optional[Any]							)      ->  List[Any]:
							import torch

							if isinstance(__lowerCamelCase      ,   (str, bytes, type(__lowerCamelCase							))							):
										return value
							elif isinstance(__lowerCamelCase      ,   (np.character, np.ndarray)							) and np.issubdtype(value.dtype      ,   np.character							):
										return value.tolist()

							a					=    {}

							if isinstance(__lowerCamelCase      ,   (np.number, np.ndarray)							) and np.issubdtype(value.dtype      ,   np.integer							):
										a					=    {"dtype": torch.intaa}
							elif isinstance(__lowerCamelCase      ,   (np.number, np.ndarray)							) and np.issubdtype(value.dtype      ,   np.floating							):
										a					=    {"dtype": torch.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
										import PIL.Image

										if isinstance(__lowerCamelCase      ,   PIL.Image.Image							):
													a					=    np.asarray(__lowerCamelCase							)
							return torch.tensor(__lowerCamelCase      ,   **{**default_dtype, **self.torch_tensor_kwargs}							)





				def 							__UpperCAmelCase							(   self       :  Tuple      ,   __lowerCamelCase       :  Optional[int]							)      ->  List[Any]:
							import torch

							# support for torch, tf, jax etc.
							if hasattr(__lowerCamelCase      ,   "__array__"							) and not isinstance(__lowerCamelCase      ,   torch.Tensor							):
										a					=    data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(__lowerCamelCase      ,   np.ndarray							):
										if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
													return self._consolidate([self.recursive_tensorize(__lowerCamelCase							) for substruct in data_struct]							)
							elif isinstance(__lowerCamelCase      ,   (list, tuple)							):
										return self._consolidate([self.recursive_tensorize(__lowerCamelCase							) for substruct in data_struct]							)
							return self._tensorize(__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  dict							)      ->  Union[str, Any]:
							return map_nested(self._recursive_tensorize      ,   __lowerCamelCase      ,   map_list=__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  pa.Table							)      ->  Mapping:
							a					=    self.numpy_arrow_extractor().extract_row(__lowerCamelCase							)
							a					=    self.python_features_decoder.decode_row(__lowerCamelCase							)
							return self.recursive_tensorize(__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[Any]      ,   __lowerCamelCase       :  pa.Table							)      ->  "torch.Tensor":
							a					=    self.numpy_arrow_extractor().extract_column(__lowerCamelCase							)
							a					=    self.python_features_decoder.decode_column(__lowerCamelCase      ,   pa_table.column_names[0]							)
							a					=    self.recursive_tensorize(__lowerCamelCase							)
							a					=    self._consolidate(__lowerCamelCase							)
							return column





				def 							__UpperCAmelCase							(   self       :  Dict      ,   __lowerCamelCase       :  pa.Table							)      ->  Mapping:
							a					=    self.numpy_arrow_extractor().extract_batch(__lowerCamelCase							)
							a					=    self.python_features_decoder.decode_batch(__lowerCamelCase							)
							a					=    self.recursive_tensorize(__lowerCamelCase							)
							for column_name in batch:
										a					=    self._consolidate(batch[column_name]							)
							return batch












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	:       List[Any]     =	logging.get_logger(__name__)


__lowerCAmelCase	:       List[Any]     =	TypeVar('DatasetType', Dataset, IterableDataset)




def     __magic_name__       (				A	:      List[DatasetType],						A	:      Optional[List[float]] = None,						A	:      Optional[int] = None,						A	:      Optional[DatasetInfo] = None,						A	:      Optional[NamedSplit] = None,						A	:      Literal["first_exhausted", "all_exhausted"] = "first_exhausted",						):






			'''simple docstring'''







			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(A     ):
						if not isinstance(A,						(Dataset, IterableDataset)     ):
									if isinstance(A,						(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(A     )}\n"""
												    F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(A     )     )}']"""     )
									raise ValueError(
									    F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(A     ).__name__}."""     )
						if i == 0:
									a					,	a					=    (
									    (Dataset, IterableDataset) if isinstance(A,						A     ) else (IterableDataset, Dataset)
									)
						elif not isinstance(A,						A     ):
									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(
						    A,						A,						A,						info=A,						split=A,						stopping_strategy=A     )
			else:
						return _interleave_iterable_datasets(
						    A,						A,						A,						info=A,						split=A,						stopping_strategy=A     )






def     __magic_name__       (				A	:      List[DatasetType],						A	:      Optional[DatasetInfo] = None,						A	:      Optional[NamedSplit] = None,						A	:      int = 0,						):






			'''simple docstring'''







			if not dsets:
						raise ValueError("Unable to concatenate an empty list of datasets."     )
			for i, dataset in enumerate(A     ):
						if not isinstance(A,						(Dataset, IterableDataset)     ):
									if isinstance(A,						(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(A     )}\n"""
												    F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(A     )     )}']"""     )
									raise ValueError(
									    F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(A     ).__name__}."""     )
						if i == 0:
									a					,	a					=    (
									    (Dataset, IterableDataset) if isinstance(A,						A     ) else (IterableDataset, Dataset)
									)
						elif not isinstance(A,						A     ):
									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(A,						info=A,						split=A,						axis=A     )
			else:
						return _concatenate_iterable_datasets(A,						info=A,						split=A,						axis=A     )






