
    ^j              	          d dl mZ d dlZd dlmZ d dlmc mZ d dlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6m7Z7  ejp                  e9      Z: ed      e	 G d de                    Z; G d de%      Z< G d de.      Z= ed      e G d de                    Z> G d de7      Z? G d d e6      Z@ G d! d"e(      ZA G d# d$e,      ZB G d% d&e/      ZC G d' d(e"      ZD G d) d*e1      ZE G d+ d,ej                        ZG G d- d.ej                        ZH G d/ d0e2      ZI G d1 d2e&      ZJ G d3 d4e!      ZK G d5 d6ej                        ZLdSd7ej                  d8ej                  d9eNd:ej                  fd;ZO G d< d=e*      ZP G d> d?e+      ZQ G d@ dAe$      ZR G dB dCe0      ZS G dD dEeS      ZT G dF dGeS      ZU G dH dIeS      ZV G dJ dKe)      ZW G dL dMe#      ZX G dN dOe-      ZY G dP dQe'      ZZg dRZ[y)T    )	dataclassN)strict   )initialization)load_backbone)ModelOutput)Unpack)TransformersKwargsauto_docstringlogging)merge_with_config_defaults)OutputRecordercapture_outputs   )
AutoConfig)DFineConfig)DFineAIFILayerDFineConvNormLayerDFineDecoderDFineDecoderLayerDFineDecoderOutputDFineEncoderLayerDFineForObjectDetection	DFineGateDFineHybridEncoderDFineIntegralDFineLQEDFineMLP
DFineModelDFineModelOutput"DFineMultiscaleDeformableAttentionDFinePreTrainedModelDFineRepVggBlockDFineSCDown(get_contrastive_denoising_training_groupreplace_batch_norm)LlamaMLPLlamaRMSNormz"Intellindust/DEIMv2_HGNetv2_N_COCO)
checkpointc                       e Zd ZU dZdZdeiZdZee	   e
e	e	f   z  dz  ed<   dZeed<   dZeed	<   dZedz  ed
<   dZeed<   dZe	ed<   dZeed<   dZeed<   dZeed<   dZeed<   y)Deimv2Configa#  
    initializer_bias_prior_prob (`float`, *optional*):
        The prior probability used by the bias initializer to initialize biases for `enc_score_head` and `class_embed`.
        If `None`, `prior_prob` computed as `prior_prob = 1 / (num_labels + 1)` while initializing model weights.
    freeze_backbone_batch_norms (`bool`, *optional*, defaults to `True`):
        Whether to freeze the batch normalization layers in the backbone.
    encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`):
        Multi level features input for encoder.
    feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`):
        Strides used in each feature map.
    encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`):
        Indexes of the projected layers to be used in the encoder.
    positional_encoding_temperature (`int`, *optional*, defaults to 10000):
        The temperature parameter used to create the positional encodings.
    encoder_activation_function (`str`, *optional*, defaults to `"gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler.
    eval_size (`list[int]` or `tuple[int, int]`, *optional*):
        Height and width used to computes the effective height and width of the position embeddings after taking
        into account the stride.
    normalize_before (`bool`, *optional*, defaults to `False`):
        Determine whether to apply layer normalization in the transformer encoder layer before self-attention and
        feed-forward modules.
    hidden_expansion (`float`, *optional*, defaults to 1.0):
        Expansion ratio to enlarge the dimension size of RepVGGBlock and CSPRepLayer.
    num_queries (`int`, *optional*, defaults to 300):
        Number of object queries.
    decoder_in_channels (`list`, *optional*, defaults to `[256, 256, 256]`):
        Multi level features dimension for decoder.
    num_feature_levels (`int`, *optional*, defaults to 3):
        The number of input feature levels.
    decoder_n_points (`int`, *optional*, defaults to 4):
        The number of sampled keys in each feature level for each attention head in the decoder.
    decoder_activation_function (`str`, *optional*, defaults to `"relu"`):
        The non-linear activation function (function or string) in the decoder.
    num_denoising (`int`, *optional*, defaults to 100):
        The total number of denoising tasks or queries to be used for contrastive denoising.
    label_noise_ratio (`float`, *optional*, defaults to 0.5):
        The fraction of denoising labels to which random noise should be added.
    box_noise_scale (`float`, *optional*, defaults to 1.0):
        Scale or magnitude of noise to be added to the bounding boxes.
    learn_initial_query (`bool`, *optional*, defaults to `False`):
        Indicates whether the initial query embeddings for the decoder should be learned during training.
    anchor_image_size (`tuple[int, int]`, *optional*):
        Height and width of the input image used during evaluation to generate the bounding box anchors.
    with_box_refine (`bool`, *optional*, defaults to `True`):
        Whether to apply iterative bounding box refinement.
    matcher_alpha (`float`, *optional*, defaults to 0.25):
        Parameter alpha used by the Hungarian Matcher.
    matcher_gamma (`float`, *optional*, defaults to 2.0):
        Parameter gamma used by the Hungarian Matcher.
    matcher_class_cost (`float`, *optional*, defaults to 2.0):
        The relative weight of the class loss used by the Hungarian Matcher.
    matcher_bbox_cost (`float`, *optional*, defaults to 5.0):
        The relative weight of the bounding box loss used by the Hungarian Matcher.
    matcher_giou_cost (`float`, *optional*, defaults to 2.0):
        The relative weight of the giou loss of used by the Hungarian Matcher.
    use_focal_loss (`bool`, *optional*, defaults to `True`):
        Parameter informing if focal loss should be used.
    focal_loss_alpha (`float`, *optional*, defaults to 0.75):
        Parameter alpha used to compute the focal loss.
    focal_loss_gamma (`float`, *optional*, defaults to 2.0):
        Parameter gamma used to compute the focal loss.
    weight_loss_vfl (`float`, *optional*, defaults to 1.0):
        Relative weight of the varifocal loss in the object detection loss.
    weight_loss_bbox (`float`, *optional*, defaults to 5.0):
        Relative weight of the L1 bounding box loss in the object detection loss.
    weight_loss_giou (`float`, *optional*, defaults to 2.0):
        Relative weight of the generalized IoU loss in the object detection loss.
    weight_loss_fgl (`float`, *optional*, defaults to 0.15):
        Relative weight of the fine-grained localization loss in the object detection loss.
    weight_loss_ddf (`float`, *optional*, defaults to 1.5):
        Relative weight of the decoupled distillation focal loss in the object detection loss.
    eval_idx (`int`, *optional*, defaults to -1):
        Index of the decoder layer to use for evaluation.
    layer_scale (`float`, *optional*, defaults to `1.0`):
        Scaling factor for the hidden dimension in later decoder layers.
    max_num_bins (`int`, *optional*, defaults to 32):
        Maximum number of bins for the distribution-guided bounding box refinement.
    reg_scale (`float`, *optional*, defaults to 4.0):
        Scale factor for the regression distribution.
    depth_mult (`float`, *optional*, defaults to 1.0):
        Multiplier for the number of blocks in RepNCSPELAN5 layers.
    top_prob_values (`int`, *optional*, defaults to 4):
        Number of top probability values to consider from each corner's distribution.
    lqe_hidden_dim (`int`, *optional*, defaults to 64):
        Hidden dimension size for the Location Quality Estimator (LQE) network.
    lqe_layers (`int`, *optional*, defaults to 2):
        Number of layers in the Location Quality Estimator MLP.
    decoder_offset_scale (`float`, *optional*, defaults to 0.5):
        Offset scale used in deformable attention.
    decoder_method (`str`, *optional*, defaults to `"default"`):
        The method to use for the decoder: `"default"` or `"discrete"`.
    up (`float`, *optional*, defaults to 0.5):
        Controls the upper bounds of the Weighting Function.
    weight_loss_mal (`float`, *optional*, defaults to 1.0):
        Relative weight of the matching auxiliary loss in the object detection loss.
    use_dense_one_to_one (`bool`, *optional*, defaults to `True`):
        Whether to use dense one-to-one matching across decoder layers.
    mal_alpha (`float`, *optional*):
        Alpha parameter for the Matching Auxiliary Loss (MAL). If `None`, uses `focal_loss_alpha`.
    encoder_fuse_op (`str`, *optional*, defaults to `"sum"`):
        Fusion operation used in the encoder FPN. DEIMv2 uses `"sum"` instead of D-FINE's `"cat"`.
    spatial_tuning_adapter_inplanes (`int`, *optional*, defaults to 16):
        Number of input planes for the STA convolutional stem.
    encoder_type (`str`, *optional*, defaults to `"hybrid"`):
        Type of encoder to use. `"hybrid"` uses the full HybridEncoder with AIFI, FPN, and PAN.
        `"lite"` uses the lightweight LiteEncoder with GAP fusion for smaller variants (Atto, Femto, Pico).
    use_gateway (`bool`, *optional*, defaults to `True`):
        Whether to use the gateway mechanism (cross-attention gating) in decoder layers. When `False`,
        uses RMSNorm on the encoder attention output instead.
    share_bbox_head (`bool`, *optional*, defaults to `False`):
        Whether to share the bounding box prediction head across all decoder layers.
    encoder_has_trailing_conv (`bool`, *optional*, defaults to `True`):
        Whether the encoder's CSP blocks include a trailing 3x3 convolution after the bottleneck path.
        `True` for RepNCSPELAN4 (used by HGNetV2 N and LiteEncoder variants).
        `False` for RepNCSPELAN5 (used by DINOv3 variants).
    deimv2backbone_configN	eval_size      ?weight_loss_malTuse_dense_one_to_one	mal_alphasumencoder_fuse_op   spatial_tuning_adapter_inplaneshybridencoder_typeuse_gatewayFshare_bbox_headencoder_has_trailing_conv)__name__
