U
    (d                     @   sf   d dl Z d dlZ d dl mZmZ ddlmZ deeeeed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krPtd| |r\|dkr`| S d| }|dkr| jd gdg| jd   }ndg| j }t j	|| j
| jd	}||}|dkr|| | | 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)torchZjitZis_scripting
is_tracingr   stochastic_depth
ValueErrorshapendimemptyr   r   Z
bernoulli_Zdiv_)r   r   r   r	   Zsurvival_ratesizeZnoise r   D/tmp/pip-unpacked-wheel-vx7f76es/torchvision/ops/stochastic_depth.pyr      s"    

r   c                       sH   e Zd ZdZeedd fddZeedddZed	d
dZ	  Z
S )StochasticDepthz'
    See :func:`stochastic_depth`.
    N)r   r   r
   c                    s"   t    t|  || _|| _d S N)super__init__r   r   r   )selfr   r   	__class__r   r   r   7   s    
zStochasticDepth.__init__)r   r
   c                 C   s   t || j| j| jS r   )r   r   r   r	   )r   r   r   r   r   forward=   s    zStochasticDepth.forward)r
   c                 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   Ztorch.fxr   r   utilsr   r)   r*   boolr   ZfxwrapModuler   r   r   r   r   <module>   s   '