ó
    Eñi2,  ã                   óT  • S SK r S SKJr  S SKrS SKrS SKJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJrJr  SS	KJr  S
SKJrJr  S rS rS r\" SS9S 5       r\R2                  R4                     SS\S\\\\   4   S\
\   S\S\S\S\4S jj5       r " S S\R@                  5      r!g)é    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                  ó   ^ • U 4S jnU$ )zcLazily wrap a function with torch.compile on the first call

This avoids eagerly importing dynamo.
c                 óJ   >^ • [         R                  " T 5      UU 4S j5       nU$ )Nc                  ó¤   >• [         R                  " T40 TD6n[        R                  " T5      " U5      [	        5       TR
                  '   U" U 0 UD6$ ©N)ÚtorchÚcompileÚ	functoolsÚwrapsÚglobalsÚ__name__)ÚargsÚkwargsÚcompiled_fnÚcompile_kwargsÚfns      €€ÚV/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torchvision/ops/roi_align.pyÚcompile_hookÚ7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook   sD   ø€ äŸ-š-¨Ñ=¨nÑ=ˆKÜ%.§_¢_°RÔ%8¸Ó%EŒG‹I�b—k‘kÑ"Ù Ð/¨Ñ/Ð/ó    )r   r   )r   r    r   s   ` €r   Údecorate_fnÚ!lazy_compile.<locals>.decorate_fn   s&   ù€ Ü	�Š˜Ó	õ	0ó 
ð	0ð
 Ðr"   © )r   r#   s   ` r   Úlazy_compiler&      s   ø€ õð Ðr"   c                 óˆ  ^ ^^^^• T R                  5       u  nmpxUR                  SS9nUR                  SS9nUR                  5       n	UR                  5       n
[        R                  " X—S-
  :¬  US-
  U	S-   5      n[        R                  " X—S-
  :¬  US-
  U	5      n	[        R                  " X—S-
  :¬  UR                  T R                  5      U5      n[        R                  " X¨S-
  :¬  US-
  U
S-   5      n[        R                  " X¨S-
  :¬  US-
  U
5      n
[        R                  " X¨S-
  :¬  UR                  T R                  5      U5      nX)-
  nX:-
  nSU-
  nSU-
  nUU UUU4S jnU" Xš5      nU" Xœ5      nU" Xº5      nU" X¼5      nS nU" UU5      nU" Xþ5      nU" UU5      nU" XÞ5      nUU-  UU-  -   UU-  -   UU-  -   nU$ )Nr   ©Úminr   ç      ð?c                 óP  >• TbI  Tc   e[         R                  " TS S 2S S S 24   U S5      n [         R                  " TS S 2S S S 24   US5      nTTS S 2S S S S S 4   [         R                  " TTR                  S9S S S 2S S S S 4   U S S 2S S S 2S S S 2S 4   US S 2S S S S 2S S S 24   4   $ )Nr   ©Údevice)r   ÚwhereÚaranger-   )ÚyÚxÚchannelsÚinputÚroi_batch_indÚxmaskÚymasks     €€€€€r   Úmasked_indexÚ+_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   • U S S 2S S S 2S S S 2S 4   US S 2S S S S 2S S S 24   -  $ r   r%   )r0   r1   s     r   Ú
outer_prodÚ)_bilinear_interpolate.<locals>.outer_prodW   s1   € Ø’�Dš!˜T¢1 dÐ*Ñ+¨a²°4¸ºqÀ$ÊÐ0IÑ.JÑJÐJr"   )ÚsizeÚclampÚintr   r.   ÚtoÚdtype)r3   r4   r0   r1   r6   r5   Ú_ÚheightÚwidthÚy_lowÚx_lowÚy_highÚx_highÚlyÚlxÚhyÚhxr7   Úv1Úv2Úv3Úv4r:   Úw1Úw2Úw3Úw4Úvalr2   s   ``  ``                      @r   Ú_bilinear_interpolaterU   #   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 a™ZÑ'¨¯©¨e¯k©kÓ):¸AÓ>€Aä�[Š[˜¨!¡)Ñ+¨U°Q©Y¸À¹	ÓB€FÜ�KŠK˜¨¡Ñ*¨E°A©I°uÓ=€EÜ�Š�E Q™YÑ&¨¯©¨U¯[©[Ó(9¸1Ó=€Aà	
‰€BØ	
‰€BØ	ˆr‰€BØ	ˆr‰€B÷

ñ 
ñ 
�eÓ	#€BÙ	�eÓ	$€BÙ	�fÓ	$€BÙ	�fÓ	%€BòKñ 
�B˜Ó	€BÙ	�BÓ	€BÙ	�B˜Ó	€BÙ	�BÓ	€Bà
ˆr‰'�B˜‘GÑ
˜b 2™gÑ
%¨¨R©Ñ
/€CØ€Jr"   c                 ó¸   • [         R                  " 5       (       a?  U R                  (       a.  U R                  [         R                  :w  a  U R                  5       $ U $ r   )r   Úis_autocast_enabledÚis_cudar@   ÚdoubleÚfloat)Útensors    r   Ú
maybe_castr\   e   s7   € Ü× Ò ×"Ñ" v§~§~¸&¿,¹,Ì%Ï,É,Ó:VØ�|‰|‹~Ðàˆr"   T)Údynamicc           
      ó  • U R                   n[        U 5      n [        U5      nU R                  5       u    p‰n
[        R                  " X0R
                  S9n[        R                  " X@R
                  S9nUS S 2S4   R                  5       nU(       a  SOSnUS S 2S4   U-  U-
  nUS S 2S4   U-  U-
  nUS S 2S4   U-  U-
  nUS S 2S4   U-  U-
  nUU-
  nUU-
  nU(       d*  [        R                  " US	S
9n[        R                  " US	S
9nUU-  nUU-  nUS:„  nU(       a  UO[        R                  " UU-  5      nU(       a  UO[        R                  " UU-  5      n U(       aR  [        UU-  S5      n[        R                  " UU R
                  S9n[        R                  " UU R
                  S9nS nS nOz[        R                  " UU-  SS
9n[        R                  " X�R
                  S9n[        R                  " X R
                  S9nUS S S 24   US S 2S 4   :  nUS S S 24   US S 2S 4   :  nS nU" U5      US S S 2S 4   U" U5      -  -   US S S S 24   S-   R                  U R                   5      U" UU-  5      -  -   n U" U5      US S S 2S 4   U" U5      -  -   US S S S 24   S-   R                  U R                   5      U" UU-  5      -  -   n![        XU U!UU5      n"U(       dJ  [        R                  " US S 2S S S S S 2S 4   U"S5      n"[        R                  " US S 2S S S S S S 24   U"S5      n"U"R                  S5      n#[        U[        R                  5      (       a  U#US S 2S S S 4   -  n#OU#U-  n#U#R                  U5      n#U#$ )Nr,   r   g      à?g        r   r   é   é   r*   r(   c                 ó   • U S S 2S S 4   $ r   r%   )Úts    r   Úfrom_KÚ_roi_align.<locals>.from_K¬   s   € Ø’�D˜$�ÑÐr"   )éÿÿÿÿéþÿÿÿ)r@   r\   r<   r   r/   r-   r>   r=   ÚceilÚmaxr?   rU   r.   ÚsumÚ
isinstancer   )$r3   ÚroisÚspatial_scaleÚpooled_heightÚpooled_widthÚsampling_ratioÚalignedÚ
orig_dtyperA   rB   rC   ÚphÚpwr4   Ú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Úixr6   r5   rc   r0   r1   rT   Úoutputs$                                       r   Ú
_roi_alignr„   r   s’  € à—‘€Jä�uÓ€EÜ�dÓ€DàŸ*™*›,Ñ€A€q�%ä	�Š�m¯L©LÑ	9€BÜ	�Š�l¯<©<Ñ	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"   r3   ÚboxesÚoutput_sizerl   ro   rp   Úreturnc           	      ó  • [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        U5        Un[        U5      n[        U[         R                  5      (       d  [        U5      n[         R                  R                  5       (       d”  [        5       (       aM  [         R                  " 5       (       ak  U R                  (       d"  U R                  (       d  U R                  (       a8  [!        U R"                  R$                  5      (       a  ['        XX2S   US   XE5      $ [)        5         [         R*                  R,                  R                  XX2S   US   XE5      $ )aö  
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   rj   r   r   r
   Ú$are_deterministic_algorithms_enabledrX   Úis_mpsÚis_xpur   r-   Útyper„   r	   ÚopsÚtorchvision)r3   r…   r†   rl   ro   rp   rk   s          r   rŒ   rŒ   Ë   s	  € ôR �9‰9×!Ñ!×#Ñ#¬E¯I©I×,@Ñ,@×,BÑ,BÜœIÔ&Ü˜%Ô Ø€DÜ˜Ó$€KÜ�dœEŸL™L×)Ñ)Ü*¨4Ó0ˆÜ�9‰9×!Ñ!×#Ñ#ä—
‘
Ü×:Ò:×<Ñ<À%Ç-Ç-ÐSX×S_×S_Ðch×co×coÜ" 5§<¡<×#4Ñ#4×5Ñ5Ü˜e¨=Àa¹.È+ÐVWÉ.ÐZhÓrÐrÜÔÜ�9‰9× Ñ ×*Ñ*Ø�]°¡N°KÀ±NÀNóð r"   c            	       ó€   ^ • \ rS rSrSr SS\\   S\S\S\4U 4S jjjr	S\
S	\\
\\
   4   S
\
4S jrS
\4S jrSrU =r$ )ÚRoIAligni  z
See :func:`roi_align`.
r†   rl   ro   rp   c                 óh   >• [         TU ]  5         [        U 5        Xl        X l        X0l        X@l        g r   )ÚsuperÚ__init__r   r†   rl   ro   rp   )Úselfr†   rl   ro   rp   Ú	__class__s        €r   r—   ÚRoIAlign.__init__  s/   ø€ ô 	‰ÑÔÜ˜DÔ!Ø&ÔØ*ÔØ,ÔØ�r"   r3   rk   r‡   c                 óp   • [        XU R                  U R                  U R                  U R                  5      $ r   )rŒ   r†   rl   ro   rp   )r˜   r3   rk   s      r   ÚforwardÚRoIAlign.forward  s.   € Ü˜ d×&6Ñ&6¸×8JÑ8JÈD×L_ÑL_Ðae×amÑamÓnÐnr"   c           
      ó    • U R                   R                   SU R                   SU R                   SU R                   SU R
                   S3
nU$ )Nz(output_size=z, spatial_scale=z, sampling_ratio=z
, aligned=Ú))r™   r   r†   rl   ro   rp   )r˜   Úss     r   Ú__repr__ÚRoIAlign.__repr__  s^   € à�~‰~×&Ñ&Ð'ð (Ø×+Ñ+Ð,Ø˜t×1Ñ1Ð2Ø × 3Ñ 3Ð4Ø˜Ÿ™˜Øðð 	
ð ˆr"   )rp   r†   ro   rl   )F)r   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r>   rZ   Úboolr—   r   r   Úlistrœ   Ústrr¡   Ú__static_attributes__Ú__classcell__)r™   s   @r   r”   r”     s   ø† ñð ñà& sÑ+ðð ðð ð	ð
 ÷ð ðo˜Vð o¨5°¸¸f¹Ð1EÑ+Fð oÈ6ô oð	˜#÷ 	ò 	r"   r”   )r*   re   F)"r   Útypingr   r   Útorch.fxr   r   Útorch._dynamo.utilsr   Útorch.jit.annotationsr   Útorch.nn.modules.utilsr   Útorchvision.extensionr	   r
   Úutilsr   Ú_utilsr   r   r&   rU   r\   r„   ÚfxÚwrapr¨   r>   rZ   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"   