__module____qualname____doc__
model_typer   sub_configsr.   listinttuple__annotations__r0   floatr1   boolr2   r4   strr6   r8   r9   r:   r;        t/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/deimv2/modular_deimv2.pyr+   r+   ;   s    tl J$j1K48ItCy5c?*T18 OU !%$%"Iut|" OS +-#S- L# K!OT!&*t*rJ   r+   c                       e Zd Zy)Deimv2DecoderOutputNr<   r=   r>   rI   rJ   rK   rM   rM          rJ   rM   c                       e Zd Zy)Deimv2ModelOutputNrN   rI   rJ   rK   rQ   rQ      rO   rJ   rQ   z
    Output type for DEIMv2 encoder modules (HybridEncoder and LiteEncoder).
    Attentions are only available for HybridEncoder variants with AIFI layers.
    )custom_introc                       e Zd ZU dZdZeej                     ed<   dZ	e
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   y)Deimv2EncoderOutputz
    feature_maps (`list[torch.FloatTensor]`):
        List of multi-scale feature maps from the encoder, one per feature level.
    Nfeature_maps.hidden_states
attentions)r<   r=   r>   r?   rU   rB   torchFloatTensorrE   rV   rD   rW   rI   rJ   rK   rT   rT      s\    
 -1L$u(()0:>M5**C/047>7;Je'',-4;rJ   rT   c                       e Zd Zy)Deimv2RMSNormNrN   rI   rJ   rK   r[   r[      rO   rJ   r[   c                       e Zd ZdefdZy)Deimv2SwiGLUFFNconfigc                 |   t         j                  j                  |        |j                  dz  }t        j                  |j
                  |d      | _        t        j                  |j
                  |d      | _        t        j                  ||j
                  d      | _        t        j                         | _
        y )Nr   T)bias)nnModule__init__decoder_ffn_dimLineard_model	gate_projup_proj	down_projSiLUact_fn)selfr^   hidden_featuress      rK   rc   zDeimv2SwiGLUFFN.__init__   sy    
		4  00A56>>?NyytL?FNNNggirJ   N)r<   r=   r>   r+   rc   rI   rJ   rK   r]   r]      s     |  rJ   r]   c                   $     e Zd Zdef fdZ xZS )
