Ë
    þÍ:j2,  ã                   óV  — d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ dd	lmZ d
dlmZmZ d„ Zd„ Zd„ Z ed¬«      d„ «       Zej2                  j4                  	 	 	 ddedeeee   f   de
e   dedededefd„«       Z G d„ dej@                  «      Z!y)é    N)ÚUnion)ÚnnÚTensor)Úis_compile_supported)ÚBroadcastingList2)Ú_pair)Ú_assert_has_opsÚ_has_opsé   )Ú_log_api_usage_onceé   )Úcheck_roi_boxes_shapeÚconvert_boxes_to_roi_formatc                  ó   ‡ — ˆ fd„}|S )zkLazily wrap a function with torch.compile on the first call

    This avoids eagerly importing dynamo.
    c                 óF   •‡ — t        j                  ‰ «      ˆˆ fd„«       }|S )Nc                  ó    •— t        j                  ‰fi ‰¤Ž} t        j                  ‰«      |«      t	        «       ‰j
                  <    || i |¤ŽS ©N)ÚtorchÚcompileÚ	functoolsÚwrapsÚglobalsÚ__name__)ÚargsÚkwargsÚcompiled_fnÚcompile_kwargsÚfns      €€ún/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchvision/ops/roi_align.pyÚcompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook   sF   ø€ äŸ-™-¨Ñ=¨nÑ=ˆKØ%8¤Y§_¡_°RÓ%8¸Ó%EŒG‹I�b—k‘kÑ"Ù Ð/¨Ñ/Ð/ó    )r   r   )r   r    r   s   ` €r   Údecorate_fnz!lazy_compile.<locals>.decorate_fn   s&   ù€ Ü	�‰˜Ó	ô	0ó 
ð	0ð
 Ðr!   © )r   r"   s   ` r   Úlazy_compiler$      s   ø€ ôð Ðr!   c                 ó¤  ‡ ‡‡‡‡— ‰ j                  «       \  }Š}}|j                  d¬«      }|j                  d¬«      }|j                  «       }	|j                  «       }
t        j                  |	|dz
  k\  |dz
  |	dz   «      }t        j                  |	|dz
  k\  |dz
  |	«      }	t        j                  |	|dz
  k\  |j                  ‰ j                  «      |«      }t        j                  |
|dz
  k\  |dz
  |
dz   «      }t        j                  |
|dz
  k\  |dz
  |
«      }
t        j                  |
|dz
  k\  |j                  ‰ j                  «      |«      }||	z
  }||
z
  }d|z
  }d|z
  }ˆˆ ˆˆˆfd„} ||	|
