Ë
    S^(h{  ã            	       óè  — d Z ddlZddlmZmZmZmZ ddl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 dd
lmZmZmZmZmZ ddlmZ  ej8                  e«      ZdZdZ g d¢Z!d<dejD                  de#de$dejD                  fd„Z% G d„ de	jL                  «      Z' G d„ de	jL                  «      Z( G d„ de	jL                  «      Z) G d„ de	jL                  «      Z* G d„ de	jL                  «      Z+ G d„ d e	jL                  «      Z, G d!„ d"e	jL                  «      Z- G d#„ d$e	jL                  «      Z. G d%„ d&e	jL                  «      Z/ G d'„ d(e«      Z0d)Z1d*Z2 ed+e1«       G d,„ d-e0«      «       Z3 G d.„ d/e	jL                  «      Z4 G d0„ d1e	jL                  «      Z5 G d2„ d3e	jL                  «      Z6 G d4„ d5e	jL                  «      Z7 G d6„ d7e	jL                  «      Z8 ed8e1«       G d9„ d:e0«      «       Z9g d;¢Z:y)=zPyTorch GLPN model.é    N)ÚListÚOptionalÚTupleÚUnion)Únné   )ÚACT2FN)ÚBaseModelOutputÚDepthEstimatorOutput)ÚPreTrainedModel)Ú find_pruneable_heads_and_indicesÚprune_linear_layer)Úadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsé   )Ú
GLPNConfigr   zvinvino02/glpn-kitti)r   i   é   é   ÚinputÚ	drop_probÚtrainingÚreturnc                 ó  — |dk(  s|s| S d|z
  }| j                   d   fd| j                  dz
  z  z   }|t        j                  || j                  | j
                  ¬«      z   }|j                  «        | j                  |«      |z  }|S )aF  
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    Comment by Ross Wightman: This is the same as the DropConnect impl I created for EfficientNet, etc networks,
    however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the
    layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the
    argument.
    ç        r   r   )r   )ÚdtypeÚdevice)ÚshapeÚndimÚtorchÚrandr   r   Úfloor_Údiv)r   r   r   Ú	keep_probr    Úrandom_tensorÚoutputs          úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/glpn/modeling_glpn.pyÚ	drop_pathr*   2   s�   € ð �CÒ™xØˆØ�I‘€IØ�[‰[˜‰^Ð ¨¯
©
°Q©Ñ 7Ñ7€EØ¤§
¡
¨5¸¿¹ÈEÏLÉLÔ YÑY€MØ×ÑÔØ�Y‰Y�yÓ! MÑ1€FØ€Mó    c                   óx   ‡ — e Zd ZdZd	dee   ddfˆ fd„Zdej                  dej                  fd„Z	de
fd„Zˆ xZS )
ÚGLPNDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr   r   c                 ó0   •— t         ‰| �  «        || _        y ©N)ÚsuperÚ__init__r   )Úselfr   Ú	__class__s     €r)   r1   zGLPNDropPath.__init__J   s   ø€ Ü‰ÑÔØ"ˆ�r+   Úhidden_statesc                 óD   — t        || j                  | j                  «      S r/   )r*   r   r   )r2   r4   s     r)   ÚforwardzGLPNDropPath.forwardN   s   € Ü˜¨¯©¸¿¹ÓFÐFr+   c                 ó8   — dj                  | j                  «      S )Nzp={})Úformatr   )r2   s    r)   Ú
extra_reprzGLPNDropPath.extra_reprQ   s   € Ø�}‰}˜TŸ^™^Ó,Ð,r+   r/   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úfloatr1   r"   ÚTensorr6   Ústrr9   Ú__classcell__©r3   s   @r)   r-   r-   G   sG   ø„ Ùbñ# (¨5¡/ð #¸Tõ #ðG U§\¡\ð G°e·l±ló Gð-˜C÷ -r+   r-   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚGLPNOverlapPatchEmbeddingsz+Construct the overlapping patch embeddings.c                 óš   •— t         ‰| �  «        t        j                  |||||dz  ¬«      | _        t        j
                  |«      | _        y )Né   ©Úkernel_sizeÚstrideÚpadding)r0   r1   r   ÚConv2dÚprojÚ	LayerNormÚ
