
    cCi                        S r SSKrSSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJrJr  SSKJrJr  SSKJrJrJrJr  SSKJ r   SSK!J"r"J#r#  SSK$J%r%  \RL                  " \'5      r(\\" SS9 " S S\5      5       5       r)\\" SS9 " S S\5      5       5       r* " S S\	RV                  5      r, " S S\	RV                  5      r- " S S\	RV                  5      r. STS\	RV                  S \R^                  S!\R^                  S"\R^                  S#\\R^                     S$\0S%\04S& jjr1 " S' S(\	RV                  5      r2 " S) S*\	RV                  5      r3 " S+ S,\	RV                  5      r4 " S- S.\	RV                  5      r5 " S/ S0\	RV                  5      r6 " S1 S2\5      r7 " S3 S4\	RV                  5      r8 " S5 S6\	RV                  5      r9S7 r: " S8 S9\	RV                  5      r; " S: S;\	RV                  5      r< " S< S=\	RV                  5      r= " S> S?\	RV                  5      r>\ " S@ SA\5      5       r?\ " SB SC\?5      5       r@ " SD SE\	RV                  5      rA " SF SG\	RV                  5      rB " SH SI\	RV                  5      rC\" SJS9 " SK SL\?5      5       rD " SM SN\	RV                  5      rE " SO SP\	RV                  5      rF\ " SQ SR\?5      5       rG/ SSQrHg)UzPyTorch DPT (Dense Prediction Transformers) model.

This implementation is heavily inspired by OpenMMLab's implementation, found here:
https://github.com/open-mmlab/mmsegmentation/blob/master/mmseg/models/decode_heads/dpt_head.py.

    N)	dataclass)CallableOptional)nn)CrossEntropyLoss   )ACT2FN)GradientCheckpointingLayer)BaseModelOutputDepthEstimatorOutputSemanticSegmenterOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel) find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputauto_docstringlogging	torch_int)load_backbone)can_return_tuplecheck_model_inputs   )	DPTConfigz
    Base class for model's outputs that also contains intermediate activations that can be used at later stages. Useful
    in the context of Vision models.:
    )custom_introc                   t    \ rS rSr% SrSr\\R                     \	S'   Sr
\\\R                  S4      \	S'   Srg)*BaseModelOutputWithIntermediateActivations,   aW  
last_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
    Sequence of hidden-states at the output of the last layer of the model.
intermediate_activations (`tuple(torch.FloatTensor)`, *optional*):
    Intermediate activations that can be used to compute hidden states of the model at various layers.
Nlast_hidden_states.intermediate_activations )__name__
__module____qualname____firstlineno____doc__r   r   torchFloatTensor__annotations__r    tuple__static_attributes__r!       ^/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/dpt/modeling_dpt.pyr   r   ,   s?     7;!2!23:HLhuU->->-C'DELr,   r   z
    Base class for model's outputs that also contains a pooling of the last hidden states as well as intermediate
    activations that can be used by the model at later stages.
    c                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S	'   S
rg)4BaseModelOutputWithPoolingAndIntermediateActivations?   a  
pooler_output (`torch.FloatTensor` of shape `(batch_size, hidden_size)`):
    Last layer hidden-state of the first token of the sequence (classification token) after further processing
    through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns
    the classification token after processing through a linear layer and a tanh activation function. The linear
    layer weights are trained from the next sentence prediction (classification) objective during pretraining.
intermediate_activations (`tuple(torch.FloatTensor)`, *optional*):
    Intermediate activations that can be used to compute hidden states of the model at various layers.
Nlast_hidden_statepooler_output.hidden_states
attentionsr    r!   )r"   r#   r$   r%   r&   r1   r   r'   r(   r)   r2   r3   r*   r4   r    r+   r!   r,   r-   r/   r/   ?   s     6:x 1 12915M8E--.5=AM8E%"3"3S"89:A:>Ju00#567>HLhuU->->-C'DELr,   r/   c                      ^  \ rS rSrSrSS\S\\\\4      4U 4S jjjr	SS jr
 SS\R                  S\S	\4S
 jjrSrU =r$ )DPTViTHybridEmbeddingsX   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.
