



"""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_						(							SCREAMING_SNAKE_CASE__	)			->      Any:
		if is_torch_version("<", "2.0.0"	) or not hasattr(SCREAMING_SNAKE_CASE__, "_dynamo"	):
				return False
		return isinstance(SCREAMING_SNAKE_CASE__, torch._dynamo.eval_frame.OptimizedModule	)



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ = True	)			->      List[str]:
		a_     :      Union[str, Any] 						=     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		a_     :      Optional[Any] 						=     is_compiled_module(SCREAMING_SNAKE_CASE__	)
		if is_compiled:
				a_     :      List[str] 						=     model
				a_     :      Union[str, Any] 						=     model._orig_mod

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

		while isinstance(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	):
				a_     :      str 						=     model.module

		if not keep_fpaa_wrapper:
				a_     :      str 						=     getattr(SCREAMING_SNAKE_CASE__, "forward"	)
				a_     :      List[Any] 						=     model.__dict__.pop("_original_forward", SCREAMING_SNAKE_CASE__	)
				if original_forward is not None:
						while hasattr(SCREAMING_SNAKE_CASE__, "__wrapped__"	):
								a_     :      List[str] 						=     forward.__wrapped__
								if forward == original_forward:
										break
						a_     :      List[str] 						=     forward
				if getattr(SCREAMING_SNAKE_CASE__, "_converted_to_transformer_engine", SCREAMING_SNAKE_CASE__	):
						convert_model(SCREAMING_SNAKE_CASE__, to_transformer_engine=SCREAMING_SNAKE_CASE__	)

		if is_compiled:
				a_     :      str 						=     model
				a_     :      List[str] 						=     compiled_model

		return model



def        lowerCAmelCase_						(							)			->      str:
		PartialState().wait_for_everyone()



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)			->      Any:
		if PartialState().distributed_type == DistributedType.TPU:
				xm.save(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)
		elif PartialState().local_process_index == 0:
				torch.save(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)



@contextmanager
def        lowerCAmelCase_						(							**SCREAMING_SNAKE_CASE__	)			->      Optional[Any]:
		for key, value in kwargs.items():
				a_     :      List[str] 						=     str(SCREAMING_SNAKE_CASE__	)

		yield

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



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__	)			->      Optional[Any]:
		if not hasattr(SCREAMING_SNAKE_CASE__, "__qualname__"	) and not hasattr(SCREAMING_SNAKE_CASE__, "__name__"	):
				a_     :      int 						=     getattr(SCREAMING_SNAKE_CASE__, "__class__", SCREAMING_SNAKE_CASE__	)
		if hasattr(SCREAMING_SNAKE_CASE__, "__qualname__"	):
				return obj.__qualname__
		if hasattr(SCREAMING_SNAKE_CASE__, "__name__"	):
				return obj.__name__
		return str(SCREAMING_SNAKE_CASE__	)



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)			->      int:
		for key, value in source.items():
				if isinstance(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	):
						a_     :      str 						=     destination.setdefault(SCREAMING_SNAKE_CASE__, {}	)
						merge_dicts(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)
				else:
						a_     :      Tuple 						=     value

		return destination



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__ = None	)			->      bool:
		if port is None:
				a_     :      Optional[Any] 						=     29_500
		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_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE_				= {
    """configuration_llama""": ["""LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LlamaConfig"""],
}

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

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

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE_				= [
					    """LlamaForCausalLM""",
					    """LlamaModel""",
					    """LlamaPreTrainedModel""",
					    """LlamaForSequenceClassification""",
					]


if TYPE_CHECKING:
					from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig

					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_llama import LlamaTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_llama_fast import LlamaTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel


else:
					import sys

					SCREAMING_SNAKE_CASE_				= _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)