+
    9iU                         ^ RI Ht ^ RIt^ RIHt ^ RIHt ^ RIH	t	 ^ RI
Ht ]! ]4      t ! R R]4      t ! R	 R
]4      tR# )    )	getLoggerN)Fusion)FusionUtils)helper)	OnnxModelc                   Z   a a ] tR t^t oRtV3R lV 3R lltR tR tR tR t	Rt
VtV ;t# )	FusionGptAttentionPastBasez3Base class for GPT Attention Fusion with past statec                &   < V ^8  d   QhRS[ RS[/#    model	num_headsr   int)format__classdict__s   "k/var/www/html/photoedit/myenv/lib/python3.14/site-packages/onnxruntime/transformers/fusion_gpt_attention.py__annotate__'FusionGptAttentionPastBase.__annotate__   s     & &i &C &    c                x   < \         SV `  VR RR.R4       W n        \        V4      V n        / V n        RV n        R# )	AttentionLayerNormalizationSkipLayerNormalizationz	with pastN)super__init__r   r   utilscasted_attention_maskmask_filter_valueselfr   r   	__class__s   &&&r   r   #FusionGptAttentionPastBase.__init__   s?    .BD\-]_jk" '
%'"!%r   c                   V P                   P                  V^ V4      pVe   VP                  R8w  d   \        P	                  R4       R# V P                   P                  V^4      ^8w  d   \        P	                  R4       R# VP                  ^ ,          pV P                   P                  V^ V4      pV'       d   VP                  R8X  d   TpMEV P                   P                  VRR.^ ^ .4      pVf   \        P	                  R4       R# VR	,          pV P                   P                  V^ 4      ^8w  d   \        P	                  R4       R# VP                  ^ ,          p	WY8w  d   \        P	                  R4       R# V# )
r   NGatherz,match_past_pattern_1: expect Gather for pastz9match_past_pattern_1: expect indices=1 for Gather of past	Transposez7match_past_pattern_1: failed match Transpose and Gatherz;match_past_pattern_1: expect indices=0 for Gather k of pastz,match_past_pattern_1: expect past to be same)r   
get_parentop_typeloggerdebugfind_constant_inputinputmatch_parent_path)
r!   concat_kconcat_voutput_name_to_nodegatherpastparentgather_past_kpast_k_nodespast_ks
   &&&&      r   match_past_pattern_1/FusionGptAttentionPastBase.match_past_pattern_1   s7   & &&x4GH>V^^x7LLGH::))&!49LLTU||A&&x4GHfnn0"M::77;PXBY\]_`[abL#VW(,M::))-;q@LLVW$$Q'>LLGHr   c                   V P                   P                  V^ V4      pVe   VP                  R8w  d   \        P	                  R4       R# V P                   P                  V^ V4      pVe   VP                  R8w  d   \        P	                  R4       R# V P                   P                  4       pV^8  do   \        P                  ! VR^ .4      '       g   \        P	                  R4       R# \        P                  ! VR^^.4      '       g   \        P	                  R	4       R# MwV P                  P                  V^^ .4      '       g   \        P	                  R4       R# V P                  P                  V^^^.4      '       g   \        P	                  R	4       R# \        P                  ! VR
^ ^ R7      '       g   \        P	                  R4       R# VP                  ^ ,          pV P                   P                  VRR.^ ^ .4      pVf   \        P	                  R4       R# VR,          P                  ^ ,          p	Wy8w  d   \        P                  R4       R# V# )r   NSqueezez:match_past_pattern_2: expect Squeeze as parent of concat_vSplitz0match_past_pattern_2: expect Split for past pathaxesz:match_past_pattern_2: axes != [0] for Squeeze in past pathsplitz<match_past_pattern_2: split != [1, 1] for Split in past pathaxis)default_valuezKmatch_past_pattern_2: attribute axis of Split are not expected in past pathz7match_past_pattern_2: failed to match past_k_nodes pathz,match_past_pattern_2: expect past to be samer'   )r   r(   r)   r*   r+   get_opset_versionr   check_node_attributer   check_node_input_valuer-   r.   info)
r!   r/   r0   r1   squeezer>   opset_versionr3   r6   r7   s
   &&&&      r   match_past_pattern_2/FusionGptAttentionPastBase.match_past_pattern_2K   s   , **''!5HI?goo:LLUV

