Ë
    ÜÓ^j.  ã                   ó  — d 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	 ddl
mZmZmZ dd	lmZ dd
lmZmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z m!Z!m"Z"m#Z#  ed¬«      e G d„ de«      «       «       Z$ G d„ de"«      Z% G d„ dejL                  «      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 G d$„ d%e,«      «       Z- ed&¬'«       G d(„ d)ee,«      «       Z.g d*¢Z/y)+zPyTorch Pixio model.é    N)Ústrict)Únné   )ÚBackboneMixinÚfilter_output_hidden_states)Úcreate_bidirectional_mask)ÚBackboneOutputÚBaseModelOutputÚBaseModelOutputWithPooling)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚ
is_tracing)Úcan_return_tupleÚmerge_with_config_defaults)Úcapture_outputsé   )ÚDinov2Config)Ú	Dinov2MLP)ÚSwinDropPath)ÚViTAttentionÚViTLayerÚViTPatchEmbeddingsÚViTPreTrainedModelzfacebook/pixio-huge)Ú
checkpointc                   óÌ   — e Zd ZU dZdZdZeed<   dZeed<   dZ	eed<   d	Z
eed
<   dZeee   z  eeef   z  ed<   dZeee   z  eeef   z  ed<    e«       Z e«       Z e«       Zy)ÚPixioConfigaû  
    apply_layernorm (`bool`, *optional*, defaults to `True`):
        Whether to apply layer normalization to the feature maps in case the model is used as backbone.
    reshape_hidden_states (`bool`, *optional*, defaults to `True`):
        Whether to reshape the feature maps to 4D tensors of shape `(batch_size, hidden_size, height, width)` in
        case the model is used as backbone. If `False`, the feature maps will be 3D tensors of shape `(batch_size,
        seq_len, hidden_size)`.
    n_cls_tokens (`int`, *optional*, defaults to 8):
        Number of class tokens in the Transformer encoder.

    Example:

    ```python
    >>> from transformers import PixioConfig, PixioModel

    >>> # Initializing a Pixio pixio-huge style configuration
    >>> configuration = PixioConfig()

    >>> # Initializing a model (with random weights) from the pixio-huge style configuration
    >>> model = PixioModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Úpixioi   Úhidden_sizeé    Únum_hidden_layersé   Únum_attention_headsé   Ún_cls_tokensé   Ú
image_sizeÚ
patch_sizeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
model_typer   ÚintÚ__annotations__r!   r#   r%   r'   ÚlistÚtupler(   ÚAttributeErrorÚlayerscale_valueÚuse_swiglu_ffnÚuse_mask_token© ó    úr/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/pixio/modular_pixio.pyr   r   !   s�   … ñð2 €Jà€K�ÓØÐ�sÓØ!Ð˜Ó!Ø€L�#ÓØ47€J��d˜3‘i‘ %¨¨S¨¡/Ñ1Ó7Ø46€J��d˜3‘i‘ %¨¨S¨¡/Ñ1Ó6á%Ó'ÐÙ#Ó%€NÙ#Ó%�Nr7   r   c                   ó   — e Zd Zy)ÚPixioPatchEmbeddingsN©r)   r*   r+   r6   r7   r8   r:   r:   K   ó   „ Ør7   r:   c                   ó¤   ‡ — e Zd ZdZdeddfˆ fd„Zdej                  dededej                  fd	„Z	d
ej                  dej                  fd„Z
ˆ xZS )ÚPixioEmbeddingszB
    Construct the CLS tokens, position and patch embeddings.
    ÚconfigÚreturnNc                 ó(  •— t         ‰| �  «        t        j                  t	        j
                  d|j                  |j                  «      «      | _        d | _	        t        |«      | _        | j                  j                  }t        j                  t	        j
                  d||j                  z   |j                  «      «      | _        t        j                  |j                  «      | _        |j                  | _        |j"                  | _        || _        y )Né   )ÚsuperÚ__init__r   Ú	ParameterÚtorchÚrandnr%   r   Ú	cls_tokenÚ
mask_tokenr:   Úpatch_embeddingsÚnum_patchesÚposition_embeddingsÚDropoutÚhidden_dropout_probÚdropoutr(   r?   )Úselfr?   rK   Ú	__class__s      €r8   rD   zPixioEmbeddings.__init__T   sÄ   ø€ Ü‰ÑÔäŸ™¤e§k¡k°!°V×5HÑ5HÈ&×J\ÑJ\Ó&]Ó^ˆŒØˆŒÜ 4°VÓ <ˆÔØ×+Ñ+×7Ñ7ˆÜ#%§<¡<´·±¸A¸{ÈV×M`ÑM`Ñ?`Ðbh×btÑbtÓ0uÓ#vˆÔ Ü—z‘z &×"<Ñ"<Ó=ˆŒØ"×/Ñ/ˆÔØ ×+Ñ+ˆŒØˆ�r7   Ú
embeddingsÚheightÚwidthc                 ó@  — |j                   d   | j                  z
  }| j                  j                   d   | j                  z
  }t        «       s||k(  r||k(  r| j                  S | j                  dd…d| j                  …f   }| j                  dd…| j                  d…f   }|j                   d   }|| j                  z  }	|| j                  z  }
