
    i$/                     $   d 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	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mZ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jF                        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* G d$ d%e*      Z+e G d& d'e*             Z, ed()       G d* d+e             Z-g d,Z.y)-zPyTorch Pixio model.    N)strict)nn   )GradientCheckpointingLayer)BackboneOutputBaseModelOutputBaseModelOutputWithPooling)Unpack)TransformersKwargsauto_docstring
is_tracing)can_return_tuplemerge_with_config_defaults)capture_outputs   )Dinov2Config)Dinov2BackboneDinov2DropPath	Dinov2MLP)ViTAttentionViTPatchEmbeddingsViTPreTrainedModelViTSelfAttentionz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     x/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/pixio/modular_pixio.pyr   r   #   s    2 JKs!!L#47Jd3i%S/1746Jd3i%S/16%'#%N#%Nr6   r   c                       e Zd Zy)PixioPatchEmbeddingsNr(   r)   r*   r5   r6   r7   r9   r9   M       r6   r9   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_tokenr9   patch_embeddingsnum_patchesposition_embeddingsDropouthidden_dropout_probdropoutr'   r>   )selfr>   rJ   	__class__s      r7   rC   zPixioEmbeddings.__init__V   s    ekk!V5H5H&J\J\&]^ 4V <++77#%<<A{VM`M`?`bhbtbt0u#v zz&"<"<="// ++r6   
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
        rA   Ng      ?r   r   r   bicubicF)sizemodealign_cornersdtypedim)shaper$   rK   r   r'   r-   reshapepermuter[   r   
functionalinterpolatetorE   float32viewcat)rO   rQ   rR   rS   rJ   num_positionsclass_pos_embedpatch_pos_embedr]   
new_height	new_widthsqrt_num_positionstarget_dtypes                r7   interpolate_pos_encodingz(PixioEmbeddings.interpolate_pos_encodingc   s    !&&q)D,=,==0066q9D<M<MM|} <5+++2216I8I8I6I3IJ221d6G6G6I3IJr"t.
T__,	 !34)11!5GI[]`a)11!Q1=&,,--33u}}-i(	 4 

 "<"
  	 *11!Q1=BB1b#Nyy/?;CCr6   pixel_valuesc                 x   |j                   \  }}}}| j                  j                  j                  j                  }| j                  |j                  |            }| j                  j                  |dd      }t        j                  ||fd      }|| j                  |||      z   }| j                  |      }|S )NrZ   rU   rA   r\   )r^   rI   
projectionweightr[   rc   rG   expandrE   rf   rn   rN   )	rO   ro   
batch_size_rR   rS   rm   rQ   
cls_tokenss	            r7   forwardzPixioEmbeddings.forward   s    '3'9'9$
Avu,,77>>DD**<???+NO
^^**:r2>
YY
J7Q?
$"?"?
FTY"ZZ
\\*-
r6   )r(   r)   r*   r+   r   rC   rE   Tensorr-   rn   rw   __classcell__rP   s   @r7   r=   r=   Q   si    { t $D5<< $D $DUX $D]b]i]i $DLELL U\\ r6   r=   c                       e Zd Zy)PixioSelfAttentionNr:   r5   r6   r7   r|   r|      r;   r6   r|   c                   $     e Zd Zdef fdZ xZS )PixioAttentionr>   c                 D    t         |   |       t        |      | _        y N)rB   rC   r|   	attentionrO   r>   rP   s     r7   rC   zPixioAttention.__init__   s     +F3r6   )r(   r)   r*   r   rC   ry   rz   s   @r7   r~   r~      s    4{ 4 4r6   r~   c                       e Zd Zy)PixioDropPathNr:   r5   r6   r7   r   r      r;   r6   r   c                       e Zd Zy)PixioMLPNr:   r5   r6   r7   r   r      r;   r6   r   c                   j     e Zd Zdeddf fdZdej                  dee   dej                  fdZ	 xZ
S )
PixioLayerr>   r?   Nc                    t         |           t        j                  |j                  |j
                        | _        t        |      | _        |j                  dkD  rt        |j                        nt        j                         | _        t        j                  |j                  |j
                        | _        t        |      | _        y )Nepsg        )rB   rC   r   	LayerNormr   layer_norm_epsnorm1r~   r   drop_path_rater   Identity	drop_pathnorm2r   mlpr   s     r7   rC   zPixioLayer.__init__   s    \\&"4"4&:O:OP
'/AGAVAVY\A\v'<'<=bdbmbmbo\\&"4"4&:O:OP
F#r6   hidden_stateskwargsc                     | j                  |      } | j                  |fi |}| j                  |      |z   }| j                  |      }| j	                  |      }| j                  |      |z   }|S r   )r   r   r   r   r   )rO   r   r   hidden_states_normself_attention_outputlayer_outputs         r7   rw   zPixioLayer.forward   sq    !ZZ6 ./A LV L'<=Mzz-0xx-~~l3mCr6   )r(   r)   r*   r   rC   rE   rx   r
   r   rw   ry   rz   s   @r7   r   r      sA    ${ $t $U\\ VDV=W \a\h\h r6   r   c                       e Zd ZeedZy)PixioPreTrainedModel)r   
attentionsN)r(   r)   r*   r   r|   _can_record_outputsr5   r6   r7   r   r      s    #(r6   r   c                   t     e Zd Zdef fdZe ed      dej                  de	e
   defd              Z xZS )	PixioEncoderr>   c                     t         |   |       t        j                  t	        |j
                        D cg c]  }t        |       c}      | _        d| _        | j                          y c c}w )NF)
rB   rC   r   
ModuleListranger    r   layergradient_checkpointing	post_init)rO   r>   ru   rP   s      r7   rC   zPixioEncoder.__init__   sS     ]]fF^F^@_#`1Jv$6#`a
&+# $as   A-F)tie_last_hidden_statesr   r   r?   c                 N    | j                   D ]  } ||fi |} t        |      S )N)last_hidden_state)r   r   )rO   r   r   layer_modules       r7   rw   zPixioEncoder.forward   s5     !JJ 	BL(A&AM	B ??r6   )r(   r)   r*   r   rC   r   r   rE   rx   r
   r   r   rw   ry   rz   s   @r7   r   r      sU    {   E2@U\\ @VDV=W @\k @ 3  @r6   r   c            	       |     e Zd Zdef fdZdefdZee	 d	de	j                  dz  dee   defd              Z xZS )