configfeature_sizec                 x  > [         T
U ]  5         UR                  UR                  pCUR                  UR
                  pe[        U[        R                  R                  5      (       a  UOX34n[        U[        R                  R                  5      (       a  UOXD4nUS   US   -  US   US   -  -  n[        U5      U l        U R                  R                  S   n[        U R                  R                  5      S:w  a+  [        S[        U R                  R                  5       35      eSS/U l        Uc  UR                   n	U	SS  nU	S   nOG[        U[        R                  R                  5      (       a  UOX"4nU R                  R                  S   nX0l        US   U l        XPl        ["        R$                  " XSS9U l        ["        R(                  " [*        R,                  " SSUR
                  5      5      U l        ["        R(                  " [*        R,                  " SUS-   UR
                  5      5      U l        g )Nr   r   r   z1Expected backbone to have 3 output features, got kernel_size)super__init__
image_size
patch_sizenum_channelshidden_size
isinstancecollectionsabcIterabler   backbonechannelslen
ValueErrorresidual_feature_map_indexbackbone_featmap_shaper   Conv2d
projection	Parameterr'   zeros	cls_tokenposition_embeddings)selfr8   r9   rA   rB   rC   rD   num_patchesfeature_dimfeat_map_shape	__class__s             r-   r@   DPTViTHybridEmbeddings.__init___   s   !'!2!2F4E4EJ$*$7$79K9Kk#-j+//:R:R#S#SZZdYq
#-j+//:R:R#S#SZZdYq
!!}
15*Q-:VW=:XY%f-mm,,R0t}}%%&!+PQTUYUbUbUkUkQlPmnoo+,a&'#::N)"#.L(+K !+<9Q9Q R RYeXt  --004K$$Q-())K!Lekk!Q8J8J&KL#%<<A{QPVPbPb0c#d r,   c                 `   US S 2S U24   nUSUS 24   n[        [        U5      S-  5      nUR                  SXwS5      R                  SSSS5      n[        R
                  R                  XbU4SS9nUR                  SSSS5      R                  SX#-  S5      n[        R                  " XV/SS	9nU$ 
Nr         ?r   r;   r      bilinear)sizemodedim)	r   rK   reshapepermuter   
functionalinterpolater'   catrU   posembgrid_size_heightgrid_size_widthstart_index
posemb_tokposemb_gridold_grid_sizes           r-   _resize_pos_embed(DPTViTHybridEmbeddings._resize_pos_embed   s    A||O,
Q_-!#k"2c"9:!))!]2NVVWXZ[]^`abmm//UdBelv/w!))!Q15==aAQAceghJ4!<r,   pixel_valuesinterpolate_pos_encodingreturnc                    UR                   u  p4pVX@R                  :w  a  [        S5      eU(       dV  XPR                  S   :w  d  X`R                  S   :w  a2  [        SU SU SU R                  S    SU R                  S    S3	5      eU R	                  U R
                  XPR                  -  X`R                  -  5      nU R                  U5      nUR                  S   n	U R                   V
s/ s H  oR                  U
   PM     nn
U R                  U	5      R                  S	5      R                  SS	5      nU R                  R                  USS5      n[        R                   " X4SS
9nX-   n[#        UUS9$ s  sn
f )NeMake sure that the channel dimension of the pixel values match with the one set in the configuration.r   r   zInput image size (*z) doesn't match model (z).r;   r^   rb   )r   r    )shaperC   rL   rA   rq   rT   rB   rI   feature_mapsrM   rP   flatten	transposerS   expandr'   rh   r   )rU   rs   rt   
batch_sizerC   heightwidthrT   backbone_outputfeaturesindexoutput_hidden_states
embeddings
cls_tokenss                 r-   forwardDPTViTHybridEmbeddings.forward   s    3?2D2D/
&,,,w  (++u8J/J (% 9+,Adooa.@-AE 
 #44$$f&?//AY
 --5"//3 RVQpQpqQp < <U CQpq__X.66q9CCAqI
^^**:r2>
YY
7Q?
  5
 :)%9
 	
  rs   *E5)rI   rS   rA   rC   rB   rT   rP   rM   Nr   )F)r"   r#   r$   r%   r&   r   r   r*   intr@   rq   r'   Tensorboolr   r   r+   __classcell__rY   s   @r-   r6   r6   X   sd     ey  esCx8Q  e  eD LQ&
!LL&
DH&
	3&
 &
r,   r6   c                   \   ^  \ rS rSrSrU 4S jrS	S jrS\R                  S\	4S jr
SrU =r$ )
DPTViTEmbeddings   z:
Construct the CLS token, position and patch embeddings.

c                   > [         TU ]  5         [        R                  " [        R
                  " SSUR                  5      5      U l        [        U5      U l	        U R                  R                  n[        R                  " [        R
                  " SUS-   UR                  5      5      U l        [        R                  " UR                  5      U l        Xl        g )Nr   )r?   r@   r   rQ   r'   rR   rD   rS   DPTViTPatchEmbeddingspatch_embeddingsrV   rT   Dropouthidden_dropout_probdropoutr8   )rU   r8   rV   rY   s      r-   r@   DPTViTEmbeddings.__init__   s    ekk!Q8J8J&KL 5f =++77#%<<A{QPVPbPb0c#d zz&"<"<=r,   c                 l   US S 2S U24   nUSUS 24   n[        UR                  S5      S-  5      nUR                  SXwS5      R                  SSSS5      n[        R
                  R                  XbU4SS9nUR                  SSSS5      R                  SX#-  S5      n[        R                  " XV/SS	9nU$ r\   )	r   r`   rd   re   r   rf   rg   r'   rh   ri   s           r-   rq   "DPTViTEmbeddings._resize_pos_embed   s    A||O,
Q_-!+"2"21"5"<=!))!]2NVVWXZ[]^`abmm//UdBelv/w!))!Q15==aAQAceghJ4!<r,   rs   ru   c                 x   UR                   u  p#pEU R                  R                  nU R                  U R                  XF-  XV-  5      nU R                  U5      nUR                  5       u  p)n
U R                  R                  USS5      n[        R                  " X4SS9nX-   nU R                  U5      n[        US9$ )Nr;   r   rb   )r   )ry   r8   rB   rq   rT   r   r`   rS   r}   r'   rh   r   r   )rU   rs   r~   rC   r   r   rB   rT   r   seq_len_r   s               r-   r   DPTViTEmbeddings.forward   s    2>2D2D/
& [[++
"44$$f&:E<O
 **<8
!+!2
Q ^^**:r2>
YY
7Q?
  5
\\*-
9ZXXr,   )rS   r8   r   r   rT   r   )r"   r#   r$   r%   r&   r@   rq   r'   r   r   r   r+   r   r   s   @r-   r   r      s3    
YELL Y5_ Y Yr,   r   c                   n   ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	r      z
Image to Patch Embedding.

r8   c                   > [         TU ]  5         UR                  UR                  p2UR                  UR
                  pT[        U[        R                  R                  5      (       a  UOX"4n[        U[        R                  R                  5      (       a  UOX34nUS   US   -  US   US   -  -  nX l        X0l        X@l        X`l
        [        R                  " XEX3S9U l        g )Nr   r   )r>   stride)r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rV   r   rO   rP   )rU   r8   rA   rB   rC   rD   rV   rY   s          r-   r@   DPTViTPatchEmbeddings.__init__   s    !'!2!2F4E4EJ$*$7$79K9Kk#-j+//:R:R#S#SZZdYq
#-j+//:R:R#S#SZZdYq
!!}
15*Q-:VW=:XY$$(&))L:ir,   rs   ru   c                     UR                   u  p#pEX0R                  :w  a  [        S5      eU R                  U5      R	                  S5      R                  SS5      nU$ )Nrw   r^   r   )ry   rC   rL   rP   r{   r|   )rU   rs   r~   rC   r   r   r   s          r-   r   DPTViTPatchEmbeddings.forward  s\    2>2D2D/
&,,,w  __\2::1=GG1M
r,   )rA   rC   rV   rB   rP   r"   r#   r$   r%   r&   r   r@   r'   r   r   r+   r   r   s   @r-   r   r      s6    
jy jELL U\\  r,   r   modulequerykeyvalueattention_maskscalingr   c                    [         R                  " XR                  SS5      5      U-  n[        R                  R                  US[         R                  S9R                  UR                  5      n[        R                  R                  XU R                  S9nUb  X-  n[         R                  " X5      n	U	R                  SS5      R                  5       n	X4$ )Nr;   r<   )rc   dtype)ptrainingr   r^   )r'   matmulr|   r   rf   softmaxfloat32tor   r   r   
contiguous)
r   r   r   r   r   r   r   kwargsattn_weightsattn_outputs
             r-   eager_attention_forwardr     s     <<}}R'<=GL ==((2U]](SVVW\WbWbcL ==((6??([L !#4,,|3K''1-88:K$$r,   c            	          ^  \ rS rSrS\4U 4S jjr S	S\R                  S\\R                     S\	\R                  \R                  4   4S jjr
SrU =r$ )
DPTSelfAttentioni.  r8   c                 0  > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eXl        UR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l	        UR                  U l        U R                  S-  U l        SU l        [        R                  " UR                  U R                  UR                   S9U l        [        R                  " UR                  U R                  UR                   S9U l        [        R                  " UR                  U R                  UR                   S9U l        g )	Nr   embedding_sizezThe hidden size z4 is not a multiple of the number of attention heads .g      F)bias)r?   r@   rD   num_attention_headshasattrrL   r8   r   attention_head_sizeall_head_sizeattention_probs_dropout_probdropout_probr   	is_causalr   Linearqkv_biasr   r   r   rU   r8   rY   s     r-   r@   DPTSelfAttention.__init__/  sG    : ::a?PVXhHiHi"6#5#5"6 7334A7 
 #)#=#= #&v'9'9F<V<V'V#W !558P8PP"??//5YYv1143E3EFOO\
99V//1C1C&//ZYYv1143E3EFOO\
r,   r3   	head_maskru   c                    UR                   S   nUSU R                  U R                  4nU R                  U5      R                  " U6 R                  SS5      nU R                  U5      R                  " U6 R                  SS5      nU R                  U5      R                  " U6 R                  SS5      n[        nU R                  R                  S:w  a  [        U R                  R                     nU" U UUUUU R                  U R                  U R                  (       d  SOU R                  S9u  pU	R!                  5       S S U R"                  4-   nU	R%                  U5      n	X4$ )	Nr   r;   r   r^   eager        )r   r   r   r<   )ry   r   r   r   viewr|   r   r   r   r8   _attn_implementationr   r   r   r   r   r`   r   rd   )rU   r3   r   r~   	new_shape	key_layervalue_layerquery_layerattention_interfacecontext_layerattention_probsnew_context_layer_shapes               r-   r   DPTSelfAttention.forwardC  sH    #((+
D$<$<d>V>VV	HH]+00)<FFq!L	jj/44i@JJ1aPjj/44i@JJ1aP(?;;++w6"9$++:Z:Z"[)<nnLL#}}C$2C2C	*
& #0"4"4"6s";t?Q?Q>S"S%--.EF--r,   )
r   r   r8   r   r   r   r   r   r   r   r   )r"   r#   r$   r%   r   r@   r'   r   r   r*   r   r+   r   r   s   @r-   r   r   .  sY    ]y ]* PT."\\.6>u||6L.	u||U\\)	*. .r,   r   c                      ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  S\R                  4S jr	S	r
U =r$ )
DPTViTSelfOutputic  z
The residual connection is defined in ViTLayer instead of here (as is the case with other models), due to the
layernorm applied before each block.
r8   c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  5      U l        g r   )	r?   r@   r   r   rD   denser   r   r   r   s     r-   r@   DPTViTSelfOutput.__init__i  sB    YYv1163E3EF
zz&"<"<=r,   r3   input_tensorru   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r   rU   r3   r   s      r-   r   DPTViTSelfOutput.forwardn  s$    

