+
    åûjf1  ã                  óª   € ^ RI Ht ^ RIt^ RIHtHtHt ^ RIt^ RIHtH	t	 ^ RI
Ht ^ RIHt ^ RIHt ^ RIHtHt R	.t ! R
 R	]	P*                  4      tR# )é    )ÚannotationsN)ÚCallableÚOptionalÚUnion)ÚTensorÚnn)Ú
functional)Útorch_version_lt)ÚKORNIA_CHECK)Ú_warn_at_callerÚis_compilingÚNaFlexc                  óN   a € ] tR t^!tRtR	R V 3R llltR R ltR R ltRtV ;t	# )
r   ak  NaFlex wrapper for vision embeddings that supports flexible image sizes.

This module wraps a patch embedding function and a square position embedding table. It computes the patch
embeddings of an image and adds the table resized to the image's patch grid, as transformers'
``Siglip2VisionEmbeddings.resize_positional_embeddings`` does for SigLIP 2's NaFlex variant: the resize is
bilinear interpolation (``align_corners=False``), antialiased when the grid is smaller than the table along an
axis, so a downsampled axis averages the table instead of sampling it. transformers always antialiases; when no
axis is downsampled antialiasing changes nothing (up to float rounding), and the plain resize keeps working where
the antialiased one does not (see the note). A square table is added unresized only when the patch grid is the
table's own grid.

Args:
    patch_embedding_fcn: A callable (e.g., Conv2d) that takes images and returns
        patch embeddings of shape :math:`(B, C, h, w)` or ``(B, N, C)``. A :math:`(B, C, h, w)` output gives
        the patch grid. A ``(B, N, C)`` output carries no grid: without ``patch_size`` its :math:`N` tokens
        are read as a square :math:`\sqrt{N} \times \sqrt{N}` grid in raster order, and a :math:`N` that is not
        a square number raises. A non-square grid therefore needs ``patch_size`` or a :math:`(B, C, h, w)` output:
        the :math:`8 \times 32` grid of a :math:`128 \times 512` image at patch 16 is read right with
        ``patch_size=16``, and as :math:`16 \times 16` without it. A square reading that takes rectangular patches,
        on an image whose sides differ by at least the patch height or width of its longer side, warns with a
        ``UserWarning`` naming ``patch_size`` (outside ``torch.compile`` and ``torch.export``), since it is wrong
        for square patches; ``patch_size`` silences it, also for a patch embedding that is rectangular. A
        square-patch convolution padded by at most half its kernel never warns.
    position_embedding: Position embedding table of shape :math:`(S^2, C)`, the raster-order rows of an
        :math:`S \times S` grid, to be interpolated to match input resolution. An ``nn.Parameter`` stays a
        parameter of the module, so it is trained with it; any other tensor is registered as a buffer. A table
        that is not a square grid is added as is when it has one row per patch.
    patch_size: The patch size :math:`p` or :math:`(p_h, p_w)` of a patch embedding that returns
        ``(B, N, C)``, which then has the grid :math:`(H // p_h, W // p_w)` of whole patches with the remainder
        dropped (a ``Conv2d`` with that kernel and stride); the grid must have :math:`N` cells. Ignored for a
        :math:`(B, C, h, w)` output. Default: ``None``, the square reading above.

Note:
    A float16 or bfloat16 table on the CPU is resized in float32 and cast back, as transformers does: the CPU has
    no half-precision antialiased bilinear kernel. A table on MPS that is downsampled is resized on the CPU in
    float32 with torch older than 2.7, which has no MPS antialiased kernel.

    Eager mode picks the resize per call, from the grid and the table size. An exported graph with dynamic H and W
    keeps the choice made for its example input. With the dynamo ONNX exporter
    (``torch.onnx.export(..., dynamo=True)``), export from an input whose grid is smaller than the table along both
    axes: the graph then resizes with antialiasing at every size, which is right for every grid because
    antialiasing changes nothing when upsampling. ``torch.export.export`` reports that size comparison as a
    constraint violation; ``torch.onnx.export`` exports anyway on torch 2.14 and refuses on torch 2.5.1. The
    legacy TorchScript exporter cannot export the antialiased resize (``UnsupportedOperatorError`` for
    ``aten::_upsample_bilinear2d_aa``), so it exports only from an example grid at least as large as the table
    along both axes, and its graph resizes without antialiasing at every size, which is exact only for such grids.

Example:
    >>> import torch
    >>> from kornia.models.processors.naflex import NaFlex
    >>> patch_fcn = torch.nn.Conv2d(3, 768, kernel_size=16, stride=16)
    >>> pos_embed = torch.randn(196, 768)  # 14x14 grid
    >>> model = NaFlex(patch_fcn, pos_embed)
    >>> x = torch.randn(1, 3, 224, 224)
    >>> out = model(x)
    >>> print(out.shape)
    torch.Size([1, 196, 768])
