
    fPiSa                     z    d dl Z d dlZd dlZd dlmZmZmZ d dlmZ  e j	        e
          Z G d de          ZdS )    N)TensorProtohelpernumpy_helper)BertOnnxModelc                   r     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z xZS )BertOnnxModelTFc                 N    t                                          |||           d S N)super__init__)selfmodel	num_headshidden_size	__class__s       /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/transformers/onnx_model_bert_tf.pyr   zBertOnnxModelTF.__init__   s%    	;77777    c                    g }|                                  D ]n}|j        dk    ra|                     |j        d                   sA|                     |j        d         |j        d                    |                    |           o|                     |           t          	                    dt          |                      d S )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_identityzBertOnnxModelTF.remove_identity   s    JJLL 	1 	1D|z))--dk!n== 133DKNDJqMRRR#**4000/***Es?/C/CEEFFFFFr   c                     |                      |g dg d          }||S |                      |g dg d          }||S |                      |g dg d          }|S )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_pathzBertOnnxModelTF.match_mask_path   s    ++%---OO
 


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


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

 r   c                     |                      |g           }i }|D ]^}|j        D ]T}|                     |          }|r;t          j        |          }t          |j                  dk    r|j        ||j        <   U_|S )zi
        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_subgraphsz9BertOnnxModelTF.get_2d_initializers_from_parent_subgraphs7   s     55lBGG  	D 	DD D D"22599 D'0==D4:!++9=[%56D r   c                 p   |                                  }||vrd S ||         }t          |          dk    rd S |                     |d         d          }t          |          dk    rt          d|           d S t          |          dk    r|d         |k    r|d         S |d         j        d         }|                     |d         g dg dfg dg d	fgd           \  }}}|r|r||d
         j        d         k    rt                              d           |d         }	|                     |	          j	        }
| 
                    t          j        d|gdg          |
           t          j        |	j        d                   }| 
                    t          j        ddgdg|          |
           d}|S )Nr)   r   T	recursivez(Found multiple candidates of segment_ids)ConstantOfShaper(   Concatr*   r(   Shape)r)   r   r   r   r   r   r@   r(   rA   r+   Squeezer*   r(   rB   )r)   r   r   r   r   r   r   r   Simplify semgent id path...rB   input_shapeinputsoutputsr@   zeros_for_input_shaperI   rJ   value)input_name_to_nodesr    get_graph_inputsprintr   match_parent_pathsr   debugget_graph_by_noder6   add_noder   	make_nodeget_attribute_value	attribute)r   segment_embedding	input_idsrN   r   graph_inputssegment_ids_segment_id_pathconstantofshape_node
graph_nameconstantofshape_values               r   find_segment_idsz BertOnnxModelTF.find_segment_idsG   s'   "6688$7774#$56u::??4,,U1X,FF|q  <lKKK4|!!l1o&B&B?" AhnQ' $ 7 7!H TSS&&&
	 	 	 -,,& +!
 !
?A0  	2y 	2Y/":M:STU:V-V-VLL6777#21#5 //0DEEJJMM )}oVVV   %+$>?S?]^_?`$a$a!MM %)?45/	      2Kr   c                    |                                  }||vrd S ||         }t          |          dk    rd S |                     |d         d          }t          |          dk    r|d         S t          d|           d S )Nr)   r   Tr>   z&Found multiple candidates of input_ids)rN   r    rO   rP   )r   word_embeddingrN   r   rZ   s        r   find_input_idszBertOnnxModelTF.find_input_ids   s    "6688!4444#N3u::??4,,U1X,FF|!!?"6EEEtr   c           	      >   |                                  D ]}|j        dk    r|                     |g dg d          }|-|\  }}}}}}	|                     |d          r|                     |d          r|                     |d          }
fd|
D             }t          |          dk    rt          d	|            d S t          |          dk    r
|d
         c S |                     |d         g dg d          }fd|
D             }|r|rt          |          dk    r|d
         |d         j        d
         k    rt          	                    d           |d
         }t          j        |j        d
                   }|                     |          j        }|                     t          j        d|d
         gdg          |           |                     t          j        ddg|	j        d
         g|          |           |	j        d
         c S d S )NSoftmax)Addr%   r&   r(   r*   r+   )r   r)   Nr)   r   r   ir)   Tr>   c                     g | ]}|v|	S  ri   .0r   excluded_graph_inputss     r   
