
    3j                        S r SSKJr  SSKJrJrJr  SSKrSSKJr       S                 S	S jjr	            S
S jr
g)a*  Pure-PyTorch implementation of deform_conv2d matching torchvision.ops.deform_conv2d.

Implements both DCNv1 (mask=None) and DCNv2 (mask provided) as described in:
  - Deformable Convolutional Networks (https://arxiv.org/abs/1703.06211)
  - Deformable ConvNets v2 (https://arxiv.org/abs/1811.11168)
    )annotations)OptionalTupleUnionN)Tensorc                $   U R                   u  ppUR                   u  pp[        U[        5      (       a  XU4n[        U[        5      (       a  Xf4n[        U[        5      (       a  XD4nUu  nnUu  nnUu  nnX-  nX-  nUR                   S   SU-  -  nU	U-  nU
SU-  -   UUS-
  -  -
  S-
  U-  S-   nUSU-  -   UUS-
  -  -
  S-
  U-  S-   nUR                  UUSU-  UU5      nUSS2SS2SSS2SS2SS24   nUSS2SS2SSS2SS2SS24   nUb  UR                  UUUUU5      n[        R
                  " UU R                  U R                  S9n[        R
                  " UU R                  U R                  S9nUU-  U-
  R                  SSSUS5      n UU-  U-
  R                  SSSSU5      n![        R
                  " XR                  U R                  S9U-  n"[        R
                  " XR                  U R                  S9U-  n#U"R                  U5      R                  SSUSS5      n$U#R                  U5      R                  SSUSS5      n%U U$-   U-   n&U!U%-   U-   n'[        U U&U'UU5      n(Ub  U(UR                  S5      -  n(U(R                  XUUU5      n(UU-  n)U(R                  UUUUUU-  5      n(UR                  UU)UU5      n*[        R                  " SU(U*5      n+U+R                  XUU5      n+Ub  U+UR                  SSSS5      -   n+U+$ )a  Deformable 2D convolution (DCNv1/v2) without torchvision dependency.

Matches the signature and behaviour of ``torchvision.ops.deform_conv2d``.

Args:
    input: input feature map ``(B, C_in, H_in, W_in)``.
    offset: offset tensor ``(B, 2 * n_offset_groups * kH * kW, H_out, W_out)``.
        Layout per kernel point: ``(dy, dx)`` interleaved.
    weight: convolution kernel ``(C_out, C_in / groups, kH, kW)``.
    bias: optional bias ``(C_out,)``.
    stride: convolution stride ``(sH, sW)``.
    padding: zero-padding ``(pH, pW)``.
    dilation: kernel dilation ``(dH, dW)``.
    mask: optional DCNv2 modulation mask
        ``(B, n_offset_groups * kH * kW, H_out, W_out)`` in ``[0, 1]``.

Returns:
    Output feature map ``(B, C_out, H_out, W_out)``.
      Nr   )devicedtypezbgckn,gock->bgon)shape
isinstanceintreshapetorcharanger   r   repeat_interleaverepeat_bilinear_sample	unsqueezeeinsum),inputoffsetweightbiasstridepaddingdilationmaskBC_inH_inW_inC_outC_in_per_groupkHkWsHsWpHpWdHdWgroupsK
n_off_grpsc_per_off_grpH_outW_outoff_yoff_xgrid_ohgrid_owbase_hbase_wkh_offskw_offskern_hkern_wsample_hsample_wsampledC_out_per_groupweight_flatouts,                                               ]/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/aliked/deform_conv2d.pydeform_conv2drF   !   sB   :  ++AT$*LL!E2'3$(C  '&#!FBFBFB#F
AaQU+JJ&MAF]R26]*Q.259EAF]R26]*Q.259E ^^Az1q5%?F1aAq!#$E1aAq!#$E||Az1eU; ll5U[[IGll5U[[IGlR((Aq%;FlR((Aq!U;Fll2ll%++FKGll2ll%++FKG&&r*221aAqAF^^B''1aA6F &H&H uh*mTG DNN1-- ooaq%7GvoOooaEEMJG...!LK
,,)7K
@C
++au
-CDLLB1--J    c                F  ^ ^^^^^ ^!^" T R                   u  mnmm UR                  5       nUR                  5       nX-
  nX'-
  n	UR                  5       nUR                  5       nUS-   n
US-   nSUU U 4S jjnU" Xg5      nU" Xk5      nU" X5      nU" X5      nUR                  S[	        TS-
  S5      5      nU
R                  S[	        TS-
  S5      5      nUR                  S[	        T S-
  S5      5      nUR                  S[	        T S-
  S5      5      nT R                  TTTTT -  5      m"UR                   S   UR                   S   -  UR                   S   -  m!S	UU UU!U"U4S jjnU" UU5      nU" UU5      nU" UU5      nU" UU5      nUR                   S   nUR                   S   nUR                   S   nTTTUUU4nUR                  U5      nUR                  U5      nUR                  U5      nUR                  U5      nUR                  S5      nU	R                  S5      n	UR                  S5      nUR                  S5      nUR                  S5      nUR                  S5      nUU-  SU-
  -  SU	-
  -  UU-  SU-
  -  U	-  -   UU-  U-  SU	-
  -  -   UU-  U-  U	-  -   $ )
a  Sample input at fractional (sample_h, sample_w) positions with zero-padding.

Uses the same corner-validity bilinear interpolation as torchvision's
deformable-conv C++ kernel.

Args:
    input: ``(B, C_in, H_in, W_in)``
    sample_h: ``(B, n_off_grps, K, H_out, W_out)``
    sample_w: ``(B, n_off_grps, K, H_out, W_out)``
    n_off_grps: number of offset groups.
    c_per_off_grp: input channels per offset group.

Returns:
    Sampled tensor ``(B, n_off_grps, c_per_off_grp, K, H_out, W_out)``.
r	   c                d   > U S:  U T:  -  US:  -  UT:  -  R                  TR                  5      $ )Nr   )tor   )hwr#   r$   r   s     rE   _valid _bilinear_sample.<locals>._valid   s7    aAH%a0AH=AA%++NNrG   r   r
         c                   > U T-  U-   R                  TTT5      nUR                  S5      R                  SSTS5      n[        R                  " TSU5      $ )Nr
   r   rO   )r   r   expandr   gather)	hhwwidxr!   r$   r2   	flat_size
input_flatr1   s	      rE   _gather!_bilinear_sample.<locals>._gather   sQ    Dy2~&&q*i@mmA%%b"mR@||J3//rG   )rK   r   rL   r   returnr   )rT   r   rU   r   r[   r   )r   floorlongclampmaxr   r   )#r   r?   r@   r1   r2   _C_inh0w0lhlwh1w1rM   m00m01m10m11h0ch1cw0cw1crY   v00v01v10v11r0   r3   r4   r   r!   r#   r$   rW   rX   s#   `  ``                         @@@@@rE   r   r      s   , !;;AudD		B		B	B	B	B	B	aB	aBO O .C
.C
.C
.C
((1c$(A&
'C
((1c$(A&
'C
((1c$(A&
'C
((1c$(A&
'C q*mTD[IJq!HNN1$55q8III0 0
 #s
C
#s
C
#s
C
#s
CqANN1ENN1E
M1eU;E
++e
C
++e
C
++e
C
++e
C	aB	aB
--
C
--
C
--
C
--
C9B1r6*S3Y!b&-AB-FFsUW[\_a[aIbbehknenqsesvxexxxrG   )Nr	   r	   )r   r   rs   N)r   r   r   r   r   r   r   Optional[Tensor]r   Union[int, Tuple[int, int]]r   ru   r   ru   r    rt   r[   r   )r   r   r?   r   r@   r   r1   r   r2   r   r[   r   )__doc__
__future__r   typingr   r   r   r   r   rF   r    rG   rE   <module>rz      s   $ # ) )   "*0+1,2!``` ` 	`
 (` )` *` ` `FPyPyPy Py 	Py
 Py PyrG   