





import functools
import operator

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


_snake_case  :   List[str]						   =  logging.get_logger(__name__)

_snake_case  :   List[Any]						   =  {
    'microsoft/wavlm-base': 'https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json',
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}



class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			"""wavlm"""






  def __init__(    self			:		Optional[Any] ,  lowerCAmelCase_			:		List[str]=3_2 ,  lowerCAmelCase_			:		Union[str, Any]=7_6_8 ,  lowerCAmelCase_			:		str=1_2 ,  lowerCAmelCase_			:		Optional[int]=1_2 ,  lowerCAmelCase_			:		Optional[Any]=3_0_7_2 ,  lowerCAmelCase_			:		Optional[int]="gelu" ,  lowerCAmelCase_			:		str=0.1 ,  lowerCAmelCase_			:		Dict=0.1 ,  lowerCAmelCase_			:		int=0.1 ,  lowerCAmelCase_			:		Any=0.0 ,  lowerCAmelCase_			:		Dict=0.1 ,  lowerCAmelCase_			:		str=0.1 ,  lowerCAmelCase_			:		str=0.02 ,  lowerCAmelCase_			:		Tuple=1e-5 ,  lowerCAmelCase_			:		str="group" ,  lowerCAmelCase_			:		str="gelu" ,  lowerCAmelCase_			:		List[str]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) ,  lowerCAmelCase_			:		Tuple=(5, 2, 2, 2, 2, 2, 2) ,  lowerCAmelCase_			:		Tuple=(1_0, 3, 3, 3, 3, 2, 2) ,  lowerCAmelCase_			:		List[str]=False ,  lowerCAmelCase_			:		str=1_2_8 ,  lowerCAmelCase_			:		str=1_6 ,  lowerCAmelCase_			:		Tuple=3_2_0 ,  lowerCAmelCase_			:		Dict=8_0_0 ,  lowerCAmelCase_			:		Union[str, Any]=False ,  lowerCAmelCase_			:		List[str]=True ,  lowerCAmelCase_			:		str=0.05 ,  lowerCAmelCase_			:		Union[str, Any]=1_0 ,  lowerCAmelCase_			:		Optional[int]=2 ,  lowerCAmelCase_			:		List[Any]=0.0 ,  lowerCAmelCase_			:		Optional[Any]=1_0 ,  lowerCAmelCase_			:		int=3_2_0 ,  lowerCAmelCase_			:		Dict=2 ,  lowerCAmelCase_			:		Optional[Any]=0.1 ,  lowerCAmelCase_			:		int=1_0_0 ,  lowerCAmelCase_			:		str=2_5_6 ,  lowerCAmelCase_			:		Union[str, Any]=2_5_6 ,  lowerCAmelCase_			:		Union[str, Any]=0.1 ,  lowerCAmelCase_			:		Any="mean" ,  lowerCAmelCase_			:		Union[str, Any]=False ,  lowerCAmelCase_			:		Union[str, Any]=False ,  lowerCAmelCase_			:		str=2_5_6 ,  lowerCAmelCase_			:		Optional[int]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) ,  lowerCAmelCase_			:		List[Any]=(5, 3, 3, 1, 1) ,  lowerCAmelCase_			:		Optional[int]=(1, 2, 3, 1, 1) ,  lowerCAmelCase_			:		Optional[Any]=5_1_2 ,  lowerCAmelCase_			:		Dict=8_0 ,  lowerCAmelCase_			:		List[Any]=0 ,  lowerCAmelCase_			:		str=1 ,  lowerCAmelCase_			:		Dict=2 ,  lowerCAmelCase_			:		Dict=False ,  lowerCAmelCase_			:		Optional[int]=3 ,  lowerCAmelCase_			:		List[str]=2 ,  lowerCAmelCase_			:		int=3 ,  lowerCAmelCase_			:		Dict=None ,  **lowerCAmelCase_			:		str ,  )							->						List[str]:
     super().__init__(**lowerCAmelCase_ ,  pad_token_id=lowerCAmelCase_ ,  bos_token_id=lowerCAmelCase_ ,  eos_token_id=lowerCAmelCase_					)
     __lowerCAmelCase							     =     hidden_size
     __lowerCAmelCase							     =     feat_extract_norm
     __lowerCAmelCase							     =     feat_extract_activation
     __lowerCAmelCase							     =     list(lowerCAmelCase_					)
     __lowerCAmelCase							     =     list(lowerCAmelCase_					)
     __lowerCAmelCase							     =     list(lowerCAmelCase_					)
     __lowerCAmelCase							     =     conv_bias
     __lowerCAmelCase							     =     num_buckets
     __lowerCAmelCase							     =     max_bucket_distance
     __lowerCAmelCase							     =     num_conv_pos_embeddings
     __lowerCAmelCase							     =     num_conv_pos_embedding_groups
     __lowerCAmelCase							     =     len(self.conv_dim					)
     __lowerCAmelCase							     =     num_hidden_layers
     __lowerCAmelCase							     =     intermediate_size
     __lowerCAmelCase							     =     hidden_act
     __lowerCAmelCase							     =     num_attention_heads
     __lowerCAmelCase							     =     hidden_dropout
     __lowerCAmelCase							     =     attention_dropout
     __lowerCAmelCase							     =     activation_dropout
     __lowerCAmelCase							     =     feat_proj_dropout
     __lowerCAmelCase							     =     final_dropout
     __lowerCAmelCase							     =     layerdrop
     __lowerCAmelCase							     =     layer_norm_eps
     __lowerCAmelCase							     =     initializer_range
     __lowerCAmelCase							     =     num_ctc_classes
     __lowerCAmelCase							     =     vocab_size
     __lowerCAmelCase							     =     do_stable_layer_norm
     __lowerCAmelCase							     =     use_weighted_layer_sum
     __lowerCAmelCase							     =     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
     __lowerCAmelCase							     =     apply_spec_augment
     __lowerCAmelCase							     =     mask_time_prob
     __lowerCAmelCase							     =     mask_time_length
     __lowerCAmelCase							     =     mask_time_min_masks
     __lowerCAmelCase							     =     mask_feature_prob
     __lowerCAmelCase							     =     mask_feature_length

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

     # ctc loss
     __lowerCAmelCase							     =     ctc_loss_reduction
     __lowerCAmelCase							     =     ctc_zero_infinity

     # adapter
     __lowerCAmelCase							     =     add_adapter
     __lowerCAmelCase							     =     adapter_kernel_size
     __lowerCAmelCase							     =     adapter_stride
     __lowerCAmelCase							     =     num_adapter_layers
     __lowerCAmelCase							     =     output_hidden_size or hidden_size

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

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






  @property
  def     lowercase		(    self			:		Optional[Any]					)							->						Tuple:
     return functools.reduce(operator.mul ,  self.conv_stride ,  1					)








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
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block



