


'''simple docstring'''



import functools
import operator

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


__A				=logging.get_logger(__name__)

__A				={
    "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 						_snake_case   (       _a		):
					lowerCAmelCase :Dict						=   """unispeech"""




					def __init__( self							,							_lowerCamelCase=32							,							_lowerCamelCase=768							,							_lowerCamelCase=12							,							_lowerCamelCase=12							,							_lowerCamelCase=3072							,							_lowerCamelCase="gelu"							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.0							,							_lowerCamelCase=0.0							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.02							,							_lowerCamelCase=1e-5							,							_lowerCamelCase="group"							,							_lowerCamelCase="gelu"							,							_lowerCamelCase=(512, 512, 512, 512, 512, 512, 512)							,							_lowerCamelCase=(5, 2, 2, 2, 2, 2, 2)							,							_lowerCamelCase=(10, 3, 3, 3, 3, 2, 2)							,							_lowerCamelCase=False							,							_lowerCamelCase=128							,							_lowerCamelCase=16							,							_lowerCamelCase=False							,							_lowerCamelCase=True							,							_lowerCamelCase=0.05							,							_lowerCamelCase=10							,							_lowerCamelCase=2							,							_lowerCamelCase=0.0							,							_lowerCamelCase=10							,							_lowerCamelCase=0							,							_lowerCamelCase=320							,							_lowerCamelCase=2							,							_lowerCamelCase=0.1							,							_lowerCamelCase=100							,							_lowerCamelCase=256							,							_lowerCamelCase=256							,							_lowerCamelCase=0.1							,							_lowerCamelCase="mean"							,							_lowerCamelCase=False							,							_lowerCamelCase=False							,							_lowerCamelCase=256							,							_lowerCamelCase=80							,							_lowerCamelCase=0							,							_lowerCamelCase=1							,							_lowerCamelCase=2							,							_lowerCamelCase=0.5							,							**_lowerCamelCase							,							):
								super().__init__(**_lowerCamelCase							,							pad_token_id=_lowerCamelCase							,							bos_token_id=_lowerCamelCase							,							eos_token_id=_lowerCamelCase)
								UpperCAmelCase__					:   Optional[Any]       				=       hidden_size
								UpperCAmelCase__					:   List[str]       				=       feat_extract_norm
								UpperCAmelCase__					:   Dict       				=       feat_extract_activation
								UpperCAmelCase__					:   List[str]       				=       list(_lowerCamelCase)
								UpperCAmelCase__					:   List[str]       				=       list(_lowerCamelCase)
								UpperCAmelCase__					:   Any       				=       list(_lowerCamelCase)
								UpperCAmelCase__					:   Optional[Any]       				=       conv_bias
								UpperCAmelCase__					:   List[Any]       				=       num_conv_pos_embeddings
								UpperCAmelCase__					:   Dict       				=       num_conv_pos_embedding_groups
								UpperCAmelCase__					:   Any       				=       len(self.conv_dim)
								UpperCAmelCase__					:   Any       				=       num_hidden_layers
								UpperCAmelCase__					:   List[Any]       				=       intermediate_size
								UpperCAmelCase__					:   Any       				=       hidden_act
								UpperCAmelCase__					:   Any       				=       num_attention_heads
								UpperCAmelCase__					:   Optional[Any]       				=       hidden_dropout
								UpperCAmelCase__					:   Optional[Any]       				=       attention_dropout
								UpperCAmelCase__					:   str       				=       activation_dropout
								UpperCAmelCase__					:   List[str]       				=       feat_proj_dropout
								UpperCAmelCase__					:   int       				=       final_dropout
								UpperCAmelCase__					:   Optional[int]       				=       layerdrop
								UpperCAmelCase__					:   Dict       				=       layer_norm_eps
								UpperCAmelCase__					:   Dict       				=       initializer_range
								UpperCAmelCase__					:   Optional[int]       				=       num_ctc_classes
								UpperCAmelCase__					:   List[str]       				=       vocab_size
								UpperCAmelCase__					:   Optional[Any]       				=       do_stable_layer_norm
								UpperCAmelCase__					:   List[str]       				=       use_weighted_layer_sum
								UpperCAmelCase__					:   Any       				=       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__					:   str       				=       apply_spec_augment
								UpperCAmelCase__					:   Tuple       				=       mask_time_prob
								UpperCAmelCase__					:   Dict       				=       mask_time_length
								UpperCAmelCase__					:   Tuple       				=       mask_time_min_masks
								UpperCAmelCase__					:   List[Any]       				=       mask_feature_prob
								UpperCAmelCase__					:   Tuple       				=       mask_feature_length
								UpperCAmelCase__					:   Optional[int]       				=       mask_feature_min_masks

								# parameters for pretraining with codevector quantized representations
								UpperCAmelCase__					:   List[Any]       				=       num_codevectors_per_group
								UpperCAmelCase__					:   Optional[int]       				=       num_codevector_groups
								UpperCAmelCase__					:   str       				=       contrastive_logits_temperature
								UpperCAmelCase__					:   Tuple       				=       feat_quantizer_dropout
								UpperCAmelCase__					:   Dict       				=       num_negatives
								UpperCAmelCase__					:   Dict       				=       codevector_dim
								UpperCAmelCase__					:   Union[str, Any]       				=       proj_codevector_dim
								UpperCAmelCase__					:   int       				=       diversity_loss_weight

								# ctc loss
								UpperCAmelCase__					:   Union[str, Any]       				=       ctc_loss_reduction
								UpperCAmelCase__					:   Optional[Any]       				=       ctc_zero_infinity

								# pretraining loss
								UpperCAmelCase__					:   str       				=       replace_prob




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



'''simple docstring'''



from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer


@dataclass
class 						_snake_case   (       a__		):
					lowerCAmelCase :torch.FloatTensor


class 						_snake_case   (       a__    ,				a__		):




					@register_to_config
					def __init__( self							,							_lowerCamelCase = 3							,							_lowerCamelCase = 3							,							_lowerCamelCase = ("DownEncoderBlock2D",)							,							_lowerCamelCase = ("UpDecoderBlock2D",)							,							_lowerCamelCase = (64,)							,							_lowerCamelCase = 1							,							_lowerCamelCase = "silu"							,							_lowerCamelCase = 3							,							_lowerCamelCase = 32							,							_lowerCamelCase = 256							,							_lowerCamelCase = 32							,							_lowerCamelCase = None							,							_lowerCamelCase = 0.18215							,							_lowerCamelCase = "group"							,							):
								super().__init__()

								# pass init params to Encoder
								UpperCAmelCase__					:   str       				=       Encoder(
								    in_channels=_lowerCamelCase							,							out_channels=_lowerCamelCase							,							down_block_types=_lowerCamelCase							,							block_out_channels=_lowerCamelCase							,							layers_per_block=_lowerCamelCase							,							act_fn=_lowerCamelCase							,							norm_num_groups=_lowerCamelCase							,							double_z=_lowerCamelCase							,							)

								UpperCAmelCase__					:   Optional[Any]       				=       vq_embed_dim if vq_embed_dim is not None else latent_channels

								UpperCAmelCase__					:   Any       				=       nn.Convad(_lowerCamelCase							,							_lowerCamelCase							,							1)
								UpperCAmelCase__					:   Optional[int]       				=       VectorQuantizer(_lowerCamelCase							,							_lowerCamelCase							,							beta=0.25							,							remap=_lowerCamelCase							,							sane_index_shape=_lowerCamelCase)
								UpperCAmelCase__					:   Optional[int]       				=       nn.Convad(_lowerCamelCase							,							_lowerCamelCase							,							1)

								# pass init params to Decoder
								UpperCAmelCase__					:   str       				=       Decoder(
								    in_channels=_lowerCamelCase							,							out_channels=_lowerCamelCase							,							up_block_types=_lowerCamelCase							,							block_out_channels=_lowerCamelCase							,							layers_per_block=_lowerCamelCase							,							act_fn=_lowerCamelCase							,							norm_num_groups=_lowerCamelCase							,							norm_type=_lowerCamelCase							,							)




					@apply_forward_hook
					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = True):
								UpperCAmelCase__					:   Union[str, Any]       				=       self.encoder(_lowerCamelCase)
								UpperCAmelCase__					:   str       				=       self.quant_conv(_lowerCamelCase)

								if not return_dict:
											return (h,)

								return VQEncoderOutput(latents=_lowerCamelCase)




					@apply_forward_hook
					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = False							,							_lowerCamelCase = True):
								# also go through quantization layer
								if not force_not_quantize:
											UpperCAmelCase__				,    UpperCAmelCase__				,    UpperCAmelCase__					:   Optional[Any]       				=       self.quantize(_lowerCamelCase)
								else:
											UpperCAmelCase__					:   Union[str, Any]       				=       h
								UpperCAmelCase__					:   Any       				=       self.post_quant_conv(_lowerCamelCase)
								UpperCAmelCase__					:   Any       				=       self.decoder(_lowerCamelCase							,							quant if self.config.norm_type == """spatial""" else None)

								if not return_dict:
											return (dec,)

								return DecoderOutput(sample=_lowerCamelCase)




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = True):
								UpperCAmelCase__					:   Dict       				=       sample
								UpperCAmelCase__					:   Dict       				=       self.encode(_lowerCamelCase).latents
								UpperCAmelCase__					:   List[str]       				=       self.decode(_lowerCamelCase).sample

								if not return_dict:
											return (dec,)

								return DecoderOutput(sample=_lowerCamelCase)