<listcomp>z3BertOnnxModelTF.find_mask_input.<locals>.<listcomp>   s$    dddNcAcAceAcAcAcr   z'Found multiple candidates of mask inputr   rE   rC   )r   r   r   r   r   r   r   r   c                     g | ]}|v |	S ri   ri   rj   s     r   rm   z3BertOnnxModelTF.find_mask_input.<locals>.<listcomp>   s$    (k(k(k5EUjLjLjLjLjLjr   rF   rB   input_shape_for_maskrH   r@   rL   )r   r   r,   has_constant_inputrO   r    rP   r   r   rR   r   rV   rW   rS   r6   rT   rU   )r   rl   r"   	mask_pathrT   mul_nodesub_node	cast_node
slice_nodeunsqueeze_noderZ   rI   path_to_be_simplifiedduplicated_inputsr^   r`   r_   s    `               r   find_mask_inputzBertOnnxModelTF.find_mask_input   s   JJLL E	3 E	3D|y(( 22GGG))) 	
 $ "**8V<< 43AXAXYacdAeAe 43#'#8#8T#8#R#RLdddddddF6{{QGPPP#tt6{{a''%ay(((,0,B,B!"	 	 	 100- -) )l(k(k(kL(k(k(k% .-   122a77-a04I"4M4STU4VVV%BCCC/DQ/G,060JK_KijkKl0m0m-%)%;%;<P%Q%Q%V
", '(9!(<'=)?(@  
 '   ", 1(>'?)7)=a)@(A&;	   '   */2222tr   c                    |                      |          }|t                              d           dS |                     ||          }|t                              d           dS |                     ||g          }|t                              d           dS |||g| _        |                     d          }| j                            ||           | 	                    |          j
        j        j        t          j        k    r| j                            |          \  }	}| 	                    |          r| j                            |          \  }	}n| j                            |          \  }}
| 	                    |          r| j                            |          \  }	}n| j                            |          \  }}|                     d          }t$          j                            d||||||j        d         |j        d	         |g||gd
          }d|_        |                     |j        d         |           |                     ||                     |          j                   d S )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)   r1   
EmbedLayerrI   rJ   r6   zcom.microsoftr   )rd   r   r   ra   ry   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   rU   r   domainr   r   rT   rS   r6   )r   normalize_noderc   rX   position_embeddingrY   r[   
mask_inputr{   castedsegment_id_cast_nodemask_input_cast_noder|   
embed_nodes                 r   create_embedding_subgraphz)BertOnnxModelTF.create_embedding_subgraph   sr   ''77	KKPQQQ5++,=yIIKKRSSS5));	*BCC
KKQRRR5%{J?**<88
++J
CCC  ++0<F+J[[[ $
 D DY O OFI  -- 	\"&*"F"F{"S"SFKK04
0N0N{0[0[-K-  ,, 	Z!%!E!Ej!Q!QFJJ/3z/M/Mj/Y/Y,J,,,^<<[**%"!$Q'$Q'	 ":. + 
 

 ,
''(=a(@,OOOj$"8"8"H"H"MNNNNNr   c                    t                               d           |                                 }|                     d          }|D ]}|                     |g dg d|          }|"|\  }}}|                     |j        d                   }|Kt          j        |          }	t          |	j
                  dk    r2t                               d|j         d	|	j
                    |j        }
n-t                               d
|j         d	|	j
                     dS |                     |d|          }|-|j        dk    r!|                     |          }t          |          dk    r t                               d|             dS d}d}|                                D ]V\  }}|d         dk    r#|}t                               d| d	|            4|}t                               d| d	|            W||t                               d            dS t                               d           |                     ||||
           |                                   dS dS )zM
        Automatically detect word, segment and position embeddings.
        z#start processing embedding layer...LayerNormalization)rg   r'   r*   )r   r)   r   Nr   r1   zFound position embedding. name:z, shape:z(Failed to find position embedding. name:rg   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,   r3   r   r   r4   r    r5   r6   
