Ë
    T^(h£”  ã                   óN  — d Z ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlZddlZddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZmZmZmZm Z m!Z! ddl"m#Z#  e jH                  e%«      Z&dZ'dZ(g d¢Z)e G d„ de«      «       Z* G d„ dejV                  «      Z, G d„ dejV                  «      Z- G d„ dejV                  «      Z. G d„ dejV                  «      Z/	 d@dejV                  dej`                  dej`                  dej`                  de	ej`                     d e1d!e1fd"„Z2 G d#„ d$ejV                  «      Z3 G d%„ d&ejV                  «      Z4 G d'„ d(ejV                  «      Z5 G d)„ d*ejV                  «      Z6 G d+„ d,ejV                  «      Z7 G d-„ d.ejV                  «      Z8 G d/„ d0ejV                  «      Z9 G d1„ d2e«      Z:d3Z;d4Z< ed5e;«       G d6„ d7e:«      «       Z= G d8„ d9ejV                  «      Z> G d:„ d;ejV                  «      Z? ed<e;«       G d=„ d>e:«      «       Z@g d?¢ZAy)AzPyTorch YOLOS model.é    N)Ú	dataclass)ÚCallableÚDictÚListÚOptionalÚSetÚTupleÚUnion)Únné   )ÚACT2FN)ÚBaseModelOutputÚBaseModelOutputWithPooling)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)Ú find_pruneable_heads_and_indicesÚprune_linear_layer)ÚModelOutputÚadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsé   )ÚYolosConfigr   zhustvl/yolos-small)r   iI  i€  c                   ó<  — e Zd ZU dZdZeej                     ed<   dZ	ee
   ed<   dZeej                     ed<   dZeej                     ed<   dZeee
      ed<   dZeej                     ed<   dZeeej                        ed	<   dZeeej                        ed
<   y)ÚYolosObjectDetectionOutputaG
  
    Output type of [`YolosForObjectDetection`].

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` are provided)):
            Total loss as a linear combination of a negative log-likehood (cross-entropy) for class prediction and a
            bounding box loss. The latter is defined as a linear combination of the L1 loss and the generalized
            scale-invariant IoU loss.
        loss_dict (`Dict`, *optional*):
            A dictionary containing the individual losses. Useful for logging.
        logits (`torch.FloatTensor` of shape `(batch_size, num_queries, num_classes + 1)`):
            Classification logits (including no-object) for all queries.
        pred_boxes (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`):
            Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). These
            values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding
            possible padding). You can use [`~YolosImageProcessor.post_process`] to retrieve the unnormalized bounding
            boxes.
        auxiliary_outputs (`list[Dict]`, *optional*):
            Optional, only returned when auxilary losses are activated (i.e. `config.auxiliary_loss` is set to `True`)
            and labels are provided. It is a list of dictionaries containing the two above keys (`logits` and
            `pred_boxes`) for each decoder layer.
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of the last layer of the decoder of the model.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states of
            the model at the output of each layer plus the optional initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in
            the self-attention heads.
    NÚlossÚ	loss_dictÚlogitsÚ
pred_boxesÚauxiliary_outputsÚlast_hidden_stateÚhidden_statesÚ
attentions)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtorchÚFloatTensorÚ__annotations__r   r   r    r!   r"   r   r#   r$   r	   r%   © ó    úf/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/yolos/modeling_yolos.pyr   r   2   sµ   … ñðB )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø $€Iˆx˜‰~Ó$Ø*.€FˆH�U×&Ñ&Ñ'Ó.Ø.2€J�˜×*Ñ*Ñ+Ó2Ø.2Ð�x  T¡
Ñ+Ó2Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ô9r.   r   c                   ód   ‡ — e Zd ZdZdeddfˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚYolosEmbeddingszT
    Construct the CLS token, detection tokens, position and patch embeddings.

    ÚconfigÚreturnNc                 ón  •— t         ‰| �  «        t        j                  t	        j
                  dd|j                  «      «      | _        t        j                  t	        j
                  d|j                  |j                  «      «      | _	        t        |«      | _        | j                  j                  }t        j                  t	        j
                  d||j                  z   dz   |j                  «      «      | _        t        j                  |j                  «      | _        t#        |«      | _        || _        y ©Nr   )ÚsuperÚ__init__r   Ú	Parameterr*   ÚzerosÚhidden_sizeÚ	cls_tokenÚnum_detection_tokensÚdetection_tokensÚYolosPatchEmbeddingsÚpatch_embeddingsÚnum_patchesÚposition_embeddingsÚDropoutÚhidden_dropout_probÚdropoutÚ$InterpolateInitialPositionEmbeddingsÚinterpolationr2   )Úselfr2   r@   Ú	__class__s      €r/   r7   zYolosEmbeddings.__init__e   sÝ   ø€ Ü‰ÑÔäŸ™¤e§k¡k°!°Q¸×8JÑ8JÓ&KÓLˆŒÜ "§¡¬U¯[©[¸¸F×<WÑ<WÐY_×YkÑYkÓ-lÓ mˆÔÜ 4°VÓ <ˆÔØ×+Ñ+×7Ñ7ˆÜ#%§<¡<Ü�K‰K˜˜;¨×)DÑ)DÑDÀqÑHÈ&×J\ÑJ\Ó]ó$
ˆÔ ô —z‘z &×"<Ñ"<Ó=ˆŒÜAÀ&ÓIˆÔØˆ�r.   Úpixel_valuesc                 ó‚  — |j                   \  }}}}| j                  |«      }|j                  «       \  }}}| j                  j	                  |dd«      }	| j
                  j	                  |dd«      }