%%gq2EF=EMMW4LLKL

446233GVaSIIYZ33E7QFKK[\ L ::44Wa!EEYZ::44UA1vFF[\//vqPQRRLLfg{{1~zz33Hy'>RUVXYTZ[LLRSb!''*>KKFGr   c                $   V P                   P                  VR VRR7      pV'       g   \        P                  R4       R# V P                   P                  VRVRR7      pV'       g   \        P                  R4       R# VP                  ^ ,          pV# )	UnsqueezeF)	recursivezexpect unsqueeze for presentNConcatzexpect concat for present)r   find_first_child_by_typer*   rD   output)r!   r0   input_name_to_nodesunsqueeze_present_vconcat_presentpresents   &&&   r   match_present(FusionGptAttentionPastBase.match_present   s    "jjAAk#6% B 
 #KK67<<+>% = 
 KK34 ''*r   c                B   WP                   9   d   V P                   V,          pV# V P                  P                  V4      '       d.   V P                  P	                  V4      w  r2W P                   V&   V# V P                  P                  V4      w  r$W P                   V&   V# N)r   r   find_graph_inputr   cast_graph_input_to_int32cast_input_to_int32)r!   
input_nameattention_mask_input_namecasted	cast_nodes   &&   r   cast_attention_mask.FusionGptAttentionPastBase.cast_attention_mask   s    333(,(B(B:(N% )( ZZ((4404

0T0TU_0`-F5N&&z2 )( 48::3Q3QR\3]0%5N&&z2((r   )r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r8   rG   rS   r^   __static_attributes____classdictcell____classcell__r"   r   s   @@r   r	   r	      s.     =& &/bAF"	) 	)r   r	   c                   N   a a ] tR t^t oRtV3R lV 3R lltR tR tRtVt	V ;t
# )FusionGptAttentionzH
Fuse GPT-2 Attention with past state subgraph into one Attention node.
c                &   < V ^8  d   QhRS[ RS[/# r   r   )r   r   s   "r   r   FusionGptAttention.__annotate__   s     + +i +C +r   c                &   < \         SV `  W4       R # rV   )r   r   r    s   &&&r   r   FusionGptAttention.__init__   s    *r   c
                   V P                   P                  R 4      p
\        P                  ! RWaW(V.V
R,           V.V
R7      pRVn        VP
                  P                  \        P                  ! RV P                  4      \        P                  ! RV	'       d   ^M^ 4      .4       V P                  eE   VP
                  P                  \        P                  ! R\        V P                  4      4      .4       \        P                  ! R	V
R,           VP                  ^,          .V
R
,           .V
R,           R7      p\        P                  ! RV
R
,           VP                  ^,          .V.V
R,           R7      pV P                  P                  WV.4       V P                  V P                  VP                  &   V P                  V P                  VP                  &   V P                  V P                  VP                  &   R# )GptAttentionr   _output)inputsoutputsnamezcom.microsoftr   unidirectionalNr   MatMul_matmul_output_matmulAdd_add)r   create_node_namer   	make_nodedomain	attributeextendmake_attributer   r   floatr-   nodes_to_addthis_graph_namenode_name_to_graph_namert   )r!   	fc_weightfc_biasgemm_qkvr3   rR   r-   rN   maskis_unidirectionalattention_node_nameattention_nodematmul_nodeadd_nodes   &&&&&&&&&&    r   create_attention_node(FusionGptAttention.create_attention_node   s    #jj99.I))gT:(94g>$	
 !0  ''%%k4>>B%%&6=NTUV	
 !!-$$++V-B-BCVX]^b^t^tXu-v,wx&&')3X^^A5FG(+;;<$y0	
 ##'*::HNN1<MNH$v-	
 	  .x!HI<@<P<P$$^%8%899=9M9M$$[%5%566:6J6J$$X]]3r   c                4   R pR p. pVP                   R8H  pR pV'       g&   V P                  P                  V. RO. ROVVR7      pM$V P                  P                  V. RO. R OVVR7      pVf   R # R p	V'       g-   Vw  p
ppppppV
P                  ^V^ ,          ,
          ,          p	M	Vw  ppppppV P                  P                  V. R!O. R"O4      pVf   \        P                  R4       R # Vw  ppppV P                  P                  V. R#O. R$OV4      pVf#   V P                  P                  V. R%O. R$OV4      pVf   V P                  P                  V. R&O. R'OV4      pVf#   V P                  P                  V. R(O. R'OV4      pVf   \        P                  R4       R # V^,          P                  ^,          pV P                  P                  V^ ,          4      w  ppV^ ,          P                  V,          pM4V^,          P                  ^,          pV^,          P                  ^,          pVR),          pV	e)   V	VP                  9  d   \        P                  R4       R # RpR pR pR pV P                  P                  V. R*O. R+O4      p V e   V w  p!p"p#p$p%V P                  P                  V". R,O. R-O4      p&V&f   \        P                  R
4       R # V&R),          p'V&^,          pV$V'8w  d   \        P                  R4       R # \        V&4      ^8  dM   V&^ ,          P                   R8X  d5   V P                  P                  V&^ ,          4      w  pp(V(R.8w  d	   V() V n        EMV P                  P                  V. R/O. R0O3. R1O. R2O3.V4      w  pp pV f   \        P                  R4       R # V R3,          p)V R4,          p$V R),          p%V^8X  d   V ^,          p*V P                  P                  V*. R5O. R6O3. R7O. R8O3. R9O. R:O3.V4      w  pppVf   \        P                  R4       R # \        V4      ^8  dL   V^ ,          P                   R8X  d4   V P                  P                  V^ ,          4      w  pp(V(R.8w  d   V(V n        V P                  P                  V). R;O. R<O3. R=O. R>O3.V4      w  pp&pV&f   \        P                  R4       R # T&V^ 8X  d   ^M^,          pV P                  P                  V&R),          ^ V4      p+V+P                   R	8X  d"   T+p'V$V'8w  d   \        P                  R4       R # M)V+P                   R8X  d   T+pM\        P                  R4       V P                  P                  VP                  ^ ,          4      p,\        V,\        P                  4      '       d\   \        V,P                  4      ^8X  dB   V,P                  R,          R?8X  d*   V,P                  ^,          V,P                  ^,          8X  g   \        P                  R4       R # \        P                   ! V,\        P"                  ! V,4      4      '       d   RpM\\        P                   ! V,\        P$                  ! \        P"                  ! V,4      4      4      '       g   \        P                  R4       R # V P                  P                  V%. R@O. RAO4      p-V-f   \        P                  R4       R # V-w  p.p/p0VV08w  d   \        P                  R4       R # V P                  P                  V%. R!O. R"O4      p1V1fF   V P                  P                  V%. RBO. RCO4      p1V1f   \        P                  R4       R # V1w  pp2p3p4p5MV1w  p2p3p4p5VV58w  d   \        P                  R4       R # V'       d   V2V8w  d   \        P                  R4       R # Rp6Ve,   VR),          P                  ^ ,          p7V P'                  V74      p6V P)                  V2VV4      ;'       g    V P+                  V2VV4      pVf   \        P-                  R4       R # V P                  P/                  V4      '       g   \        P                  R4       V P1                  VV4      pVf   \        P-                  R4       R # V P                  P3                  V4      '       g   \        P-                  R4       R # V P5                  VVVVVVP6                  ^ ,          VP6                  ^ ,          V6V4	       RV n        R # )DNr   )r1   return_indicerL   z&fuse_attention: failed to match v pathz'fuse_attention: failed to match fc pathzCUpstream Add and (Skip)LayerNormalization shall have one same inputTMulDivz8fuse_attention: failed to match unidirectional mask pathz-fuse_attention: skip since div_qk != div_maskz(fuse_attention: failed to match qk nodesz9fuse_attention: failed to match input attention mask pathz)fuse_attention: failed to match mask path:Nr   Nz4fuse_attention: skip since mask shape is not 1x1xWxWFzDfuse_attention: skip since mask is neither lower triangular nor onesz&fuse_attention: failed to match q pathz.fuse_attention: skip since split_fc != split_qz&fuse_attention: failed to match k pathz.fuse_attention: skip since split_fc != split_kz8fuse_attention: skip since concat_k != concat_k_to_match z)fuse_attention: failed to match past pathzpast is not graph input.z,fuse_attention: failed to match present pathz!expect present to be graph output)ry   ReshapeGemmr   r   r&   rv   )r   Nr   r   r   r   r   )r   r   r   r   r&   rv   )Nr   r   r   r   r   )rL   r&   r   r<   )   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )ry   rv   r   )r   Nr   )ry   rv   r   r'   )SoftmaxSubr   r   rv   )r   r   r   r   r   )
r   r   Slicer   rJ   r   r;   r   Shaper   )
r   r   r   r   r   r   r   r   r   r   i)r   Wherer   rv   )r   r   r   r   )r   ry   r   r   rv   )r   r   Nr   r   )r   r   CastrJ   rJ   r   )Nr   r   r   r   r   )r   r   rJ   rJ   r   )Nr   r   r   r   )r   r   rJ   rJ   )Nr   r   r   )r   r   r   rJ   r   r;   r   r   )r   r   r   r   r   r   r   r   )r   r   rJ   r   r;   r   r   )r   r   r   r   r   r   r   )r   r   )r&   r   r<   )r   r   r   )r&   rL   r&   r   r<   )r   r   r   r   r   )r)   r   r.   r-   r*   r+   get_constant_inputlenr   match_parent_pathsr(   get_constant_value
isinstancenpndarrayshapeallclose	ones_liketrilr^   r8   rG   rD   rW   rS   find_graph_outputr   rN   prune_graph)8r!   normalize_noderO   r1   r3   rR   r   is_normalize_node_skiplayernorm	qkv_nodesanother_inputadd_qkvreshape_qkvr   	reshape_1	reshape_2transpose_qkv
matmul_qkvv_nodesr0   transpose_v	reshape_vsplit_fcfc_nodesr   i_r   layernorm_before_attentionr   
slice_maskinput_mask_nodesconcat_k_to_matchqk_nodes
softmax_qksub_qkmul_qkdiv_qk	matmul_qk
mask_nodesdiv_maskmul_valwhere_qkadd_qkdiv_or_concat	mask_dataq_nodestranspose_q	reshape_qsplit_qk_nodesr/   transpose_k	reshape_ksplit_kr[   rZ   s8   &&&&                                                    r   fuseFusionGptAttention.fuse   s   *8*@*@D\*\'	.

44W($7+ 5 I 

44P%$7+ 5 I .  $MM!mA.>*>?M  **..z;fhtu?LLAB7>4;	8 :://@	
 zz33H#	H zz337#	H ::77? '	 FG ))!,I::00!=DAqqk''*G ))!,Iqk''*G%-b\" $>X>^>^)^LL^_ 
 :://
<fhwx>F;Z55 /J  !WX!"~H#AJ!LM:"z!}'<'<'E!ZZ:::a=I
7f$.5XD* "ZZ:::LIACUV $NAx GH|Hb\F IAv!!)-)F)F X1
 P.
 E+ (!*&#Q$ $+LL!\]'(1,1A!1D1L1LPU1U!%!>!>?OPQ?R!SJAw&(18.#zz<< d0 \-
 $ Az1 !HI#aAQ7J JJ11*R.!EXYM$$-(X%LL!PQ & &&(2$1!HI JJ11*2B2B12EF	y"**--IOO$)#v-"iooa&88LLOP;;y",,y"9:: %YY0G(HIILL_`**..y:[]fg?LLAB,3)iwLLIJ**..y:egst?jj22HG
 EFAH>Hk9g:A7X{IwwLLIJ->!>LLST$&!')"-33A6J(,(@(@(L% ((8=PQ 
 
UYUnUnh 3V
 <KKCDzz**400LL34 $$X/BC?KKFGzz++G44KK;<""&--a0q!%
	
  r   )r   r   )r`   ra   rb   rc   rd   r   r   r   re   rf   rg   rh   s   @@r   rj   rj      s'     + +.K`   r   rj   )loggingr   numpyr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   r`   r*   r	   rj    r   r   <module>r      sA   
    $   	8	X) X)vw 3 w r   