o
    
sh                     @   s   d dl mZ d dlZddlmZmZ ddlmZ ee	Z
edddZed	ddZe Zd
ejdedejfddZdeej dejdefddZ			ddejjdejdejdejdeej dedee dee deejdf fddZdS )    )OptionalN   )is_torch_xpu_availablelogging)is_torch_greater_or_equalz2.5T)
accept_devz2.8hidden_statesn_repreturnc                 C   s^   | j \}}}}|dkr| S | dddddddddf |||||} | ||| ||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_dim r   f/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/transformers/integrations/sdpa_attention.py	repeat_kv   s
   0r   attention_maskkeyc                 C   s4   t rtot|tjj S to| d u ot|tjj S )N)_is_torch_xpu_available#_is_torch_greater_or_equal_than_2_8
isinstancetorchfxProxy#_is_torch_greater_or_equal_than_2_5)r   r   r   r   r   use_gqa_in_sdpa   s   	r           modulequeryvaluedropoutscaling	is_causalc                 K   s  | dds| dd urtd i }	t| dr/t||s+t|| j}t|| j}nddi}	|d urK|jdkrK|d d d d d d d |jd	 f }|d u r`|jd
 dko_|d u o_t	| dd}t
j rot|t
jro| }t
jjj|||f||||d|	}
|
dd
 }
|
d fS )Noutput_attentionsF	head_maskz`sdpa` attention does not support `output_attentions=True` or `head_mask`. Please set your attention to `eager` if you want any of these features.num_key_value_groups
enable_gqaT   r   r   r&   )	attn_mask	dropout_pscaler&   )getloggerwarning_oncehasattrr   r   r)   ndimr   getattrr   jit
is_tracingr   Tensoritemnn
functionalscaled_dot_product_attention	transpose
contiguous)r!   r"   r   r#   r   r$   r%   r&   kwargssdpa_kwargsattn_outputr   r   r   sdpa_attention_forward+   s<   

&"
rB   )r    NN)typingr   r   utilsr   r   utils.import_utilsr   
get_logger__name__r1   r   r   r   r8   intr   boolr   r:   ModulefloattuplerB   r   r   r   r   <module>   s@    