PixioModelr>   c                     t         |   |       || _        t        |      | _        t        |      | _        t        j                  |j                  |j                        | _        | j                          y )Nr   )rB   rC   r>   r=   rQ   r   encoderr   r   r   r   	layernormr   r   s     r7   rC   zPixioModel.__init__   sW     )&1#F+f&8&8f>S>STr6   r?   c                 .    | j                   j                  S r   )rQ   rI   )rO   s    r7   get_input_embeddingszPixioModel.get_input_embeddings   s    ///r6   Nro   r   c                 H   |t        d      | j                  |      } | j                  |fi |}|j                  }| j	                  |      }|d d d | j                  j
                  d d f   j                  d      }t        |||j                  |j                        S )Nz You have to specify pixel_valuesrA   r\   )r   pooler_outputr   r   )

ValueErrorrQ   r   r   r   r$   meanr	   r   r   )rO   ro   r   embedding_outputencoder_outputssequence_outputpooled_outputs          r7   rw   zPixioModel.forward   s     ?@@??<8+74<<8H+SF+S);;..9'+IT__-I-I+I1(LMRRWXRY)-')77&11	
 	
r6   r   )r(   r)   r*   r   rC   r9   r   r   r   rE   rx   r
   r   r	   rw   ry   rz   s   @r7   r   r      sh    	{ 	0&: 0  -1
llT)
 +,
 
$	
  
r6   r   zN
    Pixio backbone, to be used with frameworks like DETR and MaskFormer.
    )custom_introc                   :    e Zd Zdej                  dee   defdZy)PixioBackbonero   r   r?   c                    d|d<   | j                  |      } | j                  |fi |}|j                  }g }t        | j                  |      D ]  \  }}|| j
                  v s| j                  j                  r| j                  |      }| j                  j                  r|dd| j                   j                  df   }|j                  \  }	}
}}| j                  j                  }|j                  |	||z  ||z  d      }|j                  dddd      j                         }|j!                  |        t#        t%        |      ||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_statesNrU   r   r   rA   r   )feature_mapsr   r   )rQ   r   r   zipstage_namesout_featuresr>   apply_layernormr   reshape_hidden_statesr$   r^   r'   r_   r`   
contiguousappendr   r0   r   )rO   ro   r   r   outputr   r   stagehidden_statert   ru   rR   rS   r'   s                 r7   rw   zPixioBackbone.forward	  sM   6 *.%&??<8".$,,/?"J6"J,,#&t'7'7#G 
	2E<)));;..#'>>,#?L;;44#/4??3O3O3Q0Q#RL3?3E3E0J65!%!7!7J#/#7#7
FjDXZ_cmZmoq#rL#/#7#71a#C#N#N#PL##L1
	2 |,'((
 	
r6   N)	r(   r)   r*   rE   rx   r
   r   r   rw   r5   r6   r7   r   r     s'    2
ELL 2
FCU<V 2
[i 2
r6   r   )r   r   r   r   )/r+   rE   huggingface_hub.dataclassesr   r   modeling_layersr   modeling_outputsr   r   r	   processing_utilsr
   utilsr   r   r   utils.genericr   r   utils.output_capturingr   dinov2.configuration_dinov2r   dinov2.modeling_dinov2r   r   r   vit.modeling_vitr   r   r   r   r   r9   Moduler=   r|   r~   r   r   r   r   r   r   r   __all__r5   r6   r7   <module>r      s0     .  9 [ [ & C C I 5 6 
 f e 01%&, %&  2%&P	- 	Dbii DN	) 	4\ 4	N 		y 	+ 2- @' @  %
% %
 %
P 
3
N 3

3
l Qr6   