c               ó(   € V ^8„  d   QhRRRRRRRR/# )	é   Úpatch_embedding_fcnzCallable[[Tensor], Tensor]Úposition_embeddingr   Ú
patch_sizez%Optional[Union[int, tuple[int, int]]]ÚreturnÚNone© )Úformats   "Ú[C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\models\processors\naflex.pyÚ__annotate__ÚNaFlex.__annotate__]   s8   € ÷ Kñ Kà7ðKð #ðKð :ð	Kð
 
ñKó    c                	ó  <€ \         SV `  4        Wn        R V n        Ve1   \	        V\
        4      '       d   W33MV^ ,          V^,          3V n        \	        V\        P                  4      '       d	   W n        R # V P                  RV4       R # )Nr   )
ÚsuperÚ__init__r   r   Ú
isinstanceÚintr   Ú	Parameterr   Úregister_buffer)Úselfr   r   r   Ú	__class__s   &&&&€r   r   ÚNaFlex.__init__]   s{   ø€ ô 	‰ÑÔØ#6Ô Ø59ˆŒØÒ!ä,6°zÄ3×,GÒ,G�Ñ(ÈjÐYZÍmÐ]gÐhiÕ]jÐMkð ŒOô Ð(¬"¯,©,×7Ò7Ø&8Ö#à× Ñ Ð!5Ð7IÖJr   c               ó    € V ^8„  d   QhRRRR/# )r   Úpixel_valuesr   r   r   )r   s   "r   r   r   o   s   € ÷ 35ñ 35 Fð 35¨vñ 35r   c                ó`  € V P                  V4      pVP                  ^8X  d   VP                  R,          MRpVP                  ^8X  d"   VP                  ^4      P	                  ^^4      pVP                  ^,          pV P
                  p\        VP                  ^ ,          4      p\        P                  ! V4      pWw,          V8w  d-   Wd8X  d   W%P                  ^ 4      ,           # \        RV R24      hVe   TM V P                  ! V.VP                  R
R O5!  w  r‰W‰3Ww38X  d   W%P                  ^ 4      ,           # W‡8  ;'       g    W—8  p
VP                  WwR4      P                  ^^ ^4      P                  ^ 4      pVP                  P                  R8X  d0   VP                   \"        P$                  \"        P&                  39   g7   V
'       dO   VP                  P                  R8X  d4   \)        ^^^ 4      '       d!   VP+                  R\"        P,                  4      p\.        P0                  ! W¸V	3RRV
R	7      pVP3                  ^ 4      P                  ^^^ 4      P                  W‰,          R4      pVP+                  VP                  VP                   4      pW,P                  ^ 4      ,           # )a  Forward pass through NaFlex wrapper.

Args:
    pixel_values: Input tensor of shape ``(B, C, H, W)``.

Returns:
    Embeddings with interpolated positional embeddings of shape ``(B, N, C)``.

Raises:
    ValueError: If original positional embedding does not form a square grid and does not have one row per
        patch.
    BaseError: If a ``(B, N, C)`` patch output's grid cannot be read: with ``patch_size``, when
        :math:`(H // p_h) (W // p_w) \neq N`; without it, when :math:`N` is not a square number.
