o
    Vh                     @   sn   d dl Z d dlZ d dl mZmZ ddlmZ ddededed	ed
ef
ddZ	e j
d G dd dejZdS )    N)nnTensor   )_log_api_usage_onceTinputpmodetrainingreturnc                 C   s   t j st j stt |dk s|dkrtd| |dvr(td| |r.|dkr0| S d| }|dkrG| jd gdg| jd   }ndg| j }t j	|| j
| jd	}||}|dkre|| | | S )
a  
    Implements the Stochastic Depth from `"Deep Networks with Stochastic Depth"
    <https://arxiv.org/abs/1603.09382>`_ used for randomly dropping residual
    branches of residual architectures.

    Args:
        input (Tensor[N, ...]): The input tensor or arbitrary dimensions with the first one
                    being its batch i.e. a batch with ``N`` rows.
        p (float): probability of the input to be zeroed.
        mode (str): ``"batch"`` or ``"row"``.
                    ``"batch"`` randomly zeroes the entire input, ``"row"`` zeroes
                    randomly selected rows from the batch.
        training: apply stochastic depth if is ``True``. Default: ``True``

    Returns:
        Tensor[N, ...]: The randomly zeroed tensor.
    g        g      ?z4drop probability has to be between 0 and 1, but got )batchrowz0mode has to be either 'batch' or 'row', but got r   r      )dtypedevice)torchjitis_scripting
is_tracingr   stochastic_depth
ValueErrorshapendimemptyr   r   
bernoulli_div_)r   r   r   r	   survival_ratesizenoise r   T/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/ops/stochastic_depth.pyr      s"   

r   c                       sN   e Zd ZdZdededdf fddZdedefd	d
ZdefddZ	  Z
S )StochasticDepthz'
    See :func:`stochastic_depth`.
    r   r   r
   Nc                    s"   t    t|  || _|| _d S N)super__init__r   r   r   )selfr   r   	__class__r   r   r#   7   s   

zStochasticDepth.__init__r   c                 C   s   t || j| j| jS r!   )r   r   r   r	   )r$   r   r   r   r   forward=   s   zStochasticDepth.forwardc                 C   s"   | j j d| j d| j d}|S )Nz(p=z, mode=))r&   __name__r   r   )r$   sr   r   r   __repr__@   s   zStochasticDepth.__repr__)r)   
__module____qualname____doc__floatstrr#   r   r'   r+   __classcell__r   r   r%   r   r    2   s
    r    )T)r   torch.fxr   r   utilsr   r/   r0   boolr   fxwrapModuler    r   r   r   r   <module>   s     '