layer_norm)r2   Ú
patch_sizerI   Únum_channelsÚhidden_sizer3   s        €r)   r1   z#GLPNOverlapPatchEmbeddings.__init__Y   sC   ø€ Ü‰ÑÔÜ—I‘IØØØ"ØØ !‘Oô
ˆŒ	ô Ÿ,™, {Ó3ˆ�r+   c                 ó´   — | j                  |«      }|j                  \  }}}}|j                  d«      j                  dd«      }| j	                  |«      }|||fS )NrF   r   )rL   r    ÚflattenÚ	transposerN   )r2   Úpixel_valuesÚ
embeddingsÚ_ÚheightÚwidths         r)   r6   z"GLPNOverlapPatchEmbeddings.forwarde   s^   € Ø—Y‘Y˜|Ó,ˆ
Ø(×.Ñ.Ñˆˆ1ˆf�eð  ×'Ñ'¨Ó*×4Ñ4°Q¸Ó:ˆ
Ø—_‘_ ZÓ0ˆ
Ø˜6 5Ð(Ð(r+   ©r:   r;   r<   r=   r1   r6   rA   rB   s   @r)   rD   rD   V   s   ø„ Ù5ô
4ö)r+   rD   c                   ó2   ‡ — e Zd ZdZˆ fd„Zd„ Z	 dd„Zˆ xZS )ÚGLPNEfficientSelfAttentionz›SegFormer's efficient self-attention mechanism. Employs the sequence reduction process introduced in the [PvT
    paper](https://arxiv.org/abs/2102.12122).c                 ó8  •— t         ‰| �  «        || _        || _        | j                  | j                  z  dk7  r&t	        d| j                  › d| j                  › d�«      ‚t        | j                  | j                  z  «      | _        | j                  | j                  z  | _        t        j                  | j                  | j                  «      | _
        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        t        j                  |j                  «      | _        || _        |dkD  r9t        j"                  ||||¬«      | _        t        j&                  |«      | _        y y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads (ú)r   )rH   rI   )r0   r1   rQ   Únum_attention_headsÚ
ValueErrorÚintÚattention_head_sizeÚall_head_sizer   ÚLinearÚqueryÚkeyÚvalueÚDropoutÚattention_probs_dropout_probÚdropoutÚsr_ratiorK   ÚsrrM   rN   ©r2   ÚconfigrQ   r_   Úsequence_reduction_ratior3   s        €r)   r1   z#GLPNEfficientSelfAttention.__init__t   sT  ø€ Ü‰ÑÔØ&ˆÔØ#6ˆÔ à×Ñ˜d×6Ñ6Ñ6¸!Ò;ÜØ# D×$4Ñ$4Ð#5ð 6Ø×2Ñ2Ð3°1ð6óð ô
 $' t×'7Ñ'7¸$×:RÑ:RÑ'RÓ#SˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔä—Y‘Y˜t×/Ñ/°×1CÑ1CÓDˆŒ
