
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__				:      Any       )  ->    List[str]:
							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__				:      str			,				UpperCAmelCase__				:      bool = True       )  ->    str:
							lowerCamelCase_							  =					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							lowerCamelCase_							  =					is_compiled_module(UpperCAmelCase__       )
							if is_compiled:
														lowerCamelCase_							  =					model
														lowerCamelCase_							  =					model._orig_mod

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

							while isinstance(UpperCAmelCase__			,				UpperCAmelCase__       ):
														lowerCamelCase_							  =					model.module

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

							if is_compiled:
														lowerCamelCase_							  =					model
														lowerCamelCase_							  =					compiled_model

							return model



def      __lowerCAmelCase						(							)  ->    Any:
							PartialState().wait_for_everyone()



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Union[str, Any]			,				UpperCAmelCase__				:      Tuple       )  ->    Dict:
							if PartialState().distributed_type == DistributedType.TPU:
														xm.save(UpperCAmelCase__			,				UpperCAmelCase__       )
							elif PartialState().local_process_index == 0:
														torch.save(UpperCAmelCase__			,				UpperCAmelCase__       )



@contextmanager
def      __lowerCAmelCase						(							**UpperCAmelCase__				:      Any       )  ->    Dict:
							for key, value in kwargs.items():
														lowerCamelCase_							  =					str(UpperCAmelCase__       )

							yield

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



def      __lowerCAmelCase						(							UpperCAmelCase__				:      Union[str, Any]       )  ->    str:
							if not hasattr(UpperCAmelCase__			,				"""__qualname__"""       ) and not hasattr(UpperCAmelCase__			,				"""__name__"""       ):
														lowerCamelCase_							  =					getattr(UpperCAmelCase__			,				"""__class__"""			,				UpperCAmelCase__       )
							if hasattr(UpperCAmelCase__			,				"""__qualname__"""       ):
														return obj.__qualname__
							if hasattr(UpperCAmelCase__			,				"""__name__"""       ):
														return obj.__name__
							return str(UpperCAmelCase__       )



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

							return destination



def      __lowerCAmelCase						(							UpperCAmelCase__				:      int = None       )  ->    bool:
							if port is None:
														lowerCamelCase_							  =					2_9_5_0_0
							with socket.socket(socket.AF_INET			,				socket.SOCK_STREAM       ) as s:
														return s.connect_ex(("""localhost""", port)       ) == 0







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
		import torch

		from transformers.generation import DisjunctiveConstraint





@require_torch
class       __A(				unittest.TestCase       ):

					def    lowercase__      (	self					:     Optional[Any]   ):
												# For consistency across different places the DisjunctiveConstraint is called,
												# dc.token_ids is a list of integers. It is also initialized only by integers.

												lowerCamelCase_							  =					[[1, 2, 4], [1, 2, 3, 4]]
												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )
												self.assertTrue(isinstance(dc.token_ids   ,					__UpperCamelCase   )   )

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]   )   )

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint([torch.LongTensor([1, 2, 4]   ), torch.LongTensor([1, 2, 3, 4, 5]   )]   )

					def    lowercase__      (	self					:     List[Any]   ):
												# We can't have constraints that are complete subsets of another. This leads to a preverse
												# interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
												# It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
												# fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
												# will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
												lowerCamelCase_							  =					[[1, 2], [1, 2, 3, 4]]

												with self.assertRaises(__UpperCamelCase   ):
																			DisjunctiveConstraint(__UpperCamelCase   )  # fails here

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												lowerCamelCase_							  =					stepped is True and completed is False and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												lowerCamelCase_							  =					stepped is True and completed is False and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(3   )
												lowerCamelCase_							  =					stepped is True and completed is True and reset is False
												self.assertTrue(__UpperCamelCase   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.current_seq == [1, 2, 3]   )



					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(4   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.current_seq == [1, 2, 4]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(5   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.current_seq == [1, 2, 4, 5]   )

												dc.reset()

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(1   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.remaining() == 3   )
												self.assertTrue(dc.current_seq == [1]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(2   )
												self.assertTrue(not dc.completed   )
												self.assertTrue(dc.remaining() == 2   )
												self.assertTrue(dc.current_seq == [1, 2]   )

												lowerCamelCase_  ,   lowerCamelCase_  ,   lowerCamelCase_							  =					dc.update(5   )
												self.assertTrue(dc.completed   )  # Completed!
												self.assertTrue(dc.remaining() == 0   )
												self.assertTrue(dc.current_seq == [1, 2, 5]   )