=1]3r,   r   r   r   s   @r-   r   r   c  sB    
>y >
U\\  RWR^R^  r,   r   c                      ^  \ rS rSrS\4U 4S jjrS\\   4S jrSS\	R                  S\\	R                     S\	R                  4S	 jjrS
rU =r$ )DPTViTAttentioniu  r8   c                    > [         TU ]  5         [        U5      U l        [	        U5      U l        [        5       U l        g r   )r?   r@   r   	attentionr   outputsetpruned_headsr   s     r-   r@   DPTViTAttention.__init__v  s0    )&1&v.Er,   headsc                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r   rb   )rK   r   r   r   r   r   r   r   r   r   r   r   r   union)rU   r   r   s      r-   prune_headsDPTViTAttention.prune_heads|  s   u:?7>>55t~~7Y7Y[_[l[l

  2$..2F2FN/0B0BEJ1$..2F2FN.t{{/@/@%QO .2^^-O-ORUV[R\-\*'+~~'I'IDNNLnLn'n$ --33E:r,   r3   r   ru   c                 N    U R                  X5      u  p4U R                  X15      nU$ r   )r   r   )rU   r3   r   self_attn_outputr   r   s         r-   r   DPTViTAttention.forward  s(    "nn]F-=r,   )r   r   r   r   )r"   r#   r$   r%   r   r@   r   r   r   r'   r   r   r   r+   r   r   s   @r-   r   r   u  sR    "y ";S ;$U\\ hu||>T `e`l`l  r,   r   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )DPTViTIntermediatei  r8   c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g r   )r?   r@   r   r   rD   intermediate_sizer   rE   
hidden_actstrr	   intermediate_act_fnr   s     r-   r@   DPTViTIntermediate.__init__  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$r,   r3   ru   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r   )rU   r3   s     r-   r   DPTViTIntermediate.forward  s&    

=100?r,   r   r"   r#   r$   r%   r   r@   r'   r   r   r+   r   r   s   @r-   r   r     s/    9y 9U\\ ell  r,   r   c                      ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  S\R                  4S jrSr	U =r
$ )	DPTViTOutputi  r8   c                    > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR                  5      U l	        g r   )
r?   r@   r   r   r   rD   r   r   r   r   r   s     r-   r@   DPTViTOutput.__init__  sB    YYv779K9KL
zz&"<"<=r,   r3   r   ru   c                 R    U R                  U5      nU R                  U5      nX-   nU$ r   r   r   s      r-   r   DPTViTOutput.forward  s,    

=1]3%4r,   r   r   r   s   @r-   r  r    s=    >y >
U\\  RWR^R^  r,   r  c                      ^  \ rS rSrSrS\4U 4S jjrS
S\R                  S\	\R                     S\R                  4S jjr
S	rU =r$ )DPTViTLayeri  z?This corresponds to the Block class in the timm implementation.r8   c                 j  > [         TU ]  5         UR                  U l        SU l        [	        U5      U l        [        U5      U l        [        U5      U l	        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  UR                  S9U l        g )Nr   eps)r?   r@   chunk_size_feed_forwardseq_len_dimr   r   r   intermediater  r   r   	LayerNormrD   layer_norm_epslayernorm_beforelayernorm_afterr   s     r-   r@   DPTViTLayer.__init__  s    '-'E'E$(0.v6"6* "V-?-?VEZEZ [!||F,>,>FDYDYZr,   r3   r   ru   c                     U R                  U5      nU R                  X25      nXA-   nU R                  U5      nU R                  U5      nU R	                  XQ5      nU$ r   )r  r   r  r  r   )rU   r3   r   hidden_states_normattention_outputlayer_outputs         r-   r   DPTViTLayer.forward  se    !22=A>>*<H )8 ++M:((6 {{<?r,   )r   r  r  r  r  r   r  r   )r"   r#   r$   r%   r&   r   r@   r'   r   r   r   r+   r   r   s   @r-   r  r    sG    I[y [U\\ hu||>T `e`l`l  r,   r  c            	       ~   ^  \ rS rSrS\4U 4S jjr S
S\R                  S\\R                     S\	S\
4S jjrS	rU =r$ )DPTViTEncoderi  r8   c                    > [         TU ]  5         Xl        [        R                  " [        UR                  5       Vs/ s H  n[        U5      PM     sn5      U l        SU l	        g s  snf NF)
r?   r@   r8   r   
ModuleListrangenum_hidden_layersr  layergradient_checkpointingrU   r8   r   rY   s      r-   r@   DPTViTEncoder.__init__  sR    ]]vG_G_A`#aA`AK$7A`#ab
&+# $bs   A&r3   r   r   ru   c                     U(       a  U/OS n[        U R                  5       H0  u  pVUb  X%   OS nU" X5      nU(       d  M  UR                  U5        M2     [        UU(       a  [	        U5      S9$ S S9$ )N)r1   r3   )	enumerater   appendr   r*   )rU   r3   r   r   all_hidden_statesilayer_modulelayer_head_masks           r-   r   DPTViTEncoder.forward  s|     0D]O(4OA.7.CilO(HM  !((7	  5 +6G% 12
 	
MQ
 	
r,   )r8   r!  r   r  )r"   r#   r$   r%   r   r@   r'   r   r   r   r   r   r+   r   r   s   @r-   r  r    sM    ,y , sx
"\\
6>u||6L
ko
	
 
r,   r  c                      ^  \ rS rSrSrU 4S jrS rS rS
S\\	R                     S\\	R                     4S jjrS	rU =r$ )DPTReassembleStagei  a  
This class reassembles the hidden states of the backbone into image-like feature representations at various
resolutions.