«      } ||	|«      } |||
«      } |||«      }d„ } |||«      } |||«      } |||«      } |||«      }||z  ||z  z   ||z  z   ||z  z   }|S )Nr   ©Úminr   ç      ð?c                 óL  •— ‰�F‰€J ‚t        j                  ‰d d …d d d …f   | d«      } t        j                  ‰d d …d d d …f   |d«      }‰‰d d …d d d d d f   t        j                  ‰‰j                  ¬«      d d d …d d d d f   | d d …d d d …d d d …d f   |d d …d d d d …d d d …f   f   S )Nr   ©Údevice)r   ÚwhereÚaranger+   )ÚyÚxÚchannelsÚinputÚroi_batch_indÚxmaskÚymasks     €€€€€r   Úmasked_indexz+_bilinear_interpolate.<locals>.masked_indexB   sÇ   ø€ ð ÐØÐ$Ð$Ð$Ü—‘˜E¢! Tª1 *Ñ-¨q°!Ó4ˆAÜ—‘˜E¢! Tª1 *Ñ-¨q°!Ó4ˆAØØš!˜T 4¨¨t°TÐ9Ñ:Ü�L‰L˜¨%¯,©,Ô7¸ºaÀÀtÈTÐSWÐ8WÑXØŠa�’q˜$¢ 4Ð'Ñ(ØŠa��tšQ ¢aÐ'Ñ(ð*ñ
ð 	
r!   c           	      óH   — | d d …d d d …d d d …d f   |d d …d d d d …d d d …f   z  S r   r#   )r.   r/   s     r   Ú
outer_prodz)_bilinear_interpolate.<locals>.outer_prodW   s1   € Ø’�Dš!˜T¢1 dÐ*Ñ+¨a²°4¸ºqÀ$ÊÐ0IÑ.JÑJÐJr!   )ÚsizeÚclampÚintr   r,   ÚtoÚdtype)r1   r2   r.   r/   r4   r3   Ú_ÚheightÚwidthÚy_lowÚx_lowÚy_highÚx_highÚlyÚlxÚhyÚhxr5   Úv1Úv2Úv3Úv4r7   Úw1Úw2Úw3Úw4Úvalr0   s   ``  ``                      @r   Ú_bilinear_interpolaterQ   #   sä  ü€ ð "'§¡£Ñ€A€x�˜ð 	
�‰�Aˆ‹€AØ	�‰�Aˆ‹€AØ�E‰E‹G€EØ�E‰E‹G€EÜ�[‰[˜ &¨1¡*Ñ,¨f°q©j¸%À!¹)ÓD€FÜ�K‰K˜ ¨!¡Ñ+¨V°a©Z¸Ó?€EÜ�‰�E˜V a™ZÑ'¨¯©¨e¯k©kÓ):¸AÓ>€Aä�[‰[˜ %¨!¡)Ñ+¨U°Q©Y¸À¹	ÓB€FÜ�K‰K˜ ¨¡Ñ*¨E°A©I°uÓ=€EÜ�‰�E˜U Q™YÑ&¨¯©¨U¯[©[Ó(9¸1Ó=€Aà	
ˆU‰€BØ	
ˆU‰€BØ	ˆr‰€BØ	ˆr‰€B÷

ð 
ñ 
�e˜UÓ	#€BÙ	�e˜VÓ	$€BÙ	�f˜eÓ	$€BÙ	�f˜fÓ	%€BòKñ 
�B˜Ó	€BÙ	�B˜Ó	€BÙ	�B˜Ó	€BÙ	�B˜Ó	€Bà
ˆr‰'�B˜‘GÑ
˜b 2™gÑ
%¨¨R©Ñ
/€CØ€Jr!   c                 ó    — t        j                  «       r9| j                  r-| j                  t         j                  k7  r| j                  «       S | S r   )r   Úis_autocast_enabledÚis_cudar<   ÚdoubleÚfloat)Útensors    r   Ú
maybe_castrX   e   s4   € Ü× Ñ Ô" v§~¢~¸&¿,¹,Ì%Ï,É,Ò:VØ�|‰|‹~Ðàˆr!   T)Údynamicc           
      óø  — | j                   }t        | «      } t        |«      }| j                  «       \  }}}	}
t        j                  || j
                  ¬«      }t        j                  || j
                  ¬«      }|d d …df   j                  «       }|rdnd}|d d …df   |z  |z
  }|d d …df   |z  |z
  }|d d …df   |z  |z
  }|d d …df   |z  |z
  }||z
  }||z
  }|s.t        j                  |d	¬
«      }t        j                  |d	¬
«      }||z  }||z  }|dkD  }|r|nt        j                  ||z  «      }|r|nt        j                  ||z  «      }	 |rVt        ||z  d«      }t        j                  || j
                  ¬«      }t        j                  || j
                  ¬«      }d }d }n‚t        j                  ||z  d¬
«      }t        j                  |	| j
                  ¬«      }t        j                  |
| j
                  ¬«      }|d d d …f   |d d …d f   k  }|d d d …f   |d d …d f   k  }d„ } ||«      |d d d …d f    ||«      z  z   |d d d d …f   dz   j                  | j                   «       |||z  «      z  z   }  ||«      |d d d …d f    ||«      z  z   |d d d d …f   dz   j                  | j                   «       |||z  «      z  z   }!t        | || |!||«      }"|sHt        j                  |d d …d d d d d …d f   |"d«      }"t        j                  |d d …d d d d d d …f   |"d«      }"|"j                  d«      }#t        |t        j                  «      r|#|d d …d d d f   z  }#n|#|z  }#|#j                  |«      }#|#S )Nr*   r   g      à?g        r   r   é   é   r(   r&   c                 ó   — | d d …d d f   S r   r#   )Úts    r   Úfrom_Kz_roi_align.<locals>.from_K¬   s   € Ø’�D˜$�ÑÐr!   )éÿÿÿÿéþÿÿÿ)r<   rX   r8   r   r-   r+   r:   r9   ÚceilÚmaxr;   rQ   r,   ÚsumÚ