Deimv2Gaterf   c                 D    t         |   |       t        |      | _        y N)superrc   r[   norm)rl   rf   	__class__s     rK   rc   zDeimv2Gate.__init__   s    !!'*	rJ   )r<   r=   r>   rC   rc   __classcell__rt   s   @rK   ro   ro      s    + + +rJ   ro   c                       e Zd Zy)	Deimv2MLPNrN   rI   rJ   rK   rx   rx      rO   rJ   rx   c                       e Zd Zy)#Deimv2MultiscaleDeformableAttentionNrN   rI   rJ   rK   rz   rz      rO   rJ   rz   c                       e Zd Zy)Deimv2ConvNormLayerNrN   rI   rJ   rK   r|   r|      rO   rJ   r|   c                       e Zd Zy)Deimv2RepVggBlockNrN   rI   rJ   rK   r~   r~      rO   rJ   r~   c                   t     e Zd ZdZ	 ddededededef
 fdZdej                  d	ej                  fd
Z
 xZS )Deimv2CSPRepLayera  
    Cross Stage Partial (CSP) network layer with RepVGG blocks.
    Differs from DFineCSPRepLayer: uses a single conv that splits into residual + processing path
    (instead of two separate convs), and has an optional trailing conv controlled by `encoder_has_trailing_conv`.
    r^   in_channelsout_channels
num_blocks	expansionc           
         t         	|           |j                  }t        ||z        }t	        |||dz  dd|      | _        t        j                  t        |      D cg c]  }t        |||       c}      | _
        |j                  rt	        |||dd|      | _        y t        j                         | _        y c c}w )Nr      
activationr   )rr   rc   activation_functionrC   r|   conv1ra   
ModuleListranger~   bottlenecksr;   Identityconv2)
rl   r^   r   r   r   r   r   hidden_channels_rt   s
            rK   rc   zDeimv2CSPRepLayer.__init__  s     	//
lY67(oPQ>QSTVWdno
==RWXbRcdQvHd

 //  q!Xbc 	
  	
 es   B<rV   returnc                     | j                  |      j                  dd      \  }}| j                  D ]
  } ||      } | j                  ||z         S Nr   r   dim)r   chunkr   r   )rl   rV   residual
bottlenecks       rK   forwardzDeimv2CSPRepLayer.forward  sX    "&**]";"A"A!"A"K-** 	6J&}5M	6zz(]233rJ   )r/   )r<   r=   r>   r?   r+   rC   rF   rc   rX   Tensorr   ru   rv   s   @rK   r   r     sW     nq
"
14
DG
UX
ej
 4U\\ 4ell 4rJ   r   c                   f     e Zd ZdZddedef fdZdej                  dej                  fdZ	 xZ
S )	Deimv2RepNCSPELAN5a]  
    Rep(VGG) N(etwork) CSP (Cross Stage Partial) ELAN (Efficient Layer Aggregation Network) block.
    Similar to DFineRepNCSPELAN4 but without intermediate convolutions between CSP branches,
    resulting in a simpler 4-way concatenation (2 split halves + 2 CSP branches) instead of D-FINE's
    4-branch design with interleaved convolutions.
    r^   numb_blocksc                    t         |           |j                  }|j                  }|j                  }|j                  dz  }t	        |j
                  |j                  z  dz        }t        |||dd|      | _        t        ||dz  ||      | _	        t        ||||      | _
        t        ||d|z  z   |dd|      | _        y )Nr   r   r   )r   )rr   rc   r   encoder_hidden_dimroundhidden_expansionr|   r   r   csp_rep1csp_rep2r   )	rl   r^   r   r   r   r   split_channelscsp_channelsrt   s	           rK   rc   zDeimv2RepNCSPELAN5.__init__'  s    //
