






import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_UpperCAmelCase			:		List[str]   				=    logging.get_logger(__name__)
class  lowerCAmelCase_       :
							UpperCamelCase_		:Union[str, Any]		      =    None





@experimental
def 	lowerCAmelCase_		(lowercase__						:       Dict     ,					lowercase__						:       Dict     ,					lowercase__						:       int     ,					lowercase__						:       List[str]     ,					lowercase__						:       List[str]     ,					lowercase__						:       List[Any]     ,					lowercase__						:       Any       )		->    Optional[Any]:


				'''simple docstring'''




				if ParallelBackendConfig.backend_name is None:
								return _map_with_multiprocessing_pool(
								    lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__       )

				return _map_with_joblib(lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__     ,					lowercase__       )





def 	lowerCAmelCase_		(lowercase__						:       Optional[int]     ,					lowercase__						:       int     ,					lowercase__						:       Union[str, Any]     ,					lowercase__						:       Optional[Any]     ,					lowercase__						:       Tuple     ,					lowercase__						:       Dict     ,					lowercase__						:       Union[str, Any]       )		->    List[Any]:


				'''simple docstring'''




				lowerCAmelCase__      			=						num_proc if num_proc <= len(lowercase__       ) else len(lowercase__       )
				lowerCAmelCase__      			=						[]  # We organize the splits ourselve (contiguous splits)
				for index in range(lowercase__       ):
								lowerCAmelCase__      			=						len(lowercase__       ) // num_proc
								lowerCAmelCase__      			=						len(lowercase__       ) % num_proc
								lowerCAmelCase__      			=						div * index + min(lowercase__     ,					lowercase__       )
								lowerCAmelCase__      			=						start + div + (1 if index < mod else 0)
								split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc)       )

				if len(lowercase__       ) != sum(len(i[1]       ) for i in split_kwds       ):
								raise ValueError(
								    f'Error dividing inputs iterable among processes. '
								    f'Total number of objects {len(lowercase__       )}, '
								    f'length: {sum(len(i[1]       ) for i in split_kwds       )}'       )

				logger.info(
				    f'Spawning {num_proc} processes for {len(lowercase__       )} objects in slices of {[len(i[1]       ) for i in split_kwds]}'       )
				lowerCAmelCase__			,		lowerCAmelCase__      			=						None, None
				if not disable_tqdm:
								lowerCAmelCase__			,		lowerCAmelCase__      			=						(RLock(),), tqdm.set_lock
				with Pool(lowercase__     ,					initargs=lowercase__     ,					initializer=lowercase__       ) as pool:
								lowerCAmelCase__      			=						pool.map(lowercase__     ,					lowercase__       )
				logger.info(f'Finished {num_proc} processes'       )
				lowerCAmelCase__      			=						[obj for proc_res in mapped for obj in proc_res]
				logger.info(f'Unpacked {len(lowercase__       )} objects'       )

				return mapped





def 	lowerCAmelCase_		(lowercase__						:       Dict     ,					lowercase__						:       Tuple     ,					lowercase__						:       int     ,					lowercase__						:       List[Any]     ,					lowercase__						:       str     ,					lowercase__						:       Union[str, Any]     ,					lowercase__						:       int       )		->    Union[str, Any]:


				'''simple docstring'''




				import joblib

				with joblib.parallel_backend(ParallelBackendConfig.backend_name     ,					n_jobs=lowercase__       ):
								return joblib.Parallel()(
								    joblib.delayed(lowercase__       )((function, obj, types, None, True, None)       ) for obj in iterable       )





@experimental
@contextlib.contextmanager
def 	lowerCAmelCase_		(lowercase__						:       str       )		->    Union[str, Any]:


				'''simple docstring'''




				lowerCAmelCase__      			=						backend_name

				if backend_name == "spark":
								from joblibspark import register_spark

								register_spark()

								# TODO: call create_cache_and_write_probe if "download" in steps
								# TODO: raise NotImplementedError when Dataset.map etc is called

				try:
								yield
				finally:
								lowerCAmelCase__      			=						None













from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_UpperCAmelCase			:		List[str]   				=    {
    "configuration_electra": ["ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "ElectraConfig", "ElectraOnnxConfig"],
    "tokenization_electra": ["ElectraTokenizer"],
}

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		Dict   				=    ["ElectraTokenizerFast"]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		Optional[Any]   				=    [
			    "ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "ElectraForCausalLM",
			    "ElectraForMaskedLM",
			    "ElectraForMultipleChoice",
			    "ElectraForPreTraining",
			    "ElectraForQuestionAnswering",
			    "ElectraForSequenceClassification",
			    "ElectraForTokenClassification",
			    "ElectraModel",
			    "ElectraPreTrainedModel",
			    "load_tf_weights_in_electra",
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		Union[str, Any]   				=    [
			    "TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "TFElectraForMaskedLM",
			    "TFElectraForMultipleChoice",
			    "TFElectraForPreTraining",
			    "TFElectraForQuestionAnswering",
			    "TFElectraForSequenceClassification",
			    "TFElectraForTokenClassification",
			    "TFElectraModel",
			    "TFElectraPreTrainedModel",
			]

try:
			if not is_flax_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		Optional[int]   				=    [
			    "FlaxElectraForCausalLM",
			    "FlaxElectraForMaskedLM",
			    "FlaxElectraForMultipleChoice",
			    "FlaxElectraForPreTraining",
			    "FlaxElectraForQuestionAnswering",
			    "FlaxElectraForSequenceClassification",
			    "FlaxElectraForTokenClassification",
			    "FlaxElectraModel",
			    "FlaxElectraPreTrainedModel",
			]


if TYPE_CHECKING:
			from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig
			from .tokenization_electra import ElectraTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_electra_fast import ElectraTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_electra import (
						    ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
						    ElectraForCausalLM,
						    ElectraForMaskedLM,
						    ElectraForMultipleChoice,
						    ElectraForPreTraining,
						    ElectraForQuestionAnswering,
						    ElectraForSequenceClassification,
						    ElectraForTokenClassification,
						    ElectraModel,
						    ElectraPreTrainedModel,
						    load_tf_weights_in_electra,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_electra import (
						    TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFElectraForMaskedLM,
						    TFElectraForMultipleChoice,
						    TFElectraForPreTraining,
						    TFElectraForQuestionAnswering,
						    TFElectraForSequenceClassification,
						    TFElectraForTokenClassification,
						    TFElectraModel,
						    TFElectraPreTrainedModel,
						)

			try:
						if not is_flax_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_flax_electra import (
						    FlaxElectraForCausalLM,
						    FlaxElectraForMaskedLM,
						    FlaxElectraForMultipleChoice,
						    FlaxElectraForPreTraining,
						    FlaxElectraForQuestionAnswering,
						    FlaxElectraForSequenceClassification,
						    FlaxElectraForTokenClassification,
						    FlaxElectraModel,
						    FlaxElectraPreTrainedModel,
						)

else:
			import sys

			_UpperCAmelCase			:		Any   				=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





