


"""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        lowercase							(		lowerCAmelCase__ :     Tuple			) ->      str:
		if is_torch_version('''<'''    ,					'''2.0.0'''			) or not hasattr(lowerCAmelCase__    ,					'''_dynamo'''			):
				return False
		return isinstance(lowerCAmelCase__    ,					torch._dynamo.eval_frame.OptimizedModule			)

def        lowercase							(		lowerCAmelCase__ :     List[Any]    ,					lowerCAmelCase__ :     bool = True			) ->      int:
		__a           =			(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

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

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

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

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

		if is_compiled:
				__a           =			model
				__a           =			compiled_model

		return model

def        lowercase							(		) ->      Optional[int]:
		PartialState().wait_for_everyone()

def        lowercase							(		lowerCAmelCase__ :     Optional[int]    ,					lowerCAmelCase__ :     Tuple			) ->      int:
		if PartialState().distributed_type == DistributedType.TPU:
				xm.save(lowerCAmelCase__    ,					lowerCAmelCase__			)
		elif PartialState().local_process_index == 0:
				torch.save(lowerCAmelCase__    ,					lowerCAmelCase__			)

@contextmanager
def        lowercase							(		**lowerCAmelCase__ :     List[str]			) ->      List[str]:
		for key, value in kwargs.items():
				__a           =			str(lowerCAmelCase__			)

		yield

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

def        lowercase							(		lowerCAmelCase__ :     Any			) ->      Optional[int]:
		if not hasattr(lowerCAmelCase__    ,					'''__qualname__'''			) and not hasattr(lowerCAmelCase__    ,					'''__name__'''			):
				__a           =			getattr(lowerCAmelCase__    ,					'''__class__'''    ,					lowerCAmelCase__			)
		if hasattr(lowerCAmelCase__    ,					'''__qualname__'''			):
				return obj.__qualname__
		if hasattr(lowerCAmelCase__    ,					'''__name__'''			):
				return obj.__name__
		return str(lowerCAmelCase__			)

def        lowercase							(		lowerCAmelCase__ :     Any    ,					lowerCAmelCase__ :     Union[str, Any]			) ->      str:
		for key, value in source.items():
				if isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
						__a           =			destination.setdefault(lowerCAmelCase__    ,					{}			)
						merge_dicts(lowerCAmelCase__    ,					lowerCAmelCase__			)
				else:
						__a           =			value

		return destination




def        lowercase							(		lowerCAmelCase__ :     int = None			) ->      bool:
		if port is None:
				__a           =			29500
		with socket.socket(socket.AF_INET    ,					socket.SOCK_STREAM			) as s:
				return s.connect_ex(('''localhost''', port)			) == 0










"""simple docstring"""




from typing import TYPE_CHECKING

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


lowercase_			     =  {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  ["XGLMTokenizer"]

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  ["XGLMTokenizerFast"]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "XGLMForCausalLM",
				    "XGLMModel",
				    "XGLMPreTrainedModel",
				]


try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "FlaxXGLMForCausalLM",
				    "FlaxXGLMModel",
				    "FlaxXGLMPreTrainedModel",
				]


try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			     =  [
				    "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "TFXGLMForCausalLM",
				    "TFXGLMModel",
				    "TFXGLMPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_xglm import XGLMTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_xglm_fast import XGLMTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_xglm import (
								    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFXGLMForCausalLM,
								    TFXGLMModel,
								    TFXGLMPreTrainedModel,
								)


else:
				import sys

				lowercase_			     =  _LazyModule(__name__, globals()["__file__"], _import_structure)






