
    gz              	          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j        e          ZdZdZ g dZ!d=dej"        de#de$dej"        fdZ% G d de	j&                  Z' G d de	j&                  Z( G d de	j&                  Z) G d de	j&                  Z* G d d e	j&                  Z+ G d! d"e	j&                  Z, G d# d$e	j&                  Z- G d% d&e	j&                  Z. G d' d(e	j&                  Z/ G d) d*e          Z0d+Z1d,Z2 ed-e1           G d. d/e0                      Z3 G d0 d1e	j&                  Z4 G d2 d3e	j&                  Z5 G d4 d5e	j&                  Z6 G d6 d7e	j&                  Z7 G d8 d9e	j&                  Z8 ed:e1           G d; d<e0                      Z9dS )>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                 F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   }|                                 |                     |          |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   )r   )dtypedevice)shapendimtorchrandr   r   floor_div)r   r   r   	keep_probr    random_tensoroutputs          b/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/glpn/modeling_glpn.py	drop_pathr*   2   s     CxII[^
Q 77E
5EL Y Y YYMYYy!!M1FM    c                   j     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                 V    t                                                       || _        d S N)super__init__r   )selfr   	__class__s     r)   r1   zGLPNDropPath.__init__J   s$    "r+   hidden_statesc                 8    t          || j        | j                  S r/   )r*   r   r   )r2   r4   s     r)   forwardzGLPNDropPath.forwardN   s    FFFr+   c                 6    d                     | 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   s        bb# #(5/ #T # # # # # #GU\ Gel G G G 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        |          | _        d S )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   s[    I"!O
 
 
	 ,{33r+   c                     |                      |          }|j        \  }}}}|                    d                              dd          }|                     |          }|||fS )NrF   r   )rL   r    flatten	transposerN   )r2   pixel_values
embeddings_heightwidths         r)   r6   z"GLPNOverlapPatchEmbeddings.forwarde   sg    YY|,,
(.1fe  ''**44Q::
__Z00
65((r+   r:   r;   r<   r=   r1   r6   rA   rB   s   @r)   rD   rD   V   sM        55
4 
4 
4 
4 
4) ) ) ) ) ) )r+   rD   c                   2     e Zd ZdZ fdZd Z	 ddZ xZS )GLPNEfficientSelfAttentionzSegFormer's efficient self-attention mechanism. Employs the sequence reduction process introduced in the [PvT
    paper](https://arxiv.org/abs/2102.12122).c                    t                                                       || _        || _        | j        | j        z  dk    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k    r8t          j        ||||          | _        t          j        |          | _        d S d S )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   s]   &#6 d66!;;6D$4 6 626 6 6  
 $'t'7$:R'R#S#S !58PPYt/1CDD
9T-t/ABBYt/1CDD
z&"EFF0#a''i[6NWo  DG !l;77DOOO	 ('r+   c                     |                                 d d         | j        | j        fz   }|                    |          }|                    dddd          S )Nr   rF   r   r   )sizer_   rb   viewpermute)r2   r4   	new_shapes      r)   transpose_for_scoresz/GLPNEfficientSelfAttention.transpose_for_scores   sY    !&&(("-1I4Kc0dd	%**955$$Q1a000r+   Fc                 "   |                      |                     |                    }| j        dk    r|j        \  }}}|                    ddd                              ||||          }|                     |          }|                    ||d                              ddd          }|                     |          }|                      |                     |                    }	|                      | 	                    |                    }
t          j        ||	                    dd                    }|t          j        | j                  z  }t           j                            |d          }|                     |          }t          j        ||
          }|                    dddd                                          }|                                d d         | j        fz   }|                    |          }|r||fn|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    //

=0I0IJJ=10=0C-J)11!Q::BB:|]cejkkM GGM22M)11*lBOOWWXY[\^_``M OOM::M--dhh}.E.EFF	//

