Ë
    ÜÓ^jÇl  ã                   óD  — d Z ddlZddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZmZmZmZ ddlmZmZ ddlmZ ddlmZ  ej@                  e!«      Z" ed¬«      e G d„ de«      «       «       Z# G d„ dejH                  «      Z% G d„ dejH                  «      Z& G d„ dejH                  «      Z' G d„ dejH                  «      Z(	 	 d@dejH                  dejR                  dejR                  d ejR                  d!ejR                  dz  d"e*dz  d#e*d$ee   fd%„Z+ G d&„ d'ejH                  «      Z, G d(„ d)ejH                  «      Z- G d*„ d+ejH                  «      Z. G d,„ d-ejH                  «      Z/ G d.„ d/ejH                  «      Z0 G d0„ d1e«      Z1 G d2„ d3ejH                  «      Z2e G d4„ d5e«      «       Z3e G d6„ d7e3«      «       Z4 G d8„ d9ejH                  «      Z5 G d:„ d;ejH                  «      Z6 ed<¬«       G d=„ d>e3«      «       Z7g d?¢Z8y)AzPyTorch YOLOS model.é    N)ÚCallable)Ú	dataclass)Únné   )ÚACT2FN)ÚGradientCheckpointingLayer)ÚBaseModelOutputÚBaseModelOutputWithPooling)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)ÚModelOutputÚTransformersKwargsÚauto_docstringÚlogging)Úcan_return_tupleÚmerge_with_config_defaults)Úcapture_outputsé   )ÚYolosConfigz5
    Output type of [`YolosForObjectDetection`].
    )Úcustom_introc                   ó<  — e Zd ZU dZdZej                  dz  ed<   dZe	dz  ed<   dZ
ej                  dz  ed<   dZej                  dz  ed<   dZee	   dz  ed<   dZej                  dz  ed<   dZeej                     dz  ed	<   dZeej                     dz  ed
<   y)ÚYolosObjectDetectionOutputa2  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` are provided)):
        Total loss as a linear combination of a negative log-likelihood (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 auxiliary 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.
    NÚlossÚ	loss_dictÚlogitsÚ
pred_boxesÚauxiliary_outputsÚlast_hidden_stateÚhidden_statesÚ
attentions)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtorchÚFloatTensorÚ__annotations__r   Údictr   r   r   Úlistr   r    Útupler!   © ó    ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/yolos/modeling_yolos.pyr   r   %   s´   … ñð, &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø!€Iˆt�d‰{Ó!Ø'+€FˆE×Ñ Ñ$Ó+Ø+/€J�×!Ñ! DÑ(Ó/Ø+/Ð�t˜D‘z DÑ(Ó/Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ô6r-   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Úinterpolationr1   )Úselfr1   r?   Ú	__class__s      €r.   r6   zYolosEmbeddings.__init__R   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&   ÚcatrE   r@   rC   )rF   rH   Ú
batch_sizeÚnum_channelsÚheightÚwidthÚ
embeddingsÚseq_lenÚ_Ú
cls_tokensr<   r@   s               r.   ÚforwardzYolosEmbeddings.forwarda   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   r6   r&   ÚTensorrY   Ú__classcell__©rG   s   @r.   r0   r0   L   s6   ø„ ñð
˜{ð ¨tõ ð E§L¡Lð °U·\±\÷ r-   r0   c                   óB   ‡ — e Zd Zdˆ fd„Zddej
                  fd„Zˆ xZS )rD   r2   c                 ó0   •— t         ‰| �  «        || _        y ©N©r5   r6   r1   ©rF   r1   rG   s     €r.   r6   z-InterpolateInitialPositionEmbeddings.__init__w   ó   ø€ Ü‰ÑÔØˆ�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©rN   ÚmodeÚalign_cornersrK   )r1   r;   Ú	transposerM   Ú
image_sizeÚ
patch_sizeÚviewr   Ú
functionalÚinterpolateÚflattenr&   rP   )rF   Ú	pos_embedÚimg_sizeÚcls_pos_embedÚdet_pos_embedÚpatch_pos_embedrQ   r9   rV   Úpatch_heightÚpatch_widthrS   rT   Únew_patch_heightÚnew_patch_widthÚscale_pos_embeds                   r.   rY   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-   ©r2   N©)i   i@  ©r"   r#   r$   r6   r&   r[   rY   r\   r]   s   @r.   rD   rD   v   s   ø„ õñ¸%¿,¹,÷ r-   rD   c                   óB   ‡ — e Zd Zdˆ fd„Zddej
                  fd„Zˆ xZS )Ú InterpolateMidPositionEmbeddingsr2   c                 ó0   •— t         ‰| �  «        || _        y r`   ra   rb   s     €r.   r6   z)InterpolateMidPositionEmbeddings.__init__”   rc   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   re   r   rf   Frg   rK   )r1   r;   rj   rM   rk   rl   rm   r   rn   ro   rp   Ú
