
    |2g              
       X    d dl Zd dlZdej                  dej                  dededef
dZy)    Npositional_embeddingx
patch_sizewhc                 R   | j                   dk(  sJ d       |j                  d   dz
  }| j                  d   dz
  }||k(  r ||k(  r| j                  |j                        S |j                  d   }| dd }| dd }	||z  }
||z  }|
|z  |k(  sJ d       |
dz   |dz   }}
t	        t        j                  |            }t        j                  j                  j                  |	j                  d|||      j                  dd	dd      |
|z  ||z  fd
dd      }t	        |
      |j                  d   k(  rt	        |      |j                  d   k(  sJ d       |j                  ddd	d      j                  d|      }t        j                  ||gd      }|j                  |j                        S )a(  
    Interpolate the positional encoding for CLIP to the number of patches in the image given width and height.
    Modified from DINO ViT `interpolate_pos_encoding` method.
    https://github.com/facebookresearch/dino/blob/7c446df5b9f45747937fb0d72314eb9f7b66930a/vision_transformer.py#L174
       zpos_encoding must be 2D   r   Nz Number of patches does not matchg?   bicubicF)scale_factormodealign_cornersrecompute_scale_factorzInterpolation error.)dim)ndimshapetodtypeintnpsqrttorchnn
functionalinterpolatereshapepermutecat)r   r   r   r   r   num_patchesnum_og_patchesr   class_pos_embedpatch_pos_embedw0h0patch_per_axpatch_pos_embed_interppos_embed_interps                  J/home/cameronsmith/repos/FeatUp/featup/featurizers/maskclip/interpolate.py interpolate_positional_embeddingr,      s     $$)D+DD ''!*q.K)//2Q6Nn$a#&&qww//
''"+C*2A.O*12.O 
jB	
jB7k!E#EE #XrCxB rww~./L"XX00<<<sCKKAqRSUVW<'l):;$ =  	B)//33BCYC_C_`bCc8c 4;;Aq!QGOOPRTWXyy/3I!JPQRqww''    )numpyr   r   Tensorr   r,    r-   r+   <module>r1      s@     1(,,1(+0<<1(EH1(MP1(UX1(r-   