=0I0IJJ !<Y5H5HR5P5PQQ+di8P.Q.QQ -//0@b/II ,,77_kBB%--aAq99DDFF"/"4"4"6"6ss";t?Q>S"S%**+BCC6G]=/22mM]r+   F)r:   r;   r<   r=   r1   rv   r6   rA   rB   s   @r)   r\   r\   p   sj        1 18 8 8 8 861 1 1  * * * * * * * *r+   r\   c                   $     e Zd Z fdZd Z xZS )GLPNSelfOutputc                     t                                                       t          j        ||          | _        t          j        |j                  | _        d S r/   )r0   r1   r   rd   denserh   hidden_dropout_probrj   )r2   rn   rQ   r3   s      r)   r1   zGLPNSelfOutput.__init__   sD    Y{K88
z&"<==r+   c                 Z    |                      |          }|                     |          }|S r/   )r   rj   )r2   r4   input_tensors      r)   r6   zGLPNSelfOutput.forward   s*    

=11]33r+   r:   r;   r<   r1   r6   rA   rB   s   @r)   r   r      sG        > > > > >
      r+   r   c                   ,     e Zd Z fdZd ZddZ xZS )GLPNAttentionc                     t                                                       t          ||||          | _        t	          ||          | _        t                      | _        d S )N)rn   rQ   r_   ro   )rQ   )r0   r1   r\   r2   r   r(   setpruned_headsrm   s        r)   r1   zGLPNAttention.__init__   sc    .# 3%=	
 
 
	 %VEEEEEr+   c                    t          |          dk    rd S 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                            |          | _        d S )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::??F7490$)2OQUQb
 
u
 -TY_eDD	*49=%@@	,TY_eDD	.t{/@%QOOO )-	(EE

(R	%"&)"?$)B_"_	 -33E::r+   Fc                     |                      ||||          }|                     |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   sM    yy?PQQ;;|AFF#%QRR(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 )
GLPNDWConv   c           	          t                                                       t          j        ||dddd|          | _        d S )Nr   r   T)biasgroups)r0   r1   r   rK   dwconv)r2   rz   r3   s     r)   r1   zGLPNDWConv.__init__   s=    iS!QSIIIr+   c                     |j         \  }}}|                    dd                              ||||          }|                     |          }|                    d                              dd          }|S )Nr   rF   )r    rT   rs   r   rS   )r2   r4   rX   rY   r   r   rP   s          r)   r6   zGLPNDWConv.forward   sv    ,9,?)
G\%//155:::|U[]bccM22%--a00::1a@@r+   )r   r   rB   s   @r)   r   r      sR        J J J J J J      r+   r   c                   &     e Zd Zd fd	Zd Z xZS )
GLPNMixFFNNc                    t                                                       |p|}t          j        ||          | _        t          |          | _        t          |j        t                    rt          |j                 | _        n|j        | _        t          j        ||          | _        t          j        |j                  | _        d S 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_== 11f'-- 	9'-f.?'@D$$'-'8D$i>>z&"<==r+   c                    |                      |          }|                     |||          }|                     |          }|                     |          }|                     |          }|                     |          }|S r/   )r   r   r   rj   r   )r2   r4   rX   rY   s       r)   r6   zGLPNMixFFN.forward  st    M22M65AA00??]33M22]33r+   )NNr   rB   s   @r)   r   r     sL        
> 
> 
> 
> 
> 
>      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                    t                                                       t          j        |          | _        t          ||||          | _        |dk    rt          |          nt          j                    | _	        t          j        |          | _
        t          ||z            }t          |||          | _        d S )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55&# 3%=	
 
 
 5>OOi000L55kI566f+___r+   Fc                 J   |                      |                     |          |||          }|d         }|dd          }|                     |          }||z   }|                     |                     |          ||          }|                     |          }||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XXd//>>NN
 ^^J//
!M1/G+r+   r   rZ   rB   s   @r)   r   r     sW        MM` ` ` ` `       r+   r   c                   ,     e Zd Z fdZ	 	 	 ddZ xZS )GLPNEncoderc                 <   t                                                       | _        d t          j        dj        t          j                            D             }g }t          j	                  D ]d}|
                    t          j        |         j        |         |dk    rj        nj        |dz
           j        |                              et!          j        |          | _        g }d}t          j	                  D ]}g }|dk    r|j        |dz
           z  }t          j        |                   D ]_}|
                    t'          j        |         j        |         |||z            j        |         j        |                              `|
                    t!          j        |                     t!          j        |          | _        t!          j        fdt          j	                  D                       | _        d S )Nc                 6    g | ]}|                                 S  )item).0xs     r)   