get_parentr   r<   warningitemsr   prune_graph)r   r   layer_norm_nodeslayer_norm_nodepos_embed_pathrT   reshape_noderu   r:   r;   r   first_parent
embeddingsrc   rX   r6   r5   s                    r   process_embeddingz!BertOnnxModelTF.process_embedding  s    	9:::"6688445IJJ/ 7	 7	O!33+++		#	 N %1?.HlJ..z/?/BCCK"(55D4:!##dk>NddX\Xbddeee%0%5""m{GWmmaeakmmnnn??8Q8KLLL'L,@E,I,I!KKLYY
z??a''NNmakmm   FF!%$(!#-#3#3#5#5 Z ZKD%Qx1}},0)$ZT$Z$ZSX$Z$Z[[[[)-$X4$X$XQV$X$XYYYY!)->-FKK PQQQFF3444..#"%&	     """o7	 7	r   c                     |||fD ]K}|j         d         }||         }||k    rt                              d| d|j        d                      dS dS )Nr   zCheck attention input failed:z, FT)r   r   rR   r   )	r   matmul_qmatmul_kmatmul_vparentr   x
root_input	root_nodes	            r   check_attention_inputz%BertOnnxModelTF.check_attention_inputW  sr    Hh/ 	 	AJ+J7IF""LLYYYv}UVGWYYZZZ55tr   c                    |                                  }g }d}g }|                     d          }|                     d          }|                    |           |                    |           |D ]C}|                     |          j        }|j        dk    r|                     |dd          }	|	|	}nE|                     |d          }
|
	|
j        dvr<|                     |d          }
|
	|
j        dvrt          	                    d           | 
                    |g dg d	          }|V| 
                    |g d
g d          }|9| 
                    |g dg d          }|t          	                    d           |d         }| 
                    |g dg d          }|9| 
                    |ddgddg          }|t          	                    d           s|d         }|d         }| 
                    |g dg d          }|9| 
                    |g dg d          }|t          	                    d           |d         }| 
                    |g dg d          }|9| 
                    |ddgddg          }|t          	                    d           7|d         }|d         }| 
                    |g dg d          }|9| 
                    |g dg d          }|t          	                    d           |d         }|d         }|                     |d                   }|t          	                    d           |                     |d         d          st          	                    d           | 
                    |d         dgdg          p| 
                    |d         dgdg          }d }|d!z   }|t          |          d"k    ry|                     |d         j        d                   S|d         j        d         }|                     t#          j        d|g|g|dg#          |           ||d         j        d<   |                     ||||
