Ë
    ÜÓ^jš*  ã                   ó  — 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	 ddl
mZ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  ed¬«      e G d„ de«      «       «       Z G d„ de«      Z G d„ de«      Z G d„ de«      Z 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!e«      Z# G d"„ d#e«      Z$ G d$„ d%e«      Z% G d&„ d'e«      Z& G d(„ d)e«      Z'g d*¢Z(y)+é    )Ú	dataclassN)Ústrict)Únné   )ÚModelOutput)ÚUnpack)ÚTransformersKwargsÚauto_docstringé   )Ú
EomtConfig)ÚEomtEmbeddingsÚEomtForUniversalSegmentationÚ	EomtLayerÚEomtLayerNorm2dÚEomtLayerScaleÚEomtMLPÚEomtPatchEmbeddingsÚEomtPreTrainedModelÚEomtScaleBlockÚEomtScaleLayerÚEomtSwiGLUFFNz&tue-mps/videomt-dinov2-small-ytvis2019)Ú
checkpointc                   ó   — e Zd ZdZy)ÚVideomtConfigÚvideomtN)Ú__name__Ú
__module__Ú__qualname__Ú
model_type© ó    úv/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/videomt/modular_videomt.pyr   r   (   s	   „ ð �Jr!   r   c                   óD   — e Zd Zdej                  dej                  fd„Zy)ÚVideomtPatchEmbeddingsÚpixel_valuesÚreturnc                 ó:  — |j                   d   }|| j                  k7  rt        d| j                  › d|› d�«      ‚|j                  | j                  j
                  j                  ¬«      }| j	                  |«      j                  d«      j                  dd«      }|S )Né   zoMake sure that the channel dimension of the pixel values match with the one set in the configuration. Expected z	 but got ú.)Údtyper   )	ÚshapeÚnum_channelsÚ
ValueErrorÚtoÚ
projectionÚweightr*   ÚflattenÚ	transpose)Úselfr%   r,   Ú
embeddingss       r"   ÚforwardzVideomtPatchEmbeddings.forward/   sš   € Ø#×)Ñ)¨!Ñ,ˆØ˜4×,Ñ,Ò,ÜðØ!×.Ñ.Ð/¨y¸¸ÀaðIóð ð
 $—‘¨T¯_©_×-CÑ-C×-IÑ-I�ÓJˆØ—_‘_ \Ó2×:Ñ:¸1Ó=×GÑGÈÈ1ÓMˆ
ØÐr!   N)r   r   r   ÚtorchÚTensorr5   r    r!   r"   r$   r$   .   s   „ ð
 E§L¡Lð 
°U·\±\ô 
r!   r$   c                   ó|   ‡ — e Zd Zdefˆ fd„Zddej                  dej                  dz  dej                  fd„Zˆ xZS )	ÚVideomtEmbeddingsÚconfigc                 ó¶   •— t         ‰| �  |«       t        |«      | _        t	        j
                  t        j                  dd|j                  «      «      | _	        y )Nr(   )
ÚsuperÚ__init__r$   Úpatch_embeddingsr   Ú	Parameterr6   ÚzerosÚhidden_sizeÚ
mask_token©r3   r:   Ú	__class__s     €r"   r=   zVideomtEmbeddings.__init__=   s@   ø€ Ü‰Ñ˜Ô Ü 6°vÓ >ˆÔÜŸ,™,¤u§{¡{°1°a¸×9KÑ9KÓ'LÓMˆ�r!   Nr%   Úbool_masked_posr&   c                 ó  — |j                   dk(  rA|j                  \  }}}}}|j                  ||z  |||«      }|�F|j                  ||z  d«      }n0|�.|j                   dkD  r|j                  |j                  d   d«      }|j                  d   }| j                  |«      }|�[|j	                  |j
                  t        j                  ¬«      j                  d«      }	t        j                  |	| j                  |«      }| j                  j                  |dd«      }
| j                  j                  |dd«      }|| j                  | j                  «      z   }t        j                   |
||gd¬«      }| j#                  |«      }|S )Né   éÿÿÿÿr   r   )Údevicer*   r(   ©Údim)Úndimr+   Úreshaper>   r.   rI   r6   ÚboolÚ	unsqueezeÚwhererB   Ú	cls_tokenÚexpandÚregister_tokensÚposition_embeddingsÚposition_idsÚcatÚdropout)r3   r%   rE   Ú
batch_sizeÚ
num_framesr,   ÚheightÚwidthr4   ÚmaskÚ
cls_tokensrS   s               r"   r5   zVideomtEmbeddings.forwardB   sq  € Ø×Ñ Ò!ØBN×BTÑBTÑ?ˆJ˜
 L°&¸%Ø'×/Ñ/°
