

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     (			UpperCamelCase__				):







							'''simple docstring'''






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





def  __lowerCamelCase     (			UpperCamelCase__       ,							UpperCamelCase__ = True				):







							'''simple docstring'''






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

							snake_case_          =   is_compiled_module(UpperCamelCase__				)
							if is_compiled:
														snake_case_          =   model
														snake_case_          =   model._orig_mod

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

							while isinstance(UpperCamelCase__       ,							UpperCamelCase__				):
														snake_case_          =   model.module

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

							if is_compiled:
														snake_case_          =   model
														snake_case_          =   compiled_model

							return model





def  __lowerCamelCase     (			):







							'''simple docstring'''






							PartialState().wait_for_everyone()





def  __lowerCamelCase     (			UpperCamelCase__       ,							UpperCamelCase__				):







							'''simple docstring'''






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





@contextmanager
def  __lowerCamelCase     (			**UpperCamelCase__				):







							'''simple docstring'''






							for key, value in kwargs.items():
														snake_case_          =   str(UpperCamelCase__				)

							yield

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





def  __lowerCamelCase     (			UpperCamelCase__				):







							'''simple docstring'''






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





def  __lowerCamelCase     (			UpperCamelCase__       ,							UpperCamelCase__				):







							'''simple docstring'''






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

							return destination





def  __lowerCamelCase     (			UpperCamelCase__ = None				):







							'''simple docstring'''






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







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
					if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
					from .pipeline_unclip import UnCLIPPipeline
					from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
					from .text_proj import UnCLIPTextProjModel