contiguousr&   rP   )rF   rq   rr   rs   rt   ru   ÚdepthrQ   r9   rV   rv   rw   rS   rT   rx   ry   rz   s                    r.   rY   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)r5   r6   rk   rl   rR   r9   Ú
isinstanceÚcollectionsÚabcÚIterabler?   r   ÚConv2dÚ
projection)rF   r1   rk   rl   rR   r9   r?   rG   s          €r.   r6   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-   rH   r2   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.re   r   )rM   rR   Ú
ValueErrorr�   rp   rj   )rF   rH   rQ   rR   rS   rT   rU   s          r.   rY   zYolosPatchEmbeddings.forwardÊ   sb   € Ø2>×2DÑ2DÑ/ˆ
�L &¨%Ø˜4×,Ñ,Ò,ÜØwóð ð —_‘_ \Ó2×:Ñ:¸1Ó=×GÑGÈÈ1ÓMˆ
ØÐr-   )	r"   r#   r$   r%   r6   r&   r[   rY   r\   r]   s   @r.   r=   r=   ´   s)   ø„ ñôjð E§L¡Lð °U·\±\÷ r-   r=   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingrC   Úkwargsc                 óœ  — |€|j                  d«      dz  }t        j                  ||j                  dd«      «      |z  }|�||z   }t        j
                  j                  |d¬«      }t        j
                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )NrJ   ç      à¿re   r   rK   )ÚpÚtrainingr   )
rN   r&   Úmatmulrj   r   rn   ÚsoftmaxrC   rš   r‚   )
r�   r‘   r’   r“   r”   r•   rC   r–   Úattn_weightsÚattn_outputs
             r.   Úeager_attention_forwardrŸ   Ö   s»   € ð €Ø—*‘*˜R“. DÑ(ˆô —<‘<  s§}¡}°Q¸Ó':Ó;¸gÑE€LàÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2Ð(Ó>€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r-   c                   ó„   ‡ — e Zd Zdefˆ fd„Zdej                  dee   de	ej                  ej                  f   fd„Z
ˆ xZS )ÚYolosSelfAttentionr1   c                 ó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 ú.r˜   F)Úbias)r5   r6   r9   Únum_attention_headsÚhasattrr�   r1   ÚintÚattention_head_sizeÚall_head_sizeÚattention_probs_dropout_probÚdropout_probr•   Ú	is_causalr   ÚLinearÚqkv_biasr‘   r’   r“   rb   s     €r.   r6   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-   r    r–   r2   c                 ó°  — |j                   d   }|d| j                  | j                  f} | j                  |«      j                  |Ž j                  dd«      } | j                  |«      j                  |Ž j                  dd«      } | j                  |«      j                  |Ž j                  dd«      }t        j                  | j                  j                  t        «      } || |||d f| j                  | j                  | j                  sdn| j                   dœ|¤Ž\  }	}
|	j#                  «       d d | j$                  fz   }|	j'                  |«      }	|	|
fS )Nr   rJ   r   re   ç        )r­   r•   rC   éþÿÿÿ)rM   r¦   r©   r’   rm   rj   r“   r‘   r   Úget_interfacer1   Ú_attn_implementationrŸ   r­   r•   rš   r¬   rN   rª   Úreshape)rF   r    r–   rQ   Ú	new_shapeÚ	key_layerÚvalue_layerÚquery_layerÚattention_interfaceÚcontext_layerÚattention_probsÚnew_context_layer_shapes               r.   rY   zYolosSelfAttention.forward  sY  € ð
 #×(Ñ(¨Ñ+ˆ
