o
    ^i                     @   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   sz  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	krPtd
| d|| |	 |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   r   block size should be odd. Got  which is even.   dtypedevice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 r8   U/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/torchvision/ops/drop_block.pyr$   	   s0   
,,
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	krRtd
| d|| |	 |
 |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"   r#   r   drop_block3dr%   r&   r'   r(   r)   r   r   r*   r+   r,   
max_pool3dr.   r/   r0   )r   r   r	   r
   r   r   r1   r2   Dr3   r4   r5   r6   r7   r8   r8   r9   r=   :   s8   
<,
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__r8   r9   rC   w   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   rE   r   r8   r8   r9   forward      zDropBlock2d.forwardc                 C   s*   | j j d| j d| j d| j d}|S )Nz(p=z, block_size=z
, inplace=))rG   __name__r   r	   r
   )rE   sr8   r8   r9   __repr__   s   &zDropBlock2d.__repr__Fr   )rN   
__module____qualname____doc__floatintboolrC   r   rK   strrP   __classcell__r8   r8   rF   r9   r@   r   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 rA   )rB   rC   rD   rF   r8   r9   rC      s   zDropBlock3d.__init__r   c                 C   rH   rI   )r=   r   r	   r
   r   r   rJ   r8   r8   r9   rK      rL   zDropBlock3d.forwardrQ   )rN   rR   rS   rT   rU   rV   rW   rC   r   rK   rY   r8   r8   rF   r9   rZ      s    $rZ   )Fr   T)r    torch.fxtorch.nn.functionalr   
functionalr+   r   utilsr   rU   rV   rW   r$   r=   fxwrapModuler@   rZ   r8   r8   r8   r9   <module>   sR    
2
5