@dataclass
class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase		,   _UpperCamelCase   ):

  """simple docstring"""


  @register_to_config
  def __init__(    self			:		List[Any] ,  lowerCAmelCase_			:		int = 6_5_5_3_6 ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		int = 2 ,  lowerCAmelCase_			:		int = 2 ,  lowerCAmelCase_			:		int = 0 ,  lowerCAmelCase_			:		str = "fourier" ,  lowerCAmelCase_			:		bool = True ,  lowerCAmelCase_			:		bool = False ,  lowerCAmelCase_			:		float = 0.0 ,  lowerCAmelCase_			:		Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") ,  lowerCAmelCase_			:		Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") ,  lowerCAmelCase_			:		Tuple[str] = "UNetMidBlock1D" ,  lowerCAmelCase_			:		str = None ,  lowerCAmelCase_			:		Tuple[int] = (3_2, 3_2, 6_4) ,  lowerCAmelCase_			:		str = None ,  lowerCAmelCase_			:		int = 8 ,  lowerCAmelCase_			:		int = 1 ,  lowerCAmelCase_			:		bool = False ,  )							->						Optional[int]:
     super().__init__()
     __lowerCAmelCase							     =     sample_size

     # time
     if time_embedding_type == "fourier":
        __lowerCAmelCase							     =     GaussianFourierProjection(
            embedding_size=8 ,  set_W_to_weight=lowerCAmelCase_ ,  log=lowerCAmelCase_ ,  flip_sin_to_cos=lowerCAmelCase_					)
        __lowerCAmelCase							     =     2 * block_out_channels[0]
     elif time_embedding_type == "positional":
        __lowerCAmelCase							     =     Timesteps(
            block_out_channels[0] ,  flip_sin_to_cos=lowerCAmelCase_ ,  downscale_freq_shift=lowerCAmelCase_					)
        __lowerCAmelCase							     =     block_out_channels[0]

     if use_timestep_embedding:
        __lowerCAmelCase							     =     block_out_channels[0] * 4
        __lowerCAmelCase							     =     TimestepEmbedding(
            in_channels=lowerCAmelCase_ ,  time_embed_dim=lowerCAmelCase_ ,  act_fn=lowerCAmelCase_ ,  out_dim=block_out_channels[0] ,  )

     __lowerCAmelCase							     =     nn.ModuleList([]					)
     __lowerCAmelCase							     =     None
     __lowerCAmelCase							     =     nn.ModuleList([]					)
     __lowerCAmelCase							     =     None

     # down
     __lowerCAmelCase							     =     in_channels
     for i, down_block_type in enumerate(lowerCAmelCase_					):
        __lowerCAmelCase							     =     output_channel
        __lowerCAmelCase							     =     block_out_channels[i]

        if i == 0:
           input_channel += extra_in_channels

        __lowerCAmelCase							     =     i == len(lowerCAmelCase_					) - 1

        __lowerCAmelCase							     =     get_down_block(
            lowerCAmelCase_ ,  num_layers=lowerCAmelCase_ ,  in_channels=lowerCAmelCase_ ,  out_channels=lowerCAmelCase_ ,  temb_channels=block_out_channels[0] ,  add_downsample=not is_final_block or downsample_each_block ,  )
        self.down_blocks.append(lowerCAmelCase_					)

     # mid
     __lowerCAmelCase							     =     get_mid_block(
         lowerCAmelCase_ ,  in_channels=block_out_channels[-1] ,  mid_channels=block_out_channels[-1] ,  out_channels=block_out_channels[-1] ,  embed_dim=block_out_channels[0] ,  num_layers=lowerCAmelCase_ ,  add_downsample=lowerCAmelCase_ ,  )

     # up
     __lowerCAmelCase							     =     list(reversed(lowerCAmelCase_					)					)
     __lowerCAmelCase							     =     reversed_block_out_channels[0]
     if out_block_type is None:
        __lowerCAmelCase							     =     out_channels
     else:
        __lowerCAmelCase							     =     block_out_channels[0]

     for i, up_block_type in enumerate(lowerCAmelCase_					):
        __lowerCAmelCase							     =     output_channel
        __lowerCAmelCase							     =     (
            reversed_block_out_channels[i + 1] if i < len(lowerCAmelCase_					) - 1 else final_upsample_channels
        )

        __lowerCAmelCase							     =     i == len(lowerCAmelCase_					) - 1

        __lowerCAmelCase							     =     get_up_block(
            lowerCAmelCase_ ,  num_layers=lowerCAmelCase_ ,  in_channels=lowerCAmelCase_ ,  out_channels=lowerCAmelCase_ ,  temb_channels=block_out_channels[0] ,  add_upsample=not is_final_block ,  )
        self.up_blocks.append(lowerCAmelCase_					)
        __lowerCAmelCase							     =     output_channel

     # out
     __lowerCAmelCase							     =     norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 ,  3_2					)
     __lowerCAmelCase							     =     get_out_block(
         out_block_type=lowerCAmelCase_ ,  num_groups_out=lowerCAmelCase_ ,  embed_dim=block_out_channels[0] ,  out_channels=lowerCAmelCase_ ,  act_fn=lowerCAmelCase_ ,  fc_dim=block_out_channels[-1] // 4 ,  )






  def     lowercase		(    self			:		Optional[Any] ,  lowerCAmelCase_			:		torch.FloatTensor ,  lowerCAmelCase_			:		Union[torch.Tensor, float, int] ,  lowerCAmelCase_			:		bool = True ,  )							->						Union[UNetaDOutput, Tuple]:
     __lowerCAmelCase							     =     timestep
     if not torch.is_tensor(lowerCAmelCase_					):
        __lowerCAmelCase							     =     torch.tensor([timesteps] ,  dtype=torch.long ,  device=sample.device					)
     elif torch.is_tensor(lowerCAmelCase_					) and len(timesteps.shape					) == 0:
        __lowerCAmelCase							     =     timesteps[None].to(sample.device					)

     __lowerCAmelCase							     =     self.time_proj(lowerCAmelCase_					)
     if self.config.use_timestep_embedding:
        __lowerCAmelCase							     =     self.time_mlp(lowerCAmelCase_					)
     else:
        __lowerCAmelCase							     =     timestep_embed[..., None]
        __lowerCAmelCase							     =     timestep_embed.repeat([1, 1, sample.shape[2]]					).to(sample.dtype					)
        __lowerCAmelCase							     =     timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])					)

     # 2. down
     __lowerCAmelCase							     =     ()
     for downsample_block in self.down_blocks:
        __lowerCAmelCase     ,     __lowerCAmelCase							     =     downsample_block(hidden_states=lowerCAmelCase_ ,  temb=lowerCAmelCase_					)
        down_block_res_samples += res_samples

     # 3. mid
     if self.mid_block:
        __lowerCAmelCase							     =     self.mid_block(lowerCAmelCase_ ,  lowerCAmelCase_					)

     # 4. up
     for i, upsample_block in enumerate(self.up_blocks					):
        __lowerCAmelCase							     =     down_block_res_samples[-1:]
        __lowerCAmelCase							     =     down_block_res_samples[:-1]
        __lowerCAmelCase							     =     upsample_block(lowerCAmelCase_ ,  res_hidden_states_tuple=lowerCAmelCase_ ,  temb=lowerCAmelCase_					)

     # 5. post-process
     if self.out_block:
        __lowerCAmelCase							     =     self.out_block(lowerCAmelCase_ ,  lowerCAmelCase_					)

     if not return_dict:
        return (sample,)

     return UNetaDOutput(sample=lowerCAmelCase_					)

