
    hhH                     f    S SK r S SKrS SKJr  S SKJr  \ R
                  " \5      r " S S\5      rg)    N)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       h/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/onnx_model_bert_keras.pyr   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   sq    ++%-


 !++%8


 !++%<


 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                 v	   U R                  5         U R                  5       n/ nSnU R                  S5      nU GHD  nU R                  US5      nUb  UR                  S;  a  UR                  S:X  aO  U R                  US5      nUb  UR                  S;  a)  [
        R                  SUb  UR                  OS  35        M  O*[
        R                  SUb  UR                  OS  35        M   U R                  U/ SQ/ SQ5      nUc  [
        R                  S	5        M  Uu  nn	n
nnn[
        R                  S
5        U R                  U/ SQ/ SQ5      nUc  [
        R                  S5        GM3  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MA  U!u  n"n#n$n%n&U R                  US   5      n'U'c  [
        R                  S5        GMx  U R                  U'S   S5      (       d  [
        R                  S5        GM  U R                  WU&UXa5      u  n(n)U((       Gaf  U R                  R                  U'S   R                  S   5      n*[
        R                  S5        U R                  R                  U*UU&UWU$UU R                   U R"                  UR$                  S   UR$                  S   S9n+U+c  GM`  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M/  [
        R                  S5        GMG     U R/                  U5        U R1                  5         [
        R3                  SU 35        g ) Nr   SkipLayerNormalization)r4   EmbedLayerNormalizationAddr   zFirst input for skiplayernorm: )r6   r   MatMulr   	Transposer7   )Nr   r   r   r   r   zFailed to match qkv nodeszMatched qkv nodes)r8   r   r6   r   r7   )r   r   r   r   r   zFailed to match v path)Softmaxr   r7   r   r   r   )r   r8   r   r6   r   r7   )r   Nr   r   r   r   )r9   r6   r   r7   )r   r   r   N)r9   r6   Divr7   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.)