<listcomp>z(GLPNEncoder.__init__.<locals>.<listcomp>N  s     ^^^Aqvvxx^^^r+   r   r   )rO   rI   rP   rQ   )rQ   r_   r*   ro   r   c                 N    g | ]!}t          j        j        |                   "S r   )r   rM   hidden_sizes)r   irn   s     r)   r   z(GLPNEncoder.__init__.<locals>.<listcomp>v  s+    \\\aR\&-a011\\\r+   )r0   r1   rn   r"   linspacedrop_path_ratesumdepthsrangenum_encoder_blocksappendrD   patch_sizesstridesrP   r   r   
ModuleListpatch_embeddingsr   r_   	sr_ratios
mlp_ratiosblockrN   )
r2   rn   dprrV   r   blockscurlayersjr3   s
    `       r)   r1   zGLPNEncoder.__init__I  s3    _^63H#fmJ\J\!]!]^^^ 
v011 	 	A*%1!4!>!,89Q!4!4FDWXY\]X]D^ & 3A 6	      !#j 9 9 v011 	1 	1AFAvvv}QU++6=+,, 
 
$*$7$:,2,Fq,I"%cAg,171A!1D"("3A"6  	 	 	 	 MM"-//0000]6**
 -\\\\5AZ;[;[\\\
 
r+   FTc                 @   |rdnd }|rdnd }|j         d         }|}t          t          | j        | j        | j                            D ]\  }	}
|
\  }}} ||          \  }}}t          |          D ])\  }} |||||          }|d         }|r||d         fz   }* ||          }|                    |||d                              dddd                                          }|r||fz   }|st          d |||fD                       S t          |||          S )	Nr   r   r   rq   r   rF   c              3      K   | ]}||V  	d S r/   r   )r   vs     r)   	<genexpr>z&GLPNEncoder.forward.<locals>.<genexpr>  s(      mmq_`_l_l_l_l_lmmr+   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  s    #7@BBD$5?bb4!'*
$D$94:t W WXX 	I 	IFC784O[*+:?=+I+I(M65#K00 T T3 #M65BS T T -a 0$ T*=qAQ@S*S'&J}55M)11*feRPPXXYZ\]_`bcddooqqM# I$58H$H! 	nmm]4EGZ$[mmmmmm++*
 
 
 	
r+   )FFTr   rB   s   @r)   r   r   H  sX        .
 .
 .
 .
 .
f  "$
 $
 $
 $
 $
 $
 $
 $
r+   r   c                   (    e Zd ZdZeZdZdZg Zd Z	dS )GLPNPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    glpnrU   c                    t          |t          j        t          j        f          rT|j        j                            d| j        j                   |j	         |j	        j        
                                 dS dS t          |t          j                  r_|j        j                            d| j        j                   |j        +|j        j        |j                 
                                 dS dS t          |t          j                  r?|j	        j        
                                 |j        j                            d           dS dS )zInitialize the weightsr   )meanstdNg      ?)r   r   rd   rK   weightdatanormal_rn   initializer_ranger   zero_	Embeddingpadding_idxrM   fill_)r2   modules     r)   _init_weightsz!GLPNPreTrainedModel._init_weights  s0   fry")455 	* M&&CT[5R&SSS{& &&((((( '&-- 	*M&&CT[5R&SSS!-"6#56<<>>>>> .--- 	*K""$$$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     sE         
 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                    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                     t                                          |           || _        t          |          | _        |                                  d S r/   )r0   r1   rn   r   encoder	post_initr2   rn   r3   s     r)   r1   zGLPNModel.__init__  sK        #6** 	r+   c                     |                                 D ]/\  }}| j        j        |         j                            |           0dS )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  sU    
 +0022 	C 	CLE5Lu%/;;EBBBB	C 	Cr+   z(batch_size, sequence_length)vision)
