
'''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 		A_(     A						:		List[str]):
							if is_torch_version('<'		,     '2.0.0') or not hasattr(A		,     '_dynamo'):
														return False
							return isinstance(A		,     torch._dynamo.eval_frame.OptimizedModule)







def 		A_(     A						:		Union[str, Any]		,     A						:		bool = True):
							UpperCamelCase   			=     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							UpperCamelCase   			=     is_compiled_module(A)
							if is_compiled:
														UpperCamelCase   			=     model
														UpperCamelCase   			=     model._orig_mod

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

							while isinstance(A		,     A):
														UpperCamelCase   			=     model.module

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

							if is_compiled:
														UpperCamelCase   			=     model
														UpperCamelCase   			=     compiled_model

							return model







def 		A_(     ):
							PartialState().wait_for_everyone()







def 		A_(     A						:		Dict		,     A						:		int):
							if PartialState().distributed_type == DistributedType.TPU:
														xm.save(A		,     A)
							elif PartialState().local_process_index == 0:
														torch.save(A		,     A)







@contextmanager
def 		A_(     **A						:		Dict):
							for key, value in kwargs.items():
														UpperCamelCase   			=     str(A)

							yield

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







def 		A_(     A						:		int):
							if not hasattr(A		,     '__qualname__') and not hasattr(A		,     '__name__'):
														UpperCamelCase   			=     getattr(A		,     '__class__'		,     A)
							if hasattr(A		,     '__qualname__'):
														return obj.__qualname__
							if hasattr(A		,     '__name__'):
														return obj.__name__
							return str(A)







def 		A_(     A						:		Tuple		,     A						:		List[str]):
							for key, value in source.items():
														if isinstance(A		,     A):
																					UpperCamelCase   			=     destination.setdefault(A		,     {})
																					merge_dicts(A		,     A)
														else:
																					UpperCamelCase   			=     value

							return destination







def 		A_(     A						:		int = None):
							if port is None:
														UpperCamelCase   			=     2_9500
							with socket.socket(socket.AF_INET		,     socket.SOCK_STREAM) as s:
														return s.connect_ex(('localhost', port)) == 0






'''simple docstring'''
import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase					:    int           = logging.get_logger(__name__)

lowerCAmelCase					:    Tuple           = {
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""unispeech-sat"""







				def __init__(       self						,				A_=32						,				A_=768						,				A_=12						,				A_=12						,				A_=3072						,				A_="gelu"						,				A_=0.1						,				A_=0.1						,				A_=0.1						,				A_=0.0						,				A_=0.0						,				A_=0.1						,				A_=0.1						,				A_=0.02						,				A_=1e-5						,				A_="group"						,				A_="gelu"						,				A_=(512, 512, 512, 512, 512, 512, 512)						,				A_=(5, 2, 2, 2, 2, 2, 2)						,				A_=(10, 3, 3, 3, 3, 2, 2)						,				A_=False						,				A_=128						,				A_=16						,				A_=False						,				A_=True						,				A_=0.05						,				A_=10						,				A_=2						,				A_=0.0						,				A_=10						,				A_=0						,				A_=320						,				A_=2						,				A_=0.1						,				A_=100						,				A_=256						,				A_=256						,				A_=0.1						,				A_="mean"						,				A_=False						,				A_=False						,				A_=256						,				A_=(512, 512, 512, 512, 1500)						,				A_=(5, 3, 3, 1, 1)						,				A_=(1, 2, 3, 1, 1)						,				A_=512						,				A_=0						,				A_=1						,				A_=2						,				A_=504						,				**A_						,				)->		Tuple:







											'''simple docstring'''






											super().__init__(**A_						,				pad_token_id=A_						,				bos_token_id=A_						,				eos_token_id=A_     )
											UpperCamelCase   			=     hidden_size
											UpperCamelCase   			=     feat_extract_norm
											UpperCamelCase   			=     feat_extract_activation
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     conv_bias
											UpperCamelCase   			=     num_conv_pos_embeddings
											UpperCamelCase   			=     num_conv_pos_embedding_groups
											UpperCamelCase   			=     len(self.conv_dim     )
											UpperCamelCase   			=     num_hidden_layers
											UpperCamelCase   			=     intermediate_size
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     num_attention_heads
											UpperCamelCase   			=     hidden_dropout
											UpperCamelCase   			=     attention_dropout
											UpperCamelCase   			=     activation_dropout
											UpperCamelCase   			=     feat_proj_dropout
											UpperCamelCase   			=     final_dropout
											UpperCamelCase   			=     layerdrop
											UpperCamelCase   			=     layer_norm_eps
											UpperCamelCase   			=     initializer_range
											UpperCamelCase   			=     vocab_size
											UpperCamelCase   			=     num_clusters
											UpperCamelCase   			=     do_stable_layer_norm
											UpperCamelCase   			=     use_weighted_layer_sum

											if (
											    (len(self.conv_stride     ) != self.num_feat_extract_layers)
											    or (len(self.conv_kernel     ) != self.num_feat_extract_layers)
											    or (len(self.conv_dim     ) != self.num_feat_extract_layers)
											):
																		raise ValueError(
																		    'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='
																		    ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='
																		    F''' {len(self.conv_dim     )}`, `len(config.conv_stride) = {len(self.conv_stride     )}`,'''
																		    F''' `len(config.conv_kernel) = {len(self.conv_kernel     )}`.'''     )

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											UpperCamelCase   			=     apply_spec_augment
											UpperCamelCase   			=     mask_time_prob
											UpperCamelCase   			=     mask_time_length
											UpperCamelCase   			=     mask_time_min_masks
											UpperCamelCase   			=     mask_feature_prob
											UpperCamelCase   			=     mask_feature_length
											UpperCamelCase   			=     mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											UpperCamelCase   			=     num_codevectors_per_group
											UpperCamelCase   			=     num_codevector_groups
											UpperCamelCase   			=     contrastive_logits_temperature
											UpperCamelCase   			=     feat_quantizer_dropout
											UpperCamelCase   			=     num_negatives
											UpperCamelCase   			=     codevector_dim
											UpperCamelCase   			=     proj_codevector_dim
											UpperCamelCase   			=     diversity_loss_weight

											# ctc loss
											UpperCamelCase   			=     ctc_loss_reduction
											UpperCamelCase   			=     ctc_zero_infinity

											# SequenceClassification-specific parameter. Feel free to ignore for other classes.
											UpperCamelCase   			=     classifier_proj_size

											# XVector-specific parameters. Feel free to ignore for other classes.
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     list(A_     )
											UpperCamelCase   			=     xvector_output_dim







				@property
				def 						UpperCAmelCase_    (       self     )->		Optional[Any]:







											'''simple docstring'''






											return functools.reduce(operator.mul						,				self.conv_stride						,				1     )




