
     `i+                        d dl mZ 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ej                 dej        def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ZdS )    )OptionalN   )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                             |||||          } |                     |||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         |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/integrations/sdpa_attention.py	repeat_kvr      s    
 2?1D.Ehzz!!!!QQQaaa"23::5BUW\^bdlmmM  (;e(CT8TTT    attention_maskkeyc                     t           r't          ot          |t          j        j                   S t          rdS t          o#| d u ot          |t          j        j                   S )NF)_is_torch_xpu_available#_is_torch_greater_or_equal_than_2_8
isinstancetorchfxProxy_is_torch_npu_available#_is_torch_greater_or_equal_than_2_5)r   r   s     r   use_gqa_in_sdpar$      sa      [2Z:c58>;Z;Z7ZZ u.q>T3IqR\]`bgbjbpRqRqNqqr           modulequeryvaluedropoutscaling	is_causalc                    |                     dd          s|                     d          t                              d           i }	t          | d          r?t	          ||          s+t          || j                  }t          || j                  }nddi}	|*|j        dk    r|d d d d d d d |j        d	         f         }|&|j        d
         dk    o|d u ot          | dd          }t          j                                        r.t          |t          j                  r|                                }t           rU|S|j        t          j        k    r>t          j        |                                                              |j                  }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itemr"   dtypeboollogical_nottodevicenn
functionalscaled_dot_product_attention	transpose
contiguous)r&   r'   r   r(   r   r)   r*   r+   kwargssdpa_kwargsattn_outputs              r   sdpa_attention_forwardrM   0   s    zz%u-- 
K1H1H1TW	
 	
 	
 Kv-.. /~s33 	/C!<==CeV%@AAEE'.K!n&9Q&>&>'111aaa39R=(@A
  KNQ&h>T+AhgfVacgFhFh	 y %*Y"E"E %NN$$	
  W%.*>%**L*L".~/B/B/D/DEEHHVVN(%B	 !	 	 	 	K ''1--88::Kr   )r%   NN)typingr   r   utilsr   r   r   utils.import_utilsr   
get_logger__name__r7   r#   r   r   r"   r>   intr   rA   r$   rE   ModulefloattuplerM    r   r   <module>rX      s          K K K K K K K K K K : : : : : : 
	H	%	% '@&?RV&W&W&W #&?&?RV&W&W&W #0022 0022 	UU\ 	U# 	U%, 	U 	U 	U 	UrHU\$: r rRV r r r r0 # $< <HO<<< 
< <	<
 U\*< < e_< ~< 5<< < < < < <r   