o
    4ѹgT                     @   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   N)shapendimZ	new_emptyZ
bernoulli_Zdiv_)xr   r   r   Z	keep_probr	   Zrandom_tensor r   j/Users/admin/.pyenv/versions/3.10.0/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*   r+   r   r)   r   _ntuple+   s   r,   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   )Zkernel_sizer   )bias)r   r   	to_2tupler   nnZConv2dZLinearfc1actZDropoutdrop1ZIdentitynormfc2drop2)r   Zin_featuresZhidden_featuresZout_featuresZ	act_layerZ
norm_layerr0   ZdropZuse_convZ
drop_probsZlinear_layerr   r   r   r   >   s   
zMlp.__init__c                 C   s@   |  |}| |}| |}| |}| |}| |}|S r   )r3   r4   r5   r6   r7   r8   r   r   r   r   r   W   s   





zMlp.forward)	r   r   r   r   r2   ZGELUr   r   r"   r   r   r   r   r/   ;   s    r/   )r   FT)Ztorch.nnr2   collections.abcr$   	itertoolsr   	functoolsr   r    r!   r   Moduler   r,   Z	to_1tupler1   Z	to_3tupleZ	to_4tupleZ	to_ntupler/   r   r   r   r   <module>   s    	