
    QЦi,                     X   S SK r S SKJr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\R4                  R6                     SS\S\\\\   4   S\\   S\S\S\S\4S jj5       r " S S\R@                  5      r!g)    N)List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      X/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchvision/ops/roi_align.pycompile_hook7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook   sD    --=n=K%.__R%8%EGIbkk"///    )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                  " XS-
  :  US-
  U	S-   5      n[        R                  " XS-
  :  US-
  U	5      n	[        R                  " XS-
  :  UR                  T R                  5      U5      n[        R                  " XS-
  :  US-
  U
S-   5      n[        R                  " XS-
  :  US-
  U
5      n
[        R                  " XS-
  :  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" X5      nU" X5      nU" X5      nU" X5      nS nU" UU5      nU" X5      nU" UU5      nU" X5      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!T1*-q!4AE!T1*-q!4A!T4tT9:LL%,,7atTSW8WXaq$4'(atQ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&   )r1   r2   s     r    
outer_prod)_bilinear_interpolate.<locals>.outer_prodW   s1    D!T1d*+a4q$0I.JJJr#   )sizeclampintr   r/   todtype)r4   r5   r1   r2   r7   r6   _heightwidthy_lowx_lowy_highx_highlylxhyhxr8   v1v2v3v4r;   w1w2w3w4valr3   s   ``  ``                      @r    _bilinear_interpolaterV   #   s    "'Ax 	
AA	AAEEGEEEGE[[1*,fqj%!)DFKK!+VaZ?EEaZ'ekk):A>A[[!)+UQY	BFKK*EAIu=EEQY&U[[(91=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
b2g
%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_cudarA   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    pn
[        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                  " XR
                  S9n[        R                  " XR
                  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#   ))rA   r]   r=   r   r0   r.   r?   r>   ceilmaxr@   rV   r/   sum
isinstancer   )$r4   roisspatial_scalepooled_heightpooled_widthsampling_ratioaligned
orig_dtyperB   rC   rD   phpwr5   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ixr7   r6   rd   r1   r2   rU   outputs$                                       r    
_roi_alignr   r   s   JuEdD**,Aq%	mLL	9B	l<<	8B
 AJNN$MSFq!t*},v5Kq!t*},v5KQT
]*V3IQT
]*V3IK'I[(JKK	s3	[[5
m+J\)J#a'N'5^5::jS`F`;aN'5^5::iR^F^;_N N^3Q7\\.>\\.>N^;C \\&6\\%547nQW5547nQW55  	{
T1d]
fZ0
0	1dD!ms"
&
&u{{
3fZ.=X6Y
Y	Z  	{
T1d]
fZ0
0	1dD!ms"
&
&u{{
3fZ.=X6Y
Y	Z 
  aE5
IC kk%4tQ <=sAFkk%4tT1 <=sAFWWXF%&&%4t+,,%YYz"FMr#   r4   boxesoutput_sizerm   rp   rq   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       (       a<  [         R                  " 5       (       aZ  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	   rk   r   r   r   $are_deterministic_algorithms_enabledrY   is_mpsr   r.   typer   r
   opstorchvision)r4   r   r   rm   rp   rq   rl   s          r    r   r      s   R 99!!##EII,@,@,B,BI&% D$KdELL))*4099!!##

uIIKKQVQ^Q^bgbnbn"5<<#4#455e=a.+VW.Zhrr99  **]NKNN 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   rm   rp   rq   c                 h   > [         TU ]  5         [        U 5        Xl        X l        X0l        X@l        g r   )super__init__r   r   rm   rp   rq   )selfr   rm   rp   rq   	__class__s        r    r   RoIAlign.__init__  s/     	D!&*,r#   r4   rl   r   c                 p    [        XU R                  U R                  U R                  U R                  5      $ r   )r   r   rm   rp   rq   )r   r4   rl   s      r    forwardRoIAlign.forward  s.    d&6&68J8JDL_L_aeamam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   rm   rp   rq   )r   ss     r    __repr__RoIAlign.__repr__  s^    ~~&&' (++,t112 3 34 	
 r#   )rq   r   rp   rm   )F)r   
__module____qualname____firstlineno____doc__r   r?   r[   boolr   r   r   r   r   strr   __static_attributes____classcell__)r   s   @r    r   r     s     &s+  	
  oV o5f1E+F o6 o	# 	 	r#   r   )r+   rf   F)"r   typingr   r   r   torch.fxr   r   torch._dynamo.utilsr   torch.jit.annotationsr   torch.nn.modules.utilsr	   torchvision.extensionr
   r   utilsr   _utilsr   r   r'   rV   r]   r   fxwrapr?   r[   r   r   Moduler   r&   r#   r    <module>r      s         4 3 ( ; ' F&=D dU Up 
 77f%&7 #3'7 	7
 7 7 7 7tryy r#   