Ø  D×$<Ñ$<¸d×>VÑ>VÐVˆ	à0�D—H‘H˜]Ó+×0Ñ0°)Ð<×FÑFÀqÈ!ÓLˆ	Ø4�d—j‘j Ó/×4Ñ4°iÐ@×JÑJÈ1ÈaÓPˆØ4�d—j‘j Ó/×4Ñ4°iÐ@×JÑJÈ1ÈaÓPˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ *=ØØØØØð
*
ð —n‘nØ—L‘LØ#Ÿ}š}‘C°$×2CÑ2Cñ
*
ð ñ
*
Ñ&ˆ�ð #0×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×-Ñ-Ð.EÓFˆà˜oÐ-Ð-r-   )r"   r#   r$   r   r6   r&   r[   r   r   r+   rY   r\   r]   s   @r.   r¡   r¡   ó   sN   ø„ ð]˜{õ ]ð(.à—|‘|ð.ð Ð+Ñ,ð.ð 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷	.r-   r¡   c                   óx   ‡ — e Zd ZdZde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.
    r1   c                 óÈ   •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  «      | _        y r`   )	r5   r6   r   r®   r9   ÚdenserA   rB   rC   rb   s     €r.   r6   zYolosSelfOutput.__init__1  sB   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r-   r    Úinput_tensorr2   c                 óJ   — | j                  |«      }| j                  |«      }|S r`   ©rÁ   rC   ©rF   r    rÂ   s      r.   rY   zYolosSelfOutput.forward6  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØÐr-   rZ   r]   s   @r.   r¿   r¿   +  s=   ø„ ñð
>˜{õ >ð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r-   r¿   c                   óf   ‡ — e Zd Zdefˆ fd„Zdej                  dee   dej                  fd„Z	ˆ xZ
S )ÚYolosAttentionr1   c                 ób   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        y r`   )r5   r6   r¡   Ú	attentionr¿   Úoutputrb   s     €r.   r6   zYolosAttention.__init__>  s&   ø€ Ü‰ÑÔÜ+¨FÓ3ˆŒÜ% fÓ-ˆ�r-   r    r–   r2   c                 óV   —  | j                   |fi |¤Ž\  }}| j                  ||«      }|S r`   )rÉ   rÊ   )rF   r    r–   Úself_attn_outputrW   rÊ   s         r.   rY   zYolosAttention.forwardC  s5   € ð
 -˜dŸn™n¨]ÑE¸fÑEÑÐ˜!Ø—‘Ð-¨}Ó=ˆØˆr-   )r"   r#   r$   r   r6   r&   r[   r   r   rY   r\   r]   s   @r.   rÇ   rÇ   =  s>   ø„ ð.˜{õ .ð
à—|‘|ðð Ð+Ñ,ðð 
�‰÷	r-   rÇ   c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚYolosIntermediater1   c                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y r`   )r5   r6   r   r®   r9   Úintermediate_sizerÁ   rˆ   Ú
hidden_actÚstrr   Úintermediate_act_fnrb   s     €r.   r6   zYolosIntermediate.__init__O  s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r-   r    r2   c                 óJ   — | j                  |«      }| j                  |«      }|S r`   )rÁ   rÓ   )rF   r    s     r.   rY   zYolosIntermediate.forwardW  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr-   ©	r"   r#   r$   r   r6   r&   r[   rY   r\   r]   s   @r.   rÎ   rÎ   N  s*   ø„ ð9˜{õ 9ð U§\¡\ð °e·l±l÷ r-   rÎ   c                   ót   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZS )ÚYolosOutputr1   c                 óÈ   •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j                  «      | _	        y r`   )
r5   r6   r   r®   rÐ   r9   rÁ   rA   rB   rC   rb   s     €r.   r6   zYolosOutput.__init___  sB   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r-   r    rÂ   r2   c                 óT   — | j                  |«      }| j                  |«      }||z   }|S r`   rÄ   rÅ   s      r.   rY   zYolosOutput.forwardd  s.   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØ%¨Ñ4ˆØÐr-   rÕ   r]   s   @r.   r×   r×   ^  s8   ø„ ð>˜{õ >ð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r-   r×   c                   ój   ‡ — e Zd ZdZdefˆ fd„Zdej                  dee	   dej                  fd„Z
ˆ xZS )Ú
YolosLayerz?This corresponds to the Block class in the timm implementation.r1   c                 ór  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        t        |«      | _        t        |«      | _	        t        j                  |j                  |j                  ¬«      | _        t        j                  |j                  |j                  ¬«      | _        y )Nr   ©Úeps)r5   r6   Úchunk_size_feed_forwardÚseq_len_dimrÇ   rÉ   rÎ   Úintermediater×   rÊ   r   Ú	LayerNormr9   Úlayer_norm_epsÚlayernorm_beforeÚlayernorm_afterrb   s     €r.   r6   zYolosLayer.__init__o  s‡   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ'¨Ó/ˆŒÜ-¨fÓ5ˆÔÜ! &Ó)ˆŒÜ "§¡¨V×-?Ñ-?ÀV×EZÑEZÔ [ˆÔÜ!Ÿ|™|¨F×,>Ñ,>ÀF×DYÑDYÔZˆÕr-   r    r–   r2   c                 óÀ   — | j                  |«      } | j                  |fi |¤Ž}||z   }| j                  |«      }| j                  |«      }| j	                  ||«      }|S r`   )rä   rÉ   rå   rá   rÊ   )rF   r    r–   Úhidden_states_normÚattention_outputÚlayer_outputs         r.   rY   zYolosLayer.forwardy  sr   € ð
 "×2Ñ2°=ÓAÐØ)˜4Ÿ>™>Ð*<ÑGÀÑGÐð )¨=Ñ8ˆð ×+Ñ+¨MÓ:ˆØ×(Ñ(¨Ó6ˆð —{‘{ <°Ó?ˆàÐr-   )r"   r#   r$   r%   r   r6   r&   r[   r   r   rY   r\   r]   s   @r.   rÛ   rÛ   l  sC   ø„ ÙIð[˜{õ [ðà—|‘|ðð Ð+Ñ,ðð 