This happens in 3 stages:
1. Map the N + 1 tokens to a set of N tokens, by taking into account the readout ([CLS]) token according to
   `config.readout_type`.
2. Project the channel dimension of the hidden states according to `config.neck_hidden_sizes`.
3. Resizing the spatial dimensions (height, width).

Args:
    config (`[DPTConfig]`):
        Model configuration class defining the model architecture.
c                    > [         TU ]  5         Xl        [        R                  " 5       U l        UR                  (       a  U R                  U5        OU R                  U5        UR                  U l	        g r   )
r?   r@   r8   r   r  layers	is_hybrid_init_reassemble_dpt_hybrid_init_reassemble_dptneck_ignore_stagesr   s     r-   r@   DPTReassembleStage.__init__  sS    mmo,,V4%%f-"(";";r,   c           	         [        [        [        UR                  5      5      UR                  5       Hs  u  p#US::  a0  U R
                  R                  [        R                  " 5       5        M;  US:  d  MC  U R
                  R                  [        XR                  U   US95        Mu     UR                  S:w  a  [        SUR                   S35      e[        R                  " 5       U l        [        U5      n[        [        UR                  5      5       H  nUS::  aD  U R                  R                  [        R                  " [        R                  " 5       5      5        MM  US:  d  MU  U R                  R                  [        R                  " [        R                   " SU-  U5      ["        UR$                     5      5        M     g)z"
For DPT-Hybrid the first 2 reassemble layers are set to `nn.Identity()`, please check the official
implementation: https://github.com/isl-org/DPT/blob/f43ef9e08d70a752195028a51be5e1aff227b913/dpt/vit.py#L438
for more details.
r   rJ   factorprojectzReadout type z! is not supported for DPT-Hybrid.r^   N)zipr  rK   neck_hidden_sizesreassemble_factorsr/  r&  r   IdentityDPTReassembleLayerreadout_typerL   r  readout_projects_get_backbone_hidden_size
Sequentialr   r	   r   )rU   r8   r(  r7  rD   s        r-   r1  .DPTReassembleStage._init_reassemble_dpt_hybrid  sI    U3v'?'?#@A6C\C\]IAAv""2;;=1Q""#5fG_G_`aGbkq#rs	 ^ )+}V-@-@,AAbcdd !#/7s63345AAv%%,,R]]2;;=-IJQ%%,,MM"))AO["I6RXRcRcKde	 6r,   c           	      B   [        [        [        UR                  5      5      UR                  5       H5  u  p#U R
                  R                  [        XR                  U   US95        M7     UR                  S:X  a  [        R                  " 5       U l        [        U5      n[        [        UR                  5      5       H\  nU R                  R                  [        R                  " [        R                  " SU-  U5      [        UR                      5      5        M^     g g )Nr6  r8  r^   )r9  r  rK   r:  r;  r/  r&  r=  r>  r   r  r?  r@  rA  r   r	   r   )rU   r8   r(  r7  rD   r   s         r-   r2  'DPTReassembleStage._init_reassemble_dpt  s    U3v'?'?#@A6C\C\]IAKK1&C[C[\]C^gmno ^ )+$&MMOD!3F;K3v7789%%,,MM"))AO["I6RXRcRcKde : ,r,   r3   ru   c                    / n[        U5       GH  u  pVXPR                  ;  Ga  USS2S4   USS2SS24   pgUR                  u  pn
Ub  Ub  UR                  XX:5      nO [	        U	S-  5      nUR                  XX5      nUR                  SSSS5      R                  5       nUR                  nU R                  R                  S:X  a  UR                  S5      R                  S5      nUR                  S5      R                  U5      nU R                  U   " [        R                  " Xm4S	5      5      nUR                  SSS5      R                  U5      nONU R                  R                  S
:X  a4  UR                  S5      UR                  S	5      -   nUR                  U5      nU R                  U   " U5      nUR!                  U5        GM     U$ )z
Args:
    hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`):
        List of hidden states from the backbone.
Nr   r   r]   r   r^   r8  )r   r^   r   r;   add)r%  r3  ry   rd   r   re   r   r8   r>  r{   	unsqueeze	expand_asr?  r'   rh   r/  r&  )rU   r3   patch_heightpatch_widthoutr(  hidden_staterS   r~   sequence_lengthrC   r`   feature_shapereadouts                 r-   r   DPTReassembleStage.forward+  s    (7OA///*6q!t*<l1ab5>Q<<H<N<N9
\+0G#/#7#7
R]#lL$_c%9:D#/#7#7
$#]L+33Aq!Q?JJL , 2 2;;++y8#/#7#7#:#B#B9#ML'11!4>>|LG#'#8#8#;EII|F]_a<b#cL#/#7#71a#@#H#H#WL[[--6#/#7#7#:Y=P=PQS=T#TL#/#7#7#FL#{{1~l;JJ|$3  86 
r,   )r8   r/  r3  r?  NN)r"   r#   r$   r%   r&   r@   r1  r2  listr'   r   r   r+   r   r   s   @r-   r-  r-    sE    
<4
#T%,,%7 #aefkfrfras # #r,   r-  c                 ~    U R                   b%  U R                  SL a  U R                   R                  $ U R                  $ r  )backbone_configr0  rD   )r8   s    r-   r@  r@  Q  s9    )f.>.>%.G%%111!!!r,   c                   >   ^  \ rS rSrS\S\S\4U 4S jjrS rSrU =r	$ )r=  iX  r8   rJ   r7  c           	      P  > [         TU ]  5         [        U5      n[        R                  " XBSS9U l        US:  a  [        R                  " X"X3SS9U l        g US:X  a  [        R                  " 5       U l        g US:  a)  [        R                  " X"S[        SU-  5      SS9U l        g g )Nr   )in_channelsout_channelsr>   r   r>   r   paddingr   )
r?   r@   r@  r   rO   rP   ConvTranspose2dresizer<  r   )rU   r8   rJ   r7  rD   rY   s        r-   r@   DPTReassembleLayer.__init__Y  s    /7))`ab A:,,XVlmnDKq[++-DKaZ))HAcRSV\R\oghiDK r,   c                 J    U R                  U5      nU R                  U5      nU$ r   rP   r\  )rU   rL  s     r-   r   DPTReassembleLayer.forwardh  s$    |4{{<0r,   r_  )
r"   r#   r$   r%   r   r   r@   r   r+   r   r   s   @r-   r=  r=  X  s+    jy jC j j r,   r=  c                   6   ^  \ rS rSrS\4U 4S jjrS rSrU =r$ )DPTFeatureFusionStagein  r8   c                    > [         TU ]  5         [        R                  " 5       U l        [        [        UR                  5      5       H'  nU R                  R                  [        U5      5        M)     g r   )
r?   r@   r   r  r/  r  rK   r:  r&  DPTFeatureFusionLayerr"  s      r-   r@   DPTFeatureFusionStage.__init__o  sM    mmos63345AKK4V<= 6r,   c                     US S S2   n/ nS n[        XR                  5       H*  u  pEUc	  U" U5      nOU" X45      nUR                  U5        M,     U$ )Nr;   )r9  r/  r&  )rU   r3   fused_hidden_statesfused_hidden_staterL  r   s         r-   r   DPTFeatureFusionStage.forwardu  sg    %dd+ !#&}kk#BL!)%*<%8"%*+=%L"&&'9: $C #"r,   )r/  )	r"   r#   r$   r%   r   r@   r   r+   r   r   s   @r-   rb  rb  n  s    >y ># #r,   rb  c                   n   ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	DPTPreActResidualLayeri  z
ResidualConvUnit, pre-activate residual unit.

Args:
    config (`[DPTConfig]`):
        Model configuration class defining the model architecture.
r8   c           	        > [         TU ]  5         UR                  U l        UR                  b  UR                  OU R                  (       + n[
        R                  " 5       U l        [
        R                  " UR                  UR                  SSSUS9U l
        [
        R                  " 5       U l        [
        R                  " UR                  UR                  SSSUS9U l        U R                  (       aK  [
        R                  " UR                  5      U l        [
        R                  " UR                  5      U l        g g )Nr   r   )r>   r   rZ  r   )r?   r@   !use_batch_norm_in_fusion_residualuse_batch_normuse_bias_in_fusion_residualr   ReLUactivation1rO   fusion_hidden_sizeconvolution1activation2convolution2BatchNorm2dbatch_norm1batch_norm2)rU   r8   ro  rY   s      r-   r@   DPTPreActResidualLayer.__init__  s   $FF 11= ..((( 	$ 779II%%%%,
 779II%%%%,
 !~~f.G.GHD!~~f.G.GHD r,   rL  ru   c                    UnU R                  U5      nU R                  U5      nU R                  (       a  U R                  U5      nU R	                  U5      nU R                  U5      nU R                  (       a  U R                  U5      nX-   $ r   )rq  rs  rn  rw  rt  ru  rx  rU   rL  residuals      r-   r   DPTPreActResidualLayer.forward  s    ''5((6++L9L''5((6++L9L&&r,   )rq  rt  rw  rx  rs  ru  rn  r   r   s   @r-   rk  rk    s7     Iy  ID'ELL 'U\\ ' 'r,   rk  c                      ^  \ rS rSrSrSS\S\4U 4S jjjrSS\R                  S\
\R                     S\R                  4S	 jjrS
rU =r$ )rd  i  a  Feature fusion layer, merges feature maps from different stages.

Args:
    config (`[DPTConfig]`):
        Model configuration class defining the model architecture.
    align_corners (`bool`, *optional*, defaults to `True`):
        The align_corner setting for bilinear upsample.
r8   align_cornersc                    > [         TU ]  5         X l        [        R                  " UR
                  UR
                  SSS9U l        [        U5      U l        [        U5      U l	        g )Nr   T)r>   r   )
r?   r@   r  r   rO   rr  rP   rk  residual_layer1residual_layer2)rU   r8   r  rY   s      r-   r@   DPTFeatureFusionLayer.__init__  sR    *))F$=$=v?X?Xfgnrs5f=5f=r,   rL  r|  ru   c                 t   Ubh  UR                   UR                   :w  a;  [        R                  R                  X!R                   S   UR                   S   4SSS9nXR	                  U5      -   nU R                  U5      n[        R                  R                  USSU R                  S9nU R                  U5      nU$ )Nr^   r   r_   Fr`   ra   r  scale_factorra   r  )ry   r   rf   rg   r  r  r  rP   r{  s      r-   r   DPTFeatureFusionLayer.forward  s    !!X^^3==44$6$6q$9<;M;Ma;P#QXbrw 5  (*>*>x*HHL++L9}}00qzI[I[ 1 
 |4r,   )r  rP   r  r  Tr   )r"   r#   r$   r%   r&   r   r   r@   r'   r   r   r   r+   r   r   s   @r-   rd  rd    sS    >y > > >ELL HU\\<R ^c^j^j  r,   rd  c                   J    \ rS rSr% \\S'   SrSrSrSr	Sr
SrSrS\0rS rSrg	)
DPTPreTrainedModeli  r8   dptrs   Tr4   c                    [        U[        R                  [        R                  [        R                  45      (       aj  UR
                  R                  R                  SU R                  R                  S9  UR                  b$  UR                  R                  R                  5         Ox[        U[        R                  [        R                  45      (       aI  UR                  R                  R                  5         UR
                  R                  R                  S5        [        U[        [         45      (       aI  UR"                  R                  R                  5         UR$                  R                  R                  5         gg)zInitialize the weightsr   )meanstdNg      ?)rE   r   r   rO   r[  weightdatanormal_r8   initializer_ranger   zero_r  rv  fill_r   r6   rS   rT   )rU   r   s     r-   _init_weights DPTPreTrainedModel._init_weights  s    fryy"))R5G5GHII MM&&CT[[5R5R&S{{&  &&(r~~ >??KK""$MM$$S)f/1GHII!!'')&&++113 Jr,   r!   N)r"   r#   r$   r%   r   r)   base_model_prefixmain_input_namesupports_gradient_checkpointing_supports_sdpa_supports_flash_attn_supports_flex_attn_supports_attention_backendr   _can_record_outputsr  r+   r!   r,   r-   r  r    sC    $O&*#N"&&4r,   r  c                      ^  \ rS rSrSS\S\4U 4S jjjrS rS r\	" SS9\
  SS	\R                  S
\\R                     S\\   S\4S jj5       5       rSrU =r$ )DPTModeli  r8   add_pooling_layerc                 b  > [         TU ]  U5        Xl        UR                  (       a  [	        U5      U l        O[        U5      U l        [        U5      U l        [        R                  " UR                  UR                  S9U l        U(       a  [        U5      OSU l        U R!                  5         g)z^
add_pooling_layer (bool, *optional*, defaults to `True`):
    Whether to add a pooling layer
r
  N)r?   r@   r8   r0  r6   r   r   r  encoderr   r  rD   r  	layernormDPTViTPoolerpooler	post_init)rU   r8   r  rY   s      r-   r@   DPTModel.__init__  s    
 	  4V<DO.v6DO$V,f&8&8f>S>ST.?l6*T 	r,   c                 |    U R                   R                  (       a  U R                  $ U R                  R                  $ r   )r8   r0  r   r   )rU   s    r-   get_input_embeddingsDPTModel.get_input_embeddings  s)    ;;  ??"??333r,   c                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)z
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
N)itemsr  r   r   r   )rU   heads_to_pruner   r   s       r-   _prune_headsDPTModel._prune_heads#  s<    
 +002LELLu%//;;EB 3r,   F)tie_last_hidden_statesrs   r   r   ru   c                    Uc  U R                   R                  nU R                  X R                   R                  5      nU R	                  U5      nUR
                  nU R                  XbUS9nUR                  nU R                  U5      nU R                  b  U R                  U5      OS n	[        UU	UR                  UR                  S9$ )Nr   r   )r1   r2   r    r3   )r8   r   get_head_maskr  r   r   r  r1   r  r  r/   r    r3   )
rU   rs   r   r   r   embedding_outputembedding_last_hidden_statesencoder_outputssequence_outputpooled_outputs
             r-   r   DPTModel.forward+  s      '#';;#C#C  &&y++2O2OP	GKWcGd'7'J'J$+/<<(Th ,8 ,
 *;;..98<8OO4UYC-'%5%N%N)77	
 	
r,   )r8   r   r  r  r  r  rQ  )r"   r#   r$   r%   r   r   r@   r  r  r   r   r'   r(   r   r/   r   r+   r   r   s   @r-   r  r    s    y T  *4C u5 26/3	!
''!
 E--.!
 'tn	!
 
>!
  6!
r,   r  c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )r  iR  r8   c                    > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                     U l	        g r   )
r?   r@   r   r   rD   pooler_output_sizer   r	   
pooler_act
activationr   s     r-   r@   DPTViTPooler.__init__S  s>    YYv1163L3LM
 !2!23r,   r3   ru   c                 \    US S 2S4   nU R                  U5      nU R                  U5      nU$ )Nr   )r   r  )rU   r3   first_token_tensorr  s       r-   r   DPTViTPooler.forwardX  s6     +1a40

#566r,   )r  r   r   r   s   @r-   r  r  R  s/    4y 4
U\\ ell  r,   r  c            
          ^  \ rS rSrSrS\4U 4S jjr  SS\\R                     S\
\   S\
\   S\\R                     4S	 jjrS
rU =r$ )DPTNeckia  a  
DPTNeck. A neck is a module that is normally used between the backbone and the head. It takes a list of tensors as
input and produces another list of tensors as output. For DPT, it includes 2 stages:

