Ë
    ÛÓ^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Ð1€Kà48€Iˆt�C‰y˜5  c ™?Ñ*¨TÑ1Ó8Ø €O�UÓ Ø!%Ð˜$Ó%Ø"€Iˆu�t‰|Ó"Ø €O�SÓ Ø+-Ð# SÓ-Ø €L�#Ó Ø€K�ÓØ!€O�TÓ!Ø&*Ð˜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\   … ñð
 -1€L�$�u×(Ñ(Ñ)Ó0Ø:>€M�5˜×*Ñ*¨CÐ/Ñ0°4Ñ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Ñ0°AÑ5ˆÜŸ™ 6§>¡>°?ÈÔNˆŒÜ—y‘y §¡°ÀtÔLˆŒÜŸ™ ?°F·N±NÈÔNˆŒÜ—g‘g“iˆ�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·   ø€ ô 	‰ÑÔØ×/Ñ/ˆ
Ü˜l¨YÑ6Ó7ˆÜ(¨°¸oÐPQÑ>QÐSTÐVWÐdnÔoˆŒ
ÜŸ=™=ÜRWÐXbÓRcÖdÈQÔ˜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Ñˆ�-Ø×*Ñ*ò 	6ˆJÙ& }Ó5‰Mð	6à�z‰z˜( ]Ñ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õ
ð 4 U§\¡\ð 4°e·l±l÷ 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Ñ2°QÑ6ˆÜ˜V×4Ñ4°v×7PÑ7PÑPÐTUÑUÓVˆÜ(¨°¸nÈaÐQRÐ_iÔjˆŒ
Ü)¨&°.ÀAÑ2EÀ|Ð`kÔlˆŒÜ)¨&°,ÀÐYdÔeˆŒÜ(Ø�N a¨,Ñ&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Ñ+JÈ1ÐRSÐ+JÓ+TÑ(ˆ˜ØŸ-™-¨Ó8ˆØŸ-™-¨Ó8ˆÜ$Ÿy™y¨/¸?ÈOÐ]lÐ)mÐstÔuÐØ�z‰zÐ.Ó/Ð/rJ   )r   )r<   r=   r>   r?   r+   rC   rc   rX   r›   rš   ru   rv   s   @rK   r�   r�     s6   ø„ ññ
˜|ð 
¸#õ 
ð0 U§\¡\ð 0°e·l±l÷ 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ˆÜ,¨V°Q¸À!ÀQÐSYÔZˆŒÜŸ™°!¸AÀqÔIˆŒÜ(¨°¸1¸x¹<ÈÈAÓNˆŒ
Ü(¨°°X±¸qÀ8¹|ÈQÐPQÓRˆŒ
Ü(¨°°X±¸qÀ8¹|ÈQÐPQÓRˆŒ
Ü—g‘g“iˆ�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 E§L¡Lð A°U¸5¿<¹<ÈÏÉÐW\×WcÑWcÐ;cÑ5d÷ 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+   € à�%ÒØ˜}Ñ,Ð,Ü�9‰9�m ]Ð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ˆÔ!Ü -¨f¯n©nÓ =ˆÔÜ" 6Ó*ˆŒØ!×-Ñ-ˆÔØ5;×5GÒ5G”z &§.¡.Ô1ÈTˆŒØ/5×/AÒ/A tˆÕ$Ä}ÐU[×UcÑUcÓGdˆÕ$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ô Ÿ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆØ  =Ñ0ˆØ×1Ñ1°-Ó@ˆà ˆð *=Ð)D™È-ÐZmÑJmˆØ×,Ñ,Ø'Ø"7Ø-Ø)Ø 3ð -ó 
Ñˆ�qô Ÿ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-Ódˆà�<‰<Ð#Ø ŸL™L¨°=ÓA‰Mà$ }Ñ4ˆMØ ×8Ñ8¸ÓGˆMð !ˆØŸ™ Ó/ˆØ  =Ñ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ð 48Ø04Ø.2Ø<@Ø59Ø6:ñ2à—|‘|ð2ð #Ÿ\™\¨DÑ0ð2ð  Ÿ,™,¨Ñ-ð	2ð
 Ÿ™ tÑ+ð2ð " %¨¨S¨¡/Ñ2°TÑ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        «      rút	        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Ô%ä�fœoÔ.Ü× Ñ  ×!1Ñ!1×!8Ñ!8Ô9Ü�N‰N˜6×+Ñ+×0Ñ0°!Ô4Ü× Ñ  §¡×!6Ñ!6Ô7Ü�N‰N˜6Ÿ>™>×.Ñ.°Ô2Ü× Ñ  ×!1Ñ!1×!8Ñ!8Ô9Ü�N‰N˜6×+Ñ+×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ô $ F¨J¸×8QÑ8QÐSTÐVWÔ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Ñ5¨fÑ5×BÑBˆÜ-0°×1HÑ1HÈ(Ó-S×T™z˜t T‘�T•
ÓTÐTùÓTs   ·A)r<   r=   r>   rc   rX   r›   r	   r
   rB   rš   ru   rv   s   @rK   r÷   r÷   ¼  s=   ø„ ôð,U E§L¡Lð U¸FÐCUÑ<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Ð'ÜŸ=™=ä# F¨IÐ8WÐZ[Ñ8[Ñ,[Ð]gÐijÐlmÓnÜ# F¨IÐ8WÐZ[Ñ8[Ñ,[Ð]gÐijÐlmÓnÜ# F¨IÐ8WÐZ[Ñ8[Ñ,[Ð]gÐijÐlmÓ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Ñ-°Ñ;ˆàÐÜ˜Ó&ˆ
Ü  Ó.ò 	.‰GˆAˆtÜ °°zÀA±~ÈÑ7IÑ1JÑ JÓKˆMÜ˜}¨q°ZÀ!±^ÀaÑ5GÑ/HÑHÓIˆLÜ—m‘m D°¸|Ð/LÐS]ÐmrÔsˆGØ×$Ñ$ WÕ-ð		.ð ×5Ñ5°lÓCˆàˆÜ5>¼sÐCTÐVeÓ?fÓ5gò 	7Ñ1ˆAÑ1Ð  .Ü—I‘IÐ/×2Ñ2°>×3HÑ3HÓIÈ>ÐZÐ`aÔbˆEØ�N‰NÐ.˜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@   ø„ ð˜|õ ð& E§L¡Lð ¸FÐCUÑ<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_×YsÑYsÖtÈ:Ô  ¨°ZÀÀAÕFÒtó
ˆŒô Ÿ,™,°1¸QÈÔJˆŒÜ-¨f°jÀ*ÈaÐQRÐ_iÔjˆŒÜŸ,™,°1¸QÈÔJˆŒÜ-¨f°jÀ*ÈaÐQRÐ_iÔjˆŒä1°&¸*ÀjÐRSÐUVÐcmÔnˆÔä˜1˜v×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 )Néÿÿÿÿr†   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ÐVcÓLd×e¹j¸aÀÐ0˜dŸo™o¨aÑ0°Õ9ÐeÐÑeØ×!Ñ! $§/¡/°$·/±/ÐBTÐUWÑBXÓ2YÓ"ZÔ[à!%×!4Ñ!4Ø˜rÑ"¤Q×%:Ñ%:Ð;MÈbÑ;QÐSTÓ%UÑUó"
Ð˜2Ñð ˆØ*¨1Ñ-´·±Ð>PÐQRÑ>SÐbeÐluÔ0vÑvˆØ�‰�t—~‘~ mÓ4Ô5à*¨1Ñ-°·±ÀÇÁÐPWÐXZÑP[Ó@\Ó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ð
ðÐð˜|õ ð,  Øð9 T¨%¯,©,Ñ%7ð 9À6ÐJ\ÑC]ð 9Ðbuò 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=  € Ü×&Ñ& t¨VÔ4ØˆŒØ!×5Ñ5ˆÔÜ! $×"2Ñ"2Ó3°aÑ7ˆÔØ"×/Ñ/ˆÔØ"(×";Ñ";ˆÔØ"(×";Ñ";ˆÔØ/5×/UÑ/UˆÔ,Ø×)Ñ)ˆŒØ>B×>NÑ>NÖO¸˜T×4Ó4ÒOˆÔØ×,Ñ,ˆÔØ×-Ñ-ˆŒä—M‘MÄEÌ#Èd×NeÑNeÓJfÓDgÖ"h¸q¤?°6Õ#:Ò"hÓiˆŒ	äŸ]™]›_ˆÔÜŸ-™-›/ˆŒÜ”s˜4×+Ñ+Ó,¨qÑ0°!°RÓ8ò 	WˆAØ×Ñ×%Ñ%Ü# F¨D×,CÑ,CÀT×E\ÑE\Ð^_ÐabÓcôô ˜q 6×#4Ñ#4Ñ4Ó5ˆJØ�O‰O×"Ñ"Ô#5°fÈ*Ô#UÕVð	Wô !#§¡£ˆÔÜŸ-™-›/ˆŒÜ”s˜4×+Ñ+Ó,¨qÑ0Ó1ò 	WˆAØ×!Ñ!×(Ñ(¬°f¸aÀÓ)CÔDÜ˜q 6×#4Ñ#4Ñ4Ó5ˆJØ�O‰O×"Ñ"Ô#5°fÈ*Ô#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Ñ(UÈfÑ(U�˜WÒ%ðVð )¨Ñ,Ð-ÐÜ.7¼¸D×<NÑ<NÐPT×P_ÑP_Ó8`Ó.aò 	9Ñ*ˆCÑ*�, 	Ø#/°×0CÑ0CÀcÑ0IÈAÑ0MÑ#NÐ Ø"2°2Ñ"6ÐÙ".Ð/BÓ"CÐØ#6Ð˜RÑ Ü"#§-¡-Ð0CÐRUÐ\eÔ"fÐÜ 1Ð2EÐG[Ð]a×]iÑ]iÓ jÐÙ"+Ð,=Ó">ÐØ×#Ñ#Ð$7Õ8ð	9ð 	× Ñ Ô"ð -¨QÑ/Ð0ÐÜ1:¼3¸t×?TÑ?TÐVZ×VeÑVeÓ;fÓ1gò 	9Ñ-ˆCÑ-�/ 9Ø"2°2Ñ"6ÐØ.¨s°Q©wÑ7ˆOÙ&5Ð6IÓ&JÐ#Ü 1Ð2IÈ?Ð\`×\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à˜EŸL™LÑ)¨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¿>¹>È1Èf×NpÑNpÓ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†  € Ü×&Ñ& t¨VÔ4ä˜F×2Ñ2°LÀ$ÓGÈ<ÑWˆ	Ù?HÔ3°FÔ;ÔN_Ð`fÓNgˆÔà)/×)<Ñ)<ÀÒ)FÔ˜fÔ%ÔL_ÐgmÔLnð 	Œð ×Ñ !Ò#Ü)+¯©Ø×!Ñ! AÑ% v§~¡~À6×CTÑCTô*ˆDÔ&ð ×%Ò%Ü$&§L¡L°×1CÑ1CÀVÇ^Á^Ó$TˆDÔ!äŸ-™-Ü�I‰I�f—n‘n f§n¡nÓ5Ü�L‰L˜Ÿ™¨V×-BÑ-BÔCó
ˆŒô !Ÿi™i¨¯©¸×8IÑ8IÓJˆÔÜ& v§~¡~°v·~±~ÀqÈ!ÓLˆÔà×#Ò#Ø,0×,AÑ,AÈÏ
É
Ð,AÓ,SÑ)ˆDŒL˜$œ/ä × :Ñ :Ó;ÐØÐØ×0Ñ0°Ñ4ˆÜÐ(Ó)ò 	ˆAØ×%Ñ%à×%Ñ%¨×)CÑ)CÀBÑ)GÒGô —‘”ä(¨°¸f¿n¹nÈaÐQRÓSõð	ô �v×0Ñ0Ð3DÑDÓEò 	ˆAØ×%Ñ%à×%Ñ%¨×)CÑ)CÀBÑ)GÒGô —‘”ä(¨°¸f¿n¹nÈaÐQRÓ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ð Ð Ø6B×6HÑ6HÑ3ˆJ˜ f¨eØ!×(Ñ(ˆFØÐ!Ü"ŸZ™Z¨*°f¸eÐ)DÈfÔU�
ð ×*Ñ*¨<Ó8‰Jà&×,Ñ,¨QÑ/ˆJØ"×)Ñ)ˆFØ&ˆJà&˜$Ÿ,™,Øñ
àñ
ˆð ˆÜ& ×'CÑ'CÓDò 	C‰MˆE�6Ø�N‰NÐ9˜4×2Ñ2°5Ñ9¸&ÓAÕBð	Cð �;‰;×)Ñ)¬C°«LÒ8Ø�N‰NÐ@˜4×2Ñ2´3°w³<Ñ@À×A]ÑA]Ð^`ÑAaÓbÔcÜœ3˜w›<¨¯©×)GÑ)GÓHò ]�Ø—‘Ð9˜t×6Ñ6°qÑ9¸/×:VÑ:VÐWYÑ:ZÓ[Õ\ð]ð ˆØ ÐÜŸ™¤c¨'£l°AÐ%6¸vÌUÏZÉZÔXˆÜ& wÓ/ò 	*‰MˆE�6Ø"ŸL™L¨¨Ð-‰MˆF�EØ'-ˆN˜5 !˜8Ñ$Ø',ˆN˜5 !˜8Ñ$Ø×&Ñ&¨° Ô7Ø—^‘^ AÓ&×0Ñ0°°AÓ6ˆFØ×!Ñ! &Õ)ð	*ô Ÿ™ >°1Ó5ˆÜ!ŸI™I ~×'?Ñ'?ÀÓ'EÀ~×GZÑGZÐ[\ÓG]×GdÑGdÐefÓGgÐhkÐikÐGlÐ&mÓnÐð �=Š=˜TŸ[™[×6Ñ6¸Ò:¸vÐ?Qô 9ØØ ŸK™K×2Ñ2Ø ŸK™K×3Ñ3Ø ×6Ñ6Ø&*§k¡k×&?Ñ&?Ø"&§+¡+×"?Ñ"?Ø $§¡× ;Ñ ;ôñØØ$ØÙ%ð \rÑXˆOÐ1°>ÐCXä˜Ó(ˆ
Ø×&Ñ&ˆØ×$Ñ$ˆð �=Š=˜DŸK™K×9Ñ9ÐAô $)Ð)<Ó#=Ð Ø"&×"7Ñ"7Ð8LÐU[ÐchÐ"7Ó"iÑˆG‘Zà"&§,¡,°·±�ZˆGØ")§*¡*¨V°UÓ";¸Z¿]¹]È6ÐSXÓ=Y�ZˆGð —‘˜~×3Ñ3Ó4°~ÑEˆàŸ™¨Ó/ˆà ×/Ñ/°Ó>ÐØ#'×#5Ñ#5°mÓ#DÀwÑ#NÐ ä—j‘jÐ!2×!6Ñ!6°rÓ!:×!AÑ!AÀ4Ç;Á;×CZÑCZÐ`aÔb‰ˆˆ8à!9×!@Ñ!@Ø˜×+Ñ+¨BÓ/×6Ñ6°q¸!Ð=U×=[Ñ=[Ð\^Ñ=_Ó`ð "Aó "
Ðô Ÿ)™)Ð$:Ó;ˆØÐ+Ü%*§\¡\Ð3GÐI_Ð2`ÐbcÓ%dÐ"à+×2Ñ2Ø˜×+Ñ+¨BÓ/×6Ñ6°q¸!Ð=N×=TÑ=TÐUWÑ=XÓYð 3ó 
ˆð
 �;‰;×*Ò*Ø×*Ñ*×/Ñ/°¸QÀÐ0BÓC‰Fà"×)Ñ)¨a°x×7IÑ7IÈ"Ó7M×7TÑ7TÐUVÐXYÐ[h×[nÑ[nÐoqÑ[rÓ7sÐ)ÓtˆFØ—]‘]“_ˆ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 /3Ø48Ø26Ø$(ñ]
à×'Ñ'ð]
ð ×$Ñ$ 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  «      | _        n¾t        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ò  € Ü×&Ñ& t¨VÔ4à+1¯?©?¸aÒ+?˜ŸšÀV×EZÑEZÐ]c×]lÑ]lÑElˆŒÜ  Ó(ˆŒ
Ü˜6×-Ñ-°×0BÑ0BÑBÓCˆ
Ø×(Ñ(ˆÜŸ=™=Ô`eÐfnÓ`oÖ)pÐ[\¬"¯)©)°F·N±NÀF×DUÑDUÕ*VÒ)pÓqˆÔØ×!Ò!Ü# F×$6Ñ$6¸×8JÑ8JÈAÐQW×QdÑQdÐghÑQhÑLiÐklÓmˆKÜ Ÿm™m¨[¨M¸HÑ,DÓEˆD�Oä Ÿm™mô # 4§=¡=°1Ñ#4Ó5öàô ˜f×0Ñ0°&×2DÑ2DÀaÈ6×K^ÑK^ÐabÑKbÑFcÐefÕgòô # 6×#8Ñ#8¸4¿=¹=Ñ#HÈ1Ñ#LÓMöàô ˜j¨*°a¸6×;NÑ;NÐQRÑ;RÑ6SÐUVÕWòñ	ó	ˆDŒOð *.×)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\ð <Ðin×iuÑiuó <ô	�]ô 	ô	�ô 	ô=Ð*ô =ô@5Ð0ô 5ô"UÐ-ô Uô8+Ð3ô +ô\09Ð-ô 09ôfQBÐ,ô QBôhr�Lô rôM
�*ô M
ô`T(Ð6ô T(òn�rJ   