
    `i	G                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
  S SKJr  \ R                  " \5      r " S S\5      rg)    N)deque)
ModelProtoTensorProtonumpy_helper)BertOnnxModelTFc                   j   ^  \ rS rSrU 4S jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rSrU =r$ )BertOnnxModelKeras   c                 &   > [         TU ]  XU5        g N)super__init__)selfmodel	num_headshidden_size	__class__s       p/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxruntime_tools/transformers/BertOnnxModelKeras.py__initBertOnnxModelKeras.__init   s    ;7    c                     U R                  U/ SQ/ SQ5      nUb  U$ U R                  U/ SQ/ SQ5      nUb  U$ U R                  U/ SQ/ SQ5      nU$ )N)MulSubReshapeCast)   Nr   r   )r   r   r   Slice	Unsqueeze)r   r   r   r   r   )r   r   r   r   r   )r   Nr   r   r   )match_parent_path)r   add_or_sub_before_softmax
mask_nodess      r   match_mask_path"BertOnnxModelKeras.match_mask_path   sl    ++,EGh,;=
!++,EGs,;=
!++,EGw,>@
r   c                 6   / nXU4 H  nUR                   S   nXX   n	X:X  a  M  U	R                  S:X  a3  U	R                   S   UR                  S   :X  a  UR                  U	5        M`  [        R                  SU SUR                  S    35        S/ 4s  $    SU4$ )Nr   r   zCheck attention input failed:z, FT)inputop_typeoutputappendloggerdebug)
r   matmul_qmatmul_kmatmul_vparentoutput_name_to_nodereshape_nodesx
root_input	root_nodes
             r   check_attention_input(BertOnnxModelKeras.check_attention_input$   s    h/AJ+7I"  I-)//!2DVWHX2X$$Y/LL8Bv}}UVGWFXYZ"9 0 ]""r   c                 z	   U R                  5       nU R                  5       n/ nSnU R                  S5      nU GHF  nU R                  US5      nUb  UR                  S;  a  UR                  S:X  a[  U R                  US5      nUb  UR                  S;  a5  [
        R                  SR                  Ub  UR                  OS 5      5        M  O6[
        R                  SR                  Ub  UR                  OS 5      5        M   U R                  U/ SQ/ SQ5      nUc  [
        R                  S	5        M  Uu  ppp[
        R                  S
5        U R                  U/ SQ/ SQ5      nUc  [
        R                  S5        GMH  Uu  nnnnnU R                  U/ SQ/ SQ5      nUb*  Uu  nnnU R                  U/ SQ/ SQ5      nUb	  Uu  nnnnnnOuU R                  U/ SQ/ SQ5      nUc2  U R                  U/ SQ/ SQ5      nUc  [
        R                  S5        GM  Uu  nn n!nU R                  U/ SQ/ SQ5      nUb  Uu  nnnnnUc  [
        R                  S5        GM$  U R                  U/ SQ/ SQ5      n"U"c  [
        R                  S5        GMV  U"u  n#n$n%n&n'U R                  US   5      n(U(c  [
        R                  S5        GM  U R                  U(S   S5      (       d  [
        R                  S5        GM  U R                  WU'UUU5      u  n)n*U)(       GaR  U R                  R                  U(S   R                  S   5      n+[
        R                  S5        U R                  R!                  U+UU'UWU%UUR"                  S   UR"                  S   5	      n,U,c  GMb  U R%                  U,5        US-  nUR'                  XU/5        UR'                  U5        UR'                  U5        UR'                  U"5        UR'                  U5        UR'                  U(5        UR'                  U*5        UR)                  U
5        U R+                  XR"                  S   UR"                  S   5        GM1  [
        R                  S5        GMI     U R-                  U5        U R/                  5         [
        R1                  SU 35        g )Nr   SkipLayerNormalization)r8   EmbedLayerNormalizationAddr   z!First input for skiplayernorm: {})r:   r   MatMulr   	Transposer;   )Nr   r   r   r   r   zFailed to match qkv nodeszMatched qkv nodes)r<   r   r:   r   r;   )r   r   r   r   r   zFailed to match v path)Softmaxr   r;   r   r   r   )r   r<   r   r:   r   r;   )r   Nr   r   r   r   )r=   r:   r   r;   )r   r   r   N)r=   r:   Divr;   zFailed to match qk path)r   r   r   r   r   zFailed to match q pathzFailed to match k pathzFailed to match mask pathz;Sub node expected to have an input with constant value 1.0.zCreate an Attention node.zRoot node not matched.zFused Attention count:)input_name_to_nodesr0   get_nodes_by_op_type
