
"""simple docstring"""




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__							=				logging.get_logger(__name__)


lowerCAmelCase__							=				TypeVar('DatasetType', Dataset, IterableDataset)



def        _lowerCamelCase ( __a,							__a = None,							__a = None,							__a = None,							__a = None,							__a = "first_exhausted",							):
		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:
						SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_						 = (
						    (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        _lowerCamelCase ( __a,							__a = None,							__a = None,							__a = 0,							):

		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:
						SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_						 = (
						    (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    )

"""simple docstring"""




import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
						from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
						import torch_xla.core.xla_model as xm



def        _lowerCamelCase ( __a    ):
		if is_torch_version('''<''',							'''2.0.0'''    ) or not hasattr(__a,							'''_dynamo'''    ):
				return False
		return isinstance(__a,							torch._dynamo.eval_frame.OptimizedModule    )



def        _lowerCamelCase ( __a,							__a = True    ):
		SCREAMING_SNAKE_CASE_						 = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		SCREAMING_SNAKE_CASE_						 = is_compiled_module(__a    )
		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = model._orig_mod

		if is_deepspeed_available():
				options += (DeepSpeedEngine,)

		while isinstance(__a,							__a    ):
				SCREAMING_SNAKE_CASE_						 = model.module

		if not keep_fpaa_wrapper:
				SCREAMING_SNAKE_CASE_						 = getattr(__a,							'''forward'''    )
				SCREAMING_SNAKE_CASE_						 = model.__dict__.pop('''_original_forward''',							__a    )
				if original_forward is not None:
						while hasattr(__a,							'''__wrapped__'''    ):
								SCREAMING_SNAKE_CASE_						 = forward.__wrapped__
								if forward == original_forward:
										break
						SCREAMING_SNAKE_CASE_						 = forward
				if getattr(__a,							'''_converted_to_transformer_engine''',							__a    ):
						convert_model(__a,							to_transformer_engine=__a    )

		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = compiled_model

		return model



def        _lowerCamelCase ( ):
		PartialState().wait_for_everyone()



def        _lowerCamelCase ( __a,							__a    ):
		if PartialState().distributed_type == DistributedType.TPU:
				xm.save(__a,							__a    )
		elif PartialState().local_process_index == 0:
				torch.save(__a,							__a    )



@contextmanager
def        _lowerCamelCase ( **__a    ):
		for key, value in kwargs.items():
				SCREAMING_SNAKE_CASE_						 = str(__a    )

		yield

		for key in kwargs:
				if key.upper() in os.environ:
						del os.environ[key.upper()]



def        _lowerCamelCase ( __a    ):
		if not hasattr(__a,							'''__qualname__'''    ) and not hasattr(__a,							'''__name__'''    ):
				SCREAMING_SNAKE_CASE_						 = getattr(__a,							'''__class__''',							__a    )
		if hasattr(__a,							'''__qualname__'''    ):
				return obj.__qualname__
		if hasattr(__a,							'''__name__'''    ):
				return obj.__name__
		return str(__a    )



def        _lowerCamelCase ( __a,							__a    ):
		for key, value in source.items():
				if isinstance(__a,							__a    ):
						SCREAMING_SNAKE_CASE_						 = destination.setdefault(__a,							{}    )
						merge_dicts(__a,							__a    )
				else:
						SCREAMING_SNAKE_CASE_						 = value

		return destination



def        _lowerCamelCase ( __a = None    ):
		if port is None:
				SCREAMING_SNAKE_CASE_						 = 29_500
		with socket.socket(socket.AF_INET,							socket.SOCK_STREAM    ) as s:
				return s.connect_ex(('''localhost''', port)    ) == 0