t        |dz  «      }|j                  d|||«      }|j                  dddd«      }|j                  }t        j                  j                  |j                  t        j                  «      |	|
fdd	¬
«      j                  |¬«      }|j                  dddd«      j                  dd|«      }t        j                   ||fd¬«      S )a#  
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher
        resolution images. This method is also adapted to support tracing and interpolation at torch.float32 precision.

        Adapted from:
        - https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
        - https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
        rB   Néÿÿÿÿg      à?r   r   r   ÚbicubicF)ÚsizeÚmodeÚalign_corners©Údtype©Údim)Úshaper%   rL   r   r(   r.   ÚreshapeÚpermuter\   r   Ú
functionalÚinterpolateÚtorF   Úfloat32ÚviewÚcat)rP   rR   rS   rT   rK   Únum_positionsÚclass_pos_embedÚpatch_pos_embedr^   Ú
new_heightÚ	new_widthÚsqrt_num_positionsÚtarget_dtypes                r8   Úinterpolate_pos_encodingz(PixioEmbeddings.interpolate_pos_encodinga   sš  € ð !×&Ñ& qÑ)¨D×,=Ñ,=Ñ=ˆØ×0Ñ0×6Ñ6°qÑ9¸D×<MÑ<MÑMˆäŒ| ¨}Ò <ÀÈ5ÂØ×+Ñ+Ð+à×2Ñ2²1Ð6I¸×8IÑ8IÐ6IÐ3IÑJˆØ×2Ñ2²1°d×6GÑ6GÑ6IÐ3IÑJˆà×Ñ˜rÑ"ˆà˜tŸ™Ñ.ˆ
Ø˜TŸ_™_Ñ,ˆ	ä  °Ñ!3Ó4ÐØ)×1Ñ1°!Ð5GÐI[Ð]`ÓaˆØ)×1Ñ1°!°Q¸¸1Ó=ˆØ&×,Ñ,ˆÜŸ-™-×3Ñ3Ø×ÑœuŸ}™}Ó-Ø˜iÐ(ØØð	 4ó 
÷
 ‰"�<ˆ"Ó
 ð 	ð *×1Ñ1°!°Q¸¸1Ó=×BÑBÀ1ÀbÈ#ÓNˆä�y‰y˜/¨?Ð;ÀÔCÐCr7   Úpixel_valuesc                 óx  — |j                   \  }}}}| j                  j                  j                  j                  }| j                  |j                  |¬«      «      }| j                  j                  |dd«      }t        j                  ||fd¬«      }|| j                  |||«      z   }| j                  |«      }|S )Nr[   rV   rB   r]   )r_   rJ   Ú
projectionÚweightr\   rd   rH   ÚexpandrF   rg   ro   rO   )	rP   rp   Ú
batch_sizeÚ_rS   rT   rn   rR   Ú
cls_tokenss	            r8   ÚforwardzPixioEmbeddings.forward‡   s©   € Ø'3×'9Ñ'9Ñ$ˆ
�A�v˜uØ×,Ñ,×7Ñ7×>Ñ>×DÑDˆØ×*Ñ*¨<¯?©?À¨?Ó+NÓOˆ
à—^‘^×*Ñ*¨:°r¸2Ó>ˆ
Ü—Y‘Y 
¨JÐ7¸QÔ?ˆ
à $×"?Ñ"?À
ÈFÐTYÓ"ZÑZˆ
à—\‘\ *Ó-ˆ
àÐr7   )r)   r*   r+   r,   r   rD   rF   ÚTensorr.   ro   rx   Ú__classcell__©rQ   s   @r8   r>   r>   O   si   ø„ ñð˜{ð ¨tõ ð$D°5·<±<ð $DÈð $DÐUXð $DÐ]b×]iÑ]ió $DðL E§L¡Lð °U·\±\÷ r7   r>   c                   ó   — e Zd Zy)ÚPixioAttentionNr;   r6   r7   r8   r}   r}   –   r<   r7   r}   c                   ó   — e Zd Zy)ÚPixioMLPNr;   r6   r7   r8   r   r   š   r<   r7   r   c                   ó   — e Zd Zy)ÚPixioDropPathNr;   r6   r7   r8   r�   r�   ž   r<   r7   r�   c            	       óˆ   ‡ — e Zd Zdefˆ fd„Z	 d	dej                  dej                  dz  dee   dej                  fd„Z	ˆ xZ
S )
Ú
PixioLayerr?   c                 óª   •— t         ‰| �  |«       |j                  dkD  rt        |j                  «      | _        y t	        j
                  «       | _        y )Ng        )rC   rD   Údrop_path_rater�   r   ÚIdentityÚ	drop_path)rP   r?   rQ   s     €r8   rD   zPixioLayer.__init__£   s@   ø€ Ü‰Ñ˜Ô ØAG×AVÑAVÐY\ÒA\œ v×'<Ñ'<Ó=ˆ�Ôbd×bmÑbmÓboˆ�r7   NÚhidden_statesÚattention_maskÚkwargsr@   c                 ó6  — |}| j                  |«      } | j                  ||fi |¤Ž\  }}| j                  |«      }| j                  |«      |z   }|}| j	                  |«      }| j                  |«      }| j                  |«      }| j                  |«      |z   }|S ©N)Úlayernorm_beforeÚ	attentionrO   r‡   Úlayernorm_afterÚmlp)rP   rˆ   r‰   rŠ   Úresidualrv   s         r8   rx   zPixioLayer.forward§   s    € ð !ˆØ×-Ñ-¨mÓ<ˆØ)˜4Ÿ>™>¨-¸ÑRÈ6ÑRÑˆ�qØŸ™ ]Ó3ˆØŸ™ }Ó5¸Ñ@ˆà ˆØ×,Ñ,¨]Ó;ˆØŸ™ Ó/ˆØŸ™ ]Ó3ˆØŸ™ }Ó5¸Ñ@ˆàÐr7   rŒ   )r)   r*   r+   r   rD   rF   ry   r   r   rx   rz   r{   s   @r8   rƒ   rƒ   ¢   sX   ø„ ðp˜{õ pð /3ñà—|‘|ðð Ÿ™ tÑ+ðð Ð+Ñ,ð	ð
 
�‰÷r7   rƒ   c                   ó   — e Zd Zy)ÚPixioPreTrainedModelNr;   r6   r7   r8   r“   r“   ¼   r<   r7   r“   c                   ó¨   ‡ — e Zd Zdefˆ fd„Ze ed¬«      e	 	 ddej                  dz  dej                  dz  de
e   d	efd
„«       «       «       Zˆ xZS )Ú
PixioModelr?   c                 ód  •— t         ‰| �  |«       || _        t        |«      | _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _
        t        j                  |j                  |j                  ¬«      | _        | j                  «        y c c}w )N©Úeps)rC   rD   r?   r>   rR   r   Ú
ModuleListÚranger!   rƒ   ÚlayersÚ	LayerNormr   Úlayer_norm_epsÚ	layernormÚ	post_init©rP   r?   rv   rQ   s      €r8   rD   zPixioModel.__init__Â   s|   ø€ Ü‰Ñ˜Ô ØˆŒä)¨&Ó1ˆŒÜ—m‘mÄÀv×G_ÑG_ÓA`Ö$a¸A¤Z°Õ%7Ò$aÓbˆŒäŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒà�‰Õùò	 %bs   ÁB-F)Útie_last_hidden_statesNrp   r‰   rŠ   r@   c                 óH  — |€t        d«      ‚| j                  |«      }t        | j                  ||¬«      }|}| j                  D ]  } |||fi |¤Ž}Œ | j                  |«      }|d d …d | j                  j                  …d d …f   j                  d¬«      }t        ||¬«      S )Nz You have to specify pixel_values)r?   Úinputs_embedsr‰   rB   r]   )Úlast_hidden_stateÚpooler_output)	Ú
ValueErrorrR   r   r?   r›   rž   r%   Úmeanr   )rP   rp   r‰   rŠ   Úembedding_outputrˆ   ÚlayerÚpooled_outputs           r8   rx   zPixioModel.forwardÍ   sº   € ð ÐÜÐ?Ó@Ð@àŸ?™?¨<Ó8ÐÜ2Ø—;‘;Ø*Ø)ô
ˆð
 )ˆØ—[‘[ò 	KˆEÙ! -°ÑJÀ6ÑJ‰Mð	KàŸ™ }Ó5ˆØ%¢aÐ)G¨4¯?©?×+GÑ+GÐ)GÊÐ&JÑK×PÑPÐUVÐPÓWˆä)Ø+Ø'ô