�‰÷	r-   rÛ   c                   óT   ‡ — e Zd Zdeddfˆ fd„Zdej                  dededefd„Z	ˆ xZ
S )	ÚYolosEncoderr1   r2   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   re   )r5   r6   r1   r   Ú
ModuleListÚrangeÚnum_hidden_layersrÛ   ÚlayerÚgradient_checkpointingrk   rl   r;   Úuse_mid_position_embeddingsr7   r&   r8   r9   Úmid_position_embeddingsr   rE   )rF   r1   rW   Ú
seq_lengthrG   s       €r.   r6   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    rS   rT   c                 óD  — | j                   j                  r| j                  | j                  ||f«      }t	        | j
                  «      D ]I  \  }} ||«      }| j                   j                  sŒ%|| j                   j                  dz
  k  sŒB||   z   }ŒK t        |¬«      S )Nr   )r   )r1   rò   rE   ró   Ú	enumeraterð   rï   r	   )rF   r    rS   rT   Ú$interpolated_mid_position_embeddingsÚiÚlayer_modules          r.   rY   zYolosEncoder.forward§  s›   € ð �;‰;×2Ò2Ø37×3EÑ3EÀd×FbÑFbÐekÐmrÐdsÓ3tÐ0ä(¨¯©Ó4ò 	\‰OˆAˆ|Ù(¨Ó7ˆMà�{‰{×6Ó6Ø˜Ÿ™×5Ñ5¸Ñ9Ó:Ø$1Ð4XÐYZÑ4[Ñ$[‘Mð	\ô °Ô?Ð?r-   )r"   r#   r$   r   r6   r&   r[   r¨   r	   rY   r\   r]   s   @r.   rë   rë   Ž  sM   ø„ ðv˜{ð v¨tõ vð0@à—|‘|ð@ð ð@ð ð	@ð
 
÷@r-   rë   c                   óF   — e Zd ZU eed<   dZdZdZdZg Z	dZ
dZdZdZeedœZy)ÚYolosPreTrainedModelr1   ÚvitrH   )ÚimageT)r    r!   N)r"   r#   r$   r   r(   Úbase_model_prefixÚmain_input_nameÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_sdpaÚ_supports_flash_attnÚ_supports_flex_attnÚ_supports_attention_backendrÛ   r¡   Ú_can_record_outputsr,   r-   r.   rû   rû   º  sJ   … àÓØÐØ$€OØ!ÐØ&*Ð#ØÐØ€NØÐØÐØ"&Ðà#Ø(ñÑr-   rû   c            
       óš   ‡ — e Zd Zddedefˆ fd„Zdefd„Ze e	d¬«      e
	 dd	ej                  dz  d
ee   defd„«       «       «       Zˆ xZS )Ú
YolosModelr1   Úadd_pooling_layerc                 ó  •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        t        j                  |j                  |j                  ¬«      | _        |rt        |«      nd| _        | j                  «        y)zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        rÝ   N)r5   r6   r1   r0   rU   rë   Úencoderr   râ   r9   rã   Ú	layernormÚYolosPoolerÚpoolerÚ	post_init)rF   r1   r
  rG   s      €r.   r6   zYolosModel.__init__Î  sk   ø€ ô
 	‰Ñ˜Ô ØˆŒä)¨&Ó1ˆŒÜ# FÓ+ˆŒäŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÙ->”k &Ô)ÀDˆŒð 	�‰Õr-   r2   c                 ó.   — | j                   j                  S r`   )rU   r>   )rF   s    r.   Úget_input_embeddingszYolosModel.get_input_embeddingsß  s   € Ø�‰×/Ñ/Ð/r-   F)Útie_last_hidden_statesNrH   r–   c                 ó  — |€t        d«      ‚| j                  |«      }|j                  dd  \  }}| j                  |||¬«      }|j                  }| j                  |«      }| j                  �| j                  |«      nd }t        ||¬«      S )Nz You have to specify pixel_valuesr²   )rS   rT   )r   Úpooler_output)r�   rU   rM   r  r   r  r  r
   )	rF   rH   r–   Úembedding_outputrS   rT   Úencoder_outputsÚsequence_outputÚpooled_outputs	            r.   rY   zYolosModel.forwardâ  s“   € ð ÐÜÐ?Ó@Ð@àŸ?™?¨<Ó8Ðà$×*Ñ*¨2¨3Ð/‰ˆ�Ø+/¯<©<Ð8HÐQWÐ_d¨<Ó+eˆØ)×;Ñ;ˆØŸ.™.¨Ó9ˆØ8<¿¹Ð8O˜Ÿ™ OÔ4ÐUYˆä)¸OÐ[hÔiÐir-   )Tr`   )r"   r#   r$   r   Úboolr6   r=   r  r   r   r   r&   r[   r   r   r
   rY   r\   r]   s   @r.   r	  r	  Ì  s�   ø„ ñ˜{ð ¸tõ ð"0Ð&:ó 0ð  Ù¨EÔ2Øð -1ñjà—l‘l TÑ)ðjð Ð+Ñ,ðjð 
$ò	jó ó 3ó  ôjr-   r	  c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )r  r1   c                 ó²   •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  «       | _        y r`   )r5   r6   r   r®   r9   rÁ   ÚTanhÚ
activationrb   s     €r.   r6   zYolosPooler.__init__ù  s9   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ'™'›)ˆ�r-   r    r2   c                 ó\   — |d d …df   }| j                  |«      }| j                  |«      }|S )Nr   )rÁ   r  )rF   r    Úfirst_token_tensorr  s       r.   rY   zYolosPooler.forwardþ  s6   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð#5Ó6ˆØŸ™¨Ó6ˆØÐr-   rÕ   r]   s   @r.   r  r  ø  s*   ø„ ð$˜{õ $ð
 U§\¡\ð °e·l±l÷ r-   r  c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚYolosMLPPredictionHeadz°
    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.

    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r`   )r   r®   )Ú.0ÚnÚks      r.   ú	<genexpr>z2YolosMLPPredictionHead.__init__.<locals>.<genexpr>  s   è ø€ Ò#g¹¸¸1¤B§I¡I¨a°§OÑ#gùs   ‚#%)r5   r6   Ú
num_layersr   rí   ÚzipÚlayers)rF   Ú	input_dimÚ
hidden_dimÚ
output_dimr)  ÚhrG   s         €r.   r6   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 r4   )rö   r+  r)  r   rn   Úrelu)rF   Úxrø   rð   s       r.   rY   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%   r6   rY   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d„ Zee	 d
dej                  de
e   dz  dee   defd	„«       «       Zˆ xZS )ÚYolosForObjectDetectionr1   c                 ó"  •— t         ‰| �  |«       t        |d¬«      | _        t	        |j
                  |j
                  |j                  dz   d¬«      | _        t	        |j
                  |j
                  dd¬«      | _        | j                  «        y )NF)r
  r   r   )r,  r-  r.  r)  é   )
r5   r6   r	  rü   r"  r9   Ú
num_labelsÚclass_labels_classifierÚbbox_predictorr  rb   s     €r.   r6   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 )NrJ   )r   r   )r*  )rF   Úoutputs_classÚoutputs_coordÚaÚbs        r.   Ú_set_aux_lossz%YolosForObjectDetection._set_aux_loss4  s7   € Ü;>¸}ÈSÈbÐ?QÐS`ÐadÐbdÐSeÓ;f×g±4°1°a˜1¨AÓ.ÓgÐgùÓgs   –)NrH   Úlabelsr–   r2   c           
      ód  —  | j                   |fi |¤Ž}|j                  }|dd…| j                  j                   d…dd…f   }| j	                  |«      }| j                  |«      j                  «       }d\  }}	}
