
    ^j                         d dl Z ddlmZmZmZ ddlmZ  ej                  e      Z	 edd      Z
 edd      Z e       Z e       Zd	e j                  d
ede j                  fdZde j                  dz  de j                  de j                  defdZde j                  de j                  dz  dede j                  de j                  de j                  fdZ	 	 	 	 dde j(                  j*                  de j                  de j                  de j                  de j                  dz  dededz  dedz  de j                  dz  dee j                  df   fdZy)    N   )is_torch_npu_availableis_torch_xpu_availablelogging)is_torch_greater_or_equalz2.5T)
accept_devz2.8hidden_statesn_repreturnc                     | j                   \  }}}}|dk(  r| S | dddddddddf   j                  |||||      } | j                  |||z  ||      S )z
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
       N)shapeexpandreshape)r	   r
   batchnum_key_value_headsslenhead_dims         s/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/integrations/sdpa_attention.py	repeat_kvr      so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TT    attention_maskkeyvaluec                     t         rt        S t        xr0 | d u xr* |j                  d   |j                  d   cxk(  xr dk  S c S )N   )_is_torch_xpu_available#_is_torch_greater_or_equal_than_2_8#_is_torch_greater_or_equal_than_2_5r   )r   r   r   s      r   use_gqa_in_sdpar!      sI     22.u>T3IuciiXZm_d_j_jkm_nNuruNuuNuur   position_bias	is_causalqueryc                    t        j                  |j                        j                  }||r|j                  }|j
                  d   |j
                  d   }}t        j                  ||      dddf   t        j                  ||      dddf   k\  }	|	j                  dd||      }	t        j                  |	| |      }
|
S | }
|
S t        j                  || |      }
|
S )a   
    Create a floating-point dtype mask to use with sdpa. The mask contains the values of `position_bias` to positions where we should
    attend to tokens, and -inf where we should not. It will be added to the QK^T result in the attention, before the softmax. Note
    that using such a mask will usually prevent sdpa from dispatching to the most efficient kernel implementations.

    Note that we cannot create this in advance when we create the mask in the model, as the position_bias is usually learned
    differently in every layer.
    Nr   )devicer   )	torchfinfodtypeminr&   r   arangeviewwhere)r"   r   r#   r$   r   	min_dtyper&   q_length	kv_lengthcausal_maskposition_bias_masks              r   create_position_bias_maskr3   )   s     CII&**IZZF"'++a.#))A,iHXf5ag>%,,yagBhimopipBqq  &**1a9EK!&[-!S  "/  #[[	Rr   moduledropoutscalingc	                 *   |	j                  dd      rt        j                  d       i }
t        | d      rM| j                  dkD  r>t        |||      s-t        || j                        }t        || j                        }nddi}
|j                  d   }|j                  d   }||nt        | d	d      }|dkD  xr |d u xr |}t        j                  j                         r*t        |t        j                        r|j                         }t        r[|Y|j                   t        j"                  k7  r<t        j$                  |j#                               j'                  |j(                        }|rA|?|dkD  r:||kD  r5|d d d d d |d d f   }|d d d d d |d d f   }||d d d d d d d |f   }|t+        |||||      }d}t        j,                  j.                  j0                  |||f||||d
|
}|j3                  dd      j5                         }|d fS )Noutput_attentionsFz`sdpa` attention does not support `output_attentions=True`. Please set your attention to `eager` if you want any of these features.num_key_value_groupsr   
enable_gqaTr   r#   )	attn_mask	dropout_pscaler#   )getloggerwarning_oncehasattrr9   r!   r   r   getattrr'   jit
is_tracing
isinstanceTensoritem_is_torch_npu_availabler)   boollogical_nottor&   r3   nn
functionalscaled_dot_product_attention	transpose
contiguous)r4   r$   r   r   r   r5   r6   r#   r"   kwargssdpa_kwargsr/   r0   attn_outputs                 r   sdpa_attention_forwardrT   O   s(    zz%u-W	
 Kv-.63N3NQR3R~sE:C!<!<=CeV%@%@AE'.K{{1~H		!I '2	UY8ZI 1E4!7EII yy*Y"ENN$	
 %.*>*>%***L"..~/B/B/DEHHVN ^+1XAU!Q		1$%aIXIq()$)!Q9H9*<=M  2=.R[]bdgh	((%%BB	 !	 	K ''1-88:Kr   )g        NNN)r'   utilsr   r   r   utils.import_utilsr   
get_logger__name__r?   r    r   r   rH   rF   intr   rI   r!   r3   rL   ModulefloattuplerT    r   r   <module>r^      s    K K : 
		H	% '@RV&W #&?RV&W #02 02 	UU\\ 	U# 	U%,, 	U
vELL4$7 
vell 
vSXS_S_ 
vdh 
v#<<#LL4'# # <<	#
 
# \\#X  !)-WHHOOW<<W 
W <<	W
 LL4'W W T\W d{W <<$&W 5<<Wr   