
    Yj-                     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        p/var/www/FlaskApp/flask-venv/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          }|>| j                             |g dg d          }|t                              d           d S |d         |d         |d         }}}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         }t          |
          dk    r|
d         j        dk    r|
d         nd }|
d         }d}| j                             |g dg d          }d }|q| j                             |g dg d          }|G| j                             |g dg d          }|t                              d           d S |d         }n|d         }|5| j                             |g dg d          }||d         j        d         }|d         |d         }}| j                             |g dg d          }|| j                             |g dg d          }|`| j                             |g d g d!          }|>| j                             |g d g d"          }|t                              d#           d S t          d$ |D             d           }t          d% |D             d           }t          d& t          |          D             d           }|||t                              d'           d S | j                             |g d(g d)          }|=|d         j        d*v r.|d         |d         k    rt                              d+           d S |$| j                             |g d,g d-          }||}d	\  }}| j                             |g d.g d/          }|| j                             |g d0g d1          }|`| j                             |g dg d          }|>| j                             |g dg d          }|t                              d2           d S n>|d3         }| j                             |dd           }|j        d         }|j        d         }t          |          dk    r|d         j        dk    r|d         nd }|d         } | 
                    |          \  }!}"|!dk    s|"dk    s	|"|!z  dk    rt                              d4           d S |j        d3         }#| j                             |#          }$|$Ht          |$j                  d5k    r0t                              d6|#t          |$j                             d}#d }%|j        d         | j        d         k    o+| j        d         |j        d         k    o|d u o|d uo|d uo|d u}&|&r<|                     ||| |||||!|"|j        d         |j        d         |#||||	7          }%n/|                     || |||||!|"|j        d         ||#||||	8          }%|%t                              d9           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   )r   r   r   r   )r   r   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   r   r   z0fuse_conformer_attention: failed to match v pathr      r   r   )Softmaxr   r   )r   r   r   )Wherer   r    r   r   )r   r   r   r   r   )r    r   r    Divr   r   )r   r   r   r   r   r   z1fuse_conformer_attention: failed to match qk path)Equal	UnsqueezeCast)r!   r   r   r   r   )r   r   r   r   r   )Mulr   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 q pathc              3   0   K   | ]}|j         d k    |V  dS )r   Nop_type.0nodes     r   	<genexpr>z0FusionConformerAttention.fuse.<locals>.<genexpr>   s.      PP4dli6O6O$6O6O6O6OPPr   c              3   0   K   | ]}|j         d k    |V  dS )r   Nr'   r)   s     r   r,   z0FusionConformerAttention.fuse.<locals>.<genexpr>   s.      HHt$,%2G2Gd2G2G2G2GHHr   c              3   0   K   | ]}|j         d k    |V  dS )r   Nr'   r)   s     r   r,   z0FusionConformerAttention.fuse.<locals>.<genexpr>   s.      XX$t|x?W?W?W?W?W?WXXr   zGfuse_conformer_attention: failed to identify q reshape/add/matmul nodes)r   r   r   r   r   r!   )r   r   r   r   r   r   )r!   r%   z6fuse_conformer_attention: failed to match extra q path)Slicer   r/   r   Padr   r   r   )r   r   r   r   r   r   r   r   )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   zPfuse_conformer_attention: skipping attention_bias %s with dims %s (expected 4-D))
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)r3   r4   r5   r6   r7   r8   r
   r	   r:   key_padding_maskadd_qkr<   r=   r>   r?   zAfuse_conformer_attention: MultiHeadAttention node creation failedr   T)r   match_parent_pathloggerdebug
get_parentr:   lenr(   nextreversedget_num_heads_and_hidden_sizeinputget_initializerdimslistcreate_attention_nodecreate_multihead_attention_nodenodes_to_addappendthis_graph_namenode_name_to_graph_namenamenodes_to_removeextendpopprune_graph)'r   normalize_nodeinput_name_to_nodesoutput_name_to_node	qkv_nodesreshape_qkvtranspose_qkv
matmul_qkvr=   r?   v_nodesconcat_vconcat_parentadd_vmatmul_v	attn_maskqk_nodeswhere_qk
mask_nodesrA   	matmul_qkq_nodes	reshape_qadd_qmatmul_qextra_q_nodesnemotron_extra_q_nodesr<   r>   k_nodesconcat_kadd_kmatmul_kr
   r	   attention_bias	bias_initnew_nodeuse_packed_attention_ops'                                          r   fusezFusionConformerAttention.fuse   s	   J00???
 
	
 
44<<< I
  QRRR1:2	"yY[}J]"	*..???OO
 

 ?j22999 G
 *66666II 
 ?LL!STTTFqzH J11(AtDDM *I")!,F"7||q00WR[5HE5Q5QW[2;	://(((II
 

 z33>>> H
 :77III&&& 
 #LL!TUUUF#A;#A;55...		 J
 %&rN1!4	$RL(2,	*..<<<OO
 

 ?j22@@@ G
 *66=== LL 
 ?"j::!AAA$ G
 %WXXXPP7PPPRVWW	HHwHHH$OOXX(7*;*;XXXZ^__1ALLbcccF
44MMM
 

 $);~)M)MRYZ[R\`mnp`qRqRqLLQRRRF %)Z%A%A]]](((& &"
 &1 6"	*..LLL
 

 ?j22FFF G
 *66=== LL 
 ?"j::!:::!		 G
 %WXXXqzH J11(AtDDM")!,F *I"7||q00WR[5HE5Q5QW[2;!%!C!CI!N!N	;>>[A--+	2Ia1O1OLL^___F  aJ..~>>	 S%8%8A%=%=LLbY^$$  
  NN1!22 "q!X^A%66"%" T!" T!	"
 T! 	  # %	11$!!!#'$N1-")!,)##! 2  HH& ;;!!!#'")!,!*%## <  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   rx   __classcell__)r   s   @r   r   r      s         HH H 	H
 &H H H H H HN  N  N  N  N  N  N r   r   )
loggingfusion_attentionr   r   
onnx_modelr   	getLoggerry   rC   r    r   r   <module>r      s   
  ; ; ; ; ; ; ; ;            		8	$	$\  \  \  \  \  \  \  \  \  \ r   