//0022Q6V44v7P7PPTUUV(naQR_ij
)&.A2E|`kl)&,Yde(Na,&67q!Xb

rJ   rV   r   c                     | j                  |      j                  dd      \  }}| j                  |      }| j                  |      }t	        j
                  ||||gd      }| j                  |      S r   )r   r   r   r   rX   catr   )rl   rV   hidden_states_1hidden_states_2hidden_states_3hidden_states_4merged_hidden_statess          rK   r   zDeimv2RepNCSPELAN5.forward5  sn    +/::m+D+J+J1RS+J+T(--8--8$yy/?O]l)mstuzz.//rJ   )r   )r<   r=   r>   r?   r+   rC   rc   rX   r   r   ru   rv   s   @rK   r   r     s6    
| 
# 
0U\\ 0ell 0rJ   r   c                       e Zd Zy)Deimv2SCDownNrN   rI   rJ   rK   r   r   =  rO   rJ   r   c                       e Zd Zy)Deimv2EncoderLayerNrN   rI   rJ   rK   r   r   A  rO   rJ   r   c                       e Zd Zy)Deimv2AIFILayerNrN   rI   rJ   rK   r   r   E  rO   rJ   r   c                        e Zd Zdef fdZdej                  deej                  ej                  ej                  f   fdZ xZ	S )Deimv2SpatialTuningAdapterr^   c                 h   t         |           |j                  }t        |d|ddd      | _        t        j                  ddd      | _        t        ||d|z  dd      | _        t        |d|z  d|z  dd      | _	        t        |d|z  d|z  dd      | _
        t        j                         | _        y )Nr   r   gelur   r   kernel_sizestridepadding   )rr   rc   r6   r|   	stem_convra   	MaxPool2d	stem_poolr   conv3conv4GELUrk   )rl   r^   inplanesrt   s      rK   rc   z#Deimv2SpatialTuningAdapter.__init__J  s    99,VQ!QSYZ!AqI(1x<AN
(Xq8|QPQR
(Xq8|QPQR
ggirJ   pixel_valuesr   c                     | j                  | j                  |            }| j                  |      }| j                  | j	                  |            }| j                  | j	                  |            }|||fS rq   )r   r   r   r   rk   r   )rl   r   r   r   r   r   s         rK   r   z"Deimv2SpatialTuningAdapter.forwardT  sb    ..)EF**_5**T[[%AB**T[[%AB@@rJ   )
r<   r=   r>   r+   rc   rX   r   rD   r   ru   rv   s   @rK   r   r   I  sD     |  AELL AU5<<W\WcWc;c5d ArJ   r   feature_map_1feature_map_2fuse_opr   c                 H    |dk(  r| |z   S t        j                  | |gd      S )zJFuses two feature maps via element-wise sum or channel-wise concatenation.r3   r   r   )rX   r   )r   r   r   s      rK   fuse_feature_mapsr   \  s+    %},,99m]3;;rJ   c                       e Zd Zy)Deimv2IntegralNrN   rI   rJ   rK   r   r   c  rO   rJ   r   c                       e Zd Zy)	Deimv2LQENrN   rI   rJ   rK   r   r   g  rO   rJ   r   c                   $    e Zd Zdef fdZ	 	 	 	 	 	 ddej                  dej                  dz  dej                  dz  dej                  dz  deee	e	f      dz  d	ej                  dz  d
ej                  dz  de
e   dej                  fdZ xZS )Deimv2DecoderLayerr^   c                    t         |   |       t        |      | _        t	        |j
                        | _        t	        |j
                        | _        t        |      | _	        |j                  | _
        |j                  rt        |j
                        nd | _        |j                  rd | _        y t	        |j
                        | _        y )Nr^   )rr   rc   rz   encoder_attnr[   rf   self_attn_layer_normfinal_layer_normr]   mlpr9   ro   gatewayencoder_attn_layer_normrl   r^   rt   s     rK   rc   zDeimv2DecoderLayer.__init__l  s     ?vN$1&..$A! -fnn ="6*!--5;5G5Gz&..1T/5/A/At$}U[UcUcGd$rJ   NrV   position_embeddingsreference_pointsspatial_shapesspatial_shapes_listencoder_hidden_statesencoder_attention_maskkwargsr   c                 D   |}	 | j                   d|||d|\  }}
t        j                  j                  || j                  | j                        }|	|z   }| j                  |      }|}	||n||z   }| j                  |||||      \  }}
t        j                  j                  || j                  | j                        }| j                  | j                  |	|      }n|	|z   }| j                  |      }|}	| j                  |      }|	|z   }| j                  |      }|S )N)rV   attention_maskr   )ptraining)rV   r   r   r   r   rI   )	self_attnra   
functionaldropoutr   r   r   r   r   r   r   )rl   rV   r   r   r   r   r   r   r   r   r   s              rK   r   zDeimv2DecoderLayer.forwardv  sM    ! *4>> 
'1 3
 	
q --mt||VZVcVc-d =011-@  *=)D-ZmJm,,'"7-) 3 - 
q --mt||VZVcVc-d<<# LL=AM$}4M 88GM !/ =0--m<rJ   )NNNNNN)r<   r=   r>   r+   rc   rX   r   rB   rD   rC   r	   r
   r   ru   rv   s   @rK   r   r   k  s    e| e 4804.2<@596:2||2 #\\D02  ,,-	2
 t+2 "%S/2T92  %||d22 !&t 32 +,2 
2rJ   r   c                   N     e Zd Zg dZ ej
                          fd       Z xZS )Deimv2PreTrainedModel)Deimv2HybridEncoderDeimv2LiteEncoderr   c                 8   t         |   |       t        |t              rt	        j
                  |j                  j                         t	        j                  |j                  j                  d       t	        j
                  |j                  j                         t	        j                  |j                  j                  d       t	        j
                  |j                  j                         t	        j                  |j                  j                  d       y y )Nr   )rr   _init_weights
isinstancer]   initxavier_uniform_rg   weight	constant_r`   rh   ri   )rl   modulert   s     rK   r   z#Deimv2PreTrainedModel._init_weights  s    f%fo.  !1!1!8!89NN6++00!4  !6!67NN6>>..2  !1!1!8!89NN6++00!4 /rJ   )r<   r=   r>   _no_split_modulesrX   no_gradr   ru   rv   s   @rK   r   r     s#    ]U]]_	5 	5rJ   r   c                   f     e Zd Z fdZdej
                  dee   deej
                     fdZ	 xZ
S )Deimv2ConvEncoderc                     t         |   |       t        |      }|j                  r(t	        j
                         5  t        |       d d d        || _        | j                  j                  | _	        t        j                  | j                  D cg c]>  }|j                  dk7  rt        |||j                  dd      nt        j                         @ c}      | _        | j#                          y # 1 sw Y   xY wc c}w )Nliter   )rr   rc   r   freeze_backbone_batch_normsrX   r   r&   modelchannelsintermediate_channel_sizesra   r   r8   r|   r   r   encoder_input_proj	post_init)rl   r^   backbone