t        j                  |	||
fd¬«      }| j                  | j                  ||f«      }||z   }| j                  |«      }|S )Néÿÿÿÿr   ©Údim)Úshaper?   Úsizer;   Úexpandr=   r*   ÚcatrF   rA   rD   )rG   rI   Ú
batch_sizeÚnum_channelsÚheightÚwidthÚ
embeddingsÚseq_lenÚ_Ú
cls_tokensr=   rA   s               r/   ÚforwardzYolosEmbeddings.forwardt   sÅ   € Ø2>×2DÑ2DÑ/ˆ
�L &¨%Ø×*Ñ*¨<Ó8ˆ
à!+§¡Ó!2Ñˆ
�G˜Qð —^‘^×*Ñ*¨:°r¸2Ó>ˆ
Ø×0Ñ0×7Ñ7¸
ÀBÈÓKÐÜ—Y‘Y 
¨JÐ8HÐIÈqÔQˆ
ð #×0Ñ0°×1IÑ1IÈFÐTYÈ?Ó[ÐàÐ"5Ñ5ˆ
à—\‘\ *Ó-ˆ
àÐr.   ©
r&   r'   r(   r)   r   r7   r*   ÚTensorrZ   Ú__classcell__©rH   s   @r/   r1   r1   _   s6   ø„ ñð
˜{ð ¨tõ ð E§L¡Lð °U·\±\÷ r.   r1   c                   óB   ‡ — e Zd Zdˆ fd„Zddej
                  fd„Zˆ xZS )rE   r3   c                 ó0   •— t         ‰| �  «        || _        y ©N©r6   r7   r2   ©rG   r2   rH   s     €r/   r7   z-InterpolateInitialPositionEmbeddings.__init__‹   ó   ø€ Ü‰ÑÔØˆ�r.   c                 ó  — |d d …dd d …f   }|d d …d f   }|d d …| j                   j                   d …d d …f   }|d d …d| j                   j                   …d d …f   }|j                  dd«      }|j                  \  }}}| j                   j                  d   | j                   j
                  z  | j                   j                  d   | j                   j
                  z  }
}	|j                  |||	|
«      }|\  }}|| j                   j
                  z  || j                   j
                  z  }}t        j                  j                  |||fdd¬«      }|j                  d«      j                  dd«      }t        j                  |||fd¬«      }|S )Nr   r   é   ÚbicubicF©rO   ÚmodeÚalign_cornersrL   )r2   r<   Ú	transposerN   Ú
image_sizeÚ
patch_sizeÚviewr   Ú
functionalÚinterpolateÚflattenr*   rQ   )rG   Ú	pos_embedÚimg_sizeÚcls_pos_embedÚdet_pos_embedÚpatch_pos_embedrR   r:   rW   Úpatch_heightÚpatch_widthrT   rU   Únew_patch_heigthÚnew_patch_widthÚscale_pos_embeds                   r/   rZ   z,InterpolateInitialPositionEmbeddings.forward�   s†  € Ø!¢! Qª 'Ñ*ˆØ%¢a¨ gÑ.ˆØ!¢! d§k¡k×&FÑ&FÐ%FÑ%HÊ!Ð"KÑLˆØ#¢A q¨D¯K©K×,LÑ,LÐ+LÐ'LÊaÐ$OÑPˆØ)×3Ñ3°A°qÓ9ˆØ+:×+@Ñ+@Ñ(ˆ
�K ð �K‰K×"Ñ" 1Ñ%¨¯©×)?Ñ)?Ñ?Ø�K‰K×"Ñ" 1Ñ%¨¯©×)?Ñ)?Ñ?ð "ˆð *×.Ñ.¨z¸;ÈÐVaÓbˆà ‰ˆ�Ø,2°d·k±k×6LÑ6LÑ,LÈeÐW[×WbÑWb×WmÑWmÑNm˜/ÐÜŸ-™-×3Ñ3ØÐ#3°_Ð"EÈIÐejð 4ó 
ˆð *×1Ñ1°!Ó4×>Ñ>¸qÀ!ÓDˆÜŸ)™) ]°OÀ]Ð$SÐYZÔ[ˆØÐr.   ©r3   N©)i   i@  ©r&   r'   r(   r7   r*   r\   rZ   r]   r^   s   @r/   rE   rE   Š   s   ø„ õñ¸%¿,¹,÷ r.   rE   c                   óB   ‡ — e Zd Zdˆ fd„Zddej
                  fd„Zˆ xZS )Ú InterpolateMidPositionEmbeddingsr3   c                 ó0   •— t         ‰| �  «        || _        y ra   rb   rc   s     €r/   r7   z)InterpolateMidPositionEmbeddings.__init__¨   rd   r.   c                 óv  — |d d …d d …dd d …f   }|d d …d f   }|d d …d d …| j                   j                   d …d d …f   }|d d …d d …d| j                   j                   …d d …f   }|j                  dd«      }|j                  \  }}}}	| j                   j                  d   | j                   j
                  z  | j                   j                  d   | j                   j
                  z  }}
|j                  ||z  ||
|«      }|\  }}|| j                   j
                  z  || j                   j
                  z  }}t        j                  j                  |||fdd¬«      }|j                  d«      j                  dd«      j                  «       j                  ||||z  |«      }t        j                  |||fd¬«      }|S )	Nr   r   rf   r   rg   Frh   rL   )r2   r<   rk   rN   rl   rm   rn   r   ro   rp   rq   Ú