Ü—9‘9˜T×-Ñ-¨t×/AÑ/AÓBˆŒÜ—Y‘Y˜t×/Ñ/°×1CÑ1CÓDˆŒ
ä—z‘z &×"EÑ"EÓFˆŒà0ˆŒØ# aÒ'Ü—i‘iØ˜[Ð6NÐWoôˆDŒGô !Ÿl™l¨;Ó7ˆD�Oð	 (r+   c                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  dddd«      S )Néÿÿÿÿr   rF   r   r   )Úsizer_   rb   ÚviewÚpermute)r2   r4   Ú	new_shapes      r)   Útranspose_for_scoresz/GLPNEfficientSelfAttention.transpose_for_scores�   sT   € Ø!×&Ñ&Ó(¨¨"Ð-°×1IÑ1IÈ4×KcÑKcÐ0dÑdˆ	Ø%×*Ñ*¨9Ó5ˆØ×$Ñ$ Q¨¨1¨aÓ0Ð0r+   c                 ó¸  — | j                  | j                  |«      «      }| j                  dkD  r{|j                  \  }}}|j	                  ddd«      j                  ||||«      }| j                  |«      }|j                  ||d«      j	                  ddd«      }| j                  |«      }| j                  | j                  |«      «      }	| j                  | j                  |«      «      }
t        j                  ||	j                  dd«      «      }|t        j                  | j                  «      z  }t         j"                  j%                  |d¬«      }| j'                  |«      }t        j                  ||
«      }|j	                  dddd«      j)                  «       }|j+                  «       d d | j,                  fz   }|j/                  |«      }|r||f}|S |f}|S )Nr   r   rF   rq   éþÿÿÿ©Údimr   )rv   re   rk   r    rt   Úreshaperl   rN   rf   rg   r"   ÚmatmulrT   ÚmathÚsqrtrb   r   Ú
functionalÚsoftmaxrj   Ú
contiguousrr   rc   rs   )r2   r4   rX   rY   Úoutput_attentionsÚquery_layerÚ
batch_sizeÚseq_lenrP   Ú	key_layerÚvalue_layerÚattention_scoresÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss                   r)   r6   z"GLPNEfficientSelfAttention.forward”   sÂ  € ð ×/Ñ/°·
±
¸=Ó0IÓJˆà�=‰=˜1ÒØ0=×0CÑ0CÑ-ˆJ˜ à)×1Ñ1°!°Q¸Ó:×BÑBÀ:È|Ð]cÐejÓkˆMà ŸG™G MÓ2ˆMà)×1Ñ1°*¸lÈBÓO×WÑWÐXYÐ[\Ð^_Ó`ˆMØ ŸO™O¨MÓ:ˆMà×-Ñ-¨d¯h©h°}Ó.EÓFˆ	Ø×/Ñ/°·
±
¸=Ó0IÓJˆô !Ÿ<™<¨°Y×5HÑ5HÈÈRÓ5PÓQÐà+¬d¯i©i¸×8PÑ8PÓ.QÑQÐô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆäŸ™ _°kÓBˆà%×-Ñ-¨a°°A°qÓ9×DÑDÓFˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ð2ˆàˆð O\ÐM]ˆàˆr+   ©F)r:   r;   r<   r=   r1   rv   r6   rA   rB   s   @r)   r\   r\   p   s   ø„ ñ1ô8ò61ð  ÷*r+   r\   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚGLPNSelfOutputc                 ó    •— t         ‰| �  «        t        j                  ||«      | _        t        j
                  |j                  «      | _        y r/   )r0   r1   r   rd   Údenserh   Úhidden_dropout_probrj   )r2   rn   rQ   r3   s      €r)   r1   zGLPNSelfOutput.__init__Ã   s6   ø€ Ü‰ÑÔÜ—Y‘Y˜{¨KÓ8ˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r+   c                 óJ   — | j                  |«      }| j                  |«      }|S r/   )r‘   rj   )r2   r4   Úinput_tensors      r)   r6   zGLPNSelfOutput.forwardÈ   s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØÐr+   ©r:   r;   r<   r1   r6   rA   rB   s   @r)   r�   r�   Â   s   ø„ ô>ö
r+   r�   c                   ó,   ‡ — e Zd Zˆ fd„Zd„ Zdd„Zˆ xZS )ÚGLPNAttentionc                 óŒ   •— t         ‰| �  «        t        ||||¬«      | _        t	        ||¬«      | _        t        «       | _        y )N)rn   rQ   r_   ro   )rQ   )r0   r1   r\   r2   r�   r(   ÚsetÚpruned_headsrm   s        €r)   r1   zGLPNAttention.__init__Ð   s@   ø€ Ü‰ÑÔÜ.ØØ#Ø 3Ø%=ô	
ˆŒ	ô % V¸ÔEˆŒÜ›EˆÕr+   c                 ó>  — t        |«      dk(  ry t        || j                  j                  | j                  j                  | j
                  «      \  }}t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _	        t        | j                  j                  |d¬«      | j                  _        | j                  j                  t        |«      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |«      | _        y )Nr   r   ry   )Úlenr   r2   r_   rb   rš   r   re   rf   rg   r(   r‘   rc   Úunion)r2   ÚheadsÚindexs      r)   Úprune_headszGLPNAttention.prune_headsÛ   s  € Üˆu‹:˜Š?ØÜ7Ø�4—9‘9×0Ñ0°$·)±)×2OÑ2OÐQU×QbÑQbó
‰ˆˆuô
 -¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ*¨4¯9©9¯=©=¸%Ó@ˆ�	‰	ŒÜ,¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ.¨t¯{©{×/@Ñ/@À%ÈQÔOˆ�‰Ôð )-¯	©	×(EÑ(EÌÈEË
Ñ(Rˆ�	‰	Ô%Ø"&§)¡)×"?Ñ"?À$Ç)Á)×B_ÑB_Ñ"_ˆ�	‰	ÔØ ×-Ñ-×3Ñ3°EÓ:ˆÕr+   c                 ój   — | j                  ||||«      }| j                  |d   |«      }|f|dd  z   }|S )Nr   r   )r2   r(   )r2   r4   rX   rY   r‚   Úself_outputsÚattention_outputrŒ   s           r)   r6   zGLPNAttention.forwardí   sE   € Ø—y‘y °¸Ð?PÓQˆàŸ;™; |°A¡¸ÓFÐØ#Ð%¨°Q°RÐ(8Ñ8ˆØˆr+   r�   )r:   r;   r<   r1   r    r6   rA   rB   s   @r)   r—   r—   Ï   s   ø„ ô	"ò;÷$r+   r—   c                   ó&   ‡ — e Zd Zdˆ fd„	Zd„ Zˆ xZS )Ú
GLPNDWConvc           	      ód   •— t         ‰| �  «        t        j                  ||dddd|¬«      | _        y )Nr   r   T)ÚbiasÚgroups)r0   r1   r   rK   Údwconv)r2   rz   r3   s     €r)   r1   zGLPNDWConv.__init__÷   s*   ø€ Ü‰ÑÔÜ—i‘i  S¨!¨Q°¸ÀSÔIˆ�r+   c                 óÒ   — |j                   \  }}}|j                  dd«      j                  ||||«      }| j                  |«      }|j	                  d«      j                  dd«      }|S )Nr   rF   )r    rT   rs   r©   rS   )r2   r4   rX   rY   r„   r…   rP   s          r)   r6   zGLPNDWConv.forwardû   sj   € Ø,9×,?Ñ,?Ñ)ˆ
�G˜\Ø%×/Ñ/°°1Ó5×:Ñ:¸:À|ÐU[Ð]bÓcˆØŸ™ MÓ2ˆØ%×-Ñ-¨aÓ0×:Ñ:¸1¸aÓ@ˆàÐr+   )i   r•   rB   s   @r)   r¥   r¥   ö   s   ø„ õJör+   r¥   c                   ó&   ‡ — e Zd Zdˆ fd„	Zd„ Zˆ xZS )Ú
GLPNMixFFNc                 óŠ  •— t         ‰| �  «        |xs |}t        j                  ||«      | _        t        |«      | _        t        |j                  t        «      rt        |j                     | _        n|j                  | _        t        j                  ||«      | _        t        j                  |j                  «      | _        y r/   )r0   r1   r   rd   Údense1r¥   r©   Ú
isinstanceÚ
hidden_actr@   r	   Úintermediate_act_fnÚdense2rh   r’   rj   )r2   rn   Úin_featuresÚhidden_featuresÚout_featuresr3   s        €r)   r1   zGLPNMixFFN.__init__  s�   ø€ Ü‰ÑÔØ#Ò2 {ˆÜ—i‘i ¨_Ó=ˆŒÜ  Ó1ˆŒÜ�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÔ$Ü—i‘i °Ó>ˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r+   c                 óÖ   — | j                  |«      }| j                  |||«      }| j                  |«      }| j                  |«      }| j	                  |«      }| j                  |«      }|S r/   )r®   r©   r±   rj   r²   )r2   r4   rX   rY   s       r)   r6   zGLPNMixFFN.forward  sb   € ØŸ™ MÓ2ˆØŸ™ M°6¸5ÓAˆØ×0Ñ0°Ó?ˆØŸ™ ]Ó3ˆØŸ™ MÓ2ˆØŸ™ ]Ó3ˆØÐr+   )NNr•   rB   s   @r)   r¬   r¬     s   ø„ õ
>ör+   r¬   c                   ó*   ‡ — e Zd ZdZˆ fd„Zdd„Zˆ xZS )Ú	GLPNLayerzCThis corresponds to the Block class in the original implementation.c                 óF  •— t         ‰| �  «        t        j                  |«      | _        t        ||||¬«      | _        |dkD  rt        |«      nt        j                  «       | _	        t        j                  |«      | _
        t        ||z  «      }t        |||¬«      | _        y )N)rQ   r_   ro   r   )r³   r´   )r0   r1   r   rM   Úlayer_norm_1r—   Ú	attentionr-   ÚIdentityr*   Úlayer_norm_2ra   r¬   Úmlp)	r2   rn   rQ   r_   r*   ro   Ú	mlp_ratioÚmlp_hidden_sizer3   s	           €r)   r1   zGLPNLayer.__init__   sƒ   ø€ Ü‰ÑÔÜŸL™L¨Ó5ˆÔÜ&ØØ#Ø 3Ø%=ô	
ˆŒð 5>À²Oœ iÔ0ÌÏÉËˆŒÜŸL™L¨Ó5ˆÔÜ˜k¨IÑ5Ó6ˆÜ˜f°+ÈÔ_ˆ�r+   c                 ó
  — | j                  | j                  |«      |||¬«      }|d   }|dd  }| j                  |«      }||z   }| j                  | j	                  |«      ||«      }| j                  |«      }||z   }	|	f|z   }|S )N)r‚   r   r   )r»   rº   r*   r¾   r½   )
r2   r4   rX   rY   r‚   Úself_attention_outputsr£   rŒ   Ú
mlp_outputÚlayer_outputs
             r)   r6   zGLPNLayer.forward.  sª   € Ø!%§¡Ø×Ñ˜mÓ,ØØØ/ð	 "0ó "
Ðð 2°!Ñ4ÐØ(¨¨Ð,ˆð  Ÿ>™>Ð*:Ó;ÐØ(¨=Ñ8ˆà—X‘X˜d×/Ñ/°Ó>ÀÈÓNˆ
ð —^‘^ JÓ/ˆ
Ø! MÑ1ˆà�/ GÑ+ˆàˆr+   r�   rZ   rB   s   @r)   r¸   r¸     s   ø„ ÙMô`÷r+   r¸   c                   ó,   ‡ — e Zd Zˆ fd„Z	 	 	 dd„Zˆ xZS )ÚGLPNEncoderc                 ó®  •— t         ‰
| �  «        || _        t        j                  d|j
                  t        |j                  «      «      D �cg c]  }|j                  «       ‘Œ }}g }t        |j                  «      D ]h  }|j                  t        |j                  |   |j                  |   |dk(  r|j                  n|j                   |dz
     |j                   |   ¬«      «       Œj t#        j$                  |«      | _        g }d}t        |j                  «      D ]¹  }g }|dk7  r||j                  |dz
     z  }t        |j                  |   «      D ]\  }	|j                  t)        ||j                   |   |j*                  |   |||	z      |j,                  |   |j.                  |   ¬«      «       Œ^ |j                  t#        j$                  |«      «       Œ» t#        j$                  |«      | _        t#        j$                  t        |j                  «      D �cg c]$  }t#        j2                  |j                   |   «      ‘Œ& c}«      | _        y c c}w c c}w )Nr   r   )rO   rI   rP   rQ   )rQ   r_   r*   ro   r¿   )r0   r1   rn   r"   ÚlinspaceÚdrop_path_rateÚsumÚdepthsÚitemÚrangeÚnum_encoder_blocksÚappendrD   Úpatch_sizesÚstridesrP   Úhidden_sizesr   Ú