* DPTReassembleStage
* DPTFeatureFusionStage.

Args:
    config (dict): config dict.
r8   c                   > [         TU ]  5         Xl        UR                  b"  UR                  R                  S:X  a  S U l        O[        U5      U l        [        R                  " 5       U l	        UR                   H=  nU R                  R                  [        R                  " X!R                  SSSS95        M?     [        U5      U l        g )Nswinv2r   r   Fr>   rZ  r   )r?   r@   r8   rT  
model_typereassemble_stager-  r   r  convsr:  r&  rO   rr  rb  fusion_stage)rU   r8   channelrY   s      r-   r@   DPTNeck.__init__m  s     !!-&2H2H2S2SW_2_$(D!$6v$>D!]]_
//GJJbii1J1JXYcdkpqr 0 2&9r,   r3   rI  rJ  ru   c                    [        U[        [        45      (       d  [        S5      e[	        U5      [	        U R
                  R                  5      :w  a  [        S5      eU R                  b  U R                  XU5      n[        U5       VVs/ s H  u  pEU R                  U   " U5      PM     nnnU R                  U5      nU$ s  snnf )z
Args:
    hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`):
        List of hidden states from the backbone.
z2hidden_states should be a tuple or list of tensorszOThe number of hidden states should be equal to the number of neck hidden sizes.)rE   r*   rR  	TypeErrorrK   r8   r:  rL   r  r%  r  r  )rU   r3   rI  rJ  r(  featurer   r   s           r-   r   DPTNeck.forward~  s     -%77PQQ}T[[%B%B!CCnoo   , 11-{[M=F}=UV=UzqDJJqM'*=UV ""8, Ws   !C)r8   r  r  r  rQ  )r"   r#   r$   r%   r&   r   r@   rR  r'   r   r   r   r   r+   r   r   s   @r-   r  r  a  sf    	:y :( '+%)	ELL) sm c]	
 
ell	 r,   r  c                   t   ^  \ rS rSrSrS\4U 4S jjrS\\R                     S\R                  4S jr
SrU =r$ )	DPTDepthEstimationHeadi  z
Output head consisting of 3 convolutional layers. It progressively halves the feature dimension and upsamples
the predictions to the input resolution after the first convolutional layer (details can be found in the paper's
supplementary material).
r8   c                   > [         TU ]  5         Xl        S U l        UR                  (       a  [
        R                  " SSSSSS9U l        UR                  n[
        R                  " [
        R                  " X"S-  SSSS9[
        R                  " SSS	S
9[
        R                  " US-  SSSSS9[
        R                  " 5       [
        R                  " SSSSSS9[
        R                  " 5       5      U l        g )N   )r   r   )r   r   rY  r^   r   r   r_   Tr      r   )r?   r@   r8   rP   add_projectionr   rO   rr  rA  Upsamplerp  headrU   r8   r   rY   s      r-   r@   DPTDepthEstimationHead.__init__  s       iiSfV]cdDO,,MMIIhA1QPQRKKQZtLIIh!mRQq!LGGIIIb!1a@GGI
	r,   r3   ru   c                     XR                   R                     nU R                  b,  U R                  U5      n[        R                  " 5       " U5      nU R                  U5      nUR                  SS9nU$ )Nr   rb   )r8   head_in_indexrP   r   rp  r  squeeze)rU   r3   predicted_depths      r-   r   DPTDepthEstimationHead.forward  sc    %kk&?&?@??& OOM:MGGIm4M))M2)11a18r,   )r8   r  rP   )r"   r#   r$   r%   r&   r   r@   rR  r'   r   r   r+   r   r   s   @r-   r  r    s9    
y 
&T%,,%7 ELL  r,   r  zu
    DPT Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2.
    c                      ^  \ rS rSrU 4S jr\\   S
S\R                  S\	\R                     S\	\R                     S\	\   S\4
S jj5       5       rS	rU =r$ )DPTForDepthEstimationi  c                 $  > [         TU ]  U5        S U l        UR                  SL a+  UR                  c  UR                  b  [        U5      U l        O[        USS9U l        [        U5      U l	        [        U5      U l        U R                  5         g NF)r  )r?   r@   rI   r0  rT  r   r  r  r  neckr  r  r  r   s     r-   r@   DPTForDepthEstimation.__init__  s}     u$&*@*@*LPVP_P_Pk)&1DM%@DH FO	 +62	 	r,   rs   r   labelsr   ru   c                   ^  Uc  T R                   R                  nSnUb  [        S5      eT R                  b,  T R                  R                  " U4SS0UD6nUR
                  nOT R                  " U4USS.UD6nUR                  nT R                   R                  (       d?  [        USS 5       V	V
s/ s H#  u  pU	T R                   R                  ;   d  M!  U
PM%     nn	n
O5UR                  nUR                  U 4S j[        USS 5       5       5        UnSu  pT R                   R                  bS  T R                   R                  S	L a:  UR                  u    pnT R                   R                  R                  nUU-  nUU-  nT R!                  XU5      nT R#                  U5      n[%        UUU(       a  UR                  OSUR&                  S
9$ s  sn
n	f )a  
labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*):
    Ground truth depth estimation maps for computing the loss.

Examples:
```python
>>> from transformers import AutoImageProcessor, DPTForDepthEstimation
>>> import torch
>>> import numpy as np
>>> from PIL import Image
>>> import requests

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> image_processor = AutoImageProcessor.from_pretrained("Intel/dpt-large")
>>> model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")

>>> # prepare image for the model
>>> inputs = image_processor(images=image, return_tensors="pt")

>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> # interpolate to original size
>>> post_processed_output = image_processor.post_process_depth_estimation(
...     outputs,
...     target_sizes=[(image.height, image.width)],
... )

>>> # visualize the prediction
>>> predicted_depth = post_processed_output[0]["predicted_depth"]
>>> depth = predicted_depth * 255 / predicted_depth.max()
>>> depth = depth.detach().cpu().numpy()
>>> depth = Image.fromarray(depth.astype("uint8"))
```NzTraining is not implemented yetr   Tr  r   c              3   j   >#    U  H(  u  pUTR                   R                  S S ;   d  M$  Uv   M*     g7fr^   Nr8   backbone_out_indices.0idxr  rU   s      r-   	<genexpr>0DPTForDepthEstimation.forward.<locals>.<genexpr>  s5      .(Ddkk>>qrBB G(D   #3	3rQ  F)lossr  r3   r4   )r8   r   NotImplementedErrorrI   forward_with_filtered_kwargsrz   r  r3   r0  r%  r  r    extendrT  ry   rB   r  r  r   r4   )rU   rs   r   r  r   r   r  outputsr3   r  r  backbone_hidden_statesrI  rJ  r   r   r   rB   r  s   `                  r-   r   DPTForDepthEstimation.forward  s   ^  '#';;#C#C %&GHH==$mm@@sdhslrsG#00Mhh|fyW[f_efG#11M ;;((09-:K0L!0LPSW[WbWbWwWwPwG0L  ! *1)I)I&&-- .(1-2C(D. 
 !7$.!;;&&2t{{7L7LPU7U"."4"4Aq%44??J!Z/L:-K		-{K))M2#+3G'//T))	
 	
-!s   2 GG)rI   r  r  r  )NNN)r"   r#   r$   r%   r@   r   r   r'   r(   r   
LongTensorr   r   r   r+   r   r   s   @r-   r  r    s    $  26-1/3X
''X
 E--.X
 ))*	X

 'tnX
 