contiguousr*   rQ   )rG   rr   rs   rt   ru   rv   ÚdepthrR   r:   rW   rw   rx   rT   rU   Únew_patch_heightrz   r{   s                    r/   rZ   z(InterpolateMidPositionEmbeddings.forward¬   sµ  € Ø!¢!¢Q¨ª1 *Ñ-ˆØ%¢a¨ gÑ.ˆØ!¢!¢Q¨¯©×)IÑ)IÐ(IÑ(KÊQÐ"NÑOˆØ#¢A¢q¨!¨t¯{©{×/OÑ/OÐ.OÐ*OÒQRÐ$RÑSˆØ)×3Ñ3°A°qÓ9ˆØ2A×2GÑ2GÑ/ˆˆz˜;¨ð �K‰K×"Ñ" 1Ñ%¨¯©×)?Ñ)?Ñ?Ø�K‰K×"Ñ" 1Ñ%¨¯©×)?Ñ)?Ñ?ð "ˆð *×.Ñ.¨u°zÑ/AÀ;ÐP\Ð^iÓjˆØ ‰ˆ�Ø,2°d·k±k×6LÑ6LÑ,LÈeÐW[×WbÑWb×WmÑWmÑNm˜/ÐÜŸ-™-×3Ñ3ØÐ#3°_Ð"EÈIÐejð 4ó 
ˆð ×#Ñ# AÓ&ß‰Y�q˜!‹_ß‰Z‹\ß‰T�%˜Ð%5¸Ñ%GÈÓUð	 	ô  Ÿ)™) ]°OÀ]Ð$SÐYZÔ[ˆØÐr.   r|   r}   r~   r^   s   @r/   r€   r€   §   s   ø„ õñ¸%¿,¹,÷ r.   r€   c                   óZ   ‡ — e Zd ZdZˆ fd„Zdej                  dej                  fd„Zˆ xZS )r>   zì
    This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
    `hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer.
    c                 óÌ  •— t         ‰| �  «        |j                  |j                  }}|j                  |j
                  }}t        |t        j                  j                  «      r|n||f}t        |t        j                  j                  «      r|n||f}|d   |d   z  |d   |d   z  z  }|| _        || _        || _        || _
        t        j                  ||||¬«      | _        y )Nr   r   )Úkernel_sizeÚstride)r6   r7   rl   rm   rS   r:   Ú
isinstanceÚcollectionsÚabcÚIterabler@   r   ÚConv2dÚ
projection)rG   r2   rl   rm   rS   r:   r@   rH   s          €r/   r7   zYolosPatchEmbeddings.__init__Ï   sÔ   ø€ Ü‰ÑÔØ!'×!2Ñ!2°F×4EÑ4E�Jˆ
Ø$*×$7Ñ$7¸×9KÑ9K�kˆä#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ü#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ø! !‘}¨
°1©Ñ5¸*ÀQ¹-È:ÐVWÉ=Ñ:XÑYˆØ$ˆŒØ$ˆŒØ(ˆÔØ&ˆÔäŸ)™) L°+È:Ð^hÔiˆ�r.   rI   r3   c                 ó¼   — |j                   \  }}}}|| j                  k7  rt        d«      ‚| j                  |«      j	                  d«      j                  dd«      }|S )NzeMake sure that the channel dimension of the pixel values match with the one set in the configuration.rf   r   )rN   rS   Ú
ValueErrorr�   rq   rk   )rG   rI   rR   rS   rT   rU   rV   s          r/   rZ   zYolosPatchEmbeddings.forwardÞ   sb   € Ø2>×2DÑ2DÑ/ˆ
�L &¨%Ø˜4×,Ñ,Ò,ÜØwóð ð —_‘_ \Ó2×:Ñ:¸1Ó=×GÑGÈÈ1ÓMˆ
ØÐr.   )	r&   r'   r(   r)   r7   r*   r\   rZ   r]   r^   s   @r/   r>   r>   È   s)   ø„ ñôjð E§L¡Lð °U·\±\÷ r.   r>   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingrD   c                 óÀ  — t        j                  ||j                  dd«      «      |z  }t        j                  j                  |dt         j                  ¬«      j                  |j                  «      }t        j                  j                  ||| j                  ¬«      }|�||z  }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )NrK   éþÿÿÿ)rM   Údtype)ÚpÚtrainingr   rf   )r*   Úmatmulrk   r   ro   ÚsoftmaxÚfloat32Útorš   rD   rœ   rƒ   )
r’   r“   r”   r•   r–   r—   rD   ÚkwargsÚattn_weightsÚattn_outputs
             r/   Úeager_attention_forwardr¤   ê   sÀ   € ô —<‘<  s§}¡}°R¸Ó'<Ó=ÀÑG€Lô —=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€Lô —=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lð Ð!Ø# nÑ4ˆä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r.   c            
       óè   ‡ — e Zd Zdeddfˆ fd„Zdej                  dej                  fd„Z	 d
deej                     de	de
eej                  ej                  f   eej                     f   fd	„Zˆ xZS )ÚYolosSelfAttentionr2   r3   Nc                 ó2  •— t         ‰| �  «        |j                  |j                  z  dk7  r2t	        |d«      s&t        d|j                  › d|j                  › d�«      ‚|| _        |j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _	        |j                  | _        | j                  dz  | _        d| _        t        j                  |j                  | j                  |j                   ¬«      | _        t        j                  |j                  | j                  |j                   ¬«      | _        t        j                  |j                  | j                  |j                   ¬«      | _        y )	Nr   Úembedding_sizezThe hidden size z4 is not a multiple of the number of attention heads ú.g      à¿F)Úbias)r6   r7   r:   Únum_attention_headsÚhasattrr‘   r2   ÚintÚattention_head_sizeÚall_head_sizeÚattention_probs_dropout_probÚdropout_probr—   Ú	is_causalr   ÚLinearÚqkv_biasr“   r”   r•   rc   s     €r/   r7   zYolosSelfAttention.__init__
  sF  ø€ Ü‰ÑÔØ×Ñ × :Ñ :Ñ:¸aÒ?ÌÐPVÐXhÔHiÜØ" 6×#5Ñ#5Ð"6ð 7Ø×3Ñ3Ð4°Að7óð ð
 ˆŒØ#)×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔØ"×?Ñ?ˆÔØ×/Ñ/°Ñ5ˆŒØˆŒä—Y‘Y˜v×1Ñ1°4×3EÑ3EÈFÏOÉOÔ\ˆŒ
Ü—9‘9˜V×/Ñ/°×1CÑ1CÈ&Ï/É/ÔZˆŒÜ—Y‘Y˜v×1Ñ1°4×3EÑ3EÈFÏOÉOÔ\ˆ�
r.   Úxc                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  dddd«      S )NrK   r   rf   r   r   )rO   r«   r®   rn   Úpermute)rG   rµ   Únew_x_shapes      r/   Útranspose_for_scoresz'YolosSelfAttention.transpose_for_scores  sL   € Ø—f‘f“h˜s �m t×'?Ñ'?À×AYÑAYÐ&ZÑZˆØ�F‰F�;ÓˆØ�y‰y˜˜A˜q !Ó$Ð$r.   Ú	head_maskÚoutput_attentionsc           
      ó˜  — | j                  | j                  |«      «      }| j                  | j                  |«      «      }| j                  | j                  |«      «      }t        }| j
                  j                  dk7  rN| j
                  j                  dk(  r|rt        j                  d«       nt        | j
                  j                     } || ||||| j                  | j                  | j                  sdn| j                  ¬«      \  }}	|j                  «       d d | j                  fz   }
|j!                  |
«      }|r||	f}|S |f}|S )NÚeagerÚsdpazã`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.ç        )r²   r—   rD   r™   )r¹   r”   r•   r“   r¤   r2   Ú_attn_implementationÚloggerÚwarning_oncer   r²   r—   rœ   r±   rO   r¯   Úreshape)rG   r$   rº   r»   Ú	key_layerÚvalue_layerÚquery_layerÚattention_interfaceÚcontext_layerÚattention_probsÚnew_context_layer_shapeÚoutputss               r/   rZ   zYolosSelfAttention.forward#  s=  € ð ×-Ñ-¨d¯h©h°}Ó.EÓFˆ	Ø×/Ñ/°·
±
¸=Ó0IÓJˆØ×/Ñ/°·
±
¸=Ó0IÓJˆä(?ÐØ�;‰;×+Ñ+¨wÒ6Ø�{‰{×/Ñ/°6Ò9Ñ>OÜ×#Ñ#ðLõô
 '>¸d¿k¹k×>^Ñ>^Ñ&_Ð#á)<ØØØØØØ—n‘nØ—L‘LØ#Ÿ}š}‘C°$×2CÑ2Cô	*
Ñ&ˆ�ð #0×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×-Ñ-Ð.EÓFˆá6G�= /Ð2ˆàˆð O\ÐM]ˆàˆr.   ©NF)r&   r'   r(   r   r7   r*   r\   r¹   r   Úboolr
   r	   rZ   r]   r^   s   @r/   r¦   r¦   	  s†   ø„ ð]˜{ð ]¨tõ ]ð(% e§l¡lð %°u·|±|ó %ð bgñ!Ø(0°·±Ñ(>ð!ØZ^ð!à	ˆu�U—\‘\ 5§<¡<Ð/Ñ0°%¸¿¹Ñ2EÐEÑ	F÷!r.   r¦   c                   ó|   ‡ — e Zd ZdZdeddfˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZ	S )	ÚYolosSelfOutputz¢
    The residual connection is defined in YolosLayer instead of here (as is the case with other models), due to the
    layernorm applied before each block.
    r2   r3   Nc                 óÈ   •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  «      | _        y ra   )	r6   r7   r   r³   r:   ÚdenserB   rC   rD   rc   s     €r/   r7   zYolosSelfOutput.__init__N  sB   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r.   r$   Úinput_tensorc                 óJ   — | j                  |«      }| j                  |«      }|S ra   ©rÑ   rD   ©rG   r$   rÒ   s      r/   rZ   zYolosSelfOutput.forwardS  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆàÐr.   r[   r^   s   @r/   rÏ   rÏ   H  sD   ø„ ñð
>˜{ð >¨tõ >ð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r.   rÏ   c                   óà   ‡ — e Zd Zdeddfˆ fd„Zdee   ddfd„Z	 	 ddej                  de
ej                     d	edeeej                  ej                  f   eej                     f   fd
„Zˆ xZS )ÚYolosAttentionr2   r3   Nc                 ó€   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        t        «       | _        y ra   )r6   r7   r¦   Ú	attentionrÏ   ÚoutputÚsetÚpruned_headsrc   s     €r/   r7   zYolosAttention.__init__\  s0   ø€ Ü‰ÑÔÜ+¨FÓ3ˆŒÜ% fÓ-ˆŒÜ›EˆÕr.   Úheadsc                 ó>  — t        |«      dk(  ry t        || j                  j                  | j                  j                  | j
                  «      \  }}t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _	        t        | j                  j                  |d¬«      | j                  _        | j                  j                  t        |«      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |«      | _        y )Nr   r   rL   )Úlenr   rÙ   r«   r®   rÜ   r   r“   r”   r•   rÚ   rÑ   r¯   Úunion)rG   rÝ   Úindexs      r/   Úprune_headszYolosAttention.prune_headsb  s  € Üˆu‹:˜Š?ØÜ7Ø�4—>‘>×5Ñ5°t·~±~×7YÑ7YÐ[_×[lÑ[ló
‰ˆˆuô
  2°$·.±.×2FÑ2FÈÓNˆ�‰ÔÜ/°·±×0BÑ0BÀEÓJˆ�‰ÔÜ1°$·.±.×2FÑ2FÈÓNˆ�‰ÔÜ.¨t¯{©{×/@Ñ/@À%ÈQÔOˆ�‰Ôð .2¯^©^×-OÑ-OÔRUÐV[ÓR\Ñ-\ˆ�‰Ô*Ø'+§~¡~×'IÑ'IÈDÏNÉN×LnÑLnÑ'nˆ�‰Ô$Ø ×-Ñ-×3Ñ3°EÓ:ˆÕr.   r$   rº   r»   c                 óh   — | j                  |||«      }| j                  |d   |«      }|f|dd  z   }|S )Nr   r   )rÙ   rÚ   )rG   r$   rº   r»   Úself_outputsÚattention_outputrË   s          r/   rZ   zYolosAttention.forwardt  sE   € ð —~‘~ m°YÐ@QÓRˆàŸ;™; |°A¡¸ÓFÐà#Ð%¨°Q°RÐ(8Ñ8ˆØˆr.   rÌ   )r&   r'   r(   r   r7   r   r­   râ   r*   r\   r   rÍ   r
   r	   rZ   r]   r^   s   @r/   r×   r×   [  s’   ø„ ð"˜{ð "¨tõ "ð;  S¡ð ;¨dó ;ð* -1Ø"'ñ	à—|‘|ðð ˜EŸL™LÑ)ðð  ð	ð
 
ˆu�U—\‘\ 5§<¡<Ð/Ñ0°%¸¿¹Ñ2EÐEÑ	F÷r.   r×   c                   ó`   ‡ — e Zd Zdeddfˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚYolosIntermediater2   r3   Nc                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y ra   )r6   r7   r   r³   r:   Úintermediate_sizerÑ   rŠ   Ú
hidden_actÚstrr   Úintermediate_act_fnrc   s     €r/   r7   zYolosIntermediate.__init__„  s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r.   r$   c                 óJ   — | j                  |«      }| j                  |«      }|S ra   )rÑ   rì   )rG   r$   s     r/   rZ   zYolosIntermediate.forwardŒ  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆàÐr.   ©	r&   r'   r(   r   r7   r*   r\   rZ   r]   r^   s   @r/   rç   rç   ƒ  s1   ø„ ð9˜{ð 9¨tõ 9ð U§\¡\ð °e·l±l÷ r.   rç   c                   óx   ‡ — e Zd Zdeddfˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZS )ÚYolosOutputr2   r3   Nc                 óÈ   •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j                  «      | _	        y ra   )
r6   r7   r   r³   ré   r:   rÑ   rB   rC   rD   rc   s     €r/   r7   zYolosOutput.__init__•  sB   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r.   r$   rÒ   c                 óT   — | j                  |«      }| j                  |«      }||z   }|S ra   rÔ   rÕ   s      r/   rZ   zYolosOutput.forwardš  s.   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆà%¨Ñ4ˆàÐr.   rî   r^   s   @r/   rð   rð   ”  s?   ø„ ð>˜{ð >¨tõ >ð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r.   rð   c                   óÎ   ‡ — e Zd ZdZdeddfˆ fd„Z	 	 d
dej                  deej                     de	de
eej                  ej                  f   eej                     f   fd	„Zˆ xZS )Ú
YolosLayerz?This corresponds to the Block class in the timm implementation.r2   r3   Nc                 ór  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        t        |«      | _        t        |«      | _	        t        j                  |j                  |j                  ¬«      | _        t        j                  |j                  |j                  ¬«      | _        y )Nr   ©Úeps)r6   r7   Úchunk_size_feed_forwardÚseq_len_dimr×   rÙ   rç   Úintermediaterð   rÚ   r   Ú	LayerNormr:   Úlayer_norm_epsÚlayernorm_beforeÚlayernorm_afterrc   s     €r/   r7   zYolosLayer.__init__§  s‡   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ'¨Ó/ˆŒÜ-¨fÓ5ˆÔÜ! &Ó)ˆŒÜ "§¡¨V×-?Ñ-?ÀV×EZÑEZÔ [ˆÔÜ!Ÿ|™|¨F×,>Ñ,>ÀF×DYÑDYÔZˆÕr.   r$   rº   r»   c                 óÞ   — | j                  | j                  |«      ||¬«      }|d   }|dd  }||z   }| j                  |«      }| j                  |«      }| j	                  ||«      }|f|z   }|S )N)r»   r   r   )rÙ   rý   rþ   rú   rÚ   )rG   r$   rº   r»   Úself_attention_outputsrå   rË   Úlayer_outputs           r/   rZ   zYolosLayer.forward±  s–   € ð "&§¡Ø×!Ñ! -Ó0ØØ/ð "0ó "
Ðð
 2°!Ñ4ÐØ(¨¨Ð,ˆð )¨=Ñ8ˆð ×+Ñ+¨MÓ:ˆØ×(Ñ(¨Ó6ˆð —{‘{ <°Ó?ˆà�/ GÑ+ˆàˆr.   rÌ   )r&   r'   r(   r)   r   r7   r*   r\   r   rÍ   r
   r	   rZ   r]   r^   s   @r/   rô   rô   ¤  s�   ø„ ÙIð[˜{ð [¨tõ [ð -1Ø"'ñ	à—|‘|ðð ˜EŸL™LÑ)ðð  ð	ð
 
ˆu�U—\‘\ 5§<¡<Ð/Ñ0°%¸¿¹Ñ2EÐEÑ	F÷r.   rô   c                   óŠ   ‡ — e Zd Zdeddfˆ fd„Z	 	 	 	 ddej                  deej                     deded	ede	e
ef   fd
„Zˆ xZS )ÚYolosEncoderr2   r3   Nc                 ó@  •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        d|j                  d   |j                  d   z  |j                  dz  z  z   |j                  z   }|j                  rBt        j                  t        j                   |j                  dz
  d||j"                  «      «      nd | _        |j                  rt'        |«      | _        y d | _        y c c}w )NFr   r   rf   )r6   r7   r2   r   Ú
ModuleListÚrangeÚnum_hidden_layersrô   ÚlayerÚgradient_checkpointingrl   rm   r<   Úuse_mid_position_embeddingsr8   r*   r9   r:   Úmid_position_embeddingsr€   rF   )rG   r2   rX   Ú
seq_lengthrH   s       €r/   r7   zYolosEncoder.__init__Ï  s  ø€ Ü‰ÑÔØˆŒÜ—]‘]ÄÀf×F^ÑF^Ó@_Ö#`¸1¤J¨vÕ$6Ò#`ÓaˆŒ
Ø&+ˆÔ#ð �×"Ñ" 1Ñ%¨×(9Ñ(9¸!Ñ(<Ñ<À×@QÑ@QÐSTÑ@TÑTÑUÐX^×XsÑXsÑsð 	ð ×1Ò1ô �L‰LÜ—‘Ø×,Ñ,¨qÑ0ØØØ×&Ñ&ó	ôð ð 	Ô$ð JP×IkÒIkÔ=¸fÓEˆÕÐquˆÕùò' $as   ½Dr$   rº   r»   Úoutput_hidden_statesÚreturn_dictc                 óP  — |rdnd }|rdnd }	| j                   j                  r| j                  | j                  ||f«      }
t	        | j
                  «      D ]¢  \  }}|r||fz   }|�||   nd }| j                  r+| j                  r| j                  |j                  |||«      }n
 ||||«      }|d   }| j                   j                  r$|| j                   j                  dz
  k  r|