get_parentr'   r*   r+   formatr    r#   has_constant_inputr5   attention_maskprocess_maskr&   attention_fusioncreate_attention_noder(   add_nodeextendr)   replace_node_inputremove_nodesupdate_graphinfo)-r   rA   r0   nodes_to_removeattention_countskip_layer_norm_nodesnormalize_noder/   	qkv_nodesaddextra_reshape_0matmulreshape_qkvtranspose_qkv
matmul_qkvv_nodestranspose_v	reshape_vadd_vextra_reshape_1r.   qk_nodes
softmax_qksub_qk	matmul_qkq_nodesmul_qtranspose_q	reshape_qadd_qextra_reshape_2r,   add_qkmul_qkk_nodestranspose_k	reshape_kadd_kextra_reshape_3r-   r"   is_same_rootr1   
mask_indexattention_nodes-                                                r   fuse_attention!BertOnnxModelKeras.fuse_attention4   s   "668"668 $ 9 9:R S3N__^Q7F~7l!l>>U*!__^Q?F~?t)t?FFY_Ykv~~quvx  *u
 LL;BBU[Ug6>>mqrt ..~/m/DFI  89U^RS6LL,-,,Z9m-<>G56IPF[)UOX--j:VXabH#2:/VY00<w1FH&X_UUKE?H  11*>acrs##55jBegvwH'%>? :B7VVY00<p1@B&QXN[)UOX56,,Y8l-<>G56IPF[)UOX--hqk:J!89**:a=!<<Z[*.*D*DXxYaciEX+Z'L-!00==jn>R>RST>UV
89!%!6!6!L!LZYackmuMRTY[`bhbobopqbrMXM_M_`aMb"d ")n-1$&&J'OP&&x0&&w/&&w/&&w/&&z2&&}5&&7''-C-CA-FVWHXY56I 4J 	/*,_,=>?r   c                 d    U R                  5         U R                  5         U R                  5         g r   )process_embedding	fuse_maskskip_reshape)r   s    r   
preprocessBertOnnxModelKeras.preprocess   s$     r   c                 V   U R                  5       nU R                  5       n/ nSnSnU R                  S5      nU HM  nU R                  US5      nUc  M  UR                  S:X  d  M,  UR
                  S   UR
                  S'   US-  nMO     US:  a  [        R                  SU 35        g g )Nr   r   r   zSkip consequent Reshape count: )rA   r0   rB   rC   r'   r&   r*   rO   )	r   rA   r0   rP   rQ   countr1   reshape_noder/   s	            r   ry   BertOnnxModelKeras.skip_reshape   s    "668"66811)<)L__\15F!fnn	&A(.Q""1%
	 * 19KK9%AB r   c                 	   UR                   S:X  d   e[        R                  SUR                  S    S35        U R	                  U/ SQ/ SQU5      nUc  [        R                  S5        gUu  pEnU R                  UR                  S   5      nUc  [        R                  S	5        g[        R                  " U5      n[        UR                  5      S
:X  aF  [        R                  SR                  UR                  UR                  5      5        UR                  n	O:[        R                  SR                  UR                  UR                  5      5        gU R                  UR                  S   5      n
U
Gb  [        R                  " U
5      n[        UR                  5      S:X  a  UR                  S   S:X  a  [        R                  " UR                  UR                  S   UR                  S
   45      S5      nU R!                  U5        [        R                  SR                  U
R                  UR                  SS  5      5        SnGOU[        R                  SR                  U
R                  UR                  5      5        gU R	                  USS/SS/U5      nUc  [        R                  S5        gUu  pU R                  UR                  S   5      n
U
c  [        R                  S5        g[        R                  " U
5      n[        UR                  5      S
:X  aF  [        R                  SR                  U
R                  UR                  5      5        U
R                  nO:[        R                  SR                  U
R                  UR                  5      5        gU R#                  USU5      nUb  UR                   S:w  a  [        R                  S5        gU R                  UR                  S   5      nUc  [        R                  S5        g[        R                  " U5      n[        UR                  5      S
:X  aF  [        R                  SR                  UR                  UR                  5      5        UR                  nO:[        R                  SR                  UR                  UR                  5      5        g[        R                  S5        U R%                  XUU5        g)NLayerNormalizationz-start fusing embedding from node with output=r   z...)r:   r:   Gatherr>   zfailed to match word_embed_pathFzfailed to get word initializer   z'Found word embedding. name:{}, shape:{}z0Failed to find word embedding. name:{}, shape:{}r      position_embeddingz+Found position embedding. name:{}, shape:{}z4Failed to find position embedding. name:{}, shape:{}r   r   zfailed to match pos_embed_pathzfailed to get pos initializerzfailed to get gatherz!failed to get segment initializerz*Found segment embedding. name:{}, shape:{}z3Failed to find segment embedding. name:{}, shape:{}zCreate Embedding nodeT)r'   r*   r+   r(   r    get_initializerr&   r   to_arraylenshaperO   rD   name
from_arrayreshapeadd_initializerrC   create_embedding_subgraph)r   noder0   word_embed_path	skip_noderJ   gather_nodeword_initializertempword_embeddingpos_initializertensorr   pos_embed_path
pos_gather	pos_slicegathersegment_initializersegment_embeddings                      r   fuse_embedding!BertOnnxModelKeras.fuse_embedding   s	   ||3333DT[[QR^DTTWXY007OQZ\op"LL:;+:(	[//0A0A!0DE#LL9:$$%56tzz?aKKAHHIYI^I^`d`j`jkl-22NKKJQQRbRgRgimisistu..x~~a/@A&((9D4::!#

1(:%00tzz!}djjYZm>\1]_st$$V,IPPQ`QeQegkgqgqrsrtguvw%9"RYY#(($**6 7!33Hx>QTUWXSY[noN%=>$2!J"22:3C3CA3FGO&<=((9D4::!#ELL_MaMacgcmcmno%4%9%9"RYY#(($**6 7A/BC>V^^x7LL/0"226<<?C&LL<=$$%89tzz?aKKDKKL_LdLdfjfpfpqr 3 8 8KKMTT#(($**6 7+,&&t=NPbcr   c                     [         R                  S5        U R                  5       nU R                  5        H-  nUR                  S:X  d  M  U R                  X!5      (       a    g  g   g)z=
Automatically detect word, segment and position embeddings.
z#start processing embedding layer...r   N)r*   rO   r0   nodesr'   r   )r   r0   r   s      r   rw   $BertOnnxModelKeras.process_embedding  sT     	9:"668JJLD||33&&tAA	 !r   c           	      F   / nU R                  5        GH  nUR                  S:X  d  M  U R                  US5      (       d  M/  U R                  U/ SQ/ SQ5      nUc  MK  Uu  pEpgU R                  R                  5       nUR                  S   U:w  a*  [        SR                  UR                  S   U5      5        M  [        R                  R                  SU/S/S	S
/S9n	[        R                  R                  SS/S/SS/S9n
[        R                  R                  SS/S/S9nUR                  R                  [        R                  R                  SS
5      /5        U R                  XDR                  S
   S5        UR                  XgU/5        U R!                  U	5        U R!                  U
5        U R!                  U5        GM     U R#                  U5        [%        U5      S:  a  U R'                  5         [(        R+                  [%        U5      S:  a  S5        g S5        g )Nr   i)r   r   r   r   )r   r   r   r   r   z!Cast input {} is not mask input{}r   mask_fuse_unsqueeze1_outputMask_UnSqueeze_1r   )inputsoutputsr   axesmask_fuse_unsqueeze2_outputMask_UnSqueeze_2r   r   mask_fuse_cast_output)r   r   toz
Fused maskzFailed to fuse mask)r   r'   rE   r    rF   get_first_maskr&   printrD   onnxhelper	make_node	attributerK   make_attributerL   rJ   rM   r   prune_graphr*   rO   )r   rP   r   	mask_pathsub_node	cast_node
slice_nodeunsqueeze_nodemask_input_nameunsqueeze_added_1unsqueeze_added_2cast_node_2s               r   rx   BertOnnxModelKeras.fuse_mask  s   JJLD||u$)@)@v)N)N 2249^`lm	$BK?Z"&"5"5"D"D"F!''*o==DD^EYEYZ[E\^mno$(KK$9$9+BQARC`Ba?Q@As	 %: %D! %)KK$9$9+B_A`C`Ba?Q@As	 %: %D! #kk33F<Y;Z=T<U 4 W %%,,dkk.H.Hq.Q-RS''..2CE\]&&
I'NO/0/0k*E !H 	/* !#C$81$<LXBWXr   c           
         U R                  S5      nSnU H  nU R                  U/ SQ/ SQ5      nUc  M  Uu	  pVpxppnUR                  S   U
R                  S'   U R	                  U5        U	R                  S   UR                  S'   U R	                  U5        UR                  S   UR                  S'   U R	                  U5        US-  nM     U$ )Nr8   r   )	r:   r   r;   r   Gelur:   r   r;   r8   )	r   r   r   r   r   r   r   r   r   r   rB   r    r(   r&   remove_node)r   skiplayernorm_nodesreshape_removedskiplayernorm_nodepathadd_1	reshape_1matmul_1	reshape_2geluadd_2	reshape_3matmul_2skiplayernorms                 r   remove_extra_reshape'BertOnnxModelKeras.remove_extra_reshapeE  s    "778PQ"5))"u+-D |eibEh4	Ub%__Q/EKKNY' $AHNN1Y'%__Q/EKKNY'q O #6" r   c                    U R                  S5      nSnU H  nU R                  U/ SQ/ SQ5      nUc  M  Uu
  pVpxpppUR                  S   UR                  S'   U R	                  U5        UR                  S   U
R                  S'   U R	                  U5        U	R                  S   UR                  S'   U R	                  U5        UR                  S   UR                  S'   U R	                  U5        US-  nM     U$ )Nr8   r   )
r:   r   r;   r   r   r:   r   r;   r   r8   )
Nr   r   r   r   r   r   r   r   r      r   )r   r   r   r   r   r   r   r   r   r   r   r   r   	reshape_4r   s                  r   remove_extra_reshape_2)BertOnnxModelKeras.remove_extra_reshape_2[  s    "778PQ"5))" A13D |ptmEh4	U^ - 4 4Q 7HNN1Y'%__Q/EKKNY' $AHNN1Y'%__Q/EKKNY'q O- #60 r   c                     U R                  5       U R                  5       -   n[        R                  SU S35        U R	                  5         g )NzRemove z Reshape nodes.)r   r   r*   rO   r   )r   r   s     r   postprocessBertOnnxModelKeras.postprocessx  s@    3358S8S8UUgo.o>?r    )__name__
__module____qualname____firstlineno___BertOnnxModelKeras__initr#   r5   rt   rz   ry   r   rw   rx   r   r   r   __static_attributes____classcell__)r   s   @r   r	   r	      sL    8# o@b
C$M^
,Y\,: r   r	   )loggingr   sysargparsenumpynpcollectionsr   r   r   r   r   	getLoggerr   r*   r	   r   r   r   <module>r      s@      
    6 6 +			8	$k kr   