ð 	
r7   )NN)r)   r*   r+   r   rD   r   r   r   rF   ry   r   r   r   rx   rz   r{   s   @r8   r•   r•   À   s~   ø„ ð	˜{õ 	ð  Ù¨EÔ2Øð -1Ø.2ñ
à—l‘l TÑ)ð
ð Ÿ™ tÑ+ð
ð Ð+Ñ,ð	
ð
 
$ò
ó ó 3ó  ô
r7   r•   zN
    Pixio backbone, to be used with frameworks like DETR and MaskFormer.
    )Úcustom_introc                   ó’   ‡ — e Zd Zdefˆ fd„Zeee	 d	dej                  dej                  dz  de
e   defd„«       «       «       Zˆ xZS )
ÚPixioBackboner?   c                 ó8  •— t         ‰| �  |«       t        |j                  dz   «      D �cg c]  }|j                  ‘Œ c}| _        t        |«      | _        t        j                  |j                  |j                  ¬«      | _        | j                  «        y c c}w )NrB   r—   )rC   rD   rš   r!   r   Únum_featuresr•   r   r   rœ   r�   rž   rŸ   r    s      €r8   rD   zPixioBackbone.__init__ñ   st   ø€ Ü‰Ñ˜Ô ä9>¸v×?WÑ?WÐZ[Ñ?[Ó9\Ö]°A˜V×/Ó/Ò]ˆÔÜ Ó'ˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒà�‰Õùò	 ^s   «BNrp   r‰   rŠ   r@   c                 ó´  — d|d<    | j                   ||fi |¤Ž}|j                  }g }t        | j                  |«      D ]î  \  }}|| j                  v sŒ| j
                  j                  r| j                  |«      }| j
                  j                  rŒ|dd…| j                   j                  j                  d…f   }|j                  \  }	}
}}| j
                  j                  }|j                  |	||z  ||z  d«      }|j                  dddd«      j                  «       }|j!                  |«       Œð t#        t%        |«      |j                  |j&                  ¬	«      S )