|   z   }|sŒš|	|d   fz   }	Œ¤ |r||fz   }|st        d„ |||	fD «       «      S t        |||	¬«      S )Nr-   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wra   r-   )Ú.0Úvs     r/   ú	<genexpr>z'YolosEncoder.forward.<locals>.<genexpr>  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š)r#   r$   r%   )r2   r
  rF   r  Ú	enumerater  r	  rœ   Ú_gradient_checkpointing_funcÚ__call__r  Útupler   )rG   r$   rT   rU   rº   r»   r  r  Úall_hidden_statesÚall_self_attentionsÚ$interpolated_mid_position_embeddingsÚiÚlayer_moduleÚlayer_head_maskÚlayer_outputss                  r/   rZ   zYolosEncoder.forwardç  sa  € ñ #7™B¸DÐÙ$5™b¸4Ðà�;‰;×2Ò2Ø37×3EÑ3EÀd×FbÑFbÐekÐmrÐdsÓ3tÐ0ä(¨¯©Ó4ò 	P‰OˆAˆ|Ù#Ø$5¸Ð8HÑ$HÐ!à.7Ð.C˜i¨šlÈˆOà×*Ò*¨t¯}ª}Ø $× AÑ AØ ×)Ñ)Ø!Ø#Ø%ó	!‘ñ !-¨]¸OÐM^Ó _�à)¨!Ñ,ˆMà�{‰{×6Ò6Ø˜Ÿ™×5Ñ5¸Ñ9Ò:Ø$1Ð4XÐYZÑ4[Ñ$[�Mâ Ø&9¸]È1Ñ=MÐ<OÑ&OÑ#ð/	Pñ2  Ø 1°]Ð4DÑ DÐáÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmÜØ+Ø+Ø*ô