:r   NNNz8Original positional embedding is not a square grid (got z embeddings)ÚcpuÚmpsÚbilinearF)ÚsizeÚmodeÚalign_cornersÚ	antialiaséþÿÿÿéÿÿÿÿ)r   ÚndimÚshapeÚflattenÚ	transposer   r!   ÚmathÚisqrtÚ	unsqueezeÚ
ValueErrorÚ_token_gridÚreshapeÚpermuteÚdeviceÚtypeÚdtypeÚtorchÚfloat16Úbfloat16r
   ÚtoÚfloat32ÚFÚinterpolateÚsqueeze)r$   r(   Ú
embeddingsÚgridÚnum_patchesr   Úorig_numÚ	orig_gridÚh_gridÚw_gridr0   ÚposÚpos_resizeds   &&           r   ÚforwardÚNaFlex.forwardo   sI  € ð ×-Ñ-¨lÓ;ˆ
Ø'1§¡¸!Ô';ˆz×Ñ Ö#ÀˆØ�?‰?˜aÔØ#×+Ñ+¨AÓ.×8Ñ8¸¸AÓ>ˆJØ ×&Ñ& qÕ)ˆØ!×4Ñ4ÐÜÐ)×/Ñ/°Õ2Ó3ˆÜ—J’J˜xÓ(ˆ	ØÕ  HÔ,àÔ&Ø!×$@Ñ$@ÀÓ$CÕCÐCÜÐWÐX`ÐWaÐamÐnÓoÐoØ!%Ò!1™°t×7GÒ7GÈÐ7nÐVb×VhÑVhÐikÐilÐVmÓ7n‰ˆØÐ 	Ð5Ô5Ø× <Ñ <¸QÓ ?Õ?Ð?ð
 Ñ&×<Ð<¨&Ñ*<ˆ	Ø ×(Ñ(¨¸rÓB×JÑJÈ1ÈaÐQRÓS×]Ñ]Ð^_Ó`ˆð
 �J‰J�O‰O˜uÔ$¨¯©´u·}±}ÄeÇnÁnÐ6UÔ)Uß˜#Ÿ*™*Ÿ/™/¨UÔ2Ô7GÈÈ1Èa×7PÒ7Pà—&‘&˜¤§¡Ó.ˆCÜ—m’mØ˜vÐ&¨ZÀuÐXaô
ˆð "×)Ñ)¨!Ó,×4Ñ4°Q¸¸1Ó=×EÑEÀfÅoÐWYÓZˆØ!—n‘nÐ%7×%>Ñ%>Ð@R×@XÑ@XÓYˆà×1Ñ1°!Ó4Õ4Ð4r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rK   r!   ÚheightÚwidthr   ztuple[int, int]r   )r   s   "r   r   r   ¤   s(   € ÷ -ñ - sð -°Cð -Àð -Èñ -r   c                óè  a aaaaa€ S P                   eE   S P                   w  rESV,          SV,          uoo\        SS,          S8H  VVVV VV3R l4       SS3# \        P                  ! S4      p\        Wf,          S8H  V3R l4       \	        4       '       gZ   \        SS,
          4      V,          \        SS4      8¼  d2   \        RS RS RS RV RV RSV,          R RSV,          R R	24       Wf3# )
aT  Return the patch grid of the ``num_patches`` tokens of a ``height x width`` image.

``(height // p_h, width // p_w)`` with ``patch_size``, else the square grid of a square ``num_patches``.

Raises:
    BaseError: if that grid does not have ``num_patches`` cells, or without ``patch_size`` if
        ``num_patches`` is not a square number.
c                 óV   <€ R SP                    RS RS RS  RS RS S,           RS R2# )zBNaFlex reads the grid of a (B, N, C) patch output from patch_size=z: a ú x z image has a z	 grid of z+ patches, but the patch embedding returned Ú.)r   )rN   rU   rK   r$   rO   rV   s   €€€€€€r   Ú<lambda>Ú$NaFlex._token_grid.<locals>.<lambda>´   sL   ø€ ØXÐY]×YhÑYhÐXiÐimØ�h˜c % ¨°f°X¸SÀÀÈ	ÐRXÐ[aÕRaÐQbð c0Ø0;¨}¸Añ?r   c                 ó   <€ R S  R2# )zONaFlex reads a (B, N, C) patch output without patch_size as a square grid, but zv patch embeddings are not a square number. Pass patch_size to NaFlex, or make the patch embedding return (B, C, h, w).r   )rK   s   €r   r[   r\   ¾   s   ø€ ØaÐbmÐanð o'ñ 'r   zNaFlex reads the z patch embeddings of a rY   z image as a z grid, which takes z.3gzå pixel patches. If the patches are square, the grid is not square and the position table is added in the wrong layout: pass patch_size to NaFlex (which also silences this warning), or make the patch embedding return (B, C, h, w).)r   r   r7   r8   r   ÚabsÚmaxr   )	r$   rK   rU   rV   Úpatch_hÚpatch_wÚsiderN   rO   s	   ffff   @@r   r;   ÚNaFlex._token_grid¤   s
  ý€ ð �?‰?Ò&Ø#Ÿ™ÑˆGØ# wÕ.°¸Õ0@ˆNˆF�FÜØ˜• ;Ñ.÷ñ ôð ˜6�>Ð!Ü�zŠz˜+Ó&ˆÜØ�K˜;Ñ&ôô	
ô �~Š~¤# f¨u¥nÓ"5¸Õ"<ÄÀFÈEÓ@RÔ"RÜØ# K =Ð0GÈÀxÈsÐSXÐRYÐYeÐfjÐekÐknÐosÐntð u%Ø%+¨d¥]°3Ð$7°s¸5À4½<ÈÐ:Lð Mnðnôð ˆzÐr   )r   r   r   )N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   rR   r;   Ú__static_attributes__Ú__classcell__)r%   s   @r   r   r   !   s%   ø† ñ9÷vKò Kõ$35÷j-ó -r   )Ú
__future__r   r7   Útypingr   r   r   rA   r   r   Útorch.nnr	   rF   Úkornia.core._compatr
   Úkornia.core.checkr   Úkornia.core.utilsr   r   Ú__all__ÚModuler   r   r   r   Ú<module>rs      s>   ðõ" #ã ß ,Ñ ,ã ß Ý $å 0Ý *ß ;àˆ*€ôpˆR�Y‰Yö pr   