ModuleListÚpatch_embeddingsr¸   r_   Ú	sr_ratiosÚ
mlp_ratiosÚblockrM   rN   )r2   rn   ÚxÚdprrV   ÚiÚblocksÚcurÚlayersÚjr3   s             €r)   r1   zGLPNEncoder.__init__I  s  ø€ Ü‰ÑÔØˆŒô "'§¡°°6×3HÑ3HÌ#ÈfÏmÉmÓJ\Ó!]Ö^˜Aˆq�v‰v�xÐ^ˆÐ^ð ˆ
Ü�v×0Ñ0Ó1ò 	ˆAØ×ÑÜ*Ø%×1Ñ1°!Ñ4Ø!Ÿ>™>¨!Ñ,Ø89¸Qº ×!4Ò!4ÀF×DWÑDWÐXYÐ\]ÑX]ÑD^Ø &× 3Ñ 3°AÑ 6ô	õð	ô !#§¡¨jÓ 9ˆÔð ˆØˆÜ�v×0Ñ0Ó1ò 	1ˆAàˆFØ�AŠvØ�v—}‘} Q¨¡UÑ+Ñ+�Ü˜6Ÿ=™=¨Ñ+Ó,ò 
�Ø—‘ÜØØ$*×$7Ñ$7¸Ñ$:Ø,2×,FÑ,FÀqÑ,IØ"% c¨A¡g¡,Ø17×1AÑ1AÀ!Ñ1DØ"(×"3Ñ"3°AÑ"6ôõ	ð
ð �M‰Mœ"Ÿ-™-¨Ó/Õ0ð!	1ô$ —]‘] 6Ó*ˆŒ
ô Ÿ-™-Ü;@À×AZÑAZÓ;[Ö\°aŒR�\‰\˜&×-Ñ-¨aÑ0Õ1Ò\ó
ˆ�ùòO _ùòP ]s   ÁIÈ)Ic                 ó
  — |rdnd }|rdnd }|j                   d   }|}t        t        | j                  | j                  | j
                  «      «      D ]‹  \  }	}
