'''simple docstring'''





import functools
import operator

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


SCREAMING_SNAKE_CASE_:     str  =logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:     List[Any]  ={
    'asapp/sew-tiny-100k': 'https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json',
    # See all SEW models at https://huggingface.co/models?filter=sew
}




class        __A      ( __A	):
    a__       :						Dict          =  "sew"



    def __init__(self    :    Dict   ,			__a    :    Any=32   ,			__a    :    Tuple=768   ,			__a    :    Optional[Any]=12   ,			__a    :    List[Any]=12   ,			__a    :    Optional[Any]=3072   ,			__a    :    List[str]=2   ,			__a    :    int="gelu"   ,			__a    :    Optional[Any]=0.1   ,			__a    :    str=0.1   ,			__a    :    Tuple=0.1   ,			__a    :    Optional[Any]=0.0   ,			__a    :    int=0.1   ,			__a    :    Optional[Any]=0.1   ,			__a    :    int=0.02   ,			__a    :    Tuple=1E-5   ,			__a    :    List[str]="group"   ,			__a    :    int="gelu"   ,			__a    :    Tuple=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)   ,			__a    :    Optional[int]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)   ,			__a    :    int=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)   ,			__a    :    List[Any]=False   ,			__a    :    Union[str, Any]=128   ,			__a    :    Any=16   ,			__a    :    str=True   ,			__a    :    Optional[int]=0.05   ,			__a    :    List[Any]=10   ,			__a    :    Optional[int]=2   ,			__a    :    Dict=0.0   ,			__a    :    Tuple=10   ,			__a    :    Any=0   ,			__a    :    Dict="mean"   ,			__a    :    Optional[Any]=False   ,			__a    :    str=False   ,			__a    :    List[Any]=256   ,			__a    :    Union[str, Any]=0   ,			__a    :    int=1   ,			__a    :    List[Any]=2   ,			**__a    :    int   ,			):
         super().__init__(**_UpperCamelCase   ,			pad_token_id=_UpperCamelCase   ,			bos_token_id=_UpperCamelCase   ,			eos_token_id=_UpperCamelCase )
         UpperCAmelCase_           =   hidden_size
         UpperCAmelCase_           =   feat_extract_norm
         UpperCAmelCase_           =   feat_extract_activation
         UpperCAmelCase_           =   list(_UpperCamelCase )
         UpperCAmelCase_           =   list(_UpperCamelCase )
         UpperCAmelCase_           =   list(_UpperCamelCase )
         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_           =   squeeze_factor
         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

         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)`,"
                  f"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)"""
                  f"""= {len(self.conv_stride )}`, `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

         # ctc loss
         UpperCAmelCase_           =   ctc_loss_reduction
         UpperCAmelCase_           =   ctc_zero_infinity

         # sequence classification
         UpperCAmelCase_           =   use_weighted_layer_sum
         UpperCAmelCase_           =   classifier_proj_size






    @property
    def 					_lowercase						(self    :    Dict ):
         return functools.reduce(operator.mul   ,			self.conv_stride   ,			1 )







import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available


def   __SCREAMING_SNAKE_CASE					(*SCREAMING_SNAKE_CASE__       ):
						if not isinstance(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__       ):
												snake_case_   =	list(SCREAMING_SNAKE_CASE__       )
						for i in range(len(SCREAMING_SNAKE_CASE__       )       ):
												snake_case_   =	None
						gc.collect()
						if is_xpu_available():
												torch.xpu.empty_cache()
						elif is_npu_available():
												torch.npu.empty_cache()
						else:
												torch.cuda.empty_cache()
						return objects


def   __SCREAMING_SNAKE_CASE					(SCREAMING_SNAKE_CASE__       ):
						snake_case_   =	[
						    '''CUDA out of memory.''',  # CUDA OOM
						    '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''',  # CUDNN SNAFU
						    '''DefaultCPUAllocator: can\'t allocate memory''',  # CPU OOM
						]
						if isinstance(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__       ) and len(exception.args       ) == 1:
												return any(err in exception.args[0] for err in _statements       )
						return False


def   __SCREAMING_SNAKE_CASE					(SCREAMING_SNAKE_CASE__ = None  ,		SCREAMING_SNAKE_CASE__ = 128       ):
						if function is None:
												return functools.partial(SCREAMING_SNAKE_CASE__  ,		starting_batch_size=SCREAMING_SNAKE_CASE__       )

						snake_case_   =	starting_batch_size

						def decorator(*SCREAMING_SNAKE_CASE__  ,		**SCREAMING_SNAKE_CASE__       ):
												nonlocal batch_size
												gc.collect()
												if is_xpu_available():
																		torch.xpu.empty_cache()
												elif is_npu_available():
																		torch.npu.empty_cache()
												else:
																		torch.cuda.empty_cache()
												snake_case_   =	list(inspect.signature(SCREAMING_SNAKE_CASE__       ).parameters.keys()       )
												# Guard against user error
												if len(SCREAMING_SNAKE_CASE__       ) < (len(SCREAMING_SNAKE_CASE__       ) + 1):
																		snake_case_   =	''', '''.join([F'''{arg}={value}''' for arg, value in zip(params[1:]  ,		args[1:]       )]       )
																		raise TypeError(
																		    F'''Batch size was passed into `{function.__name__}` as the first argument when called.'''
																		    F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`'''       )
												while True:
																		if batch_size == 0:
																								raise RuntimeError('''No executable batch size found, reached zero.'''       )
																		try:
																								return function(SCREAMING_SNAKE_CASE__  ,		*SCREAMING_SNAKE_CASE__  ,		**SCREAMING_SNAKE_CASE__       )
																		except Exception as e:
																								if should_reduce_batch_size(SCREAMING_SNAKE_CASE__       ):
																														gc.collect()
																														if is_xpu_available():
																																				torch.xpu.empty_cache()
																														elif is_npu_available():
																																				torch.npu.empty_cache()
																														else:
																																				torch.cuda.empty_cache()
																														batch_size //= 2
																								else:
																														raise

						return decorator