
    Bvh
                      b    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	y)    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                 *    t         |   ||||       y )N)super__init__)selfr   r	   r
   r   	__class__s        ]/RAG/venv/lib/python3.12/site-packages/onnxruntime/transformers/fusion_conformer_attention.pyr   z!FusionConformerAttention.__init__   s     	YG    c                 
   | j                   j                  |g dg d      }|t        j                  d       y |d   |d   |d   }}}d\  }}	| j                   j                  |g dg d	      }
|
9| j                   j                  |g d
g d      }
|
Vt        j                  d       y |
d   }| j                   j	                  |dd       }|j
                  d   }	|j
                  d   }|
d   |
d   }}d}| j                   j                  |g dg d      }|s| j                   j                  |g dg d      }|t        j                  d       y |d   }| j                   j                  |g dg d      }||d   j
                  d   }|d   |d   }}| j                   j                  |g dg d      }|9| j                   j                  |g dg d      }|t        j                  d       y |d   |d   |d   }}}| j                   j                  |g dg d      }|!|d   |d   k7  rt        j                  d       y d\  }}| j                   j                  |g dg d      }|\| j                   j                  |g d g d!      }|y| j                   j                  |g d
g d      }|Vt        j                  d"       y |d#   }| j                   j	                  |dd       }|j
                  d   }|j
                  d   }|d   |d   }}| j                  |      \  } }!| dk  s|!dk  s|!| z  dk7  rt        j                  d$       y d }"|j                  d   |j                  d   k(  xr% |j                  d   |j                  d   k(  xr |d u }#|#rI| j                  |||||||| |!|j                  d   |j
                  d   |j                  d#   ||||	%      }"n:| j                  ||||||| |!|j
                  d   ||j                  d#   ||||	&      }"|"t        j                  d'       y | j                  j                  |"       | j                  | j                  |"j                  <   | j                  j!                  |||g       | j                  j!                  |       |#sf|d   j"                  d(k(  r|j%                          |d   j"                  d(k(  r|j%                          |
d   j"                  d(k(  r|
j%                          || j                  j!                  |       | j                  j!                  |       | j                  j!                  |
       d)| _        y )*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   JJ00?
	
 LLMN1:2	"yY[}J]"	**..?

 ?jj229G
 OPqzH JJ11(AtDM *I"))!,F!"+wr{x	:://(

 zz33>H
 PQ{H55.J
 %&rN11!4	$RL(2,	**..<

 ?jj22@G
 OP%,R['"+wr{(5	

44M

 $}R7H)HLLQR"	**..L

 ?jj22FG
 **66= 
 ?LL!STqzH JJ11(AtDM"))!,F *I!"+wr{x!%!C!CI!N	;>[A-+	2Ia1OLL^_NN1!22wx~~a7HHNN[\L]7]wboswbw 	  #11$!!!#'$NN1-"))!,!<<?##! 2 H& ;;!!!#'"))!,!*||A## < H$ LL\]  *6:6J6J$$X]]3##[-$LM##H- 'r{""h.r{""h.r{""h.    ''0##G,##G,  r   )
__name__
__module____qualname____doc__r   intr   r   rd   __classcell__)r   s   @r   r   r      s>    HH H 	H
 &HC r   r   )
loggingfusion_attentionr   r   
onnx_modelr   	getLoggerre   r7   r    r   r   <module>rp      s0   
  ;  			8	$Q  Q r   