|
\  }}} ||«      \  }}}t        |«      D ]!  \  }} |||||«      }|d   }|sŒ||d   fz   }Œ#  ||«      }|j                  |||d«      j                  dddd«      j                  «       }|sŒ†||fz   }Œ� |st        d„ |||fD «       «      S t        |||¬«      S )	N© r   r   rq   r   rF   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr/   rà   )Ú.0Úvs     r)   ú	<genexpr>z&GLPNEncoder.forward.<locals>.<genexpr>˜  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š©Úlast_hidden_stater4   Ú
attentions)r    Ú	enumerateÚziprÔ   r×   rN   r{   rt   r�   Útupler
   )r2   rU   r‚   Úoutput_hidden_statesÚreturn_dictÚall_hidden_statesÚall_self_attentionsr„   r4   ÚidxrØ   Úembedding_layerÚblock_layerÚ
norm_layerrX   rY   rÚ   ÚblkÚlayer_outputss                      r)   r6   zGLPNEncoder.forwardy  sL  € ñ #7™B¸DÐÙ$5™b¸4Ðà!×'Ñ'¨Ñ*ˆ
à$ˆÜ¤ D×$9Ñ$9¸4¿:¹:ÀtÇÁÓ WÓXò 	I‰FˆC�Ø78Ñ4ˆO˜[¨*á+:¸=Ó+IÑ(ˆM˜6 5ä# KÓ0ò T‘��3Ù # M°6¸5ÐBSÓ T�Ø -¨aÑ 0�Ú$Ø*=ÀÈqÑAQÐ@SÑ*SÑ'ð	Tñ ' }Ó5ˆMà)×1Ñ1°*¸fÀeÈRÓP×XÑXÐYZÐ\]Ð_`ÐbcÓd×oÑoÓqˆMÚ#Ø$5¸Ð8HÑ$HÑ!ð	Iñ" ÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmÜØ+Ø+Ø*ô
ð 	
r+   )FFTr•   rB   s   @r)   rÆ   rÆ   H  s   ø„ ô.
ðf  Ø"Ø÷$
r+   rÆ   c                   ó&   — e Zd ZdZeZdZdZg Zd„ Z	y)ÚGLPNPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚglpnrU   c                 óB  — t        |t        j                  t        j                  f«      rm|j                  j
                  j                  d| j                  j                  ¬«       |j                  �%|j                  j
                  j                  «        yyt        |t        j                  «      rz|j                  j
                  j                  d| j                  j                  ¬«       |j                  �2|j                  j
                  |j                     j                  «        yyt        |t        j                  t        j                  f«      rJ|j                  j
                  j                  «        |j                  j
                  j                  d«       yy)zInitialize the weightsr   )ÚmeanÚstdNg      ð?)r¯   r   rd   rK   ÚweightÚdataÚnormal_rn   Úinitializer_ranger§   Úzero_Ú	EmbeddingÚpadding_idxrM   ÚBatchNorm2dÚfill_)r2   Úmodules     r)   Ú_init_weightsz!GLPNPreTrainedModel._init_weights¬  s  € ä�fœrŸy™y¬"¯)©)Ð4Ô5ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜¤§¡¬r¯~©~Ð >Ô?Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)ð @r+   N)
