
    ^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#47Jd3i%S/1746Jd3i%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    ekk!V5H5H&J\J\&]^ 4V <++77#%<<A{VM`M`?`bhbtbt0u#v zz&"<"<="// ++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   Ng      ?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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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$
Avu,,77>>DD**<???+NO
^^**:r2>
YY
J7Q?
$"?"?
FTY"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ELL 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@     AGAVAVY\A\v'<'<=bdbmbmbo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>>-R6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mmvG_G_A`$aAZ%7$abf&8&8f>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!-J6JM	K}5%a)G4??+G+G)G&JKPPUV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
llT)
 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?WZ[?[9\]AV//]'
f&8&8f>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 *.%&",$**\>"TV"T,,#&t'7'7#G 
	2E<)));;..#'>>,#?L;;44#/4::3H3H3U3U3W0W#XL3?3E3E0J65!%!7!7J#/#7#7
FjDXZ_cmZmoq#rL#/#7#71a#C#N#N#PL##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
ll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bii DN	\ 		y 		L 	 4	- 	 '
% '
 '
T 
C
M#7 C

C
L Qr7   