in_channelrt   s       rK   rc   zDeimv2ConvEncoder.__init__  s      (-- -"8,-
*.***=*='"$--
 #'"A"A	  &&&0 $FJ8Q8QSTVWX[[]##
 	- -
s   C/AC;/C8r   r   r   c                      | j                   |fi |j                  }t        | j                  |      D cg c]  \  }} ||       c}}S c c}}w rq   )r   rU   zipr   )rl   r   r   featuresprojfeats         rK   r   zDeimv2ConvEncoder.forward  sF    4::l5f5BB-01H1H(-STztTT
TTTs   A)r<   r=   r>   rc   rX   r   r	   r
   rB   r   ru   rv   s   @rK   r   r     s=    ,UELL UFCU<V U[_`e`l`l[m UrJ   r   c                   l     e Zd Zdef fdZdej                  dee   de	ej                     fdZ
 xZS )Deimv2DINOv3ConvEncoderr^   c                    t         |   |       t        |      | _        t	        |      | _        |j                  j                  }|j                  }|j                  }t        j                  t        |||dz  z   |dd      t        |||dz  z   |dd      t        |||dz  z   |dd      g      | _        | j                          y )Nr   r   r   )rr   rc   r   r   r   spatial_tuning_adapterr-   hidden_sizer   r6   ra   r   r|   fusion_projr   )rl   r^   	embed_dim
hidden_dimspatial_tuning_adapter_channelsrt   s        rK   rc   z Deimv2DINOv3ConvEncoder.__init__  s     %f-&@&H#**66	..
*0*P*P'==#FI8WZ[8[,[]gijlmn#FI8WZ[8[,[]gijlmn#FI8WZ[8[,[]gijlmn
 	rJ   r   r   r   c                     | j                   |fi |}|j                  }| j                   j                  j                  }|j                  d   |z  }|j                  d   |z  }g }t        |      }	t        |      D ]_  \  }
}t        |d|	dz
  |
z
  z  z        }t        |d|	dz
  |
z
  z  z        }t        j                  |||gdd      }|j                  |       a | j                  |      }g }t        t        ||            D ]^  \  }
\  }}t        j                  |j                  |j                         |gd      }|j                   | j"                  |
   |             ` |S )Nr   r   bilinearF)sizemodealign_cornersr   r   )r   rU   r^   
patch_sizeshapelen	enumeraterC   Finterpolateappendr
  r  rX   r   todevicer  )rl   r   r   backbone_outputrU   r  height_patcheswidth_patchessemantic_features
num_scalesir  resize_heightresize_widthspatialdetail_featuresoutputssemantic_featuredetail_featurefuseds                       rK   r   zDeimv2DINOv3ConvEncoder.forward  sy   '$--??&33]]))44
%++A.*<$**1-;&
 . 	.GAtzA~7I1J JKM}qZ!^a5G/HHILmmD|/LS]mrsG$$W-		. 55lC5>sCTVe?f5g 	71A1 .II/22>3H3HI>Z`abENN.4++A.u56	7 rJ   )r<   r=   r>   r+   rc   rX   r   r	   r
   rB   r   ru   rv   s   @rK   r  r    s@    | &ELL FCU<V [_`e`l`l[m rJ   r  c                        e Zd Zd eed       eed      giZdef fdZee	de
ej                     dee   d	efd
              Z xZS )r   rV   
input_proj)
layer_namebi_fusion_convr^   c                 l   t         |   |       |j                  }|j                  }t	        j
                  |j                  D cg c]  }t        |||dd       c}      | _        t	        j                  ddd      | _
        t        |||dd|      | _        t	        j                  ddd      | _        t        |||dd|      | _        t        |||dd|      | _        t        d|j                   z        }t#        ||      | _        t#        ||      | _        | j)                          y c c}w )Nr   r   r   r   r   r   )rr   rc   r   r   ra   r   encoder_in_channelsr|   r-  	AvgPool2d
down_pool1
down_conv1
down_pool2
down_conv2r/  r   
depth_multr   	fpn_block	pan_blockr   )rl   r^   r  r   r  r   rt   s         rK   rc   zDeimv2LiteEncoder.__init__  s    ..
//
--Y_YsYst: ZAFt
 ,,1QJ-fj*aQR_ij,,1QJ-fj*aQR_ij1&*jRSUVcmn1v0001
+F
K+F
K us   D1inputs_embedsr   r   c                 d   t        |      D cg c]  \  }} | j                  |   |       }}}|j                  | j                  | j	                  |d                      | j                  |d   t        j                  |d   d      z         |d<   g }|d   t        j                  |d   dd      z   }|j                  | j                  |             |d   | j                  | j                  |d               z   }|j                  | j                  |             t        |      S c c}}w )Nr   r          @nearestscale_factorr  rU   )r  r-  r  r5  r4  r/  r  adaptive_avg_pool2dr  r9  r7  r6  r:  rT   )rl   r;  r   r#  featureprojected_featuresr(  fused_features           rK   r   zDeimv2LiteEncoder.forward%  s"    MVVcLdeja0dooa09ee!!$//$//BTUWBX2Y"Z[!%!4!4r"Q%:%:;Mb;QST%UU"
2 *1->PQR>Sbelu0vvt~~m45*1-PWXZP[@\0]]t~~m45"88 fs   D,)r<   r=   r>   r   r|   _can_record_outputsr+   rc   r   r   rB   rX   r   r	   r
   rT   r   ru   rv   s   @rK   r   r     sy     	.<H.;KL
| ,  9T%,,%7 96J\C] 9bu 9   9rJ   r   c                   Z    e Zd ZdZdefdZ	 d	deej                     dz  de	e
   defdZy)
