





import functools
import operator

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


_UpperCAmelCase        =				logging.get_logger(__name__)

_UpperCAmelCase        =				{
    'microsoft/unispeech-large-1500h-cv': (
        'https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class 							_UpperCamelCase					(	lowerCAmelCase_     ):
       _UpperCamelCase				:					str         =   '''unispeech'''

       def __init__(		self: str					,   _SCREAMING_SNAKE_CASE: List[Any]=32					,   _SCREAMING_SNAKE_CASE: Tuple=768					,   _SCREAMING_SNAKE_CASE: List[Any]=12					,   _SCREAMING_SNAKE_CASE: Optional[Any]=12					,   _SCREAMING_SNAKE_CASE: int=3072					,   _SCREAMING_SNAKE_CASE: List[Any]="gelu"					,   _SCREAMING_SNAKE_CASE: Dict=0.1					,   _SCREAMING_SNAKE_CASE: Tuple=0.1					,   _SCREAMING_SNAKE_CASE: Tuple=0.1					,   _SCREAMING_SNAKE_CASE: str=0.0					,   _SCREAMING_SNAKE_CASE: List[str]=0.0					,   _SCREAMING_SNAKE_CASE: Any=0.1					,   _SCREAMING_SNAKE_CASE: Optional[int]=0.1					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=0.02					,   _SCREAMING_SNAKE_CASE: List[Any]=1e-5					,   _SCREAMING_SNAKE_CASE: List[Any]="group"					,   _SCREAMING_SNAKE_CASE: Optional[int]="gelu"					,   _SCREAMING_SNAKE_CASE: List[str]=(512, 512, 512, 512, 512, 512, 512)					,   _SCREAMING_SNAKE_CASE: Optional[Any]=(5, 2, 2, 2, 2, 2, 2)					,   _SCREAMING_SNAKE_CASE: Tuple=(10, 3, 3, 3, 3, 2, 2)					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=False					,   _SCREAMING_SNAKE_CASE: Dict=128					,   _SCREAMING_SNAKE_CASE: Any=16					,   _SCREAMING_SNAKE_CASE: int=False					,   _SCREAMING_SNAKE_CASE: Optional[int]=True					,   _SCREAMING_SNAKE_CASE: Optional[int]=0.05					,   _SCREAMING_SNAKE_CASE: Optional[int]=10					,   _SCREAMING_SNAKE_CASE: Optional[int]=2					,   _SCREAMING_SNAKE_CASE: Any=0.0					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=10					,   _SCREAMING_SNAKE_CASE: Tuple=0					,   _SCREAMING_SNAKE_CASE: Tuple=320					,   _SCREAMING_SNAKE_CASE: Dict=2					,   _SCREAMING_SNAKE_CASE: str=0.1					,   _SCREAMING_SNAKE_CASE: Optional[Any]=100					,   _SCREAMING_SNAKE_CASE: Any=256					,   _SCREAMING_SNAKE_CASE: Tuple=256					,   _SCREAMING_SNAKE_CASE: Tuple=0.1					,   _SCREAMING_SNAKE_CASE: Optional[Any]="mean"					,   _SCREAMING_SNAKE_CASE: int=False					,   _SCREAMING_SNAKE_CASE: str=False					,   _SCREAMING_SNAKE_CASE: Any=256					,   _SCREAMING_SNAKE_CASE: Optional[Any]=80					,   _SCREAMING_SNAKE_CASE: List[str]=0					,   _SCREAMING_SNAKE_CASE: Optional[int]=1					,   _SCREAMING_SNAKE_CASE: Dict=2					,   _SCREAMING_SNAKE_CASE: Optional[int]=0.5					,   **_SCREAMING_SNAKE_CASE: Union[str, Any]					,   )       -> str:




              """simple docstring"""


              super().__init__(**_SCREAMING_SNAKE_CASE					,   pad_token_id=_SCREAMING_SNAKE_CASE					,   bos_token_id=_SCREAMING_SNAKE_CASE					,   eos_token_id=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  hidden_size
              UpperCamelCase_          =  feat_extract_norm
              UpperCamelCase_          =  feat_extract_activation
              UpperCamelCase_          =  list(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  list(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  list(_SCREAMING_SNAKE_CASE		)
              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_          =  num_ctc_classes
              UpperCamelCase_          =  vocab_size
              UpperCamelCase_          =  do_stable_layer_norm
              UpperCamelCase_          =  use_weighted_layer_sum
              UpperCamelCase_          =  classifier_proj_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)`, 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

              # pretraining loss
              UpperCamelCase_          =  replace_prob



       @property
       def lowercase (		self: List[Any]		)       -> Optional[int]:




              """simple docstring"""


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








import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory




def  lowerCAmelCase_ (							)     ->		Dict:
       raise RuntimeError("CUDA out of memory."  )



class 							_UpperCamelCase					(	nn.Module     ):

       def __init__(		self: int		)       -> List[Any]:




              """simple docstring"""


              super().__init__()
              UpperCamelCase_          =  nn.Linear(3					,   4		)
              UpperCamelCase_          =  nn.BatchNormad(4		)
              UpperCamelCase_          =  nn.Linear(4					,   5		)



       def lowercase (		self: Any					,   _SCREAMING_SNAKE_CASE: Optional[int]		)       -> Optional[Any]:




              """simple docstring"""


              return self.lineara(self.batchnorm(self.lineara(_SCREAMING_SNAKE_CASE		)		)		)



class 							_UpperCamelCase					(	unittest.TestCase     ):

       def lowercase (		self: List[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  []

              @find_executable_batch_size(starting_batch_size=128		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: str		):
                     nonlocal batch_sizes
                     batch_sizes.append(_SCREAMING_SNAKE_CASE		)
                     if batch_size != 8:
                            raise_fake_out_of_memory()

              mock_training_loop_function()
              self.assertListEqual(_SCREAMING_SNAKE_CASE					,   [128, 64, 32, 16, 8]		)

       def lowercase (		self: Optional[int]		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  []

              @find_executable_batch_size(starting_batch_size=128		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: List[str]					,   _SCREAMING_SNAKE_CASE: Optional[Any]		):
                     nonlocal batch_sizes
                     batch_sizes.append(_SCREAMING_SNAKE_CASE		)
                     if batch_size != 8:
                            raise_fake_out_of_memory()
                     return batch_size, arga

              UpperCamelCase_       , UpperCamelCase_          =  mock_training_loop_function("hello"		)
              self.assertListEqual(_SCREAMING_SNAKE_CASE					,   [128, 64, 32, 16, 8]		)
              self.assertListEqual([bs, arga]					,   [8, "hello"]		)

       def lowercase (		self: Any		)       -> Optional[int]:




              """simple docstring"""


              @find_executable_batch_size(starting_batch_size=0		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: Union[str, Any]		):
                     pass

              with self.assertRaises(_SCREAMING_SNAKE_CASE		) as cm:
                     mock_training_loop_function()
                     self.assertIn("No executable batch size found, reached zero."					,   cm.exception.args[0]		)

       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              @find_executable_batch_size(starting_batch_size=16		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: Optional[int]		):
                     if batch_size > 0:
                            raise_fake_out_of_memory()
                     pass

              with self.assertRaises(_SCREAMING_SNAKE_CASE		) as cm:
                     mock_training_loop_function()
                     self.assertIn("No executable batch size found, reached zero."					,   cm.exception.args[0]		)

       def lowercase (		self: Optional[int]		)       -> Dict:




              """simple docstring"""


              @find_executable_batch_size(starting_batch_size=128		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: Any					,   _SCREAMING_SNAKE_CASE: int					,   _SCREAMING_SNAKE_CASE: Tuple		):
                     if batch_size != 8:
                            raise raise_fake_out_of_memory()

              with self.assertRaises(_SCREAMING_SNAKE_CASE		) as cm:
                     mock_training_loop_function(128					,   "hello"					,   "world"		)
                     self.assertIn("Batch size was passed into `f`"					,   cm.exception.args[0]		)
                     self.assertIn("`f(arg1='hello', arg2='world')"					,   cm.exception.args[0]		)

       def lowercase (		self: List[str]		)       -> Optional[Any]:




              """simple docstring"""


              @find_executable_batch_size(starting_batch_size=16		)
              def mock_training_loop_function(_SCREAMING_SNAKE_CASE: List[Any]		):
                     raise ValueError("Oops, we had an error!"		)

              with self.assertRaises(_SCREAMING_SNAKE_CASE		) as cm:
                     mock_training_loop_function()
                     self.assertIn("Oops, we had an error!"					,   cm.exception.args[0]		)



       @require_cuda
       def lowercase (		self: Optional[int]		)       -> Union[str, Any]:




              """simple docstring"""


              UpperCamelCase_          =  torch.cuda.memory_allocated()
              UpperCamelCase_          =  ModelForTest()
              model.cuda()
              self.assertGreater(torch.cuda.memory_allocated()					,   _SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  release_memory(_SCREAMING_SNAKE_CASE		)
              self.assertEqual(torch.cuda.memory_allocated()					,   _SCREAMING_SNAKE_CASE		)