mask_indexq_matmulk_matmulv_matmulq_addk_addv_addr   r   first_inputr$   zRoot node not matched.zFused Attention count:)input_name_to_nodesr,   get_nodes_by_op_type
get_parentr#   r&   r'   r   r   has_constant_inputr1   attention_maskprocess_maskr"   attention_fusioncreate_attention_noder   r   r$   add_nodeextendr%   replace_node_inputremove_nodesupdate_graphinfo),r   r,   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_rootr-   r=   attention_nodes,                                               r   fuse_attention!BertOnnxModelKeras.fuse_attention;   s     ""668 $ 9 9:R S3N__^Q7F~ 8 " >>U*!__^Q?F~ @ * 'FY_Ykv~~quFv%wx * LL#BU[Ug6>>mqBr!st ..N%I
  89 LL,-,,DG
 56IPF[)UOX--j:VXabH#2:/VY00O)
 &  #!'   11*>acrs##55jBegvwH'%>? :B7VVY00H#
 &QXN[)UOX56,,DG
 56IPF[)UOX--hqk:J!89**:a=!<<Z[*.*D*D(Hf+'L- !00==jn>R>RST>UV
89!%!6!6!L!L)%%%"nn $ 0 0 &a 0&--a0 "M " ")n-1$&&J'OP&&x0&&w/&&w/&&w/&&z2&&}5&&7''-C-CA-FVWHXY56_ 4` 	/*,_,=>?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                 N   U R                  5         U R                  5         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: )rE   r,   rF   rG   r#   r"   r&   rR   )r   countr-   reshape_noder+   s        r   r{   BertOnnxModelKeras.skip_reshape   s      "  "11)<)L__\15F!fnn	&A(.Q""1%
	 * 19KK9%AB r   c                 6	   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  a<  [        R                  SUR                   SUR                   35        UR                  n	O0[        R                  SUR                   SUR                   35        gU R                  UR                  S   5      n
U
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U
R                   SUR                  SS   35        SnGO7[        R                  SU
R                   SUR                   3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  a<  [        R                  SU
R                   SUR                   35        U
R                  nO0[        R                  SU
R                   SUR                   3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  a<  [        R                  SUR                   SUR                   35        UR                  nO0[        R                  SUR                   SUR                   35        g[        R                  S5        U R#                  XUU5        g)NLayerNormalizationz-start fusing embedding from node with output=r   z...)r6   r6   Gatherr:   zfailed to match word_embed_pathFzfailed to get word initializer   zFound word embedding. name:z, shape:z$Failed to find word embedding. name:r      position_embeddingzFound position embedding. name:z(Failed to find position embedding. name: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:z'Failed to find segment embedding. name:zCreate Embedding nodeT)r#   r&   r'   r$   r   get_initializerr"   r   to_arraylenshaperR   name
from_arrayreshapeadd_initializerrG   create_embedding_subgraph)r   noder,   word_embed_path	skip_noderM   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56F6K6K5LHUYU_U_T`ab-22NKK>?O?T?T>UU]^b^h^h]ijk..x~~a/@A&((9D4::!#

1(:%00tzz!}djjYZm>\1]_st$$V,=o>R>R=SS[\`\f\fghgi\j[klm%9"FG[G[F\\deieoeodpqr!33Hx>QTUWXSY[noN%=>$2!J"22:3C3CA3FGO&<=((9D4::!#9/:N:N9OxX\XbXbWcde%4%9%9"FG[G[F\\deieoeodpqrA/BC>V^^x7LL/0"226<<?C&LL<=$$%89tzz?aKK89L9Q9Q8RRZ[_[e[eZfgh 3 8 8KKABUBZBZA[[cdhdndncopq+,&&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&   rR   r,   nodesr#   r   )r   r,   r   s      r   ry   $BertOnnxModelKeras.process_embedding7  sT     	9:"668JJLD||33&&tAA	 !r   c           	      2   / 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UR                  S    SU 3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 z 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#   rH   r   rI   get_first_maskr"   printonnxhelper	make_node	attributerN   make_attributerO   rM   rP   r   prune_graphr&   rR   )r   rS   r   	mask_pathsub_node	cast_node
slice_nodeunsqueeze_nodemask_input_nameunsqueeze_added_1unsqueeze_added_2cast_node_2s               r   rz   BertOnnxModelKeras.fuse_maskC  s   JJLD||u$)@)@v)N)N 2249^`lm	$BK?Z"&"5"5"D"D"F!''*o=K(<(<Q(?'@@STcSdef$(KK$9$9+,:;+ %: %! %)KK$9$99::;+ %: %! #kk339:45 4 
 %%,,dkk.H.Hq.Q-RS''..2CE\]&&
I'NO/0/0k*Q !T 	/* !#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	  nnnnn	n
nnn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$ )Nr4   r   )	r6   r   r7   r   Gelur6   r   r7   r4   )	r   r   r   r   r   r   r   r   r   r   rF   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_reshapew  s    "778PQ"5))"
 ,D | 
%__Q/EKKNY' $AHNN1Y'%__Q/EKKNY'q OI #6L 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
  nnnnn	n
nnnn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R                  S   UR                  S'   U R	                  U5        US-  nM     U$ )Nr4   r   )
r6   r   r7   r   r   r6   r   r7   r   r4   )
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))" 2D  |  !. 4 4Q 7HNN1Y'%__Q/EKKNY' $AHNN1Y'%__Q/EKKNY'q O[ #6^ 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&   rR   r   )r   r   s     r   postprocessBertOnnxModelKeras.postprocess  s@    3358S8S8UUgo.o>?r    )__name__
__module____qualname____firstlineno__r   r   r1   rv   r|   r{   r   ry   rz   r   r   r   __static_attributes____classcell__)r   s   @r   r   r      sN    80# Z@x
CJX
2Yh)V2h r   r   )	loggingr   r   onnx_model_bert_tfr   	getLoggerr   r&   r   r   r   r   <module>r      s1       .			8	$K Kr   