r   aE  
    DEIMv2 variant of DFineHybridEncoder. Uses element-wise sum fusion (`fuse_feature_maps`) instead of
    D-FINE's channel concatenation, Deimv2RepNCSPELAN5 (simplified 4-way concat) instead of DFineRepNCSPELAN4,
    and returns Deimv2EncoderOutput with feature_maps instead of BaseModelOutput with last_hidden_state.
    r^   c           
         t         j                  | |       || _        |j                  | _        t        | j                        dz
  | _        |j                  | _        |j                  | _        |j                  | _	        |j                  | _
        |j                  | _        | j                  D cg c]  }| j                   c}| _        | j                  | _        |j                  | _        t!        j"                  t%        t        | j                              D cg c]  }t'        |       c}      | _        t!        j"                         | _        t!        j"                         | _        t%        t        | j                        dz
  dd      D ]|  }| j*                  j/                  t1        || j                  | j                  dd             t3        d|j4                  z        }| j,                  j/                  t7        ||             ~ t!        j"                         | _        t!        j"                         | _        t%        t        | j                        dz
        D ]f  }| j8                  j/                  t=        |dd             t3        d|j4                  z        }| j:                  j/                  t7        ||             h | j?                          y c c}w c c}w )Nr   r   r=  r   r1  r   ) r   rc   r^   r2  r   r  num_fpn_stagesfeat_stridesr   encode_proj_layerspositional_encoding_temperaturer.   r   out_stridesr4   r   ra   r   r   r   aifilateral_convs
fpn_blocksr  r|   r   r8  r   downsample_convs
pan_blocksr   r   )rl   r^   r   r   s       rK   rc   zDeimv2HybridEncoder.__init__@  s=   &&tV4!55!$"2"23a7"//"(";";"(";";/5/U/U,))>B>N>NOT44O,,--MME#dNeNeJfDg"hq?6#:"hi	]]_--/s4++,q0!R8 	WA%%#FD,C,CTE\E\^_abc q6#4#445JOO""#5f*#UV	W !#--/s4++,q01 	WA!!((fa)CDq6#4#445JOO""#5f*#UV	W
 	- P #is   /KKNr;  r   r   c                 2   |}| j                   j                  dkD  r9t        | j                        D ]!  \  }} | j                  |   ||   fi |||<   # |d   g}t        t        | j                  | j                              D ]w  \  }\  }}	|| j                  |z
  dz
     }
|d   } ||      }||d<   t        j                  |dd      }t        ||
| j                        } |	|      }|j                  |       y |j                          |d   g}t        t        | j                  | j                               D ]M  \  }\  }}|d   }||dz      } ||      }t        ||| j                        } ||      }|j                  |       O t#        |      S )z
        Args:
            inputs_embeds (`list[torch.FloatTensor]`):
                Multi-scale feature maps from the backbone (one tensor per feature level) passed to the encoder.
        r   r=  r   r>  r?  r@  rB  )r^   encoder_layersr  rL  rO  r  rP  rQ  rJ  r  r  r   r   r  reverserR  rS  rT   )rl   r;  r   rU   r#  enc_indfpn_feature_mapsidxlateral_convr9  backbone_feature_maptop_fpn_feature_mapfused_feature_mapnew_fpn_feature_mappan_feature_mapsdownsample_convr:  top_pan_feature_mapfpn_feature_mapdownsampled_feature_mapnew_pan_feature_maps                        rK   r   zDeimv2HybridEncoder.forwardb  s    %;;%%)'(?(?@ V
7(4		!\'5J(Uf(UW%V ),-.7D<N<NPTP_P_8`.a 	9*C*,	#/0C0Cc0IA0M#N "22"6"./B"C#6R "#--0CRU\e"f 12EG[]a]i]i j"+,=">##$78	9 	  " -Q/01:3t?T?TVZVeVe;f1g 	9-C-/9"22"6.sQw7O&56I&J# 12I?\`\h\h i"+,=">##$78	9 #0@AArJ   rq   )r<   r=   r>   r?   r+   rc   rB   rX   r   r	   r
   rT   r   rI   rJ   rK   r   r   9  sR     |  H 48(BELL)D0(B +,(B 
	(BrJ   r   c                   $     e Zd Zdef fdZ xZS )Deimv2Decoderr^   c                     t         |   |       t        d|j                  |j                  d|j                        | _        y )Nr   r   r   )rr   rc   rx   rf   decoder_activation_functionquery_pos_headr   s     rK   rc   zDeimv2Decoder.__init__  s6    ''6>>6>>1fNpNpqrJ   )r<   r=   r>   r+   rc   ru   rv   s   @rK   rf  rf    s    r| r rrJ   rf  c                       e Zd ZdefdZ	 	 	 	 ddej                  dej                  dz  dej                  dz  dej                  dz  dee	   dz  d	e
e   fd
Zy)Deimv2Modelr^   c           
      l   t         j                  | |       t        |j                  dd       dk(  }|rt	        |      n
