
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    A (      lowercase				)     ->						List[str]:
  '''simple docstring'''

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


def    A (      lowercase					, lowercase = True				)     ->						Any:
  '''simple docstring'''

  UpperCamelCase						=  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  UpperCamelCase						=  is_compiled_module(lowercase				)
  if is_compiled:
    UpperCamelCase						=  model
    UpperCamelCase						=  model._orig_mod

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

  while isinstance(lowercase					, lowercase				):
    UpperCamelCase						=  model.module

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

  if is_compiled:
    UpperCamelCase						=  model
    UpperCamelCase						=  compiled_model

  return model


def    A (      )     ->						Optional[int]:
  '''simple docstring'''

  PartialState().wait_for_everyone()


def    A (      lowercase					, lowercase				)     ->						Union[str, Any]:
  '''simple docstring'''

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


@contextmanager
def    A (      **lowercase				)     ->						Optional[Any]:
  '''simple docstring'''

  for key, value in kwargs.items():
    UpperCamelCase						=  str(lowercase				)

  yield

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


def    A (      lowercase				)     ->						str:
  '''simple docstring'''

  if not hasattr(lowercase					, '__qualname__'				) and not hasattr(lowercase					, '__name__'				):
    UpperCamelCase						=  getattr(lowercase					, '__class__'					, lowercase				)
  if hasattr(lowercase					, '__qualname__'				):
    return obj.__qualname__
  if hasattr(lowercase					, '__name__'				):
    return obj.__name__
  return str(lowercase				)


def    A (      lowercase					, lowercase				)     ->						int:
  '''simple docstring'''

  for key, value in source.items():
    if isinstance(lowercase					, lowercase				):
      UpperCamelCase						=  destination.setdefault(lowercase					, {}				)
      merge_dicts(lowercase					, lowercase				)
    else:
      UpperCamelCase						=  value

  return destination


def    A (      lowercase = None				)     ->						bool:
  '''simple docstring'''

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


import pytest

import datasets.config
from datasets.utils.info_utils import is_small_dataset


@pytest.mark.parametrize('dataset_size'					, [None, 400 * 2**20, 600 * 2**20]				)
@pytest.mark.parametrize('input_in_memory_max_size'					, ['default', 0, 100 * 2**20, 900 * 2**20]				)
def    A (      lowercase					, lowercase					, lowercase				)     ->						Union[str, Any]:
		'''simple docstring'''

		if input_in_memory_max_size != "default":
				monkeypatch.setattr(datasets.config					, 'IN_MEMORY_MAX_SIZE'					, lowercase				)
		UpperCamelCase						=  datasets.config.IN_MEMORY_MAX_SIZE
		if input_in_memory_max_size == "default":
				assert in_memory_max_size == 0
		else:
				assert in_memory_max_size == input_in_memory_max_size
		if dataset_size and in_memory_max_size:
				UpperCamelCase						=  dataset_size < in_memory_max_size
		else:
				UpperCamelCase						=  False
		UpperCamelCase						=  is_small_dataset(lowercase				)
		assert result == expected