|          }|r| j                            |d         j        d                   }t          	                    d$           | j                            |||||||| j        | j        |
j        d         |d%         j        d         &          }||d         j        dk    rt#          j        |d         j        d'z   t8          j        d(gt=          j        dd| j        tA          | j        | j        z            gg          !                                d)*          } | "                    | |           t#          j        d+|j        d         |d         j        d'z   g|d         j        d,z   g|d         j        d-z   .          }!|d         j        d,z   |d         j        d<   |                     |!|           |
j        d+k    rtG          j$        | %                    |
j        d                             d         }"t#          j        |
j        d/z   t8          j        d0gt=          j        dd|"gg          !                                d)*          } | "                    | |           |
j        d/z   |
j        d<   |                     ||           |dz  }|                    |d%d                     |                    |           |                    |           |                    |           |                    |           |                    |           )t          	                    d1           E| &                    |           | '                                 t          (                    d2|            d S )3Nr   SkipLayerNormalizationr   rg   r)   )r   r   r'   z(Failed to match parent of normalize_node)rg   MatMulr'   	Transposer   )r   r   r   r   r   )r   r'   r   r   )r)   r   r   r   )rg   Einsumr   )r   r   r   zFailed to match qkv nodesrE   )r   r'   rg   r   r   zFailed to match v path)rf   rg   r%   r   )r   r   r   r   )rf   rg   r   zFailed to match qk_pathszFailed to match q path)r%   rg   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.rD   ExpandSqueeze_3d_to_2d_mask_output   )axeszCreate an Attention node.r1   )r{   q_matmulk_matmulv_matmulq_addk_addv_addr   r   first_inputr   	_newshape   T)r6   	data_typedimsvalsrawr'   _reshape_output_reshaper   	_modified   zRoot node not matched.zFused Attention count:))r   r   extendrS   r6   r   match_parentr   r   rR   r,   r/   rp   r    r   r   rT   r   rU   r   r   process_maskattention_fusioncreate_attention_noder   r   r   make_tensorr   INT64npint64inttobytesadd_initializerr   r4   r3   r   update_graphr   )#r   r   r!   attention_countstart_nodesskip_layer_norm_nodesr   r   r_   add_before_layernormr   	qkv_nodes
matmul_qkvv_nodesadd_vr   qk_nodes	matmul_qkq_nodesadd_qr   k_nodesadd_kr   r.   squeeze_nodesqueeze_node_namesqueeze_output_namer   is_same_rootr{   attention_nodetensorreshape_r   s#                                      r   fuse_attentionzBertOnnxModelTF.fuse_attentionb  s   "6688 $ 9 9:R S S445IJJ 	0111+,,,) x	 x	N//??DJ%)==='+'8'8PQ'R'R$'3%9NN__^Q77F~ 8 " "
 ;;>V^ < & &
 LL!KLLL..CCC I
   22"@@@ LL 	
 $ $ 6 6~GbGbGbdmdmdm n nI (%@AAA "2J,,Z9b9b9bdpdpdpqqG00eX=NQRTUPVWW?LL!9:::BKEr{H--j:]:]:]_k_k_kllH11*>Z>Z>Z\e\e\eff#LL!;<<< I,,Y8a8a8acococoppG00UH<MPQSTvVV?LL!9:::BKEr{H,,Y8a8a8acococoppG00<T<T<TV_V_V_``?LL!9:::BKEr{H--hqk::J!6777**:a=!<< Z[[[  11*R.9+PQsSS W[WmWm2
QCX XL !8"3i"?#J1(<(<AVAVWabdWeWklmWnAoAoAw'^1!4
$!#,-)S   	 	 	 +>
2$Q'55h(TZ\oppL K!0==jn>RST>UVV
8999 "&!6!L!L)%%%"n $ 0 &a 0$Q<.q1 "M " " ")Q<'833#/&q\.<"-"3SX %&$%$(N$'(84>(I$J$J	!"	 	 "'))   F  ((<<<%/!*1!4%aL-;  "+1!25F!F G&q\.;     H -6aL,=@Q,QIaL&q)MM(J777>Y.."."78L8LV\Z[_8]8]"^"^_`"aK#/#[;6"-"3SX2{';&<==EEGG   F ((<<<&,kK&?FLOnj9991$&&y}555&&x000&&w///&&w///&&w///&&z22225666/***>_>>?????r   c                 ~    |                                   |                                  |                                  d S r
   )r#   r   skip_reshaper   s    r   
preprocesszBertOnnxModelTF.preprocess-  s>       r   c                    d}|                      d          }|D ]?}|                     |d          }|%|j        dk    r|j        d         |j        d<   |dz  }@|dk    rt                              d|            d S d S )Nr   r'   r)   zSkip consequent Reshape count: )r   r   r   r   r   r   )r   countreshape_nodesr   r   s        r   r   zBertOnnxModelTF.skip_reshape2  s    11)<<) 	 	L__\155F!fn	&A&A(.Q"1%
199KKA%AABBBBB 9r   c                 :   |                      d          }|D ]}|                     |ddgddg          }| t                              d           |\  }}|                     |j        d         |j        d                    |                     |            d S d S )N	Attentionr'   r}   r   z+Remove Reshape before first Attention node.)r   r,   r   r   r   r   r   remove_node)r   attention_nodesr   pathreshaper\   s         r   %remove_reshape_before_first_attentionz5BertOnnxModelTF.remove_reshape_before_first_attention>  s    33K@@- 	 	N)).9F_:`cdfgbhiiD|KKEFFFJGQ++GN1,=w}Q?OPPPW%%%EE	 	r   c                 V    |                                   |                                  d S r
   )r   r   r   s    r   postprocesszBertOnnxModelTF.postprocessJ  s,    22444r   )__name__
__module____qualname__r   r#   r/   r<   ra   rd   ry   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s!       8 8 8 8 8G G G  2   > > >@   G G GR4O 4O 4Ol? ? ?B	 	 	I@ I@ I@V  

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