t        |      | _        |j                  dk(  rt        |      nt        |      | _
        |j                  dkD  r>t        j                  |j                  dz   |j                  |j                        | _        |j"                  r/t        j                  |j$                  |j                        | _        t        j(                  t        j*                  |j                  |j                        t        j,                  |j                  |j.                              | _        t        j*                  |j                  |j                        | _        t5        |j                  |j                  d	d
      | _        |j8                  r)| j;                  | j<                        \  | _        | _         tC        |jD                        }g }|jD                  d   }tG        |      D ]Z  }|jI                  |jJ                  |jD                  d   k(  rt        jL                         ntO        |||j                  dd             \ tG        |jP                  |z
        D ]Z  }|jI                  |jJ                  |jD                  d   k(  rt        jL                         ntO        |||j                  d
d             \ t        jR                  |      | _*        tW        |      | _,        | j[                          y )Nr@   
dinov3_vitr   r   r   r   )padding_idx)epsr   r   )dtyper=  r   ).r   rc   getattrr-   r  r   conv_encoderr8   r   r   encodernum_denoisingra   	Embedding
num_labelsrf   denoising_class_embedlearn_initial_querynum_queriesweight_embedding
Sequentialre   	LayerNormlayer_norm_eps
enc_outputenc_score_headrx   enc_bbox_headanchor_image_sizegenerate_anchorsrp  anchors
valid_maskr  decoder_in_channelsr   r  r  r   r|   num_feature_levelsr   decoder_input_projrf  decoderr   )rl   r^   	is_dinov3num_backbone_outsr  r   r   s          rK   rc   zDeimv2Model.__init__  s   &&tV4F22L$G<W	?H3F;N_`fNg)/)<)<)Ff%L_gmLn 	 !#)+!!A%v~~6CTCT*D& %%$&LL1C1CV^^$TD!--IIfnnfnn5LLV-B-BC
 !ii8I8IJ&v~~v~~q!L##,0,A,A

,A,S)DL$/ : :;004() 	A%%%%)C)CB)GG (fnnaQRS	 v003DDE 	A%%%%)C)CB)GG (fnnaQRS	 #%--0B"C$V,rJ   Nr   
pixel_maskencoder_outputsr;  labelsr   c                 p   ||t        d      |K|j                  \  }}}	}
|j                  }|t        j                  ||	|
f|      }| j                  |      }n|j                  d   }|j                  }|} | j                  |fi |}g }t        |j                        D ])  \  }}|j                   | j                  |   |             + | j                  j                  t        |      kD  r|j                   | j                  t        |         |j                  d                t        t        |      | j                  j                        D ]3  }|j                   | j                  |   |j                  d                5 g }g }t        j                  t        |      df|t        j                         }t        |      D ]j  \  }}|j                  dd  \  }	}
|	||df<   |
||df<   |j                  |	|
f       |j#                  d      j%                  dd      }|j                  |       l t        j&                  |d      }t        j&                  |j)                  d	      |j+                  d      j-                  d      d d f      }| j.                  r| j                  j0                  dkD  r|t3        || j                  j4                  | j                  j6                  | j8                  | j                  j0                  | j                  j:                  | j                  j<                  
      \  }}}}nd\  }}}}t        |      }|j                  }|j>                  }| j.                  s| j                  j@                  #tC        |      }| jE                  |||      \  }}n<| jF                  | jH                  }}|jK                  ||      |jK                  ||      }}|jK                  |j>                        |z  }| jM                  |      }| jO                  |      }| jQ                  |      |z   } t        jR                  |jU                  d      jV                  | j                  j6                  d      \  }!}"| jY                  d|"j[                  d      j]                  dd| j                  d               }#t_        j`                  |#      }$|t        jb                  ||#gd      }#|jY                  d|"j[                  d      j]                  dd|j                  d               }%| j                  jd                  r| jf                  ji                  |ddg      }&nP|jY                  d|"j[                  d      j]                  dd|j                  d               }&|&jk                         }&|t        jb                  ||&gd      }&|#jk                         }' | jl                  d!|&|||'|||d|}(to        d!i d|(jp                  d|(jr                  d|(jt                  d|(jv                  d|(jx                  d|(jz                  d|(j|                  d|(j~                  d|(j                  d|j                  d|j|                  d|j~                  d|'d|%d|$d|d| d |S )"Nz8You have to specify either pixel_values or inputs_embeds)r  r   r=  r   )r  rp  r   )r   )targetsnum_classesry  class_embednum_denoising_querieslabel_noise_ratiobox_noise_scaleNNNNr   )r   index)r;  r   r   r   r   r   level_start_indexlast_hidden_stateintermediate_hidden_statesintermediate_logitsintermediate_reference_pointsintermediate_predicted_cornersinitial_reference_pointsdecoder_hidden_statesdecoder_attentionscross_attentionsencoder_last_hidden_stater   encoder_attentionsinit_reference_pointsenc_topk_logitsenc_topk_bboxesenc_outputs_classenc_outputs_coord_logitsdenoising_meta_valuesrI   )A
ValueErrorr  r  rX   onesrr  rs  r  rU   r  r  r^   r  r  r   emptylongflatten	transposer   	new_zerosprodcumsumr   rt  r%   rv  ry  rw  r  r  rp  r  rD   r  r  r  r  r~  r  r  topkmaxvaluesgather	unsqueezerepeatr  sigmoidconcatrx  rz  tiledetachr  rQ   r  r  r  r  r  r  rV   rW   r  ))rl   r   r  r  r;  r  r   
batch_sizenum_channelsheightwidthr  
proj_featssourceslevelsourcer#  source_flattenr   r   r  denoising_classdenoising_bbox_unactr   r  rp  spatial_shapes_tupler  r  memoryoutput_memoryr  r  r   topk_indreference_points_unactr  r  targetr  decoder_outputss)                                            rK   r   zDeimv2Model.forward  s    M$9WXX  6B6H6H3Jfe!((F!"ZZ*fe)DfU
 **<8J&,,Q/J"))F&J&$,,

 &'C'CD 	CME6NN942259&AB	C ;;))CL8NN@4223w<@A]A]^`Aabc3w<)G)GH ]9t66q9/:V:VWY:Z[\]  c'lA%6vUZZX&w/ 	*ME6"LL-MFE'-N5!8$',N5!8$&&7^^A&00A6F!!&)	* >15!II~'?'?'E~GZGZ[\G]GdGdefGghkikGl&mn ==T[[66:v?Q 9 KK22 KK33 66&*kk&?&?"&++"?"? $ ; ;$% \rXO1>CX(
&&$$ ==DKK99A $))<#= "&"7"78LU[ch"7"iGZ"&,,ZG")**VU";Z]]6SX=YZG ~334~E/ //>#'#5#5m#Dw#N jj!2!6!6r!:!A!A4;;CZCZ`ab8!9!@!@++B/66q!=U=[=[\^=_` "A "
 ))$:;+%*\\3GI_2`bc%d"+22++B/66q!=N=T=TUW=XY 3 

 ;;****//Q0BCF"))ax7I7I"7M7T7TUVXY[h[n[noq[r7s)tF]]_F&\\?F";Q?F 6 = = ? '$,, 	
 "0#12) 3/	
 	
 ! 
-??
'6'Q'Q
 !0 C C
 +:*W*W	

 ,;+Y+Y
 &5%M%M
 #2"?"?
  /99
 -==
 '6&B&B
 #2"?"?
  /99
 #8
 ,
 ,
  0!
" &>#
$ #8%
 	
rJ   r  )r<   r=   r>   r+   rc   rX   rY   
LongTensorrB   dictr	   r
   r   rI   rJ   rK   rk  rk    s    -| -d /34826$(]
'']
 $$t+]
 **T1	]

 ((4/]
 T
T!]
 +,]
