
    h
                      b    S SK r S SKJrJr  S SKJr  \ R                  " \5      r " S S\5      r	g)    N)AttentionMaskFusionAttention)	OnnxModelc                   F   ^  \ rS rSrSrS\S\S\S\4U 4S jjrS r	S	r
U =r$ )
FusionConformerAttention   zE
Fuse Conformer Attention subgraph into one MultiHeadAttention node.
modelhidden_size	num_headsattention_maskc                 &   > [         TU ]  XX45        g )N)super__init__)selfr	   r
   r   r   	__class__s        m/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/fusion_conformer_attention.pyr   !FusionConformerAttention.__init__   s     	YG    c                    U R                   R                  U/ SQ/ SQ5      nUc  [        R                  S5        g US   US   US   pvnSu  pU R                   R                  U/ SQ/ S	Q5      n
U
c;  U R                   R                  U/ S
Q/ SQ5      n
U
c  [        R                  S5        g O@U
S   nU R                   R	                  USS 5      nUR
                  S   n	UR
                  S   nU
S   U
S   pSnU R                   R                  U/ SQ/ SQ5      nUcu  U R                   R                  U/ SQ/ SQ5      nUc  [        R                  S5        g US   nU R                   R                  U/ SQ/ SQ5      nUb  US   R
                  S   nUS   US   nnU R                   R                  U/ SQ/ SQ5      nUc:  U R                   R                  U/ SQ/ SQ5      nUc  [        R                  S5        g US   US   US   nnnU R                   R                  U/ SQ/ SQ5      nUb"  US   US   :w  a  [        R                  S5        g Su  nnU R                   R                  U/ SQ/ SQ5      nUc_  U R                   R                  U/ S Q/ S!Q5      nUc:  U R                   R                  U/ S
Q/ SQ5      nUc  [        R                  S"5        g O@US#   nU R                   R	                  USS 5      nUR
                  S   nUR
                  S   nUS   US   nnU R                  U5      u  n n!U S::  d  U!S::  d	  U!U -  S:w  a  [        R                  S$5        g S n"UR                  S   UR                  S   :H  =(       a*    UR                  S   UR                  S   :H  =(       a    US L n#U#(       aF  U R                  UUUUUUUU U!UR                  S   UR
                  S   UR                  S#   UUUU	S%9n"O7U R                  UUUUUUU U!UR
                  S   UUR                  S#   UUUU	S&9n"U"c  [        R                  S'5        g U R                  R                  U"5        U R                  U R                  U"R                  '   U R                  R!                  XVU/5        U R                  R!                  U5        U#(       di  US   R"                  S(:X  a  UR%                  5         US   R"                  S(:X  a  UR%                  5         U
S   R"                  S(:X  a  U
R%                  5         Uc  U R                  R!                  U5        U R                  R!                  U5        U R                  R!                  U
5        S)U l        g )*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
   r0   key_padding_maskadd_qkr2   r3   r4   r5   zAfuse_conformer_attention: MultiHeadAttention node creation failedr   T)r	   match_parent_pathloggerdebug
get_parentr0   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_qkvr3   r5   v_nodesconcat_vconcat_parentadd_vmatmul_v	attn_maskqk_nodeswhere_qk
mask_nodesr7   	matmul_qkq_nodes	reshape_qadd_qmatmul_qextra_q_nodesr2   r4   k_nodesconcat_kadd_kmatmul_kr   r
   new_nodeuse_packed_attention_ops$                                       r   fuse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   )rI   )__name__
__module____qualname____firstlineno____doc__r   intr   r   rf   __static_attributes____classcell__)r   s   @r   r   r      sD    HH H 	H
 &HC  C r   r   )
loggingfusion_attentionr   r   
onnx_modelr   	getLoggerrh   r9   r    r   r   <module>ru      s.   
  ;  			8	$Q  Q r   