





import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_lowerCAmelCase 				=     logging.get_logger(__name__)

class    _UpperCAmelCase							:
						a						       =							None






@experimental
def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase     ):
			'''simple docstring'''


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

			return _map_with_joblib(_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase     )



def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase     ):
			'''simple docstring'''


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

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

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

			return mapped



def     _lowerCAmelCase		(  _lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase  ,_lowerCAmelCase     ):
			'''simple docstring'''


			import joblib

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







@experimental
@contextlib.contextmanager
def     _lowerCAmelCase		(  _lowerCAmelCase     ):
			'''simple docstring'''


			A_     :				Union[str, Any]			      =	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:
						A_     :				Dict			      =	None













# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


_lowerCAmelCase 				=     """3"""

print("""Python version:""", sys.version)
print("""transformers version:""", transformers.__version__)

try:
      import torch

      print("""Torch version:""", torch.__version__)
      print("""Cuda available:""", torch.cuda.is_available())
      print("""Cuda version:""", torch.version.cuda)
      print("""CuDNN version:""", torch.backends.cudnn.version())
      print("""Number of GPUs available:""", torch.cuda.device_count())
      print("""NCCL version:""", torch.cuda.nccl.version())
except ImportError:
      print("""Torch version:""", None)

try:
      import deepspeed

      print("""DeepSpeed version:""", deepspeed.__version__)
except ImportError:
      print("""DeepSpeed version:""", None)

try:
      import tensorflow as tf

      print("""TensorFlow version:""", tf.__version__)
      print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU""")))
      print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU""")))
except ImportError:
      print("""TensorFlow version:""", None)