ð 	
r.   )NFFT)r&   r'   r(   r   r7   r*   r\   r   rÍ   r
   r  r   rZ   r]   r^   s   @r/   r  r  Î  s}   ø„ ðv˜{ð v¨tõ vð: -1Ø"'Ø%*Ø ñ2
à—|‘|ð2
ð
 ˜EŸL™LÑ)ð2
ð  ð2
ð #ð2
ð ð2
ð 
ˆu�oÐ%Ñ	&÷2
r.   r  c                   ó„   — e Zd ZdZeZdZdZdZg Z	dZ
dZdeej                  ej                  ej                   f   ddfd„Zy)	ÚYolosPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚvitrI   Tr’   r3   Nc                 óú  — t        |t        j                  t        j                  f«      rm|j                  j
                  j                  d| j                  j                  ¬«       |j                  �%|j                  j
                  j                  «        yyt        |t        j                  «      rJ|j                  j
                  j                  «        |j                  j
                  j                  d«       yy)zInitialize the weightsr¿   )ÚmeanÚstdNg      ð?)rŠ   r   r³   rŽ   ÚweightÚdataÚnormal_r2   Úinitializer_rangerª   Úzero_rû   Úfill_)rG   r’   s     r/   Ú_init_weightsz"YolosPreTrainedModel._init_weights*  s¨   € ä�fœrŸy™y¬"¯)©)Ð4Ô5ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)ð .r.   )r&   r'   r(   r)   r   Úconfig_classÚbase_model_prefixÚmain_input_nameÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_sdpaÚ_supports_flash_attn_2r
   r   r³   rŽ   rû   r+  r-   r.   r/   r   r     s\   „ ñð
 €LØÐØ$€OØ&*Ð#ØÐØ€NØ!Ðð