aw  
        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoBackbone
        >>> 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()))

        >>> processor = AutoImageProcessor.from_pretrained("facebook/pixio-huge")
        >>> model = AutoBackbone.from_pretrained(
        ...     "facebook/pixio-huge", out_features=["stage7", "stage15", "stage23", "stage31"]
        ... )

        >>> inputs = processor(image, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> feature_maps = outputs.feature_maps
        >>> list(feature_maps[-1].shape)
        [1, 1280, 16, 16]
        ```TÚoutput_hidden_statesNrV   r   r   rB   r   )Úfeature_mapsrˆ   Ú
attentions)r   rˆ   ÚzipÚstage_namesÚout_featuresr?   Úapply_layernormrž   Úreshape_hidden_statesrR   r%   r_   r(   r`   ra   Ú
contiguousÚappendr	   r1   r³   )rP   rp   r‰   rŠ   Úoutputrˆ   r²   ÚstageÚhidden_stateru   rv   rS   rT   r(   s                 r8   rx   zPixioBackbone.forwardú   sL  € ðF *.ˆÐ%Ñ&à", $§*¡*¨\¸>Ñ"TÈVÑ"TˆØ×,Ñ,ˆàˆÜ#& t×'7Ñ'7¸Ó#Gò 
	2ÑˆE�<Ø˜×)Ñ)Ò)Ø—;‘;×.Ò.Ø#'§>¡>°,Ó#?�LØ—;‘;×4Ò4Ø#/²°4·:±:×3HÑ3H×3UÑ3UÑ3WÐ0WÑ#X�LØ3?×3EÑ3EÑ0�J  6¨5Ø!%§¡×!7Ñ!7�JØ#/×#7Ñ#7¸