X
  X
r,   r  c                   p   ^  \ rS rSrS\4U 4S jjrS\\R                     S\R                  4S jr	Sr
U =r$ )DPTSemanticSegmentationHeadi8  r8   c                   > [         TU ]  5         Xl        UR                  n[        R
                  " [        R                  " X"SSSS9[        R                  " U5      [        R                  " 5       [        R                  " UR                  5      [        R                  " X!R                  SS9[        R                  " SSSS	95      U l        g )
Nr   r   Fr  r=   r^   r_   Tr  )r?   r@   r8   rr  r   rA  rO   rv  rp  r   semantic_classifier_dropout
num_labelsr  r  r  s      r-   r@   $DPTSemanticSegmentationHead.__init__9  s    ,,MMIIhaONN8$GGIJJv99:IIh 1 1qAKKQZtL
	r,   r3   ru   c                 X    XR                   R                     nU R                  U5      nU$ r   )r8   r  r  rU   r3   logitss      r-   r   #DPTSemanticSegmentationHead.forwardG  s'    %kk&?&?@=)r,   )r8   r  )r"   r#   r$   r%   r   r@   rR  r'   r   r   r+   r   r   s   @r-   r  r  8  s4    
y 
T%,,%7 ELL  r,   r  c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )DPTAuxiliaryHeadiN  r8   c                 T  > [         TU ]  5         UR                  n[        R                  " [        R
                  " X"SSSS9[        R                  " U5      [        R                  " 5       [        R                  " SS5      [        R
                  " X!R                  SS95      U l
        g )Nr   r   Fr  g?r=   )r?   r@   rr  r   rA  rO   rv  rp  r   r  r  r  s      r-   r@   DPTAuxiliaryHead.__init__O  sr    ,,MMIIhaONN8$GGIJJsE"IIh 1 1qA
	r,   r3   ru   c                 (    U R                  U5      nU$ r   r  r  s      r-   r   DPTAuxiliaryHead.forward[  s    =)r,   r  r   r   s   @r-   r  r  N  s/    

y 

U\\ ell  r,   r  c                      ^  \ rS rSrS\4U 4S jjr\\    SS\\	R                     S\\	R                     S\\	R                     S\\   S\4
S	 jj5       5       rS
rU =r$ )DPTForSemanticSegmentationi`  r8   c                    > [         TU ]  U5        [        USS9U l        [	        U5      U l        [        U5      U l        UR                  (       a  [        U5      OS U l
        U R                  5         g r  )r?   r@   r  r  r  r  r  r  use_auxiliary_headr  auxiliary_headr  r   s     r-   r@   #DPTForSemanticSegmentation.__init__b  s^     Fe< FO	 07	:@:S:S.v6Y] 	r,   rs   r   r  r   ru   c                    ^  Uc  T R                   R                  nUb%  T R                   R                  S:X  a  [        S5      eT R                  " U4USS.UD6nUR
                  nT R                   R                  (       d?  [        USS 5       VV	s/ s H#  u  pUT R                   R                  ;   d  M!  U	PM%     nnn	O5UR                  n
U
R                  U 4S j[        USS 5       5       5        U
nT R                  US9nT R                  U5      nSnT R                  b  T R                  US   5      nSnUb  [        R                  R!                  XR"                  S	S S
SS9nUb,  [        R                  R!                  XR"                  S	S S
SS9n[%        T R                   R&                  S9nU" X5      nU" WU5      nUT R                   R(                  U-  -   n[+        UUU(       a  UR
                  OSUR,                  S9$ s  sn	nf )aL  
labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*):
    Ground truth semantic segmentation maps for computing the loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels > 1`, a classification loss is computed (Cross-Entropy).

Examples:
```python
>>> from transformers import AutoImageProcessor, DPTForSemanticSegmentation
>>> from PIL import Image
>>> import requests

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> image_processor = AutoImageProcessor.from_pretrained("Intel/dpt-large-ade")
>>> model = DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade")

>>> inputs = image_processor(images=image, return_tensors="pt")

>>> outputs = model(**inputs)
>>> logits = outputs.logits
```Nr   z/The number of labels should be greater than oneTr  c              3   j   >#    U  H(  u  pUTR                   R                  S S ;   d  M$  Uv   M*     g7fr  r  r  s      r-   r  5DPTForSemanticSegmentation.forward.<locals>.<genexpr>  s6      *,HLCCSWS^S^SsSstutvSwLw,Hr  )r3   r;   r<   r_   Fr  )ignore_index)r  r  r3   r4   )r8   r   r  rL   r  r3   r0  r%  r  r    r  r  r  r  r   rf   rg   ry   r   semantic_loss_ignore_indexauxiliary_loss_weightr   r4   )rU   rs   r   r  r   r   r  r3   r  r  r  r  auxiliary_logitsr  upsampled_logitsupsampled_auxiliary_logitsloss_fct	main_lossauxiliary_losss   `                  r-   r   "DPTForSemanticSegmentation.forwardq  s'   @  '#';;#C#C $++"8"8A"=NOOHLI
$-DI
LRI
  -- {{$$,5mAB6G,H,HLCCSWS^S^SsSsLs,H  M &-%E%E"")) *,5mAB6G,H*  3M			>=)*#22=3DE!}}88\\"#.Zu  9    +-/]]-F-F$<<+<:]b .G .* (T[[5[5[\H !1:I%&@&INt{{@@>QQD&3G'//T))	
 	