* E¨"¯)©)°R·Y±YÀÇÁÐ*LÑ$Mð 
*ÐRVô 
*r.   r   aG  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`YolosConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aM  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`YolosImageProcessor.__call__`] for details.

        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
z_The bare YOLOS Model transformer outputting raw hidden-states without any specific head on top.c                   ó  ‡ — e Zd Zddedefˆ fd„Zdefd„Zdee	e
e	   f   ddfd„Z ee«       eeeed	e¬
«      	 	 	 	 	 ddeej*                     deej*                     dee   dee   dee   deeef   fd„«       «       Zˆ xZS )Ú
YolosModelr2   Úadd_pooling_layerc                 ó  •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        t        j                  |j                  |j                  ¬«      | _        |rt        |«      nd | _        | j                  «        y )Nrö   )r6   r7   r2   r1   rV   r  Úencoderr   rû   r:   rü   Ú	layernormÚYolosPoolerÚpoolerÚ	post_init)rG   r2   r5  rH   s      €r/   r7   zYolosModel.__init__^  si   ø€ Ü‰Ñ˜Ô ØˆŒä)¨&Ó1ˆŒÜ# FÓ+ˆŒäŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÙ->”k &Ô)ÀDˆŒð 	�‰Õr.   r3   c                 ó.   — | j                   j                  S ra   )rV   r?   )rG   s    r/   Úget_input_embeddingszYolosModel.get_input_embeddingsk  s   € Ø�‰×/Ñ/Ð/r.   Úheads_to_pruneNc                 ó˜   — |j                  «       D ]7  \  }}| j                  j                  |   j                  j	                  |«       Œ9 y)a	  
        Prunes heads of the model.

        Args:
            heads_to_prune (`dict`):
                See base class `PreTrainedModel`. The input dictionary must have the following format: {layer_num:
                list of heads to prune in this layer}
        N)Úitemsr7  r  rÙ   râ   )rG   r>  r  rÝ   s       r/   Ú_prune_headszYolosModel._prune_headsn  sE   € ð +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr.   Úvision)Ú
checkpointÚoutput_typer,  ÚmodalityÚexpected_outputrI   rº   r»   r  r  c           	      ó`  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|€t	        d«      ‚| j                  || j                   j                  «      }| j                  |«      }| j                  ||j                  d   |j                  d   ||||¬«      }|d   }| j                  |«      }| j                  �| j                  |«      nd }	|s|	�||	fn|f}
|
|dd  z   S t        ||	|j                  |j                  ¬«      S )Nz You have to specify pixel_valuesr™   rK   )rT   rU   rº   r»   r  r  r   r   )r#   Úpooler_outputr$   r%   )r2   r»   r  Úuse_return_dictr‘   Úget_head_maskr  rV   r7  rN   r8  r:  r   r$   r%   )rG   rI   rº   r»   r  r  Úembedding_outputÚencoder_outputsÚsequence_outputÚpooled_outputÚhead_outputss              r/   rZ   zYolosModel.forwardz  sR  € ð  2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÜÐ?Ó@Ð@ð ×&Ñ& y°$·+±+×2OÑ2OÓPˆ	àŸ?™?¨<Ó8ÐàŸ,™,ØØ×%Ñ% bÑ)Ø×$Ñ$ RÑ(ØØ/Ø!5Ø#ð 'ó 
ˆð *¨!Ñ,ˆØŸ.™.¨Ó9ˆØ8<¿¹Ð8O˜Ÿ™ OÔ4ÐUYˆáØ?LÐ?X˜O¨]Ñ;Ð_nÐ^pˆLØ /°!°"Ð"5Ñ5Ð5ä)Ø-Ø'Ø)×7Ñ7Ø&×1Ñ1ô	
ð 	
r.   )T)NNNNN)r&   r'   r(   r   rÍ   r7   r>   r=  r   r­   r   rA  r   ÚYOLOS_INPUTS_DOCSTRINGr   Ú_CHECKPOINT_FOR_DOCr   Ú_CONFIG_FOR_DOCÚ_EXPECTED_OUTPUT_SHAPEr   r*   r\   r
   r	   rZ   r]   r^   s   @r/   r4  r4  Y  sñ   ø„ ñ