|�†d\  }}| j                  j                  r<|j                  }| j	                  |«      }| j                  |«      j                  «       }| j                  ||| j                  || j                  ||«      \  }}	}
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)`.

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoModelForObjectDetection
        >>> import torch
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> 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)NNN)NN)r   r   r   r   r   r   r    r!   )rü   r   r1   r;   r8  r9  ÚsigmoidÚauxiliary_lossr    Úloss_functionÚdevicer   r!   )rF   rH   r@  r–   Úoutputsr  r   r   r   r   r   r;  r<  rá   s                 r.   rY   zYolosForObjectDetection.forward7  s;  € ðn /7¨d¯h©h°|Ñ.NÀvÑ.NˆØ!×3Ñ3ˆð *ª!¨d¯k©k×.NÑ.NÐ-NÑ-PÒRSÐ*SÑTˆð ×-Ñ-¨oÓ>ˆØ×(Ñ(¨Ó9×AÑAÓCˆ
à-=Ñ*ˆˆiÐ*ØÐØ+5Ñ(ˆM˜=Ø�{‰{×)Ò)Ø&×4Ñ4�Ø $× <Ñ <¸\Ó J�Ø $× 3Ñ 3°LÓ A× IÑ IÓ K�Ø15×1CÑ1CØ˜ §¡¨Z¸¿¹ÀmÐUbó2Ñ.ˆD�)Ð.ô *ØØØØ!Ø/Ø%×7Ñ7Ø!×/Ñ/Ø×)Ñ)ô	
ð 		
r-   r`   )r"   r#   r$   r   r6   r?  r   r   r&   r'   r*   r)   r   r   r   rY   r\   r]   s   @r.   r4  r4    sw   ø„ ð˜{õ ò&hð Øð %)ñS
à×'Ñ'ðS
ð �T‘
˜TÑ!ðS
ð Ð+Ñ,ð	S
ð
 
