
    ^jN                        d dl mZmZ d dlZd dlmZ d dlmc mZ d dl	m
Z
  G d dej                  j                        Z G d dej                        Z G d d	ej                        Z G d
 dej                        Zdej$                  dej$                  dedej$                  fdZ	 	 ddej$                  deej$                  gej$                  f   dedededej$                  fdZdej$                  dej$                  fdZy)    )AnyCallableN)_bilinear_grid_samplec                      e Zd ZdZedej                  j                  j                  dej                  dej                  dej                  dz  de
edf   d	e
edf   d
e
edf   dedej                  fd       Zedej                  j                  j                  dej                  de
ej                  dz  ej                  dz  ej                  dz  ddddf   fd       Zy)_DepthwiseConvWithoutCuDNNa  Depthwise conv2d with cuDNN disabled in both forward and backward.

    ``torch.backends.cudnn.flags(enabled=False)`` as a context manager only covers operations executed within its scope.
    ``nn.Conv2d`` records the forward op in the autograd graph; the corresponding backward kernels run later,
    **outside** that scope, with cuDNN re-enabled.  On some CUDA stacks (T4 / P100 on Kaggle / Colab) cuDNN fails engine
    selection for depthwise conv backward, raising::

        RuntimeError: GET was unable to find an engine to execute this computation

    This ``Function`` disables cuDNN in ``backward`` as well, fixing the crash.

    See: https://github.com/roboflow/rf-detr/issues/731
    ctxxweightbiasNstride.paddingdilationgroupsreturnc           
      (   | j                  ||       |du| _        || _        || _        || _        || _        t        j                  j                  j                  d      5  t        j                  |||||||      cddd       S # 1 sw Y   yxY w)a  Run depthwise conv2d forward with cuDNN disabled.

        Args:
            ctx: Autograd context.
            x: Input feature map ``(N, C, H, W)``.
            weight: Convolution weight tensor.
            bias: Optional convolution bias tensor.
            stride: Convolution stride.
            padding: Convolution padding.
            dilation: Convolution dilation.
            groups: Number of groups (equals ``C`` for depthwise).

        Returns:
            Output feature map ``(N, C, H, W)``.
        NFenabledr   r   r   r   )save_for_backwardhas_biasr   r   r   r   torchbackendscudnnflagsFconv2d)r   r	   r
   r   r   r   r   r   s           k/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/models/heads/segmentation.pyforwardz"_DepthwiseConvWithoutCuDNN.forward   s    4 	a(4'


 ^^!!'''6 	o88AvtFGV^gmn	o 	o 	os   "BBgrad_outputc           
         | j                   \  }}| j                  d   }| j                  d   }| j                  xr | j                  d   }d}d}d}	|s|r1|j                  |j                        }
t
        j                  j                  j                  d      5  |rbt
        j                  j                  j                  |j                  ||
| j                  | j                  | j                  | j                         }|r|t
        j                  j                  j#                  |j                  |j                        |j                  |
| j                  | j                  | j                  | j                         }ddd       |r,|j                  |j                        j%                  d	
      }	|||	ddddfS # 1 sw Y   @xY w)u  Compute gradients with cuDNN disabled.

        Args:
            ctx: Autograd context with saved tensors and conv parameters.
            grad_output: Upstream gradient ``(N, C, H, W)``.

        Returns:
            Gradients for each ``forward`` input.  Inputs that do not require gradients (``ctx.needs_input_grad[i]`` is
            ``False``) get ``None``. Non-tensor inputs always get ``None``.

        Note:
            Under AMP (``"bf16-mixed"`` or ``"16-mixed"``), ``grad_output`` may arrive in a reduced dtype while the
            saved ``weight`` stays ``fp32``.  Both tensors are upcast to ``weight.dtype`` before calling
            ``conv2d_input`` / ``conv2d_weight``.  ``grad_input`` is kept in ``weight.dtype`` (fp32) so that upstream
            gradient accumulation into fp32 leaf parameters stays in fp32 — matching standard ``F.conv2d`` backward
            behaviour.  Casting back to the activation dtype (``x.dtype``) would propagate a reduced-precision gradient
            to fp32 backbone parameters, causing a ``params, grads, exp_avgs, and exp_avg_sqs must have same dtype``
            crash in fused AdamW (see issue #959).
        r         N)dtypeFr   r   )r   r"      dim)saved_tensorsneeds_input_gradr   tor#   r   r   r   r   nngradconv2d_inputshaper   r   r   r   conv2d_weightsum)r   r   r	   r
   needs_x_gradneeds_w_gradneeds_b_grad
grad_inputgrad_weight	grad_biasgrad_output_casts              r   backwardz#_DepthwiseConvWithoutCuDNN.backwardF   s   0 %%	6++A.++A.||?(<(<Q(?
	<
  +~~FLL~A%%++E+: !&!;!;("zz #!$"zz "< "J  "'((--"="=6<<0("zz #!$"zz #> #K, #V\\:>>9>MI;	4tTII3 s   C#F>>G)__name__
__module____qualname____doc__staticmethodr   autogradfunctionFunctionCtxTensortupleintr   r7        r   r   r      s8    $o^^$$00$o<<$o $o llT!	$o
 c3h$o sCx$o S/$o $o 
$o $oL AJ^^$$00AJ\\AJ 
u||d"ELL4$79LdTXZ^`dd	eAJ AJrD   r   c                   b     e Zd ZdZd fd	Zdej                  dej                  fdZd Z xZ	S )DepthwiseConvBlockz1Simplified ConvNeXt block without the MLP subnet.c                 ~   t         |           t        j                  ||dd|      | _        t        j
                  |d      | _        t        j                  ||      | _        t        j                         | _
        |dkD  r3t        j                  |t        j                  |      z  d      | _        y d | _        y )	Nr$   r!   )kernel_sizer   r   gư>)epsr   Trequires_grad)super__init__r*   Conv2ddwconv	LayerNormnormLinearpwconv1GELUact	Parameterr   onesgammaselfr&   layer_scale_init_value	__class__s      r   rM   zDepthwiseConvBlock.__init__   s    iiSa3OLL$/	yyc*779 &) LL/%**c2CCSWX 	
  	
rD   r	   r   c           	      (   t         j                  || j                  j                  | j                  j                  | j                  j
                  | j                  j                  | j                  j                  | j                  j                        S N)	r   applyrO   r
   r   r   r   r   r   )rZ   r	   s     r   _depthwise_convz"DepthwiseConvBlock._depthwise_conv   sh    
 *//KKKKKKKKKK  KK
 	
rD   c                    |}| j                  |      }|j                  dddd      }| j                  |      }| j                  |      }| j	                  |      }| j
                  | j
                  |z  }|j                  dddd      }||z   S )Nr   r"   r$   r!   )r`   permuterQ   rS   rU   rX   )rZ   r	   inputs      r   r   zDepthwiseConvBlock.forward   s      #IIaAq!IIaLLLOHHQK::!

QAIIaAq!5yrD   r   )
r8   r9   r:   r;   rM   r   r@   r`   r   __classcell__r\   s   @r   rF   rF      s+    <


 
%,, 
rD   rF   c                   &     e Zd Zd fd	Zd Z xZS )MLPBlockc                    t         |           t        j                  |      | _        t        j
                  t        j                  ||dz        t        j                         t        j                  |dz  |      g      | _        |dkD  r3t        j                  |t        j                  |      z  d      | _        y d | _        y )N   r   TrJ   )rL   rM   r*   rP   norm_in
ModuleListrR   rT   layersrV   r   rW   rX   rY   s      r   rM   zMLPBlock.__init__   s    ||C(mm		#sQw'			#'3'
 &) LL/%**c2CCSWX 	
  	
rD   c                     |}| j                  |      }| j                  D ]
  } ||      } | j                  | j                  |z  }||z   S r^   )rk   rm   rX   )rZ   r	   rc   layers       r   r   zMLPBlock.forward   sQ    LLO[[ 	EaA	::!

QA5yrD   rd   )r8   r9   r:   rM   r   re   rf   s   @r   rh   rh      s    
 rD   rh   c                       e Zd Zddededef fdZd Z	 ddej                  deej                     de	eef   d	e
d
eej                     f
dZ	 ddej                  deej                     de	eef   d	e
d
eej                     f
dZ	 ddej                  deej                     de	eef   d	e
d
eej                     f
dZ xZS )SegmentationHead
num_blocksbottleneck_ratiodownsample_ratioc                 F   t         |           || _        |||z  n|| _        t	        j
                  t        |      D cg c]  }t        |       c}      | _        |t	        j                         n!t	        j                  || j                  d      | _        t        |      | _        |t	        j                         nt	        j                  || j                        | _        t	        j                   t#        j$                  d      d      | _        d| _        y c c}w )Nr!   )rH   TrJ   F)rL   rM   rt   interaction_dimr*   rl   rangerF   blocksIdentityrN   spatial_features_projrh   query_features_blockrR   query_features_projrV   r   zerosr   _export)rZ   in_dimrr   rs   rt   _r\   s         r   rM   zSegmentationHead.__init__   s     0=M=Yv)99_emmzIZ$[A%7%?$[\-5BKKM299VTMaMaop;q 	" %-V$4!-5BKKM299VTMaMa;b 	  LLQtD	 %\s   Dc                 *   d| _         | j                  | _        | j                  | _        | j	                         D ]W  \  }}t        |d      st        |j                  t              s.t        |d      s;|j                   rH|j                          Y y )NTexportr~   )	r~   r   _forward_originforward_exportnamed_moduleshasattr
isinstancer   r   )rZ   namems      r   r   zSegmentationHead.export   sr    #||**))+ 	GD!q(#
188X(F7STV_K`ijirir
	rD   spatial_featuresquery_features
image_sizeskip_blocksr   c                 v   |d   | j                   z  |d   | j                   z  f}t        j                  ||dd      }g }|st        | j                  |      D ]q  \  }} ||      }| j                  |      }	| j                  | j                  |            }|j                  t        j                  d|	|      | j                  z          s |S t        |      dk(  sJ d       | j                  | j                  |d               }|j                  t        j                  d||      | j                  z          |S )Nr   r!   bilinearFsizemodealign_cornersbchw,bnc->bnhw9skip_blocks is only supported for length 1 query features)rt   r   interpolateziprx   rz   r|   r{   appendr   einsumr   len)
rZ   r   r   r   r   target_sizemask_logitsblockqfrz   s
             r   r   zSegmentationHead.forward   sD    "!}(=(==z!}PTPePe?ef==)9R\lqr n= j	r#()9#: (,(B(BCS(T%--d.G.G.KL""5<<0@BWY[#\_c_h_h#hi	j 	 ~&!+h-hh+))$*C*CNSTDU*VWBu||,<>NPRSVZV_V__`rD   c                 &   |d   | j                   z  |d   | j                   z  f}t        j                  ||dd      }g }|sxt        | j                  |      D ]]  \  }} ||      }| j                  |      }	| j                  | j                  |            }|j                  |	|| j                  d       _ |S t        |      dk(  sJ d       | j                  | j                  |d               }|j                  ||| j                  d       |S )Nr   r!   r   Fr   )r   r   r   r   )rt   r   r   r   rx   rz   r|   r{   r   r   r   )
rZ   r   r   r   r   r   output_dictsr   r   rz   s
             r   sparse_forwardzSegmentationHead.sparse_forward	  s2    "!}(=(==z!}PTPePe?ef==)9R\lqr  n= 	r#()9#: (,(B(BCS(T%--d.G.G.KL##,A*, $		2  ~&!+h-hh+))$*C*CNSTDU*VWB(8&( II rD   c                    t        |      dk(  sJ d       |d   | j                  z  |d   | j                  z  f}t        j                  ||dd      }|s| j                  D ]
  } ||      } | j                  |      }| j                  | j                  |d               }t        j                  d||      | j                  z   gS )Nr!   zCat export time, segmentation head expects exactly one query featurer   r   Fr   r   )r   rt   r   r   rx   rz   r|   r{   r   r   r   )	rZ   r   r   r   r   r   r   rz   r   s	            r   r   zSegmentationHead.forward_export6  s     >"a'n)nn'!!}(=(==z!}PTPePe?ef==)9R\lqr ;#()9#: ; !% : :;K L%%d&?&?q@Q&RS-/DbIDIIUVVrD   )r!   rj   )F)r8   r9   r:   rB   rM   r   r   r@   listrA   boolr   r   r   re   rf   s   @r   rq   rq      s3   3 # ]` & ",, U\\* #s(O	
  
ell	@ "+,,+ U\\*+ #s(O	+
 + 
ell	+d "W,,W U\\*W #s(O	W
 W 
ell	WrD   rq   rc   point_coordskwargsr   c                    d}|j                         dk(  rd}|j                  d      }d|z  dz
  }|j                  dd      }|j                  d	d      }|j                  d
d      }|dk(  rg|r6dj                  t	        |j                                     }t        d|       |dvrt        j                  | ||||      }	n+t        | |||      }	nt        j                  | |f|||d|}	|r|	j                  d      }	|	S )a  A wrapper around :func:`~rfdetr.utilities.tensors._bilinear_grid_sample` to support 3D point_coords tensors.
    Unlike :func:`torch.nn.functional.grid_sample` it assumes `point_coords` to lie inside [0, 1] x [0, 1] square.

    Args:
        input: A tensor of shape (N, C, H, W) that contains features map on a H x W grid.
        point_coords: A tensor of shape (N, P, 2) or (N, Hgrid, Wgrid, 2) that contains
            [0, 1] x [0, 1] normalized point coordinates.

    Returns:
        A tensor of shape (N, C, P) or (N, C, Hgrid, Wgrid) that contains
            features for points in `point_coords`. The features are obtained via bilinear
            interpolation from `input` the same way as :func:`~rfdetr.utilities.tensors._bilinear_grid_sample`.
    Fr$   Tr"   g       @g      ?r   r   r   padding_modeborderz, z2Unexpected keyword argument(s) for bilinear mode: )r}   r   )r   r   r   )r   r   )r&   	unsqueezepopjoinsortedkeys	TypeErrorr   grid_sampler   squeeze)
rc   r   r   add_dimgridr   r   r   
unexpectedoutputs
             r   point_sampler   L  s3    GQ#--a0 #D ::fj)DJJ6M::nh7Lz6&++-#89JPQ[P\]^^ 22]])+F +)+	F 
 %'
 
 "MrD   coarse_logitsuncertainty_func
num_pointsoversample_ratioimportance_sample_ratioc           	         |dk\  sJ |dk  r|dk\  sJ | j                   d   }t        ||z        }t        j                  ||d| j                        }t        | |d      } ||      }	t        ||z        }
||
z
  }t        j                  |	dddddf   |
d      d   }|t        j                  |t        j                  | j                  	      z  }||dddf   z  }|j                  d
d      |j                  d
      ddf   j                  ||
d      }|dkD  r:t        j                  |t        j                  ||d| j                        gd      }|S )a  Sample points in [0, 1] x [0, 1] coordinate space based on their uncertainty.

    The uncertainties are calculated for each point using 'uncertainty_func' function that takes point's logit
    prediction as input. See PointRend paper for details.

    Args:
        coarse_logits: A tensor of shape (N, C, Hmask, Wmask) or (N, 1, Hmask, Wmask) for
            class-specific or class-agnostic prediction.
        uncertainty_func: A function that takes a Tensor of shape (N, C, P) or (N, 1, P) that
            contains logit predictions for P points and returns their uncertainties as a Tensor of shape (N, 1, P).
        num_points: The number of points P to sample.
        oversample_ratio: Oversampling parameter.
        importance_sample_ratio: Ratio of points that are sampled via importance sampling.

    Returns:
        A tensor of shape (N, P, 2) that contains the coordinates of sampled points.
    r!   r   r"   )deviceF)r   N)kr&   )r#   r   r%   )r-   rB   r   randr   r   topkarangelongviewcat)r   r   r   r   r   	num_boxesnum_sampledr   point_logitspoint_uncertaintiesnum_uncertain_pointsnum_random_pointsidxshifts                 r   *get_uncertain_point_coords_with_randomnessr     sj   0 q   "a',Cq,HHH##A&Ij#334K::ia@T@TUL|5QL +<86CD"%99
**(Aq15Iq
QRS
TC%,,y

=K_K_``E5D>C$$R+CHHRL!O<AA)MacdeL1yy

9&7=CWCWX 
 rD   logitsc                 v    | j                   d   dk(  sJ | j                         }t        j                  |       S )a"  We estimate uncertainty as L1 distance between 0.0 and the logit prediction in 'logits' for the foreground class
    in `classes`.

    Args:
        logits: A tensor of shape (R, 1, ...) for class-specific or
            class-agnostic, where R is the total number of predicted masks in all images and C is the number of
            foreground classes. The values are logits.

    Returns:
        A tensor of shape (R, 1, ...) that contains uncertainty scores with the most uncertain locations having the
        highest uncertainty score.
    r!   )r-   cloner   abs)r   gt_class_logitss     r   calculate_uncertaintyr     s7     <<?allnOYY'((rD   )r$   g      ?)typingr   r   r   torch.nnr*   torch.nn.functional
functionalr   rfdetr.utilities.tensorsr   r=   Functionr   ModulerF   rh   rq   r@   r   rB   floatr   r   rC   rD   r   <module>r      s   !     :xJ!8!8 xJv) )Xryy 6wWryy wWt@ @ELL @C @TYT`T` @N %)4<<4~u||;<4 4 	4
 #4 \\4n)%,, )5<< )rD   