
    hSa                     v    S SK r S SKrS SKrS SKJrJrJr  S SKJr  \ R                  " \
5      r " S S\5      rg)    N)TensorProtohelpernumpy_helper)BertOnnxModelc                   |   ^  \ 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S rS rSrU =r$ )BertOnnxModelTF   c                 &   > [         TU ]  XU5        g N)super__init__)selfmodel	num_headshidden_size	__class__s       e/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/onnx_model_bert_tf.pyr   BertOnnxModelTF.__init__   s    ;7    c                    / nU R                  5        Hw  nUR                  S:X  d  M  U R                  UR                  S   5      (       a  M:  U R	                  UR                  S   UR
                  S   5        UR                  U5        My     U R                  U5        [        R                  S[        U5       35        g )NIdentityr   zRemoved Identity count: )nodesop_typefind_graph_outputoutputreplace_input_of_all_nodesinputappendremove_nodesloggerinfolen)r   nodes_to_removenodes      r   remove_identityBertOnnxModelTF.remove_identity   s    JJLD||z)--dkk!n==33DKKNDJJqMR#**40	 !
 	/*.s?/C.DEFr   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BertOnnxModelTF.match_mask_path   sq    ++%-


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


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

 r   c                 ,   U R                  U/ 5      n0 nU Hy  nUR                   Hf  nU R                  U5      nU(       d  M  [        R                  " U5      n[        UR                  5      S:X  d  MN  UR                  X6R                  '   Mh     M{     U$ )zY
Find initializers that is 2D. Returns a dictionary with name as key and shape as value.
   )get_parent_subgraph_nodesr   get_initializerr   to_arrayr"   shapename)r   current_nodeparent_nodesinitializersr$   r   initializertemps           r   )get_2d_initializers_from_parent_subgraphs9BertOnnxModelTF.get_2d_initializers_from_parent_subgraphs7   s     55lBG D"2259;'00=D4::!+9=%5%56 $ ! r   c           	          U R                  5       nX;  a  g X1   n[        U5      S:w  a  g U R                  US   SS9n[        U5      S:  a  [        SU5        g [        U5      S:X  a  US   U:w  a  US   $ US   R                  S   nU R                  US   / SQ/ SQ4/ SQ/ S	Q4/S 5      u  pxnU(       a  U(       a  X(S
   R                  S   :X  a  [        R                  S5        US   n	U R                  U	5      R                  n
U R                  [        R                  " SU/S/S9U
5        [        R                  " U	R                  S   5      nU R                  [        R                  " SS/S/US9U
5        SnU$ )Nr,   r   T	recursivez(Found multiple candidates of segment_ids)ConstantOfShaper+   Concatr-   r+   Shape)r,   r   r   r   r   r   rE   r+   rF   r.   Squeezer-   r+   rG   )r,   r   r   r   r   r   r   r   Simplify semgent id path...rG   input_shapeinputsoutputsrE   zeros_for_input_shaperN   rO   value)input_name_to_nodesr"   get_graph_inputsprintr   match_parent_pathsr    debugget_graph_by_noder:   add_noder   	make_nodeget_attribute_value	attribute)r   segment_embedding	input_idsrS   r   graph_inputssegment_ids_segment_id_pathconstantofshape_node
graph_nameconstantofshape_values               r   find_segment_ids BertOnnxModelTF.find_segment_idsG   s   "6687#6u:?,,U1X,F|q <lK|!l1o&B?" AhnnQ' $ 7 7!H T&
	 -& +!
A0 yY":M:S:STU:V-VLL67#21#5 //0DEJJJMM  )}oV %+$>$>?S?]?]^_?`$a!MM  %)?45/	  2Kr   c                     U R                  5       nX;  a  g X!   n[        U5      S:w  a  g U R                  US   SS9n[        U5      S:X  a  US   $ [        SU5        g )Nr,   r   TrC   z&Found multiple candidates of input_ids)rS   r"   rT   rU   )r   word_embeddingrS   r   r_   s        r   find_input_idsBertOnnxModelTF.find_input_ids   sn    "6684#3u:?,,U1X,F|!?"6Er   c           
      4   U R                  5        GH  nUR                  S:X  d  M  U R                  U/ SQ/ SQ5      nUc  M2  Uu  nnnnnn	U R                  US5      (       d  MT  U R                  US5      (       d  Mm  U R	                  USS9n
U
 Vs/ s H  oU;  d  M
  UPM     nn[        U5      S:  a  [        SU5          g [        U5      S:X  a  US	   s  $ U R                  US
   / SQ/ SQ5      nU
 Vs/ s H  oU;   d  M
  UPM     nnU(       a  U(       a  [        U5      S:X  a  US	   US
   R                  S	   :X  a  [        R                  S5        US	   n[        R                  " UR                  S	   5      nU R                  U5      R                  nU R                  [        R                   " SUS	   /S/S9U5        U R                  [        R                   " SS/U	R                  S	   /US9U5        U	R                  S	   s  $    g s  snf s  snf )NSoftmax)Addr(   r)   r+   r-   r.   )r   r,   Nr,   r   r   ir,   TrC   z'Found multiple candidates of mask inputr   rJ   rH   )r   r   r   r   r   r   r   r   rK   rG   input_shape_for_maskrM   rE   rQ   )r   r   r/   has_constant_inputrT   r"   rU   r   r    rW   r   r[   r\   rX   r:   rY   rZ   )r   excluded_graph_inputsr$   	mask_pathrY   mul_nodesub_node	cast_node
slice_nodeunsqueeze_noder_   r   rN   path_to_be_simplifiedduplicated_inputsrc   re   rd   s                     r   find_mask_inputBertOnnxModelTF.find_mask_input   s=   JJLD||y( 22G)	
 $ "**8V<<AXAXYacdAeAe#'#8#8T#8#RL1=dNcAceFd6{QGP#6{a'%ay(,0,B,B!"	 1-) =I(kL5UjLjL%(k .- 12a7-a04I"4M4S4STU4VV%BC/DQ/G,060J0JK_KiKijkKl0m-%)%;%;<P%Q%V%V
",, '(9!(<'=)?(@
 ' ",, 1(>'?)7)=)=a)@(A&;	 ' *//22K !L g e* )ls   	HH8	HHc                    U R                  U5      nUc  [        R                  S5        gU R                  X55      nUc  [        R                  S5        gU R	                  Xe/5      nUc  [        R                  S5        gXVU/U l        U R                  S5      nU R                  R                  Xx5        U R                  U5      R                  R                  R                  [        R                  :w  a  U R                  R!                  U5      u  pU R                  U5      (       a  U R                  R!                  U5      u  pOU R                  R#                  U5      u  pjU R                  U5      (       a  U R                  R!                  U5      u  pOU R                  R#                  U5      u  p{U R                  S5      n[$        R&                  R)                  SUUUUUUR*                  S   UR*                  S	   U/X/S
S9nSUl        U R/                  UR0                  S   U5        U R3                  XR5                  U5      R6                  5        g )Nz6Failed to find input_ids. Cannot fuse embedding layer.Fz8Failed to find segment_ids. Cannot fuse embedding layer.z7Failed to find input_mask. Cannot fuse embedding layer.
mask_indexembed_outputEmbedLayerNormalizationr,   r5   
EmbedLayerrN   rO   r:   zcom.microsoftr   )rj   r    r!   rf   rz   bert_inputscreate_node_nameattention_maskset_mask_indicefind_graph_inputtypetensor_type	elem_typer   INT32utilscast_graph_input_to_int32cast_input_to_int32onnxr   rZ   r   domainr   r   rY   rX   r:   )r   normalize_noderi   r]   position_embeddingr^   r`   
mask_inputr}   castedsegment_id_cast_nodemask_input_cast_noder~   
embed_nodes                 r   create_embedding_subgraph)BertOnnxModelTF.create_embedding_subgraph   s   ''7	KKPQ++,=IKKRS));*BC
KKQR%J?**<8
++JC  +00<<FF+J[J[[ $

 D DY OF  --"&**"F"F{"SFK04

0N0N{0[-K  ,,!%!E!Ej!QFJ/3zz/M/Mj/Y,J,,^<[[**%"!$$Q'$$Q'	 ". + 

 ,
''(=(=a(@,Oj"8"8"H"M"MNr   c                    [         R                  S5        U R                  5       nU R                  S5      nU GH  nU R	                  U/ SQ/ SQU5      nUc  M!  Uu  pVnU R                  UR                  S   5      nUc  MI  [        R                  " U5      n	[        U	R                  5      S:X  a<  [         R                  SUR                   S	U	R                   35        UR                  n
O1[         R                  S
UR                   S	U	R                   35          gU R                  USU5      nUc  M  UR                  S:X  d  GM  U R                  U5      n[        U5      S:w  a  [         R                  SU 35          gSnSnUR!                  5        HK  u  nnUS   S:X  a  Un[         R                  SU S	U 35        M.  Un[         R                  SU S	U 35        MM     Ub  Uc  [         R                  S5          g[         R                  S5        U R#                  UUUU
5        U R%                  5           g   g)z=
Automatically detect word, segment and position embeddings.
z#start processing embedding layer...LayerNormalization)rn   r*   r-   )r   r,   r   Nr   r5   zFound position embedding. name:z, shape:z(Failed to find position embedding. name:rn   zFFailed to find two embeddings (word and segment) from Add node. Found zFound segment embedding. name:zFound words embedding. name:z.Failed to find both word and segment embeddingzCreate Embedding node)r    r!   output_name_to_nodeget_nodes_by_op_typer/   r7   r   r   r8   r"   r9   r:   
get_parentr   r@   warningitemsr   prune_graph)r   r   layer_norm_nodeslayer_norm_nodepos_embed_pathrY   reshape_noderv   r>   r?   r   first_parent
embeddingsri   r]   r:   r9   s                    r   process_embedding!BertOnnxModelTF.process_embedding  s9    	9:"668445IJ/O!33+#	N %1?.HJ..z/?/?/BCK"((5D4::!#=k>N>N=OxX\XbXbWcde%0%5%5"F{GWGWFXX`aeakak`lmn??8Q8KLL'L,@,@E,I!KKLY
z?a'NN`ak`lm !%$(!#-#3#3#5KD%Qx1},0)&DTF(SXRY$Z[)-&B4&QVPW$XY $6 ")->-FKK PQ34..#"%&	   "o  0r   c                     XU4 HE  nUR                   S   nXW   nX:X  a  M  [        R                  SU SUR                  S    35          g   g)Nr   zCheck attention input failed:z, FT)r   r    rW   r   )	r   matmul_qmatmul_kmatmul_vparentr   x
root_input	root_nodes	            r   check_attention_input%BertOnnxModelTF.check_attention_inputW  s\    h/AJ+7I"LL8Bv}}UVGWFXYZ 0 r   c                    U R                  5       n/ nSn/ nU R                  S5      nU R                  S5      nUR                  U5        UR                  U5        U GH  nU R                  U5      R                  nUR
                  S:X  a  U R                  USS5      n	U	b  U	nOMJ  U R                  US5      n
U
b  U
R
                  S;  a<  U R                  US5      n
U
b  U
R
                  S;  a  [        R                  S5        M  U R                  U/ SQ/ S	Q5      nUcL  U 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S   nU R                  U/ SQ/ SQ5      nUc2  U R                  USS/SS/5      nUc  [        R                  S5        GMb  US   nUS   nU 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S   nU R                  U/ SQ/ SQ5      nUc2  U R                  USS/SS/5      nUc  [        R                  S5        GM	  US   nUS   nU 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S   nUS   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                  US   S/S/5      =(       d    U R                  US   S/S/5      nS nUS!-   nUc  [        U5      S":X  aq  U R                  US   R                  S   5      cO  US   R                  S   nU R!                  ["        R$                  " SU/U/US/S#9U5        UUS   R                  S'   U R'                  UUXU5      nU(       Ga#  U R(                  R+                  US   R                  S   5      n[        R                  S$5        U R,                  R/                  UUUUUUUU R0                  U R2                  U
R4                  S   US%   R4                  S   S&9nUc  GMA  US   R
                  S:X  Ga%  ["        R6                  " US   R                  S'-   [8        R:                  S(/[<        R>                  " SSU R0                  [A        U R2                  U R0                  -  5      //5      RC                  5       S)S*9n U RE                  U U5        ["        R$                  " S+UR4                  S   US   R                  S'-   /US   R                  S,-   /US   R                  S--   S.9n!US   R                  S,-   US   R                  S'   U R!                  U!U5        U
R
                  S+:X  a  [F        RH                  " U RK                  U
R                  S   5      5      S   n"["        R6                  " U
R                  S/-   [8        R:                  S0/[<        R>                  " SSU"//5      RC                  5       S)S*9n U RE                  U U5        U
R                  S/-   U
R                  S'   U R!                  UU5        US-  nUR                  US%S  5        UR                  U5        UR                  U5        UR                  U5        UR                  U5        UR                  U5        GM  [        R                  S15        GM     U RM                  U5        U RO                  5         [        RQ                  S2U 35        g )3Nr   SkipLayerNormalizationr   rn   r,   )r   r   r*   z(Failed to match parent of normalize_node)rn   MatMulr*   	Transposer   )r   r   r   r   r   )r   r*   r   r   )r,   r   r   r   )rn   Einsumr   )r   r   r   zFailed to match qkv nodesrJ   )r   r*   rn   r   r   zFailed to match v path)rm   rn   r(   r   )r   r   r   r   )rm   rn   r   zFailed to match qk_pathszFailed to match q path)r(   rn   r   )r,   r   r   zFailed to match k pathzCannot find mask_nodes.z;Sub node expected to have an input with constant value 1.0.rI   ExpandSqueeze_3d_to_2d_mask_output   )axeszCreate an Attention node.r5   )r}   q_matmulk_matmulv_matmulq_addk_addv_addr   r   first_inputr   	_newshape   T)r:   	data_typedimsvalsrawr*   _reshape_output_reshaper   	_modified   zRoot node not matched.zFused Attention count:))r   r   extendrX   r:   r   match_parentr   r    rW   r/   r2   rp   r"   r   r   rY   r   rZ   r   r   process_maskattention_fusioncreate_attention_noder   r   r   make_tensorr   INT64npint64inttobytesadd_initializerr   r8   r7   r   update_graphr!   )#r   r   r#   attention_countstart_nodesskip_layer_norm_nodesr   r   rd   add_before_layernormr   	qkv_nodes
matmul_qkvv_nodesadd_vr   qk_nodes	matmul_qkq_nodesadd_qr   k_nodesadd_kr   r1   squeeze_nodesqueeze_node_namesqueeze_output_namer   is_same_rootr}   attention_nodetensorreshape_r   s#                                      r   fuse_attentionBertOnnxModelTF.fuse_attentionb  s{   "668 $ 9 9:R S445IJ 	01+,)N//?DDJ%%)=='+'8'8PQ'R$'3%9N__^Q7F~ 8 "
 ;>V^^ < &
 LL!KL..CI
   22"@ 	
 $ $ 6 6~Gbdm nI (%@A "2J,,Z9bdpqG00eX=NQRTUPVW?LL!9:BKEr{H--j:]_klH11*>Z\ef#LL!;< I,,Y8acopG00UH<MPQSTvV?LL!9:BKEr{H,,Y8acopG00<TV_`?LL!9:BKEr{H--hqk:J!67**:a=!<<Z[  11*R.9+PQsS W[WmWm2
QCXL !8"3i"?#J1(<AVAVWabdWeWkWklmWnAoAw'^11!4
$$!#,-)S 	 +>
2$$Q'55h(\opL!00==jn>R>RST>UV
89 "&!6!6!L!L)%%%"nn $ 0 0 &a 0$Q<..q1 "M " ")Q<''83#//&q\..<"-"3"3SXX %&$%$(NN$'(8(84>>(I$J	!"	 "') F  ((<%//!*11!4%aL--;  "+1!2!25F!F G&q\..; H -6aL,=,=@Q,QIaL&&q)MM(J7>>Y."."7"78L8LV\\Z[_8]"^_`"aK#//#[[;6"-"3"3SXX2{';&<=EEG F ((<&,kkK&?FLLOnj91$&&y}5&&x0&&w/&&w/&&w/&&z256q *r 	/*,_,=>?r   c                 d    U R                  5         U R                  5         U R                  5         g r   )r%   r   skip_reshaper   s    r   
preprocessBertOnnxModelTF.preprocess-  s&     r   c                    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: )r   r   r   r   r    r!   )r   countreshape_nodesr   r   s        r   r   BertOnnxModelTF.skip_reshape2  s    11)<)L__\15F!fnn	&A(.Q""1%
	 * 19KK9%AB r   c                    U R                  S5      nU Hu  nU R                  USS/SS/5      nUc  M  [        R                  S5        Uu  pEU R	                  UR
                  S   UR                  S   5        U R                  U5          g    g )N	Attentionr*   r   r   z+Remove Reshape before first Attention node.)r   r/   r    r!   r   r   r   remove_node)r   attention_nodesr   pathreshapera   s         r   %remove_reshape_before_first_attention5BertOnnxModelTF.remove_reshape_before_first_attention>  s    33K@-N)).9F_:`cdfgbhiD|KKEFJG++GNN1,=w}}Q?OPW% .r   c                 D    U R                  5         U R                  5         g r   )r  r   r   s    r   postprocessBertOnnxModelTF.postprocessJ  s    224r   )r   )__name__
__module____qualname____firstlineno__r   r%   r2   r@   rf   rj   rz   r   r   r   r   r   r   r  r  __static_attributes____classcell__)r   s   @r   r   r      s^    8G2 >@ GR4Ol?B	I@V

C
 r   r   )loggingnumpyr   r   r   r   r   onnx_model_bertr   	getLoggerr  r    r    r   r   <module>r     s7       2 2 )			8	$|m |r   