$òS
ó ó ôS
r-   r4  )r4  r	  rû   )Nr±   )9r%   Úcollections.abcr‰   r   Údataclassesr   r&   r   Úactivationsr   Úmodeling_layersr   Úmodeling_outputsr	   r
   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   r   Úutils.genericr   r   Úutils.output_capturingr   Úconfiguration_yolosr   Ú
get_loggerr"   Úloggerr   ÚModuler0   rD   r   r=   r[   ÚfloatrŸ   r¡   r¿   rÇ   rÎ   r×   rÛ   rë   rû   r	  r  r"  r4  Ú__all__r,   r-   r.   ú<module>rW     s9  ðñ ã Ý $Ý !ã Ý å !Ý 9ß Kß FÝ &ß MÓ Mß IÝ 5Ý ,ð 
ˆ×	Ñ	˜HÓ	%€ñ ðôð
 ô7 ó 7ó óð7ôB'�b—i‘iô 'ôT¨2¯9©9ô ô: r§y¡yô ôB˜2Ÿ9™9ô ðP !Øñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð �T‰\ð%ð ð%ð Ð'Ñ(ó%ô:4.˜Ÿ™ô 4.ôp�b—i‘iô ô$�R—Y‘Yô ô"˜Ÿ	™	ô ô 
�"—)‘)ô 
ôÐ+ô ôD)@�2—9‘9ô )@ðX ô˜?ó ó ðð" ô(jÐ%ó (jó ð(jôV�"—)‘)ô ô ˜RŸY™Yô ñ& ðôô
l
Ð2ó l
óð
l
ò^ L�r-   