¸ZÑ0GÈÐW]Ð_dÓeˆLàÐ*Ø"1×"9Ñ"9¸*ÀzÑ:QÐSUÓ"V‘ØÐ(¨_×-AÑ-AÀAÒ-EØ-×5Ñ5°o×6KÑ6KÈAÑ6NÐPRÓSˆOà!×'Ñ'¨Ñ*ˆ
Ø×*Ñ*¨<Ó8ˆ
àÐ&Ø"×%Ñ%¨Z×->Ñ->ÄeÇjÁjÐ%ÓQ×[Ñ[Ð\^Ó_ˆDÜŸ™ T¨4¯?©?¸JÓGˆJà—^‘^×*Ñ*¨:°r¸2Ó>ˆ
Ø×.Ñ.×5Ñ5°jÀ"ÀbÓIˆà $×":Ñ":¸4×;LÑ;LÓ"MÑMˆ
Ü—Y‘Y 
¨O¸ZÐHÈaÔPˆ
Ø—\‘\ *Ó-ˆ
ØÐr!   ©N)	r   r   r   r   r=   r6   r7   r5   Ú__classcell__©rD   s   @r"   r9   r9   <   s@   ø„ ðN˜}õ Nñ
 E§L¡Lð À5Ç<Á<ÐRVÑCVð Ðbg×bnÑbn÷ r!   r9   c                   ó   — e Zd Zy)Ú
VideomtMLPN©r   r   r   r    r!   r"   rb   rb   \   ó   „ Ør!   rb   c                   ó   — e Zd Zy)ÚVideomtGatedMLPNrc   r    r!   r"   rf   rf   `   rd   r!   rf   c                   ó   — e Zd Zy)ÚVideomtLayerNrc   r    r!   r"   rh   rh   d   rd   r!   rh   c                   ó   — e Zd Zy)ÚVideomtLayerScaleNrc   r    r!   r"   rj   rj   h   rd   r!   rj   a¨  
    Class for outputs of [`VideomtForUniversalSegmentationOutput`].

    This output can be directly passed to [`~VideomtVideoProcessor.post_process_semantic_segmentation`] or
    [`~VideomtVideoProcessor.post_process_instance_segmentation`] or
    [`~VideomtVideoProcessor.post_process_panoptic_segmentation`] to compute final segmentation maps. Please, see
    [`~VideomtVideoProcessor`] for details regarding usage.
    )Úcustom_introc                   ó  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  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)	Ú%VideomtForUniversalSegmentationOutputa€  
    loss (`torch.Tensor`, *optional*):
        The computed loss, returned when labels are present.
    class_queries_logits (`torch.FloatTensor`):
        A tensor of shape `(batch_size, num_queries, num_labels + 1)` representing the proposed classes for each
        query. Note the `+ 1` is needed because we incorporate the null class.
    masks_queries_logits (`torch.FloatTensor`):
        A tensor of shape `(batch_size, num_queries, height, width)` representing the proposed masks for each
        query.
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
        Last hidden states (final feature map) of the last layer.
    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 + one for the output of each stage) of
        shape `(batch_size, sequence_length, hidden_size)`. Hidden-states all layers of the model.
    attentions (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `tuple(torch.FloatTensor)` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`. Self and Cross Attentions weights from transformer decoder.
    NÚlossÚclass_queries_logitsÚmasks_queries_logitsÚlast_hidden_stateÚhidden_statesÚ
attentions)r   r   r   Ú__doc__rn   r6   ÚFloatTensorÚ__annotations__ro   rp   rq   rr   Útuplers   r    r!   r"   rm   rm   l   s”   … ñð& &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø59Ð˜%×+Ñ+¨dÑ2Ó9Ø59Ð˜%×+Ñ+¨dÑ2Ó9Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ô6r!   rm   c                   ól   ‡ — e Zd ZdZdZ ej                  «       dej                  ddfˆ fd„«       Z	ˆ xZ
S )ÚVideomtPreTrainedModelÚpixel_values_videos)ÚvideoÚmoduler&   Nc                 ó˜   •— t         ‰| �  |«       t        |t        «      r*t        j
                  j                  |j                  «       y y r^   )r<   Ú_init_weightsÚ