˜{ð ¸tõ ð0Ð&:ó 0ð
C¨4°°T¸#±Y°Ñ+?ð 
CÀDó 
Cñ +Ð+AÓBÙØ&Ø.Ø$ØØ.ôð 04Ø,0Ø,0Ø/3Ø&*ñ0
à˜uŸ|™|Ñ,ð0
ð ˜EŸL™LÑ)ð0
ð $ D™>ð	0
ð
 ' t™nð0
ð ˜d‘^ð0
ð 
ˆuÐ0Ð0Ñ	1ò0
óó Cô0
r.   r4  c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )r9  r2   c                 ó²   •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  «       | _        y ra   )r6   r7   r   r³   r:   rÑ   ÚTanhÚ
activationrc   s     €r/   r7   zYolosPooler.__init__¶  s9   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ'™'›)ˆ�r.   c                 ó\   — |d d …df   }| j                  |«      }| j                  |«      }|S )Nr   )rÑ   rW  )rG   r$   Úfirst_token_tensorrN  s       r/   rZ   zYolosPooler.forward»  s6   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð#5Ó6ˆØŸ™¨Ó6ˆØÐr.   )r&   r'   r(   r   r7   rZ   r]   r^   s   @r/   r9  r9  µ  s   ø„ ð$˜{õ $ö
r.   r9  c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚYolosMLPPredictionHeada  
    Very simple multi-layer perceptron (MLP, also called FFN), used to predict the normalized center coordinates,
    height and width of a bounding box w.r.t. an image.

    Copied from https://github.com/facebookresearch/detr/blob/master/models/detr.py

    c                 ó¨   •— t         ‰| �  «        || _        |g|dz
  z  }t        j                  d„ t        |g|z   ||gz   «      D «       «      | _        y )Nr   c              3   óN   K  — | ]  \  }}t        j                  ||«      –— Œ y ­wra   )r   r³   )r  ÚnÚks      r/   r  z2YolosMLPPredictionHead.__init__.<locals>.<genexpr>Ò  s   è ø€ Ò#g¹¸¸1¤B§I¡I¨a°§OÑ#gùs   ‚#%)r6   r7   Ú
num_layersr   r  ÚzipÚlayers)rG   Ú	input_dimÚ
hidden_dimÚ
output_dimr`  ÚhrH   s         €r/   r7   zYolosMLPPredictionHead.__init__Î  sS   ø€ Ü‰ÑÔØ$ˆŒØˆL˜J¨™NÑ+ˆÜ—m‘mÑ#gÄÀYÀKÐRSÁOÐUVÐZdÐYeÑUeÓ@fÔ#gÓgˆ�r.   c                 ó¾   — t        | j                  «      D ]D  \  }}|| j                  dz
  k  r%t        j                  j                   ||«      «      n ||«      }ŒF |S r5   )r  rb  r`  r   ro   Úrelu)rG   rµ   r  r  s       r/   rZ   zYolosMLPPredictionHead.forwardÔ  sT   € Ü! $§+¡+Ó.ò 	V‰HˆAˆuØ01°D·O±OÀaÑ4GÒ0G”—‘×"Ñ"¡5¨£8Ô,ÉUÐSTËX‰Að	Vàˆr.   )r&   r'   r(   r)   r7   rZ   r]   r^   s   @r/   r[  r[  Å  s   ø„ ñôhör.   r[  zy
    YOLOS Model (consisting of a ViT encoder) with object detection heads on top, for tasks such as COCO detection.
    c                   óò   ‡ — e Zd Zdefˆ fd„Zej                  j                  d„ «       Z e	e
