
"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_					     =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_					     =	{
    '''transfo-xl-wt103''': '''https://huggingface.co/transfo-xl-wt103/resolve/main/config.json''',
}
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''transfo-xl'''
							_snake_case						      =		['''mems''']
							_snake_case						      =		{
							    '''n_token''': '''vocab_size''',
							    '''hidden_size''': '''d_model''',
							    '''num_attention_heads''': '''n_head''',
							    '''num_hidden_layers''': '''n_layer''',
							}




							def __init__( self    ,				snake_case_=267_735    ,				snake_case_=[20_000, 40_000, 200_000]    ,				snake_case_=1_024    ,				snake_case_=1_024    ,				snake_case_=16    ,				snake_case_=64    ,				snake_case_=4_096    ,				snake_case_=4    ,				snake_case_=False    ,				snake_case_=18    ,				snake_case_=1_600    ,				snake_case_=1_000    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=0    ,				snake_case_=-1    ,				snake_case_=True    ,				snake_case_=0.1    ,				snake_case_=0.0    ,				snake_case_=True    ,				snake_case_="normal"    ,				snake_case_=0.01    ,				snake_case_=0.01    ,				snake_case_=0.02    ,				snake_case_=1e-5    ,				snake_case_=0    ,				**snake_case_    ,				)       ->  int:
													__lowerCAmelCase     =  vocab_size
													__lowerCAmelCase     =  []
													self.cutoffs.extend(snake_case_				)
													if proj_share_all_but_first:
																			__lowerCAmelCase     =  [False] + [True] * len(self.cutoffs				)
													else:
																			__lowerCAmelCase     =  [False] + [False] * len(self.cutoffs				)
													__lowerCAmelCase     =  d_model
													__lowerCAmelCase     =  d_embed
													__lowerCAmelCase     =  d_head
													__lowerCAmelCase     =  d_inner
													__lowerCAmelCase     =  div_val
													__lowerCAmelCase     =  pre_lnorm
													__lowerCAmelCase     =  n_layer
													__lowerCAmelCase     =  n_head
													__lowerCAmelCase     =  mem_len
													__lowerCAmelCase     =  same_length
													__lowerCAmelCase     =  attn_type
													__lowerCAmelCase     =  clamp_len
													__lowerCAmelCase     =  sample_softmax
													__lowerCAmelCase     =  adaptive
													__lowerCAmelCase     =  dropout
													__lowerCAmelCase     =  dropatt
													__lowerCAmelCase     =  untie_r
													__lowerCAmelCase     =  init
													__lowerCAmelCase     =  init_range
													__lowerCAmelCase     =  proj_init_std
													__lowerCAmelCase     =  init_std
													__lowerCAmelCase     =  layer_norm_epsilon
													super().__init__(eos_token_id=snake_case_    ,				**snake_case_				)




							@property
							def    A__						( self				)       ->  Tuple:
													# Message copied from Transformer-XL documentation
													logger.info(f"""The model {self.model_type} is one of the few models that has no sequence length limit."""				)
													return -1




							@max_position_embeddings.setter
							def    A__						( self    ,				snake_case_				)       ->  int:
													# Message copied from Transformer-XL documentation
													raise NotImplementedError(
													    f"""The model {self.model_type} is one of the few models that has no sequence length limit."""				)


"""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						):
						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 ,    _lowerCAmelCase = True						):
						__lowerCAmelCase     =  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

						__lowerCAmelCase     =  is_compiled_module(_lowerCAmelCase						)
						if is_compiled:
												__lowerCAmelCase     =  model
												__lowerCAmelCase     =  model._orig_mod

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

						while isinstance(_lowerCAmelCase ,    _lowerCAmelCase						):
												__lowerCAmelCase     =  model.module

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

						if is_compiled:
												__lowerCAmelCase     =  model
												__lowerCAmelCase     =  compiled_model

						return model

def  lowercase     ():
						PartialState().wait_for_everyone()

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

@contextmanager
def  lowercase     (**_lowerCAmelCase						):
						for key, value in kwargs.items():
												__lowerCAmelCase     =  str(_lowerCAmelCase						)

						yield

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

def  lowercase     (_lowerCAmelCase						):
						if not hasattr(_lowerCAmelCase ,    """__qualname__"""						) and not hasattr(_lowerCAmelCase ,    """__name__"""						):
												__lowerCAmelCase     =  getattr(_lowerCAmelCase ,    """__class__""" ,    _lowerCAmelCase						)
						if hasattr(_lowerCAmelCase ,    """__qualname__"""						):
												return obj.__qualname__
						if hasattr(_lowerCAmelCase ,    """__name__"""						):
												return obj.__name__
						return str(_lowerCAmelCase						)

def  lowercase     (_lowerCAmelCase ,    _lowerCAmelCase						):
						for key, value in source.items():
												if isinstance(_lowerCAmelCase ,    _lowerCAmelCase						):
																		__lowerCAmelCase     =  destination.setdefault(_lowerCAmelCase ,    {}						)
																		merge_dicts(_lowerCAmelCase ,    _lowerCAmelCase						)
												else:
																		__lowerCAmelCase     =  value

						return destination



def  lowercase     (_lowerCAmelCase = None						):
						if port is None:
												__lowerCAmelCase     =  2_9500
						with socket.socket(socket.AF_INET ,    socket.SOCK_STREAM						) as s:
												return s.connect_ex(("""localhost""", port)						) == 0