isinstancer9   r   ÚinitÚzeros_rB   )r3   r|   rD   s     €r"   r~   z$VideomtPreTrainedModel._init_weights—   s6   ø€ ä‰Ñ˜fÔ%Ü�fÔ/Ô0Ü�G‰G�N‰N˜6×,Ñ,Õ-ð 1r!   )r   r   r   Úmain_input_nameÚinput_modalitiesr6   Úno_gradr   ÚModuler~   r_   r`   s   @r"   ry   ry   “   s:   ø„ Ø+€OØ!Ðà€U‡]�]ƒ_ð. B§I¡Ið .°$ô .ó ô.r!   ry   c                   ó   — e Zd Zy)ÚVideomtLayerNorm2dNrc   r    r!   r"   r‡   r‡   ž   rd   r!   r‡   c                   ó   — e Zd Zy)ÚVideomtScaleLayerNrc   r    r!   r"   r‰   r‰   ¢   rd   r!   r‰   c                   ó   — e Zd Zy)ÚVideomtScaleBlockNrc   r    r!   r"   r‹   r‹   ¦   rd   r!   r‹   c                   óØ   ‡ — e Zd ZdZdefˆ fd„Zd„ Z	 	 	 	 ddej                  dz  de	ej                     dz  de	ej                     dz  de	ej                     dz  d	e
e   d
efd„Zˆ xZS )ÚVideomtForUniversalSegmentationrz   r:   c                 ó‚   •— t         ‰| �  |«       t        j                  |j                  |j                  «      | _        y r^   )r<   r=   r   ÚLinearrA   Úquery_updaterrC   s     €r"   r=   z(VideomtForUniversalSegmentation.__init__­   s/   ø€ Ü‰Ñ˜Ô ÜŸY™Y v×'9Ñ'9¸6×;MÑ;MÓNˆÕr!   c                 ó   — t        d«      ‚)NzNot needed for Videomt)ÚAttributeError)Ú	attn_maskÚprobÚnum_query_tokensÚencoder_start_tokensrI   s        r"   Ú_disable_attention_maskz7VideomtForUniversalSegmentation._disable_attention_mask±   s   € ÜÐ5Ó6Ð6r!   NÚmask_labelsÚclass_labelsÚpatch_offsetsÚkwargsr&   c           	      ó|  — d|v rt        d«      ‚|€t        d«      ‚|j                  dk7  rt        d«      ‚|€|�t        d«      ‚|j                  \  }}}}	}
|j                  ||z  ||	|
«      }| j	                  |«      }| j
                  | j                  j                  z
  }| j                  d| D ]
  } ||«      }Œ |j                  |||j                  d   |j                  d	   «      }g }g }g }d}t        |«      D �]l  }|dd…|f   }|€K| j                  j                  ddd…dd…f   j                  |d
d
«      j                  |j                  «      }nd| j!                  |«      j                  |j                  «      | j                  j                  ddd…dd…f   j                  |j                  «      z   }t#        j$                  ||fd¬«      }| j                  |d D ]
  } ||«      }Œ | j'                  |«      }| j)                  |«      \  }}|j+                  |«       |j+                  |«       |j+                  |«       |dd…d| j                  j,                  …dd…f   }�Œo t/        dt#        j$                  |d¬«      t#        j$                  |d¬«      t#        j$                  |d¬«      ¬«      S )aø  
        pixel_values_videos (`torch.Tensor`, *optional*):
            Video inputs of shape `(batch_size, num_frames, num_channels, height, width)`.
        mask_labels (`list[torch.Tensor]`, *optional*):
            Not supported for 5D video inputs.
        class_labels (`list[torch.LongTensor]`, *optional*):
            Not supported for 5D video inputs.
        patch_offsets (`list[torch.Tensor]`, *optional*):
            Unused for video inputs and only kept for modular compatibility.
        r%   zAUse `pixel_values_videos` with `VideomtForUniversalSegmentation`.Nz'You have to specify pixel_values_videosrG   zyVideomtForUniversalSegmentation only supports 5D video inputs of shape (batch_size, num_frames, channels, height, width).z“Training with 5D video inputs is not supported in `VideomtForUniversalSegmentation`. Flatten frames and use `EomtForUniversalSegmentation` instead.r(   r   rH   rJ   r   )rn   rp   ro   rq   )r-   rL   r+   rM   r4   Únum_hidden_layersr:   Ú
