o
    Vh                     @   s   d dl Z d dlZ d dlm  mZ d dl mZmZ ddlmZ 	dded	e	d
e
dede	dedefddZ	dded	e	d
e
dede	dedefddZe jd G dd dejZe jd G dd deZdS )    N)nnTensor   )_log_api_usage_onceFư>Tinputp
block_sizeinplaceepstrainingreturnc                 C   s^  t j st j stt |dk s|dkrtd| d| jdkr,td| j d|r2|dkr4| S |  \}}}}	t	||	|}|| |	 |d || d	 |	| d	    }
t j
|||| d	 |	| d	 f| j| jd
}||
 tj||d gd dd}tj|d||f|d d}d	| }| ||   }|r| || | S | | | } | S )a  
    Implements DropBlock2d from `"DropBlock: A regularization method for convolutional networks"
    <https://arxiv.org/abs/1810.12890>`.

    Args:
        input (Tensor[N, C, H, W]): The input tensor or 4-dimensions with the first one
                    being its batch i.e. a batch with ``N`` rows.
        p (float): Probability of an element to be dropped.
        block_size (int): Size of the block to drop.
        inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``.
        eps (float): A value added to the denominator for numerical stability. Default: 1e-6.
        training (bool): apply dropblock if is ``True``. Default: ``True``.

    Returns:
        Tensor[N, C, H, W]: The randomly zeroed tensor after dropblock.
                  ?4drop probability has to be between 0 and 1, but got .   z#input should be 4 dimensional. Got  dimensions.r      dtypedevicer   value)r   r   stridekernel_sizepadding)torchjitis_scripting
is_tracingr   drop_block2d
ValueErrorndimsizeminemptyr   r   
bernoulli_Fpad
max_pool2dnumelsummul_)r   r   r	   r
   r   r   NCHWgammanoisenormalize_scale r6   N/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/ops/drop_block.pyr"   	   s,   
,,
r"   c                 C   s~  t j st j stt |dk s|dkrtd| d| jdkr,td| j d|r2|dkr4| S |  \}}}}	}
t	|||	|
}|| |	 |
 |d || d	 |	| d	  |
| d	    }t j
|||| d	 |	| d	 |
| d	 f| j| jd
}|| tj||d gd dd}tj|d|||f|d d}d	| }| ||   }|r| || | S | | | } | S )a  
    Implements DropBlock3d from `"DropBlock: A regularization method for convolutional networks"
    <https://arxiv.org/abs/1810.12890>`.

    Args:
        input (Tensor[N, C, D, H, W]): The input tensor or 5-dimensions with the first one
                    being its batch i.e. a batch with ``N`` rows.
        p (float): Probability of an element to be dropped.
        block_size (int): Size of the block to drop.
        inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``.
        eps (float): A value added to the denominator for numerical stability. Default: 1e-6.
        training (bool): apply dropblock if is ``True``. Default: ``True``.

    Returns:
        Tensor[N, C, D, H, W]: The randomly zeroed tensor after dropblock.
    r   r   r   r      z#input should be 5 dimensional. Got r      r   r   r      r   r   )r   r   r   r   )r   r   r    r!   r   drop_block3dr#   r$   r%   r&   r'   r   r   r(   r)   r*   
max_pool3dr,   r-   r.   )r   r   r	   r
   r   r   r/   r0   Dr1   r2   r3   r4   r5   r6   r6   r7   r;   7   s4   
<,
r;   c                       sX   e Zd ZdZddede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 )DropBlock2dz#
    See :func:`drop_block2d`.
    Fr   r   r	   r
   r   r   Nc                    s&   t    || _|| _|| _|| _d S N)super__init__r   r	   r
   r   selfr   r	   r
   r   	__class__r6   r7   rA   q   s
   

zDropBlock2d.__init__r   c                 C      t || j| j| j| j| jS z
        Args:
            input (Tensor): Input feature map on which some areas will be randomly
                dropped.
        Returns:
            Tensor: The tensor after DropBlock layer.
        )r"   r   r	   r
   r   r   rC   r   r6   r6   r7   forwardy      zDropBlock2d.forwardc                 C   s*   | j j d| j d| j d| j d}|S )Nz(p=z, block_size=z
, inplace=))rE   __name__r   r	   r
   )rC   sr6   r6   r7   __repr__   s   &zDropBlock2d.__repr__Fr   )rL   
__module____qualname____doc__floatintboolrA   r   rI   strrN   __classcell__r6   r6   rD   r7   r>   l   s
    $
r>   c                       sJ   e Zd ZdZddededededd	f
 fd
dZdedefddZ	  Z
S )DropBlock3dz#
    See :func:`drop_block3d`.
    Fr   r   r	   r
   r   r   Nc                    s   t  |||| d S r?   )r@   rA   rB   rD   r6   r7   rA      s   zDropBlock3d.__init__r   c                 C   rF   rG   )r;   r   r	   r
   r   r   rH   r6   r6   r7   rI      rJ   zDropBlock3d.forwardrO   )rL   rP   rQ   rR   rS   rT   rU   rA   r   rI   rW   r6   r6   rD   r7   rX      s    $rX   )Fr   T)r   torch.fxtorch.nn.functionalr   
functionalr)   r   utilsr   rS   rT   rU   r"   r;   fxwrapModuler>   rX   r6   r6   r6   r7   <module>   sR    
/
2