o
    پi                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlZddl	m
Z
 ddlm
  mZ ddlmZ ddlmZ G d	d
 d
e
jZddde
jfdedee dededeee
j  de
jfddZdS )z
BlurPool layer inspired by
 - Kornia's Max_BlurPool2d
 - Making Convolutional Networks Shift-Invariant Again :cite:`zhang2019shiftinvar`

Hacked together by Chris Ha and Ross Wightman
    )partialcomb)OptionalTypeN   )get_padding)	LayerTypec                       sZ   e Zd ZdZ				ddee deded	ed
df
 fddZdej	d
ej	fddZ
  ZS )
BlurPool2da  Creates a module that computes blurs and downsample a given feature map.
    See :cite:`zhang2019shiftinvar` for more details.
    Corresponds to the Downsample class, which does blurring and subsampling

    Args:
        channels = Number of input channels
        filt_size (int): binomial filter size for blurring. currently supports 3 (default) and 5.
        stride (int): downsampling filter stride

    Returns:
        torch.Tensor: the transformed tensor.
    N      reflectchannels	filt_sizestridepad_modereturnc                    s   t t|    dksJ || _ | _|| _|| _t |ddgd | _t	j
 fddt D t	jdd d   }|d d d f |d d d f  d d d d d d f }|d urb|| jddd}| jd|d	d
 d S )Nr   )dilation   c                    s   g | ]	}t  d  |qS )r   r   ).0kr    I/home/ubuntu/.local/lib/python3.10/site-packages/timm/layers/blur_pool.py
<listcomp>2   s    z'BlurPool2d.__init__.<locals>.<listcomp>)dtyper   filtF)
persistent)superr
   __init__r   r   r   r   r   paddingtorchtensorrangefloat32repeatregister_buffer)selfr   r   r   r   coeffsblur_filter	__class__r   r   r   !   s"   
4zBlurPool2d.__init__xc                 C   s`   t j|| j| jd}| jd u r |jd }| j|d| j| j}n| j}| j}t j	||| j
|dS )N)moder   )r   groups)Fpadr    r   r   shaper   expandr   conv2dr   )r'   r,   r   weightr   r   r   forward:   s   

zBlurPool2d.forward)Nr   r   r   )__name__
__module____qualname____doc__r   intstrr   r!   Tensorr5   __classcell__r   r   r*   r   r
      s$    r
   r   Taa_layerr   r   enablenoopr   c              
   C   s   | r|s|dur| S dS t | trH|  dddd} | dks&| dkr*tj} n| dks2| dkr5t} n| d	kr@ttd
d} nJ d|  dz| ||dW S  tye } z
| |W  Y d}~S d}~ww )z Anti-aliasing N_ -avgavgpoolblurblurpoolblurpcconstant)r   FzUnknown anti-aliasing layer (z).)r   r   )	
isinstancer;   lowerreplacenn	AvgPool2dr
   r   	TypeError)r>   r   r   r?   r@   er   r   r   	create_aaE   s"   
rQ   )r9   	functoolsr   mathr   typingr   r   r!   torch.nnrM   torch.nn.functional
functionalr/   r    r   r	   Moduler
   Identityr:   boolrQ   r   r   r   r   <module>   s6    3