num_blocksÚlayersÚviewÚrangeÚqueryr0   rR   r.   rI   r�   r6   rV   Ú	layernormÚpredictÚappendÚnum_queriesrm   )r3   rz   r˜   r™   rš   r›   rX   rY   r,   rZ   r[   Úflat_pixel_valuesrr   Úquery_start_idxÚlayer_moduleÚall_masks_queries_logitsÚall_class_queries_logitsÚall_last_hidden_statesÚpropagated_queryÚ	frame_idxÚframe_hidden_statesÚquery_tokensÚsequence_outputrp   ro   s                            r"   r5   z'VideomtForUniversalSegmentation.forward´   sð  € ð$ ˜VÑ#ÜÐ`ÓaÐaàÐ&ÜÐFÓGÐGà×#Ñ# qÒ(ÜðEóð ð
 Ð" lÐ&>ÜðQóð ð
 ?R×>WÑ>WÑ;ˆ
�J ¨f°eØ/×7Ñ7¸
ÀZÑ8OÐQ]Ð_eÐglÓmÐàŸ™Ð(9Ó:ˆØ×0Ñ0°4·;±;×3IÑ3IÑIˆà ŸK™KÐ(8¨Ð9ò 	8ˆLÙ(¨Ó7‰Mð	8ð &×*Ñ*¨:°zÀ=×CVÑCVÐWXÑCYÐ[h×[nÑ[nÐopÑ[qÓrˆà#%Ð Ø#%Ð Ø!#ÐØÐä˜zÓ*ó 	TˆIØ"/²°9°Ñ"=ÐàÐ'Ø#Ÿz™z×0Ñ0°²qº!°Ñ<×CÑCÀJÐPRÐTVÓW×ZÑZÐ[n×[uÑ[uÓv‘à#×1Ñ1Ð2BÓC×FÑFÐGZ×GaÑGaÓbÐei×eoÑeo×evÑevØš!šQ�Jñfç‘"Ð(×/Ñ/Ó0ñ 1�ô #(§)¡)¨\Ð;NÐ,OÐUVÔ"WÐà $§¡¨OÐ,<Ð =ò H�Ù&2Ð3FÓ&GÑ#ðHð #Ÿn™nÐ-@ÓAˆOØ9=¿¹ÀoÓ9VÑ6Ð Ð"6à$×+Ñ+Ð,@ÔAØ$×+Ñ+Ð,@ÔAØ"×)Ñ)¨/Ô:Ø2²1Ð6O¸¿¹×8OÑ8OÐ6OÒQRÐ3RÑSÒð)	Tô, 5ØÜ!&§¡Ð+CÈÔ!KÜ!&§¡Ð+CÈÔ!KÜ#Ÿi™iÐ(>ÀAÔFô	
ð 	
r!   )NNNN)r   r   r   r‚   r   r=   r—   r6   r7   Úlistr   r	   rm   r5   r_   r`   s   @r"   r�   r�   ª   s¨   ø„ Ø+€OðO˜}õ Oò7ð
 48Ø15Ø26Ø37ñO
à"Ÿ\™\¨DÑ0ðO
ð ˜%Ÿ,™,Ñ'¨$Ñ.ðO
ð ˜5Ÿ<™<Ñ(¨4Ñ/ð	O
ð
 ˜EŸL™LÑ)¨DÑ0ðO
ð Ð+Ñ,ðO
ð 
/÷O
r!   r�   )r   ry   r�   ))Údataclassesr   r6   Úhuggingface_hub.dataclassesr   r   Ú
file_utilsr   Úprocessing_utilsr   Úutilsr	   r
   Úeomt.configuration_eomtr   Úeomt.modeling_eomtr   r   r   r   r   r   r   r   r   r   r   r   r$   r9   rb   rf   rh   rj   rm   ry   r‡   r‰   r‹   r�   Ú__all__r    r!   r"   ú<module>r»      s  ðõ "ã Ý .Ý å %Ý &ß 7Ý 0÷÷ ÷ ñ ñ ÐCÔDØô�Jó ó ó EðôÐ0ô ô˜ô ô@	�ô 	ô	�mô 	ô	�9ô 	ô	˜ô 	ñ ðô	ð ô7¨Kó 7ó ó	ð7ô8.Ð0ô .ô	˜ô 	ô	˜ô 	ô	˜ô 	ôY
Ð&Bô Y
òx�r!   