
    fPi
                      f    d dl Z d dlmZmZ d dlmZ  e j        e          Z G d de          Z	dS )    N)AttentionMaskFusionAttention)	OnnxModelc                   :     e Zd ZdZdedededef fdZd Z xZ	S )FusionConformerAttentionzM
    Fuse Conformer Attention subgraph into one MultiHeadAttention node.
    modelhidden_size	num_headsattention_maskc                 P    t                                          ||||           d S )N)super__init__)selfr   r	   r
   r   	__class__s        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/transformers/fusion_conformer_attention.pyr   z!FusionConformerAttention.__init__   s)     	YGGGGG    c                 ^   | j                             |g dg d          }|t                              d           d S |d         |d         |d         }}}d\  }}	| j                             |g dg d	          }
|
?| j                             |g d
g d          }
|
t                              d           d S n>|
d         }| j                             |dd           }|j        d         }	|j        d         }|
d         |
d         }}d}| j                             |g dg d          }|{| j                             |g dg d          }|t                              d           d S |d         }| j                             |g dg d          }||d         j        d         }|d         |d         }}| j                             |g dg d          }|>| j                             |g dg d          }|t                              d           d S |d         |d         |d         }}}| j                             |g dg d          }|.|d         |d         k    rt                              d           d S d\  }}| j                             |g dg d          }|a| j                             |g d g d!          }|>| j                             |g d
g d          }|t                              d"           d S n>|d#         }| j                             |dd           }|j        d         }|j        d         }|d         |d         }}|                     |          \  } }!| dk    s|!dk    s	|!| z  dk    rt                              d$           d S d }"|j        d         |j        d         k    o|j        d         |j        d         k    o|d u }#|#rG|                     |||||||| |!|j        d         |j        d         |j        d#         ||||	%          }"n:| 	                    ||||||| |!|j        d         ||j        d#         ||||	&          }"|"t                              d'           d S | j
                            |"           | j        | j        |"j        <   | j                            |||g           | j                            |           |#so|d         j        d(k    r|                                 |d         j        d(k    r|                                 |
d         j        d(k    r|
                                 || j                            |           | j                            |           | j                            |
           d)| _        d S )*N)AddMatMulReshape	Transposer   )   Nr   r   r   z2fuse_conformer_attention: failed to match qkv path) r   )Concatr   r   r   r   )r   r   r   r   r   )r   r   r   r   )r   r   r   r   z0fuse_conformer_attention: failed to match v pathr   r   )Softmaxr   r   )r   r   r   )Wherer   r   r   r   )r      r   r    r   z1fuse_conformer_attention: failed to match qk pathr    )Equal	UnsqueezeCast)Divr   r   r   r   )r   r   r   r   r   )Mulr   r   r   r   )r   r   r   r   r   z0fuse_conformer_attention: failed to match q path)r   r   r   r   r   r$   )r   r   r   r   r   r   z6fuse_conformer_attention: failed to match extra q path)r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   z0fuse_conformer_attention: failed to match k pathr   zCfuse_conformer_attention: failed to detect num_heads or hidden_size)
mask_indexq_matmulk_matmulv_matmulq_addk_addv_addr
   r	   first_inputoutput
add_qk_strpast_kpast_v	present_k	present_v)r'   r(   r)   r*   r+   r,   r
   r	   r.   key_padding_maskadd_qkr0   r1   r2   r3   zAfuse_conformer_attention: MultiHeadAttention node creation failedr   T)r   match_parent_pathloggerdebug
get_parentr.   get_num_heads_and_hidden_sizeinputcreate_attention_nodecreate_multihead_attention_nodenodes_to_addappendthis_graph_namenode_name_to_graph_namenamenodes_to_removeextendop_typepopprune_graph)$r   normalize_nodeinput_name_to_nodesoutput_name_to_node	qkv_nodesreshape_qkvtranspose_qkv
matmul_qkvr1   r3   v_nodesconcat_vconcat_parentadd_vmatmul_v	attn_maskqk_nodeswhere_qk
mask_nodesr5   	matmul_qkq_nodes	reshape_qadd_qmatmul_qextra_q_nodesr0   r2   k_nodesconcat_kadd_kmatmul_kr
   r	   new_nodeuse_packed_attention_ops$                                       r   fusezFusionConformerAttention.fuse   s   J00???
 
	
 LLMNNNF1:2	"yY[}J]"	*..???OO
 

 ?j22999 G
 OPPP  qzH J11(AtDDM *I")!,F!"+wr{x	://(((II
 

 z33>>> H
 PQQQ{H55...		 J
 %&rN1!4	$RL(2,	*..<<<OO
 

 ?j22@@@ G
 OPPP%,R['"+wr{(5	
44MMM
 

 $}R7H)H)HLLQRRRF"	*..LLL
 

 ?j22FFF G
 *66=== LL 
 ?LL!STTTFqzH J11(AtDDM")!,F *I!"+wr{x!%!C!CI!N!N	;>>[A--+	2Ia1O1OLL^___FN1!22wx~a7HHN[\L]7]wboswbw 	  # %	11$!!!#'$N1-")!,!<?##! 2  HH& ;;!!!#'")!,!*|A## <  H$ LL\]]]F  ***6:6J$X]3##[-$LMMM##H--- ' 	r{"h..r{"h..r{"h..   ''000##G,,,##G,,,  r   )
__name__
__module____qualname____doc__r   intr   r   rd   __classcell__)r   s   @r   r   r      s         HH H 	H
 &H H H H H HC  C  C  C  C  C  C r   r   )
loggingfusion_attentionr   r   
onnx_modelr   	getLoggerre   r7   r    r   r   <module>rp      s   
  ; ; ; ; ; ; ; ;            		8	$	$Q  Q  Q  Q  Q  Q  Q  Q  Q  Q r   