ÀFÈjÑDXÐZ_ÐcmÑZmÐoqÓ#r�LØ#/×#7Ñ#7¸¸1¸aÀÓ#C×#NÑ#NÓ#P�LØ×#Ñ# LÕ1ð
	2ô Ü˜|Ó,Ø ×.Ñ.Ø×(Ñ(ô
ð 	
r7   rŒ   )r)   r*   r+   r   rD   r   r   r   rF   ry   r   r   r	   rx   rz   r{   s   @r8   r­   r­   ë   sq   ø„ ð˜{õ ð Ø Øð /3ñ6
à—l‘lð6
ð Ÿ™ tÑ+ð6
ð Ð+Ñ,ð	6
ð
 
ò6
ó ó !ó ô6
r7   r­   )r   r•   r“   r­   )0r,   rF   Úhuggingface_hub.dataclassesr   r   Úbackbone_utilsr   r   Úmasking_utilsr   Úmodeling_outputsr	   r
   r   Úprocessing_utilsr   Úutilsr   r   r   Úutils.genericr   r   Úutils.output_capturingr   Údinov2.configuration_dinov2r   Údinov2.modeling_dinov2r   Úswin.modeling_swinr   Úvit.modeling_vitr   r   r   r   r   r:   ÚModuler>   r}   r   r�   rƒ   r“   r•   r­   Ú__all__r6   r7   r8   ú<module>rÌ      s  ðñ ã Ý .Ý ç HÝ 6ß [Ñ [Ý &ß CÑ Cß IÝ 5Ý 6Ý .Ý -ß ]Ó ]ñ Ð0Ô1Øô%&�,ó %&ó ó 2ð%&ôP	Ð-ô 	ôD�b—i‘iô DôN	�\ô 	ô	ˆyô 	ô	�Lô 	ô�ô ô4	Ð-ô 	ð ô'
Ð%ó '
ó ð'
ñT ðôô
C
�MÐ#7ó C
óð
C
òL Q�r7   