a
    h                     @   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ejeejd
ddZeej ejedddZdejjejejejeej eee ee eejdf d	ddZdS )    )OptionalN   )is_torch_xpu_availablelogging)is_torch_greater_or_equalz2.5T)Z
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expandZreshape)r   r   batchZnum_key_value_headsslenZhead_dim r   d/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/integrations/sdpa_attention.py	repeat_kv   s
    0r   )attention_maskkeyr	   c                 C   s4   t rtot|tjj S to2| d u o2t|tjj S )N)_is_torch_xpu_available#_is_torch_greater_or_equal_than_2_8
isinstancetorchZfxZProxy#_is_torch_greater_or_equal_than_2_5)r   r   r   r   r   use_gqa_in_sdpa   s    	r           )	modulequeryr   valuer   dropoutscaling	is_causalr	   c                 K   s  | dds| dd ur$td i }	t| dr^t||sVt|| j}t|| j}nddi}	|d ur|jdkr|d d d d d d d |jd	 f }|d u r|jd
 dko|d u ot	| dd}t
j rt|t
jr| }t
jjj|||f||||d|	}
|
dd
 }
|
d fS )NZoutput_attentionsFZ	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_groupsZ
enable_gqaT   r   r
   r    )Z	attn_maskZ	dropout_pscaler    )getloggerZwarning_oncehasattrr   r   r!   ndimr   getattrr   Zjit
is_tracingr   TensoritemnnZ
functionalZscaled_dot_product_attentionZ	transpose
contiguous)r   r   r   r   r   r   r   r    kwargsZsdpa_kwargsZattn_outputr   r   r   sdpa_attention_forward+   s<    

&"
r0   )r   NN)typingr   r   utilsr   r   Zutils.import_utilsr   Z
get_logger__name__r&   r   r   r   r+   intr   boolr   r-   Modulefloattupler0   r   r   r   r   <module>   s.   
   