checkpointoutput_typer	  modalityexpected_outputNrU   r   r   r   r   c                     ||n| j         j        }||n| j         j        }||n| 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--TXT_Tq$8$D  $+Jj 	 &1%<kk$+B],,/!5#	 ' 
 
 *!, 	<#%(;;;-)7&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 C C +*+@+G+GHg+h+hii&#$.   -1/3&*
 
'
 $D>
 'tn	

 d^
 
uo%	&
 
 
  ji
 
 
 
 
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           	         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                    | _        d S )NrF   r   r   )in_channelsout_channelsrH   rI   rJ   )r0   r1   r   
SequentialrK   ra   BatchNorm2dReLUconvolutional_layer1convolutional_layer2convolutional_layer3Sigmoidsigmoid)r2   
in_channelr3   s     r)   r1   z#GLPNSelectiveFeatureFusion.__init__   s   $&MI#j1n"5"5J\]fgqrsssN:&&GII%
 %
! %'MI*3zA~;N;N\]fgqrsssN3zA~..//GII%
 %
! %'IJN++!ST^_%
 %
 %
! z||r+   c                    t          j        ||fd          }|                     |          }|                     |          }|                     |          }|                     |          }||d d dd d d d f                             d          z  ||d d dd d d d f                             d          z  z   }|S )Nr   ry   r   )r"   catr1  r2  r3  r5  	unsqueeze)r2   local_featuresglobal_featuresfeaturesattnhybrid_featuress         r)   r6   z"GLPNSelectiveFeatureFusion.forward5  s    9no>AFFF,,X66,,X66,,X66||H%%(41aaa
+;+E+Ea+H+HH?]aAAq!!!QQQJ^

)A,,L  r+   )r*  rZ   rB   s   @r)   r)  r)    sV         
$ $ $ $ $ $*      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          | _	        d S )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t29[,ANNNNgigrgtgt0>>SXYYYr+   Nc                     |                      |          }||                     ||          }|                     |          }|S r/   )rG  rH  rJ  )r2   hidden_stateresiduals      r)   r6   zGLPNDecoderStage.forwardN  sE    ''55;;|X>>L}}\22r+   r/   r   rB   s   @r)   r@  r@  F  sQ        Z Z Z Z Z	 	 	 	 	 	 	 	r+   r@  c                   Z     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        fd|D                       | _        d | j        d         _        t	          j        ddd          | _	        d S )Nrq   c                 0    g | ]}t          |          S r   )r@  )r   rQ   r-  s     r)   r   z(GLPNDecoder.__init__.<locals>.<listcomp>b  s$    bbb[k<88bbbr+   r   rF   rB  FrC  )
r0   r1   r   decoder_hidden_sizer   r   stagesrH  rI  final_upsample)r2   rn   reserved_hidden_sizesr-  r3   s      @r)   r1   zGLPNDecoder.__init__[  s     & 3DDbD 91mbbbbLabbb
 
 !%A kqzY^___r+   r4   r   c                     g }d }t          |d d d         | j                  D ]&\  }} |||          }|                    |           '|                     |          |d<   |S )Nrq   )r   rT  r   rU  )r2   r4   stage_hidden_statesstage_hidden_staterM  stages         r)   r6   zGLPNDecoder.forwardi  s     !#&}TTrT':DK#H#H 	; 	;L%!&|5G!H!H&&'9::::"&"5"56H"I"IB""r+   	r:   r;   r<   r1   r   r"   r?   r6   rA   rB   s   @r)   rP  rP  Z  sm        ` ` ` ` `	#T%,%7 	#D<N 	# 	# 	# 	# 	# 	# 	# 	#r+   rP  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                 V    t                                                       || _        d S r/   )r0   r1   lambd)r2   r`  r3   s     r)   r1   zSiLogLoss.__init__~  s$    


r+   c                 r   |dk                                     }t          j        ||                   t          j        ||                   z
  }t          j        t          j        |d                                          | j        t          j        |                                d          z  z
            }|S )Nr   rF   )detachr"   logr~   powr   r`  )r2   predtarget
