
    h2,                     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      W/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/ops/roi_align.pycompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook   sF    --=n=K%8Y__R%8%EGIbkk"///    )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!T1*-q!4AE!T1*-q!4A!T4tT9:LL%,,7atTSW8WXaq$4'(atQ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!T1d*+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    "'Ax 	
AA	AAEEGEEEGE[[&1*,fqj%!)DFKK!+VaZ?EEVaZ'ekk):A>A[[%!)+UQY	BFKK*EAIu=EEUQY&U[[(91=A	
UB	
UB	rB	rB

 
 
eU	#B	eV	$B	fe	$B	ff	%BK 
B	B	B	B	B	B	B	B
r'BG
b2g
%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 }nt        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**,Aq&%	mELL	9B	l5<<	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!   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 99!!#EII,@,@,BI&% D$KdELL)*4099!!#
::<%--SXS_S_chcoco"5<<#4#45eT=+a.+VW.Zhjqrr99  **t]KNKNNT[ 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8J8JDL_L_aeamam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ryy r!   