r:   r;   r<   r=   r   Úconfig_classÚbase_model_prefixÚmain_input_nameÚ_no_split_modulesr  rà   r+   r)   rö   rö      s$   „ ñð
 €LØÐØ$€OØÐó*r+   rö   aG  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`GLPNConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aG  

    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`GLPNImageProcessor.__call__`] for details.

        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
zfThe bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top.c                   óÌ   ‡ — e Zd Zˆ fd„Zd„ Z eej                  d«      «       ee	e
ede¬«      	 	 	 ddej                  dee   dee   d	ee   d
eee
f   f
d„«       «       Zˆ xZS )Ú	GLPNModelc                 ór   •— t         ‰| �  |«       || _        t        |«      | _        | j                  «        y r/   )r0   r1   rn   rÆ   ÚencoderÚ	post_init©r2   rn   r3   s     €r)   r1   zGLPNModel.__init__à  s1   ø€ Ü‰Ñ˜Ô ØˆŒô # 6Ó*ˆŒð 	�‰Õr+   c                 ó˜   — |j                  «       D ]7  \  }}| j                  j                  |   j                  j	                  |«       Œ9 y)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  Úlayerr»   r    )r2   Úheads_to_pruner  rž   s       r)   Ú_prune_headszGLPNModel._prune_headsê  sE   € ð
 +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr+   z(batch_size, sequence_length)Úvision)Ú
checkpointÚoutput_typer  ÚmodalityÚexpected_outputrU   r‚   rë   rì   r   c                 ó,  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  ||||¬«      }|d   }|s	|f|dd  z   S t        ||j                  |j                  ¬«      S )N©r‚   rë   rì   r   r   rå   )rn   r‚   rë   Úuse_return_dictr  r
   r4   rç   )r2   rU   r‚   rë   rì   Úencoder_outputsÚsequence_outputs          r)   r6   zGLPNModel.forwardò  s·   € ð  2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàŸ,™,ØØ/Ø!5Ø#ð	 'ó 
ˆð *¨!Ñ,ˆáØ#Ð%¨¸¸Ð(;Ñ;Ð;äØ-Ø)×7Ñ7Ø&×1Ñ1ô
ð 	
r+   )NNN)r:   r;   r<   r1   r  r   ÚGLPN_INPUTS_DOCSTRINGr8   r   Ú_CHECKPOINT_FOR_DOCr
   Ú_CONFIG_FOR_DOCÚ_EXPECTED_OUTPUT_SHAPEr"   ÚFloatTensorr   Úboolr   r   r6   rA   rB   s   @r)   r  r  Ú  s¨   ø„ ôòCñ +Ð+@×+GÑ+GÐHgÓ+hÓiÙØ&Ø#Ø$ØØ.ôð -1Ø/3Ø&*ñ
à×'Ñ'ð
ð $ D™>ð
ð ' t™nð	
ð
 ˜d‘^ð
ð 
ˆu�oÐ%Ñ	&ò
óó jô
r+   r  c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )ÚGLPNSelectiveFeatureFusionzó
    Selective Feature Fusion module, as explained in the [paper](https://arxiv.org/abs/2201.07436) (section 3.4). This
    module adaptively selects and integrates local and global features by attaining an attention map for each feature.
    c           	      óV  •— t         ‰| �  «        t        j                  t        j                  t        |dz  «      |ddd¬«      t        j                  |«      t        j                  «       «      | _        t        j                  t        j                  |t        |dz  «      ddd¬«      t        j                  t        |dz  «      «      t        j                  «       «      | _	        t        j                  t        |dz  «      dddd¬«      | _
        t        j                  «       | _        y )NrF   r   r   )Úin_channelsÚout_channelsrH   rI   rJ   )r0   r1   r   Ú
SequentialrK   ra   r  ÚReLUÚconvolutional_layer1Úconvolutional_layer2Úconvolutional_layer3ÚSigmoidÚsigmoid)r2   Ú
in_channelr3   s     €r)   r1   z#GLPNSelectiveFeatureFusion.__init__   sÝ   ø€ Ü‰ÑÔä$&§M¡MÜ�I‰I¤# j°1¡nÓ"5ÀJÐ\]ÐfgÐqrÔsÜ�N‰N˜:Ó&Ü�G‰G‹Ió%
ˆÔ!ô %'§M¡MÜ�I‰I *¼3¸zÈA¹~Ó;NÐ\]ÐfgÐqrÔsÜ�N‰Nœ3˜z¨A™~Ó.Ó/Ü�G‰G‹Ió%
ˆÔ!ô %'§I¡IÜ˜J¨™NÓ+¸!ÈÐSTÐ^_ô%
ˆÔ!ô —z‘z“|ˆ�r+   c                 óF  — t        j                  ||fd¬«      }| j                  |«      }| j                  |«      }| j	                  |«      }| j                  |«      }||d d …dd d …d d …f   j                  d«      z  ||d d …dd d …d d …f   j                  d«      z  z   }|S )Nr   ry   r   )r"   Úcatr,  r-  r.  r0  Ú	unsqueeze)r2   Úlocal_featuresÚglobal_featuresÚfeaturesÚattnÚhybrid_featuress         r)   r6   z"GLPNSelectiveFeatureFusion.forward5  s©   € ä—9‘9˜n¨oÐ>ÀAÔFˆà×,Ñ,¨XÓ6ˆØ×,Ñ,¨XÓ6ˆØ×,Ñ,¨XÓ6ˆà�|‰|˜HÓ%ˆà(¨4²°1²aº°
Ñ+;×+EÑ+EÀaÓ+HÑHÈ?Ð]aÚˆq’!’QˆJñ^
ç
‰)�A‹,ñLñ ˆð Ðr+   )é@   rZ   rB   s   @r)   r&  r&    s   ø„ ñõ
$ö*r+   r&  c                   ó&   ‡ — e Zd Zˆ fd„Zdd„Zˆ xZS )ÚGLPNDecoderStagec                 óì   •— t         ‰| �  «        ||k(  }|st        j                  ||d¬«      nt        j                  «       | _        t        |«      | _        t        j                  ddd¬«      | _	        y )Nr   )rH   rF   ÚbilinearF©Úscale_factorÚmodeÚalign_corners)
r0   r1   r   rK   r¼   Úconvolutionr&  ÚfusionÚUpsampleÚupsample)r2   r(  r)  Úshould_skipr3   s       €r)   r1   zGLPNDecoderStage.__init__G  s\   ø€ Ü‰ÑÔØ! \Ñ1ˆÙVaœ2Ÿ9™9 [°,ÈAÕNÔgi×grÑgrÓgtˆÔÜ0°Ó>ˆŒÜŸ™°¸ÐSXÔYˆ�r+   c                 ór   — | j                  |«      }|�| j                  ||«      }| j                  |«      }|S r/   )rC  rD  rF  )r2   Úhidden_stateÚresiduals      r)   r6   zGLPNDecoderStage.forwardN  s<   € Ø×'Ñ'¨Ó5ˆØÐØŸ;™; |°XÓ>ˆLØ—}‘} \Ó2ˆàÐr+   r/   r•   rB   s   @r)   r<  r<  F  s   ø„ ôZ÷	r+   r<  c                   ób   ‡ — e Zd Zˆ fd„Zdeej                     deej                     fd„Zˆ xZS )ÚGLPNDecoderc           	      ó.  •— t         ‰| �  «        |j                  d d d…   }|j                  }t	        j
                  |D �cg c]  }t        ||«      ‘Œ c}«      | _        d | j                  d   _        t	        j                  ddd¬«      | _
        y c c}w )Nrq   r   rF   r>  Fr?  )r0   r1   rÒ   Údecoder_hidden_sizer   rÓ   r<  ÚstagesrD  rE  Úfinal_upsample)r2   rn   Úreserved_hidden_sizesr)  rQ   r3   s        €r)   r1   zGLPNDecoder.__init__[  s‚   ø€ Ü‰ÑÔà &× 3Ñ 3±D°b°DÑ 9ÐØ×1Ñ1ˆä—m‘mØLaÖb¸[Ô˜k¨<Õ8Òbó
ˆŒð !%ˆ�‰�A‰Ôä Ÿk™k°q¸zÐY^Ô_ˆÕùò cs   ÁBr4   r   c                 ó²   — g }d }t        |d d d…   | j                  «      D ]  \  }} |||«      }|j                  |«       Œ! | j                  |«      |d<   |S )Nrq   )ré   rO  rÏ   rP  )r2   r4   Ústage_hidden_statesÚstage_hidden_staterI  Ústages         r)   r6   zGLPNDecoder.forwardi  sr   € Ø ÐØ!ÐÜ#& }±T°r°TÑ':¸D¿K¹KÓ#Hò 	;ÑˆL˜%Ù!& |Ð5GÓ!HÐØ×&Ñ&Ð'9Õ:ð	;ð #'×"5Ñ"5Ð6HÓ"IÐ˜BÑà"Ð"r+   ©	r:   r;   r<   r1   r   r"   r?   r6   rA   rB   s   @r)   rL  rL  Z  s.   ø„ ô`ð	# T¨%¯,©,Ñ%7ð 	#¸DÀÇÁÑ<N÷ 	#r+   rL  c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )Ú	SiLogLossz÷
    Implements the Scale-invariant log scale loss [Eigen et al., 2014](https://arxiv.org/abs/1406.2283).

    $$L=\frac{1}{n} \sum_{i} d_{i}^{2}-\frac{1}{2 n^{2}}\left(\sum_{i} d_{i}^{2}\right)$$ where $d_{i}=\log y_{i}-\log
    y_{i}^{*}$.

    c                 ó0   •— t         ‰| �  «        || _        y r/   )r0   r1   Úlambd)r2   rZ  r3   s     €r)   r1   zSiLogLoss.__init__~  s   ø€ Ü‰ÑÔØˆ�
r+   c                 ó`  — |dkD  j                  «       }t        j                  ||   «      t        j                  ||   «      z
  }t        j                  t        j                  |d«      j                  «       | j                  t        j                  |j                  «       d«      z  z
  «      }|S )Nr   rF   )Údetachr"   Úlogr~   Úpowrù   rZ  )r2   ÚpredÚtargetÚ
valid_maskÚdiff_logÚlosss         r)   r6   zSiLogLoss.forward‚  s…   € Ø˜q‘j×(Ñ(Ó*ˆ
Ü—9‘9˜V JÑ/Ó0´5·9±9¸TÀ*Ñ=MÓ3NÑNˆÜ�z‰zœ%Ÿ)™) H¨aÓ0×5Ñ5Ó7¸$¿*¹*ÄuÇyÁyÐQY×Q^ÑQ^ÓQ`ÐbcÓGdÑ:dÑdÓeˆàˆr+   )g      à?rZ   rB   s   @r)   rX  rX  u  s   ø„ ñõör+   rX  c                   ó\   ‡ — e Zd Zˆ fd„Zdeej                     dej                  fd„Zˆ xZS )ÚGLPNDepthEstimationHeadc                 ó  •— t         ‰| �  «        || _        |j                  }t	        j
                  t	        j                  ||ddd¬«      t	        j                  d¬«      t	        j                  |dddd¬«      «      | _        y )Nr   r   rG   F)Úinplace)	r0   r1   rn   rN  r   r*  rK   r+  Úhead)r2   rn   Úchannelsr3   s      €r)   r1   z GLPNDepthEstimationHead.__init__‹  sd   ø€ Ü‰ÑÔàˆŒà×-Ñ-ˆÜ—M‘MÜ�I‰I�h °aÀÈ1ÔMÜ�G‰G˜EÔ"Ü�I‰I�h ¨q¸ÀAÔFó