valid_maskdiff_loglosss         r)   r6   zSiLogLoss.forward  s    qj((**
9VJ/0059T*=M3N3NNz%)Ha005577$*uyQYQ^Q^Q`Q`bcGdGd:ddeer+   )r^  rZ   rB   s   @r)   r]  r]  u  sV                    r+   r]  c                   N     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                    | _        d S )Nr   r   rG   F)inplace)	r0   r1   rn   rS  r   r.  rK   r0  head)r2   rn   channelsr3   s      r)   r1   z GLPNDepthEstimationHead.__init__  s    -MIha1MMMGE"""IhqAFFF
 
			r+   r4   r   c                     || j         j                 }|                     |          }t          j        |          | j         j        z  }|                    d          }|S )Nr   ry   )rn   head_in_indexrn  r"   r5  	max_depthsqueeze)r2   r4   predicted_depths      r)   r6   zGLPNDepthEstimationHead.forward  sW    %dk&?@		-00-669NN)11a188r+   r[  rB   s   @r)   rk  rk    sc        

 

 

 

 

	T%,%7 	EL 	 	 	 	 	 	 	 	r+   rk  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                    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          |          | _        |                                  d S r/   )	r0   r1   r  r   rP  decoderrk  rn  r  r  s     r)   r1   zGLPNForDepthEstimation.__init__  s`       f%%	"6**+F33	 	r+   zbatch_size, sequence_length)r  r	  NrU   labelsr   r   r   r   c                    ||n| j         j        }||n| j         j        }|                     ||d|          }|r|j        n|d         }|                     |          }|                     |          }	d}
|t                      } ||	|          }
|s)|r|	f|dd         z   }n|	f|dd         z   }|
|
f|z   n|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)
        ...     predicted_depth = outputs.predicted_depth

        >>> # interpolate to original size
        >>> prediction = torch.nn.functional.interpolate(
        ...     predicted_depth.unsqueeze(1),
        ...     size=image.size[::-1],
        ...     mode="bicubic",
        ...     align_corners=False,
        ... )

        >>> # visualize the prediction
        >>> output = prediction.squeeze().cpu().numpy()
        >>> formatted = (output * 255 / np.max(output)).astype("uint8")
        >>> depth = Image.fromarray(formatted)
        ```NTr  r   rF   )ri  rt  r4   r   )
rn   r  r   r   r4   rx  rn  r]  r   r   )r2   rU   ry  r   r   r   r   r4   outrt  ri  loss_fctr(   s                r)   r6   zGLPNForDepthEstimation.forward  s:   f &1%<kk$+B]$8$D  $+Jj 	 ))/!%#	  
 
 2=L--'!*ll=))))C.. {{H8OV44D 	F# :)+gabbk9)+gabbk9)-)9TGf$$vE#+3GQ'//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)   rv  rv    s       
     +*+@+G+GHe+f+fgg+?o^^^ /3,0/3&*S
 S
'S
 *+S
 $D>	S

 'tnS
 d^S
 
uU\"$88	9S
 S
 S
 _^ hgS
 S
 S
 S
 S
r+   rv  )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@  rP  r]  rk  rv  r   r+   r)   <module>r     s      / / / / / / / / / / / /            ! ! ! ! ! ! E E E E E E E E - - - - - - Q Q Q Q Q Q Q Q              + * * * * * 
	H	%	%  - )))  U\ e T V[Vb    *- - - - -29 - - -) ) ) ) ) ) ) )4N N N N N N N Nd	 	 	 	 	RY 	 	 	# # # # #BI # # #N              0( ( ( ( (	 ( ( (VU
 U
 U
 U
 U
") U
 U
 U
p* * * * */ * * *:	  $ l 9
 9
 9
 9
 9
# 9
 9
	 9
x) ) ) ) ) ) ) )X    ry   (# # # # #") # # #6    	   *    bi   2 g `
 `
 `
 `
 `
0 `
 `
	 `
 `
 `
r+   