o
    *jU                     @   sl   d dl mZ d dlZd dlmZ 	ddejdejdee fddZdd
e	de
fddZG dd dejZdS )    )OptionalNmaskdtypetgt_lenc                 C   s^   |   \}}|dur|n|}| ddddddf |d|||}|| t|jS )z_
    Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
    N   )sizeexpandtoZmasked_fillbooltorchZfinfomin)r   r   r   ZbszZsrc_lenZexpanded_mask r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/multi_modal/ofa/utils/utils.pyexpand_mask   s   

r           F	drop_probtrainingc                 C   sZ   |dks|s| S d| }d| j d df}|tj|| j| jd }|  | || }|S )a  
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    Args:
        x (`nn.Modules`): input nn layers.
        drop_prob (`float`): drop path ratio.
        training (`bool`): whether is training or inference.
    r   r   )r   device)shaper   Zrandr   r   Zfloor_div)xr   r   Z	keep_probr   Zrandom_tensoroutputr   r   r   	drop_path   s   	
r   c                       s8   e Zd ZdZd
 fdd	Zdd Zdefdd	Z  ZS )DropPathz
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
    See more details about drop path from https://arxiv.org/pdf/1605.07648v4.pdf.

    Args:
        drop_prob: drop path ratio.
    Nc                    s   t    || _d S N)super__init__r   )selfr   	__class__r   r   r   4   s   

zDropPath.__init__c                 C   s   t || j| jS r   )r   r   r   )r   r   r   r   r   forward8   s   zDropPath.forwardreturnc                 C   s   d | jS )Nzp={})formatr   )r   r   r   r   
extra_repr;   s   zDropPath.extra_reprr   )	__name__
__module____qualname____doc__r   r    strr#   __classcell__r   r   r   r   r   +   s
    r   r   )r   F)typingr   r   Ztorch.nnnnZTensorr   intr   floatr
   r   Moduler   r   r   r   r   <module>   s   

