
    i
                     \    d dl mZ d dlmZmZ ddlmZmZmZ  G d dej	                  Z
dS )    )Callable)Tensornn   )	Attention
LayerScaleMlpc                       e Zd Zddddddddej        ej        eeddfdedede	d	e
d
e
de
de	de	de	dedej        f         dedej        f         dedej        f         dedej        f         de
ddf fdZddedefdZ xZS )Blockg      @T        NFdim	num_heads	mlp_ratioqkv_bias	proj_biasffn_biasdrop	attn_drop	drop_path	act_layer.
norm_layer
attn_class	ffn_layerqk_normreturnc           
         t                                                        ||          | _         |||||||||          | _        |	rt	          ||	          nt          j                    | _         ||          | _        t          ||z            } ||||||          | _
        |	rt	          ||	          nt          j                    | _        d| _        d S )N)r   r   r   r   	proj_dropr   rope)init_values)in_featureshidden_featuresr   r   biasr   )super__init__norm1attnr   r   Identityls1norm2intmlpls2sample_drop_ratio)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mlp_hidden_dim	__class__s                     F/home/robot-lab/lab/da3_repo/src/depth_anything_3/model/utils/block.pyr$   zBlock.__init__   s   & 	Z__
J	
 	
 	
	 @K]:c{;;;;PRP[P]P]Z__
S9_--9*
 
 
 @K]:c{;;;;PRP[P]P]!$    xc                      ddt           dt           f fd}dt           dt           f fd}| ||||          z   }| ||          z   }|S )Nr3   r   c                                                                                  |           ||                    S )Npos	attn_mask)r(   r&   r%   )r3   r7   r8   r.   s      r1   attn_residual_funcz)Block.forward.<locals>.attn_residual_funcH   s1    88DIIdjjmm	IRRSSSr2   c                 z                                                                 |                               S )N)r,   r+   r)   )r3   r.   s    r1   ffn_residual_funcz(Block.forward.<locals>.ffn_residual_funcK   s+    88DHHTZZ]]33444r2   r6   NN)r   )r.   r3   r7   r8   r9   r;   s   `     r1   forwardzBlock.forwardG   s    	T 	T& 	Tv 	T 	T 	T 	T 	T 	T	5 	5F 	5 	5 	5 	5 	5 	5 ""1#CCCC!!!$$$r2   r<   )__name__
__module____qualname__r   GELU	LayerNormr   r	   r*   floatboolr   Moduler$   r   r=   __classcell__)r0   s   @r1   r   r      sZ       
 .0g/1|/8.1#.% .%.% .% 	.%
 .% .% .% .% .% .% CN+.% S")^,.% S")^,.% CN+.%  !.%$ 
%.% .% .% .% .% .%`
 
 
f 
 
 
 
 
 
 
 
r2   r   N)typingr   torchr   r   	attentionr   r   r	   rE   r    r2   r1   <module>rK      s                   1 1 1 1 1 1 1 1 1 1; ; ; ; ;BI ; ; ; ; ;r2   