rJ   rk  c                   >     e Zd ZdZed        ZdefdZ fdZ xZ	S )Deimv2ForObjectDetectionNc                 R    dddd}| j                   j                  r
d|d<   d|d<   |S )	Nz^class_embed.0zmodel.decoder.class_embedzmodel.decoder.bbox_embed)zclass_embed.(?![0])\d+r  
bbox_embedzmodel.decoder.bbox_embed.0z&model\.decoder\.bbox_embed\.(?![0])\d+zbbox_embed.0zbbox_embed.(?![0])\d+)r^   r:   )rl   keyss     rK   _tied_weights_keysz+Deimv2ForObjectDetection._tied_weights_keysf  s>     (964

 ;;&&>[D:;-<D)*rJ   r^   c                    t         j                  | |       |j                  dk\  r|j                  n|j                  |j                  z   | _        t	        |      | _        t        |j                  |j                  z        }|j                  }t        j                  t        |      D cg c],  }t        j                  |j                  |j                        . c}      | _        |j                   rQt#        |j                  |j                  d|j$                  dz   z  d      }t        j                  |g|z        | _        nt        j                  t        | j                  dz         D cg c]4  }t#        |j                  |j                  d|j$                  dz   z  d      6 c}t        |j                  | j                  z
  dz
        D cg c]   }t#        ||d|j$                  dz   z  d      " c}z         | _        | j                  | j
                  j(                  _        | j&                  | j
                  j(                  _        | j+                          y c c}w c c}w c c}w )Nr   r   r   r   )r   rc   eval_idxdecoder_layersrk  r   r   layer_scaler  ra   r   r   re   rf   rv  r  r:   rx   max_num_binsr  r  r   )rl   r^   
scaled_dimnum_predr   shared_bboxs         rK   rc   z!Deimv2ForObjectDetection.__init__r  s   &&tV4+1??a+?VEZEZ]c]l]lEl (
6--0B0BBC
((==`efn`o)p[\"))FNNFDUDU*V)pq!!#F$6$68J8JAQWQdQdghQhLiklmK mm[MH,DEDO mm #4==1#45 f00&2D2Da6K^K^abKbFcefg #6#8#84==#H1#LM j*a6;N;NQR;R6SUVW		DO *.)9)9

&(,

%% *qs   *1I-9I"%I'
c                  :     t               j                  di |  y)aW  
        Example:

        ```python
        >>> import torch
        >>> from transformers.image_utils import load_image
        >>> from transformers import AutoImageProcessor, Deimv2ForObjectDetection

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = load_image(url)

        >>> image_processor = AutoImageProcessor.from_pretrained("harshaljanjani/DEIMv2_HGNetv2_N_COCO_Transformers")
        >>> model = Deimv2ForObjectDetection.from_pretrained("harshaljanjani/DEIMv2_HGNetv2_N_COCO_Transformers")

        >>> # prepare image for the model
        >>> inputs = image_processor(images=image, return_tensors="pt")

        >>> # forward pass
        >>> outputs = model(**inputs)

        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 300, 80]

        >>> boxes = outputs.pred_boxes
        >>> list(boxes.shape)
        [1, 300, 4]

        >>> # convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
        >>> target_sizes = torch.tensor([image.size[::-1]])
        >>> results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)
        >>> result = results[0]  # first image in batch

        >>> for score, label, box in zip(result["scores"], result["labels"], result["boxes"]):
        ...     box = [round(i, 2) for i in box.tolist()]
        ...     print(
        ...         f"Detected {model.config.id2label[label.item()]} with confidence "
        ...         f"{round(score.item(), 3)} at location {box}"
        ...     )
        ```
        NrI   )rr   r   )super_kwargsrt   s    rK   r   z Deimv2ForObjectDetection.forward  s    T 	','rJ   )
r<   r=   r>   r   propertyr  r+   rc   r   ru   rv   s   @rK   r  r  c  s1    	 	| 6*( *(rJ   r  )r+   rk  r   r  )r3   )\dataclassesr   rX   torch.nnra   torch.nn.functionalr   r  huggingface_hub.dataclassesr    r   r   backbone_utilsr   modeling_outputsr   processing_utilsr	   utilsr
   r   r   utils.genericr   utils.output_capturingr   r   autor   d_fine.configuration_d_finer   d_fine.modeling_d_finer   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   llama.modeling_llamar'   r(   
get_loggerr<   loggerr+   rM   rQ   rT   r[   r]   ro   rx   rz   r|   r~   rb   r   r   r   r   r   r   r   rH   r   r   r   r   r   r   r  r   r   rf  rk  r  __all__rI   rJ   rK   <module>r     sh   "     . & + + & @ @ 7 E  5     , : 
		H	% ?@C+; C+  AC+L	, 		( 	  <+ < <	L 	 h  + +	 		*L 		, 		( 	4		 4<0 0<	; 		* 		n 	A A&<U\\ <%,, <Y\ <iniuiu <	] 		 	=* =@50 5"U- U8+3 +\09- 09fQB, QBhrL rM
* M
`T(6 T(nrJ   