isinstancer   )$r1   ÚroisÚspatial_scaleÚpooled_heightÚpooled_widthÚsampling_ratioÚalignedÚ
orig_dtyper=   r>   r?   ÚphÚpwr2   ÚoffsetÚroi_start_wÚroi_start_hÚ	roi_end_wÚ	roi_end_hÚ	roi_widthÚ
roi_heightÚ
bin_size_hÚ
bin_size_wÚexact_samplingÚroi_bin_grid_hÚroi_bin_grid_wÚcountÚiyÚixr4   r3   r_   r.   r/   rP   Úoutputs$                                       r   Ú
_roi_alignr   r   s›  € à—‘€Jä�uÓ€EÜ�dÓ€DàŸ*™*›,Ñ€A€qˆ&�%ä	�‰�m¨E¯L©LÔ	9€BÜ	�‰�l¨5¯<©<Ô	8€Bð
 š˜A˜‘J—N‘NÓ$€MÙ‰S €FØ’q˜!�t‘*˜}Ñ,¨vÑ5€KØ’q˜!�t‘*˜}Ñ,¨vÑ5€KØ’Q˜�T‘
˜]Ñ*¨VÑ3€IØ’Q˜�T‘
˜]Ñ*¨VÑ3€Ià˜KÑ'€IØ˜[Ñ(€JÙÜ—K‘K 	¨sÔ3ˆ	Ü—[‘[ °Ô5ˆ
à˜mÑ+€JØ˜\Ñ)€Jà# aÑ'€Ná'5‘^¼5¿:¹:ÀjÐS`ÑF`Ó;a€NÙ'5‘^¼5¿:¹:ÀiÐR^ÑF^Ó;_€Nðñ Ü�N ^Ñ3°QÓ7ˆÜ�\‰\˜.°·±Ô>ˆÜ�\‰\˜.°·±Ô>ˆØˆØ‰ä—‘˜N¨^Ñ;ÀÔCˆô �\‰\˜&¨¯©Ô6ˆÜ�\‰\˜%¨¯©Ô5ˆØ�4š�7‘˜nªQ°¨WÑ5Ñ5ˆØ�4š�7‘˜nªQ°¨WÑ5Ñ5ˆò ñ 	ˆ{ÓØ
ˆT’1�dˆ]Ñ
™f ZÓ0Ñ
0ñ	1àˆd�Dš!ˆmÑ˜sÑ"×
&Ñ
& u§{¡{Ó
3±f¸ZÈ.Ñ=XÓ6YÑ
Yñ	Zð ñ 	ˆ{ÓØ
ˆT’1�dˆ]Ñ
™f ZÓ0Ñ
0ñ	1àˆd�Dš!ˆmÑ˜sÑ"×
&Ñ
& u§{¡{Ó
3±f¸ZÈ.Ñ=XÓ6YÑ
Yñ	Zð ô
    }°a¸¸EÀ5Ó
I€Cñ Ü�k‰k˜%¢ 4¨¨t²Q¸Ð <Ñ=¸sÀAÓFˆÜ�k‰k˜%¢ 4¨¨t°Tº1Ð <Ñ=¸sÀAÓFˆà�W‰W�XÓ€FÜ�%œŸ™Ô&Ø�%š˜4  tÐ+Ñ,Ñ,‰à�%‰ˆà�Y‰Y�zÓ"€Fà€Mr!   r1   ÚboxesÚoutput_sizerg   rj   rk   Úreturnc           	      ó¼  — t         j                  j                  «       s-t         j                  j                  «       st	        t
        «       t        |«       |}t        |«      }t        |t         j                  «      st        |«      }t         j                  j                  «       sxt        «       r8t        j                  «       rZ| j                  s| j                  s| j                  r6t!        | j"                  j$                  «      rt'        | |||d   |d   ||«      S t)        «        t         j*                  j,                  j                  | |||d   |d   ||«      S )aj  
    Performs Region of Interest (RoI) Align operator with average pooling, as described in Mask R-CNN.

    Args:
        input (Tensor[N, C, H, W]): The input tensor, i.e. a batch with ``N`` elements. Each element
            contains ``C`` feature maps of dimensions ``H x W``.
            If the tensor is quantized, we expect a batch size of ``N == 1``.
        boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
            format where the regions will be taken from.
            The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
            If a single Tensor is passed, then the first column should
            contain the index of the corresponding element in the batch, i.e. a number in ``[0, N - 1]``.
            If a list of Tensors is passed, then each Tensor will correspond to the boxes for an element i
            in the batch.
        output_size (int or Tuple[int, int]): the size of the output (in bins or pixels) after the pooling
            is performed, as (height, width).
        spatial_scale (float): a scaling factor that maps the box coordinates to
            the input coordinates. For example, if your boxes are defined on the scale
            of a 224x224 image and your input is a 112x112 feature map (resulting from a 0.5x scaling of
            the original image), you'll want to set this to 0.5. Default: 1.0
        sampling_ratio (int): number of sampling points in the interpolation grid
            used to compute the output value of each pooled output bin. If > 0,
            then exactly ``sampling_ratio x sampling_ratio`` sampling points per bin are used. If
            <= 0, then an adaptive number of grid points are used (computed as
            ``ceil(roi_width / output_width)``, and likewise for height). Default: -1
        aligned (bool): If False, use the legacy implementation.
            If True, pixel shift the box coordinates it by -0.5 for a better alignment with the two
            neighboring pixel indices. This version is used in Detectron2

    Returns:
        Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
    r   r   )r   ÚjitÚis_scriptingÚ