ˆ�	r+   r4   r   c                 óÖ   — || j                   j                     }| j                  |«      }t        j                  |«      | j                   j
                  z  }|j                  d¬«      }|S )Nr   ry   )rn   Úhead_in_indexrh  r"   r0  Ú	max_depthÚsqueeze)r2   r4   Úpredicted_depths      r)   r6   zGLPNDepthEstimationHead.forward—  sZ   € à% d§k¡k×&?Ñ&?Ñ@ˆàŸ	™	 -Ó0ˆäŸ-™-¨Ó6¸¿¹×9NÑ9NÑNˆØ)×1Ñ1°aÐ1Ó8ˆàÐr+   rV  rB   s   @r)   re  re  Š  s(   ø„ ô

ð	 T¨%¯,©,Ñ%7ð 	¸E¿L¹L÷ 	r+   re  z]GLPN Model transformer with a lightweight depth estimation head on top e.g. for KITTI, NYUv2.c                   óú   ‡ — e Zd Zˆ fd„Z eej                  d«      «       eee	¬«      	 	 	 	 dde
j                  dee
j                     dee   dee   dee   d	eee
j                      ef   fd
„«       «       Zˆ xZS )ÚGLPNForDepthEstimationc                 ó¤   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        t        |«      | _        | j                  «        y r/   )	r0   r1   r  r÷   rL  Údecoderre  rh  r  r  s     €r)   r1   zGLPNForDepthEstimation.__init__¨  s@   ø€ Ü‰Ñ˜Ô ä˜fÓ%ˆŒ	Ü" 6Ó*ˆŒÜ+¨FÓ3ˆŒ	ð 	�‰Õr+   zbatch_size, sequence_length)r  r  rU   Úlabelsr‚   rë   rì   r   c                 ó¼  — |�|n| j                   j                  }|�|n| j                   j                  }| j                  ||d|¬«      }|r|j                  n|d   }| j                  |«      }| j                  |«      }	d}
|�t        «       } ||	|«      }
|s|r
|	f|dd z   }n	|	f|dd z   }|
�|
f|z   S |S t        |
|	|r|j                  nd|j                  ¬«      S )a¨  
        labels (`torch.FloatTensor` of shape `(batch_size, height, width)`, *optional*):
            Ground truth depth estimation maps for computing the loss.

        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, GLPNForDepthEstimation
        >>> 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("vinvino02/glpn-kitti")
        >>> model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-kitti")

        >>> # 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"))
        ```NTr  r   rF   )rc  rn  r4   rç   )
rn   r  rë   r÷   r4   rr  rh  rX  r   rç   )r2   rU   rs  r‚   rë   rì   rŒ   r4   Úoutrn  rc  Úloss_fctr(   s                r)   r6   zGLPNForDepthEstimation.forward²  s  € ðb &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð —)‘)ØØ/Ø!%Ø#ð	 ó 
ˆñ 2=˜×-Ò-À'È!Á*ˆà�l‰l˜=Ó)ˆØŸ)™) C›.ˆàˆØÐÜ “{ˆHÙ˜O¨VÓ4ˆDáÙ#Ø)Ð+¨g°a°b¨kÑ9‘à)Ð+¨g°a°b¨kÑ9�Ø)-Ð)9�T�G˜fÑ$ÐE¸vÐEä#ØØ+Ù3G˜'×/Ò/ÈTØ×)Ñ)ô	
ð 	
r+   )NNNN)r:   r;   r<   r1   r   r  r8   r   r   r!  r"   r#  r   r$  r   r   r?   r6   rA   rB   s   @r)   rp  rp  £  sÁ   ø„ ô
ñ +Ð+@×+GÑ+GÐHeÓ+fÓgÙÐ+?ÈoÔ^ð /3Ø,0Ø/3Ø&*ñQ
à×'Ñ'ðQ
ð ˜×*Ñ*Ñ+ðQ
ð $ D™>ð	Q
ð
 ' t™nðQ
ð ˜d‘^ðQ
ð 
ˆu�U—\‘\Ñ"Ð$8Ð8Ñ	9òQ
ó _ó hôQ
r+   rp  )rp  r¸   r  rö   )r   F);r=   r}   Útypingr   r   r   r   r"   Útorch.utils.checkpointr   Úactivationsr	   Úmodeling_outputsr
   r   Úmodeling_utilsr   Úpytorch_utilsr   r   Úutilsr   r   r   r   r   Úconfiguration_glpnr   Ú
get_loggerr:   Úloggerr!  r   r"  r?   r>   r$  r*   ÚModuler-   rD   r\   r�   r—   r¥   r¬   r¸   rÆ   rö   ÚGLPN_START_DOCSTRINGr  r  r&  r<  rL  rX  re  rp  Ú__all__rà   r+   r)   ú<module>r„     sì  ðñ ã ß /Ó /ã Û Ý å !ß EÝ -ß Q÷õ õ +ð 
ˆ×	Ñ	˜HÓ	%€ð €ð -Ð Ú)Ð ñ�U—\‘\ð ¨eð ÀTð ÐV[×VbÑVbó ô*-�2—9‘9ô -ô) §¡ô )ô4N §¡ô Nôd	�R—Y‘Yô 	ô#�B—I‘Iô #ôN�—‘ô ô�—‘ô ô0(�—	‘	ô (ôVU
�"—)‘)ô U
ôp*˜/ô *ð:	Ð ðÐ ñ$ ØlØóô9
Ð#ó 9
ó	ð9
ôx) §¡ô )ôX�r—y‘yô ô(#�"—)‘)ô #ô6�—	‘	ô ô*˜bŸi™iô ñ2 ØgØóô^
Ð0ó ^
ó	ð^
òB V�r+   