Es    H
5H
)r  r  r  r  )NNNN)r"   r#   r$   r%   r   r@   r   r   r   r'   r(   r  r   r   r   r+   r   r   s   @r-   r  r  `  s    y   5915-1/3S
u001S
 E--.S
 ))*	S

 'tnS
 
!S
  S
r,   r  )r  r  r  r  )r   )Ir&   collections.abcrF   dataclassesr   typingr   r   r'   r   torch.nnr   activationsr	   modeling_layersr
   modeling_outputsr   r   r   modeling_utilsr   r   pytorch_utilsr   r   utilsr   r   r   r   utils.backbone_utilsr   utils.genericr   r   configuration_dptr   
get_loggerr"   loggerr   r/   Moduler6   r   r   r   floatr   r   r   r   r   r  r  r  r-  r@  r=  rb  rk  rd  r  r  r  r  r  r  r  r  r  __all__r!   r,   r-   <module>r3     s    ! %   % ! 9 ^ ^ F Q D D 1 A ( 
		H	% 	M 	M 	M M; M M$]
RYY ]
@4Yryy 4YnBII N %II%<<% 
% <<	%
 U\\*% % %>1.ryy 1.jryy $bii @  
299 
, >
BII 
.e eP" ,#BII #0:'RYY :'z"BII "J 4 4 4: G
! G
 G
V299 7bii 7t%RYY %P 
m
. m

m
`")) ,ryy $ e
!3 e
 e
P dr,   