+
    9i5                     l    ^ RI Ht ^ RIHtHt ^ RIHt ^ RIHt ^ RI	H
t
 ]! ]4      t ! R R]4      tR# )	    )	getLogger)AttentionMaskFusionAttention)AttentionMaskFormat)	NodeProto)	OnnxModelc                   Z   a a ] tR t^t oRtV3R lV 3R lltV3R lR ltR tRtVt	V ;t
# )FusionAttentionClipz:
Fuse Attention subgraph of Clip into one Attention node.
c                ,   < V ^8  d   QhRS[ RS[RS[/# )   modelhidden_size	num_heads)r   int)format__classdict__s   "l/var/www/html/photoedit/myenv/lib/python3.14/site-packages/onnxruntime/transformers/fusion_attention_clip.py__annotate__ FusionAttentionClip.__annotate__   s)     
 

 
 	
    c           	     t   < \        V4      p\        P                  Vn        \        SV `  VVVVR R.R7       R# )FSkipLayerNormalization)use_multi_head_attentionsearch_op_typesN)r   r   NoMaskmask_formatsuper__init__)selfr   r   r   attention_mask	__class__s   &&&& r   r   FusionAttentionClip.__init__   sD     'u-%8%?%?"%*56 	 	
r   c                <   < V ^8  d   QhRS[ RS[S[S[3,          /# )r   	reshape_qreturn)r   tupler   )r   r   s   "r   r   r   &   s$     ,& ,&y ,&U3PS8_ ,&r   c                Z   V P                   P                  VR^4      pVe   \        VP                  4      ^8w  d   V P                  V P
                  3# V P                   P                  VP                  ^,          4      pVf   V P                  V P
                  3# \        V4      ^8w  g   V^ ,          ^ 8:  d   V P                  V P
                  3# V^ ,          pV P                   P                  VP                  ^,          4      pVf   V P                  V P
                  3# \        V4      ^8w  g   V^ ,          ^ 8:  d   V P                  V P
                  3# V^ ,          pWF,          pV P                  ^ 8  dP   W@P                  8w  d@   V P                  '       d.   \        P                  RV P                   RV R24       RV n        V P
                  ^ 8  dP   WpP
                  8w  d@   V P                  '       d.   \        P                  RV P
                   RV R24       RV n
        WG3# )zDetect num_heads and hidden_size for ONNX model from MiDaS
Args:
    reshape_q (NodeProto): reshape node for q
Returns:
    Tuple[int, int]: num_heads and hidden_size
Concatz--num_heads is z. Detected value is z. Using detected value.Fz--hidden_size is )r   match_parentleninputr   r   get_constant_valuenum_heads_warningloggerwarninghidden_size_warning)r   r$   concatnum_head_valuer   head_size_value	head_sizer   s   &&      r   get_num_heads_and_hidden_size1FusionAttentionClip.get_num_heads_and_hidden_size&   s    ((Ha@>S.!3>>4#3#333 66v||AG!>>4#3#333~!#~a'8A'=>>4#3#333"1%	**77QH">>4#3#3331$(:a(?>>4#3#333#A&	+>>A)~~"=%%%0@@TU^T__vwx).&aK3C3C$C''''(8(8'99Mk]Zqr ,1(%%r   c                   R pR pR F*  pV P                   P                  VRV4      pVf   K&  TpTpK,  	  R pVe   VP                  ^ ,          pMR F  pR pV P                   P                  VRV4      p	V P                   P                  VRV4      p
V	e   T	pMV
e   T
pVf   KR  V P                   P                  VRVR4      pVf   Kv  VP                  ^ ,          pTp M	  Vf   R # V P                   P	                  V. RO^V,
          R R ^ ^ ^ .4      pVf=   V P                   P	                  V. RO. RO4      pVf   \
        P                  R4       R # V^,          V^,          VR,          rpV P                   P	                  V. RO. RO4      pVf=   V P                   P	                  V. RO. RO4      pVf   \
        P                  R4       R # VR,          VR,          ppR pR p. pV P                   P	                  V. R O. R!OVR	7      pVf   V P                   P	                  VRR.^ ^ .4      pVf   V P                   P	                  V. R"O. R#O4      pVe   V^,          pMV P                   P	                  V. R$O. R%O4      pVfl   V P                   P	                  V. R&O. R'O4      pVe   V^,          pMhV P                   P	                  V. R(O. R)O4      pVf   \
        P                  R
4       R # M+\        V4      ^8X  g   Q h^V^ ,          ,
          pV^,          pVR,          pV P                   P	                  V. R*O. R+O4      pVfG   V P                   P	                  V. RO. R,O4      pVf   \
        P                  R4       R # V^,          pM	V^,          pVR,          VR,          ppV P                   P	                  V. R-O. R.O4      pVf=   V P                   P	                  V. RO. RO4      pVf   \
        P                  R4       R # VR,          VR,          ppVP                  ^ ,          V8w  g1   VP                  ^ ,          V8w  g   VP                  ^ ,          V8w  d   \
        P                  R4       R # V P                  V4      w  pp V^ 8:  g   V ^ 8:  d   \
        P                  R4       R # Tp!Rp"R p#R p$Ve   VP                  ^,          R8X  d   VP                  ^,          p"MV P                   P	                  V. R/O. R0O4      p%V%e   VP                  ^,          p"MhV P                   P	                  V. R1OV^ ^ ^ ^ ^ .4      p#V P                   P	                  V. R2OV^ ^ ^ ^ .4      p$V#f   V$f   \
        P                  R4       R # T P                  R TTTTTTTT TV!P                  ^ ,          T"R V#R J;'       g    V$R JR7      p&V&f   \
        P                  R4       R # V P                  P                  V&4       V P                  V P                  V&P                  &   V P                   P#                  V!V.4       RV n        R # )3Nr   AddLayerNormalizationFMatMulz(fuse_attention: failed to match qkv pathz&fuse_attention: failed to match v pathSoftmax)return_indicez'fuse_attention: failed to match qk pathz&fuse_attention: failed to match q pathz&fuse_attention: failed to match k pathz>fuse_attention: expect to have same input to q, k and v matmulz9fuse_attention: failed to detect num_heads or hidden_size r    z4fuse_attention: failed to match causal mask subgraph)
mask_indexq_matmulk_matmulv_matmulq_addk_addv_addr   r   first_inputoutput
add_qk_strscalecausalz+fuse_attention: failed to create fused nodeT)   r   )r   rJ   )r8   r:   Reshape	TransposerK   r:   )r8   r:   rK   rL   r:   )rJ   Nr   r   r   )rK   rL   rK   r8   r:   )rJ   r   r   r   N)rL   rK   r8   r:   )rJ   r   r   N)r;   rK   r8   rK   r:   )r   r   r   Nr   )r;   r8   Mulr:   )r   r   r   r   )r;   rO   r:   )r   r   r   )CastrP   r;   r8   rO   r:   )r   r   r   r   r   r   )rP   rP   r;   rO   r:   )r   r   r   r   r   )rK   rL   rK   rO   r8   r:   )r   r   r   r   NN)r   r   r   N)rL   rK   rL   rK   r8   r:   )rJ   r   r   r   r   N)	WhereSubrP   Expand	UnsqueezerT   rK   rK   rP   )	rJ   r   rJ   r   r   r   r   r   r   )r(   rS   rT   rT   rQ   Less)rS   rT   rT   rQ   rU   )r   r)   rF   find_first_child_by_typematch_parent_pathr.   debugr*   r+   r5   create_attention_nodenodes_to_addappendthis_graph_namenode_name_to_graph_namenamenodes_to_removeextendprune_graph)'r   normalize_nodeinput_name_to_nodesoutput_name_to_nodeskip_input_indexnode_before_layer_normiparent
root_inputnode_before_layer_norm_1node_before_layer_norm_2child	qkv_nodesreshape_qkvtranspose_qkv
matmul_qkvv_nodesadd_vmatmul_vcausal_mask_input_indexadd_maskadd_mask_indicesqk_nodes	matmul_qkq_nodesr$   add_qmatmul_qk_nodesadd_kmatmul_kr   r   attention_last_nodeadd_qkcausal_mask_nodes_1causal_mask_nodes_2add_qk_nodesnew_nodes'   &&&&                                   r   fuseFusionAttentionClip.fuseT   ss   !%AZZ,,^=UWXYF!#$ )/&	  
!-/66q9J )-&+/::+B+B>SXZ[+\(+/::+B+B>Sgij+k(+7 .F*-9 .F*)1

;;*('	 ="\\!_
#$ Q T  'JJ00J!!4q!Q7
	
 

44C"I
  GHaLaLbM %/ **..@

 ?jj22EG EF!"+wr{x"&:://>*	 0 
 zz33H%AH
 ::77
Dgiuv''{H  $zz;;JHdfopH'#'::#?#?&O.$
 $/'/{H (,zz'C'C * L /(H
  (/ &-V W &'(A---&'*:1*=&=#{HRL	**..G$

 ?jj22DoG EF
I
I!"+wr{x**..M!

 ?jj22DoG EF!"+wr{x>>!
*hnnQ.?:.MQYQ_Q_`aQbfpQpLLYZ!%!C!CI!N	;>[A-LLTU)""~~a $44!*  $zz;;
 0   +%^^A.F +/***F*F W0!Q1a@+' +/***F*F M0!Q1=+' +27J7R%[\--#"&--a0't3YY9LTX9X . 
  LLFG  *6:6J6J$$X]]3##%8-$HI  r   )r0   r-   ra   )__name__
__module____qualname____firstlineno____doc__r   r5   r   __static_attributes____classdictcell____classcell__)r!   r   s   @@r   r
   r
      s+     
 
$,& ,&\@  @ r   r
   N)loggingr   fusion_attentionr   r   fusion_optionsr   onnxr   
onnx_modelr   r   r.   r
    r   r   <module>r      s.   
  ; .   	8	E / E r   