o
    iT                     @   s   d dl mZ d dlZd dlmZ d dlmZ ddede	d	e	fd
dZ
G dd dejZdd ZedZedZedZedZeZG dd dejZdS )    N)repeat)partial        FT	drop_probtrainingscale_by_keepc                 C   s`   |dks|s| S d| }| j d fd| jd   }| ||}|dkr,|r,|| | | S )a(  Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
    the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
    changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
    'survival rate' as the argument.

    r      r   )r   )shapendim	new_empty
bernoulli_div_)xr   r   r   	keep_probr	   random_tensor r   Z/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/emotion2vec/timm_modules.py	drop_path   s   

r   c                       s<   e Zd ZdZddedef fddZdd	 Zd
d Z  Z	S )DropPathzYDrop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).r   Tr   r   c                    s   t t|   || _|| _d S N)superr   __init__r   r   )selfr   r   	__class__r   r   r      s   
zDropPath.__init__c                 C   s   t || j| j| jS r   )r   r   r   r   r   r   r   r   r   forward#      zDropPath.forwardc                 C   s   dt | jddS )Nz
drop_prob=   z0.3f)roundr   )r   r   r   r   
extra_repr&   r   zDropPath.extra_repr)r   T)
__name__
__module____qualname____doc__floatboolr   r   r    __classcell__r   r   r   r   r      s
    r   c                    s    fdd}|S )Nc                    s.   t | tjjrt | tst| S tt|  S r   )
isinstancecollectionsabcIterablestrtupler   )r   nr   r   parse,   s   z_ntuple.<locals>.parser   )r/   r0   r   r.   r   _ntuple+   s   r1   r      r      c                       s:   e Zd ZdZddejddddf fdd	Zdd	 Z  ZS )
MlpzAMLP as used in Vision Transformer, MLP-Mixer and related networksNTr   Fc	                    s   t    |p|}|p|}t|}t|}	|rttjddntj}
|
|||d d| _| | _t	|	d | _
|d ur?||nt | _|
|||d d| _t	|	d | _d S )Nr   )kernel_sizer   )bias)r   r   	to_2tupler   nnConv2dLinearfc1actDropoutdrop1Identitynormfc2drop2)r   in_featureshidden_featuresout_features	act_layer
norm_layerr6   dropuse_conv
drop_probslinear_layerr   r   r   r   >   s   
zMlp.__init__c                 C   s@   |  |}| |}| |}| |}| |}| |}|S r   )r;   r<   r>   r@   rA   rB   r   r   r   r   r   W   s   





zMlp.forward)	r!   r"   r#   r$   r8   GELUr   r   r'   r   r   r   r   r4   ;   s    r4   )r   FT)torch.nnr8   collections.abcr)   	itertoolsr   	functoolsr   r%   r&   r   Moduler   r1   	to_1tupler7   	to_3tuple	to_4tuple	to_ntupler4   r   r   r   r   <module>   s    	