is_tracingr   Ú	roi_alignr   r   re   r   r   r
   Ú$are_deterministic_algorithms_enabledrT   Úis_mpsÚis_xpur   r+   Útyper   r	   ÚopsÚtorchvision)r1   r€   r�   rg   rj   rk   rf   s          r   r‡   r‡   Ë   s  € ôR �9‰9×!Ñ!Ô#¬E¯I©I×,@Ñ,@Ô,BÜœIÔ&Ü˜%Ô Ø€DÜ˜Ó$€KÜ�dœEŸL™LÔ)Ü*¨4Ó0ˆÜ�9‰9×!Ñ!Ô#ä”
Ü×:Ñ:Ô<À%Ç-Â-ÐSX×S_ÒS_Ðch×coÒcoÜ" 5§<¡<×#4Ñ#4Ô5Ü˜e T¨=¸+Àa¹.È+ÐVWÉ.ÐZhÐjqÓrÐrÜÔÜ�9‰9× Ñ ×*Ñ*Øˆt�] K°¡N°KÀ±NÀNÐT[óð r!   c            	       ón   ‡ — e Zd ZdZ	 ddee   dededefˆ fd„Zde	de
e	ee	   f   d	e	fd
„Zd	efd„Zˆ xZS )ÚRoIAlignz 
    See :func:`roi_align`.
    r�   rg   rj   rk   c                 óp   •— t         ‰| �  «        t        | «       || _        || _        || _        || _        y r   )ÚsuperÚ__init__r   r�   rg   rj   rk   )Úselfr�   rg   rj   rk   Ú	__class__s        €r   r’   zRoIAlign.__init__  s7   ø€ ô 	‰ÑÔÜ˜DÔ!Ø&ˆÔØ*ˆÔØ,ˆÔØˆ�r!   r1   rf   r‚   c                 ór   — t        ||| j                  | j                  | j                  | j                  «      S r   )r‡   r�   rg   rj   rk   )r“   r1   rf   s      r   ÚforwardzRoIAlign.forward  s0   € Ü˜  d×&6Ñ&6¸×8JÑ8JÈD×L_ÑL_Ðae×amÑamÓnÐnr!   c           
      ó    — | j                   j                  › d| j                  › d| j                  › d| j                  › d| j
                  › d�
}|S )Nz(output_size=z, spatial_scale=z, sampling_ratio=z
, aligned=ú))r”   r   r�   rg   rj   rk   )r“   Úss     r   Ú__repr__zRoIAlign.__repr__  s^   € à�~‰~×&Ñ&Ð'ð (Ø×+Ñ+Ð,Ø˜t×1Ñ1Ð2Ø × 3Ñ 3Ð4Ø˜Ÿ™˜Øðð 	
ð ˆr!   )F)r   Ú
__module__Ú__qualname__Ú__doc__r   r:   rV   Úboolr’   r   r   Úlistr–   Ústrrš   Ú__classcell__)r”   s   @r   r�   r�     su   ø„ ñð ñà& sÑ+ðð ðð ð	ð
 õðo˜Vð o¨5°¸¸f¹Ð1EÑ+Fð oÈ6ó oð	˜#÷ 	r!   r�   )r(   r`   F)"r   Útypingr   r   Útorch.fxr   r   Útorch._dynamo.utilsr   Útorch.jit.annotationsr   Útorch.nn.modules.utilsr   Útorchvision.extensionr	   r
   Úutilsr   Ú_utilsr   r   r$   rQ   rX   r   ÚfxÚwraprŸ   r:   rV   rž   r‡   ÚModuler�   r#   r!   r   ú<module>r­      sß   ðÛ Ý ã Û ß Ý 4Ý 3Ý (ß ;å 'ß Fòò&=òDñ �dÔñUó ðUðp ‡�‡�ð
 ØØñ8Øð8à�˜˜f™Ð%Ñ&ð8ð # 3Ñ'ð8ð ð	8ð
 ð8ð ð8ð ò8ó ð8ôvˆr�y‰yõ r!   