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     UpperCamelCase						(	__lowercase						:			int     ):
		'''simple docstring'''




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




def     UpperCamelCase						(	__lowercase						:			str						,__lowercase						:			bool = True     ):
		'''simple docstring'''




		A_		:    Union[str, Any]         =				(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		A_		:    Any         =				is_compiled_module(lowerCAmelCase__     )
		if is_compiled:
				A_		:    Optional[Any]         =				model
				A_		:    List[Any]         =				model._orig_mod

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

		while isinstance(lowerCAmelCase__						,lowerCAmelCase__     ):
				A_		:    str         =				model.module

		if not keep_fpaa_wrapper:
				A_		:    Union[str, Any]         =				getattr(lowerCAmelCase__						,'forward'     )
				A_		:    Optional[Any]         =				model.__dict__.pop('_original_forward'						,lowerCAmelCase__     )
				if original_forward is not None:
						while hasattr(lowerCAmelCase__						,'__wrapped__'     ):
								A_		:    Dict         =				forward.__wrapped__
								if forward == original_forward:
										break
						A_		:    str         =				forward
				if getattr(lowerCAmelCase__						,'_converted_to_transformer_engine'						,lowerCAmelCase__     ):
						convert_model(lowerCAmelCase__						,to_transformer_engine=lowerCAmelCase__     )

		if is_compiled:
				A_		:    Optional[Any]         =				model
				A_		:    Tuple         =				compiled_model

		return model




def     UpperCamelCase						(	):
		'''simple docstring'''




		PartialState().wait_for_everyone()




def     UpperCamelCase						(	__lowercase						:			Any						,__lowercase						:			Optional[Any]     ):
		'''simple docstring'''




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




@contextmanager
def     UpperCamelCase						(	**__lowercase						:			int     ):
		'''simple docstring'''




		for key, value in kwargs.items():
				A_		:    Any         =				str(lowerCAmelCase__     )

		yield

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




def     UpperCamelCase						(	__lowercase						:			Union[str, Any]     ):
		'''simple docstring'''




		if not hasattr(lowerCAmelCase__						,'__qualname__'     ) and not hasattr(lowerCAmelCase__						,'__name__'     ):
				A_		:    Union[str, Any]         =				getattr(lowerCAmelCase__						,'__class__'						,lowerCAmelCase__     )
		if hasattr(lowerCAmelCase__						,'__qualname__'     ):
				return obj.__qualname__
		if hasattr(lowerCAmelCase__						,'__name__'     ):
				return obj.__name__
		return str(lowerCAmelCase__     )




def     UpperCamelCase						(	__lowercase						:			List[Any]						,__lowercase						:			str     ):
		'''simple docstring'''




		for key, value in source.items():
				if isinstance(lowerCAmelCase__						,lowerCAmelCase__     ):
						A_		:    Any         =				destination.setdefault(lowerCAmelCase__						,{}     )
						merge_dicts(lowerCAmelCase__						,lowerCAmelCase__     )
				else:
						A_		:    Optional[Any]         =				value

		return destination




def     UpperCamelCase						(	__lowercase						:			int = None     ):
		'''simple docstring'''




		if port is None:
				A_		:    List[Any]         =				2_95_00
		with socket.socket(socket.AF_INET						,socket.SOCK_STREAM     ) as s:
				return s.connect_ex(('localhost', port)     ) == 0







import warnings

from ...utils import logging
from .image_processing_donut import DonutImageProcessor


_UpperCAmelCase            =		logging.get_logger(__name__)





class UpperCAmelCase						( __A  ):


				'''simple docstring'''






				def __init__(						self		,     *lowercase		,     **lowercase ):



						"""simple docstring"""





						warnings.warn(
						    'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
						    ' use DonutImageProcessor instead.'		,     lowercase		,     )
						super().__init__(*lowercase		,     **lowercase )