«       eee¬«      	 	 	 	 ddej                  deee      dee   dee   d	ee   d
eeef   fd„«       «       Zˆ xZS )ÚYolosForObjectDetectionr2   c                 ó"  •— t         ‰| �  |«       t        |d¬«      | _        t	        |j
                  |j
                  |j                  dz   d¬«      | _        t	        |j
                  |j
                  dd¬«      | _        | j                  «        y )NF)r5  r   r   )rc  rd  re  r`  é   )
r6   r7   r4  r!  r[  r:   Ú
num_labelsÚclass_labels_classifierÚbbox_predictorr;  rc   s     €r/   r7   z YolosForObjectDetection.__init__á  s„   ø€ Ü‰Ñ˜Ô ô ˜f¸Ô>ˆŒô (>Ø×(Ñ(°V×5GÑ5GÐTZ×TeÑTeÐhiÑTiÐvwô(
ˆÔ$ô 5Ø×(Ñ(°V×5GÑ5GÐTUÐbcô
ˆÔð
 	�‰Õr.   c                 ó^   — t        |d d |d d «      D ��cg c]
  \  }}||dœ‘Œ c}}S c c}}w )NrK   )r    r!   )ra  )rG   Úoutputs_classÚoutputs_coordÚaÚbs        r/   Ú_set_aux_lossz%YolosForObjectDetection._set_aux_lossô  s9   € ô
 <?¸}ÈSÈbÐ?QÐS`ÐadÐbdÐSeÓ;f×g±4°1°a˜1¨AÓ.ÓgÐgùÓgs   –))rD  r,  rI   Úlabelsr»   r  r  r3   c           
      óÞ  — |�|n| j                   j                  }| j                  ||||¬«      }|d   }|dd…| j                   j                   d…dd…f   }| j	                  |«      }| j                  |«      j                  «       }	d\  }
}}|��d\  }}| j                   j                  rC|r|j                  n|d   }| j	                  |«      }| j                  |«      j                  «       }| j                  ||| j                  |	| j                   ||«      \  }
}}|s|�||	f|z   |z   }n||	f|z   }|
�|
|f|z   S |S t        |
|||	||j                  |j                  |j                  ¬«      S )a!	  
        labels (`List[Dict]` of len `(batch_size,)`, *optional*):
            Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the
            following 2 keys: `'class_labels'` and `'boxes'` (the class labels and bounding boxes of an image in the
            batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding
            boxes in the image,)` and the boxes a `torch.FloatTensor` of shape `(number of bounding boxes in the image,
            4)`.

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoModelForObjectDetection
        >>> import torch
        >>> from PIL import Image
        >>> import requests

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> image_processor = AutoImageProcessor.from_pretrained("hustvl/yolos-tiny")
        >>> model = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny")

        >>> inputs = image_processor(images=image, return_tensors="pt")
        >>> outputs = model(**inputs)

        >>> # 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)[
        ...     0
        ... ]

        >>> for score, label, box in zip(results["scores"], results["labels"], results["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}"
        ...     )
        Detected remote with confidence 0.991 at location [46.48, 72.78, 178.98, 119.3]
        Detected remote with confidence 0.908 at location [336.48, 79.27, 368.23, 192.36]
        Detected cat with confidence 0.934 at location [337.18, 18.06, 638.14, 373.09]
        Detected cat with confidence 0.979 at location [10.93, 53.74, 313.41, 470.67]
        Detected remote with confidence 0.974 at location [41.63, 72.23, 178.09, 119.99]
        ```N)r»   r  r  r   )NNN)NNrl  )r   r   r    r!   r"   r#   r$   r%   )r2   rI  r!  r<   rn  ro  ÚsigmoidÚauxiliary_lossÚintermediate_hidden_statesÚloss_functionÚdevicer   r#   r$   r%   )rG   rI   rv  r»   r  r  rË   rM  r    r!   r   r   r"   rq  rr  rú   rÚ   s                    r/   rZ   zYolosForObjectDetection.forwardû  s¯  € ðn &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆð —(‘(ØØ/Ø!5Ø#ð	 ó 
ˆð " !™*ˆð *ª!¨d¯k©k×.NÑ.NÐ-NÑ-PÒRSÐ*SÑTˆð ×-Ñ-¨oÓ>ˆØ×(Ñ(¨Ó9×AÑAÓCˆ
à-=Ñ*ˆˆiÐ*ØÐØ+5Ñ(ˆM˜=Ø�{‰{×)Ò)ÙEP˜w×AÒAÐV]Ð^_ÑV`�Ø $× <Ñ <¸\Ó J�Ø $× 3Ñ 3°LÓ A× IÑ IÓ K�Ø15×1CÑ1CØ˜ §¡¨Z¸¿¹ÀmÐUbó2Ñ.ˆD�)Ð.ñ Ø Ð,Ø  *Ð-Ð0AÑAÀGÑK‘à  *Ð-°Ñ7�Ø37Ð3C�T˜9Ð%¨Ñ.ÐOÈÐOä)ØØØØ!Ø/Ø%×7Ñ7Ø!×/Ñ/Ø×)Ñ)ô	
ð 		
r.   )NNNN)r&   r'   r(   r   r7   r*   ÚjitÚunusedru  r   rP  r   r   rR  r+   r   r   r   rÍ   r
   r	   rZ   r]   r^   s   @r/   rj  rj  Ú  sÒ   ø„ ð˜{õ ð& ‡Y�Y×Ññhó ðhñ +Ð+AÓBÙÐ+EÐTcÔdð (,Ø,0Ø/3Ø&*ñc
à×'Ñ'ðc
ð ˜˜d™Ñ$ðc
ð $ D™>ð	c
ð
 ' t™nðc
ð ˜d‘^ðc
ð 
ˆuÐ0Ð0Ñ	1òc
ó eó Côc
r.   rj  )rj  r4  r   )r¿   )Br)   Úcollections.abcr‹   Údataclassesr   Útypingr   r   r   r   r   r	   r
   r*   Útorch.utils.checkpointr   Úactivationsr   Úmodeling_outputsr   r   Úmodeling_utilsr   r   Úpytorch_utilsr   r   Úutilsr   r   r   r   r   r   Úconfiguration_yolosr   Ú
get_loggerr&   rÁ   rR  rQ  rS  r   ÚModuler1   rE   r€   r>   r\   Úfloatr¤   r¦   rÏ   r×   rç   rð   rô   r  r   ÚYOLOS_START_DOCSTRINGrP  r4  r9  r[  rj  Ú__all__r-   r.   r/   ú<module>rŽ     s>  ðñ ã Ý !ß D× DÑ Dã Û Ý å !ß Kß Fß Q÷÷ õ -ð 
ˆ×	Ñ	˜HÓ	%€ð  €ð +Ð Ú'Ð ð ô): ó ):ó ð):ôX(�b—i‘iô (ôV¨2¯9©9ô ô: r§y¡yô ôB˜2Ÿ9™9ô ðR ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 ˜UŸ\™\Ñ*ð%ð ð%ð ó%ô>;˜Ÿ™ô ;ô~�b—i‘iô ô&$�R—Y‘Yô $ôP˜Ÿ	™	ô ô"�"—)‘)ô ô '�—‘ô 'ôTK
�2—9‘9ô K
ô\*˜?ô *ð6	Ð ðÐ ñ. ØeØóôU
Ð%ó U
ó	ðU
ôp�"—)‘)ô ô ˜RŸY™Yô ñ* ðð ó	ô@
Ð2ó @
óð@
òF L�r.   