
    `i5:                         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)BertOnnxModelc                   p   ^  \ 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U =r$ )BertOnnxModelTF   c                 &   > [         TU ]  XU5        g N)super__init__)selfmodel	num_headshidden_size	__class__s       m/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxruntime_tools/transformers/BertOnnxModelTF.py__init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                 "   / nU R                  5        GH2  nUR                  S:X  d  M  U R                  U/ SQ/ SQ5      nUc  M2  Uu  pEpgpppnU R                  U/ SQ/ SQ5      nUc  MW  Uu  nnnnnUU	:X  d  Mg  [        R                  R                  SUR                  S   /S/S	S
/S9n[        R                  R                  SS/UR                  S   /SS/S9nUR                  S   UR                  S
'   UR                  XEXgXXXU/5        UR                  UUU/5        U R                  U5        U R                  U5        GM5     U R                  U5        [        U5      S:  a  [        R                  S5        g U R                  5         g )NSub)ReshapeMulConstantOfShapeCastConcat	Unsqueezer.   SqueezeSlicer.   Shape)   r   r   r   r   r   r   r   r   r   r   )r.   r+   r.   r/   r0   )r4   r   r4   r   r   r0   r   mask_fuse_unsqueeze1_outputMask_UnSqueeze_1r4   inputsoutputsnameaxesMask_UnSqueeze_2   
Fused mask)r   r   match_parent_pathonnxhelper	make_noder   r   extendadd_noder!   r$   r"   r#   fuse_mask_2)r   r%   r&   parent_path_constantreshape_node_0
mul_node_0constantofshape_nodecast_node_0concat_node_0unsqueeze_nodecast_node_1squeeze_node
slice_nodecast_node_2
shape_nodeparent_path_maskcast_node_3reshape_node_1cast_node_4concat_node_1unsqueeze_node_1unsqueeze_added_1unsqueeze_added_2s                          r   	fuse_maskBertOnnxModelTF.fuse_mask    s   JJLD||u$'+'='= F F(G$ (/ pD  m,@}ny  JT  cm#'#9#9FF$H 
 $+\lY^[-IY'>9$(KK$9$9+BPBVBVWXBYAZC`Ba?Q@As	 %: %D! %)KK$9$9+B_A`CNCTCTUVCWBX?Q@As	 %: %D!
 !, 2 21 5

1&&"0DS`z
(   &&]'ST/0/0U !X 	/*!#KK%r   c           	      J   / 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  pEpgpU 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                  XVXxU	/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,   )r*   r0   r,   r.   r+   r.   )r   r4   r   r4   r   r   r   z!Cast input {} is not mask input{}r0   r5   r6   r4   r7   mask_fuse_unsqueeze2_outputr<   r=   r.   mask_fuse_cast_output)r8   r9   tor>   zFailed to fuse mask)r   r   has_constant_inputr?   attention_maskget_first_maskr   printformatr@   rA   rB   	attributerC   make_attributereplace_node_inputrD   r!   r$   prune_graphr"   r#   )r   r%   r&   	mask_pathsub_noderL   mul_noderJ   rG   rM   mask_input_namerX   rY   rP   s                 r   rE   BertOnnxModelTF.fuse_mask_2T   s	   JJLD||u$)@)@v)N)N 2249o3EG	$_h\("&"5"5"D"D"F$$Q'?:=DD[EVEVWXEY[jkl$(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\]&&+_j'kl/0/0k*G !J 	/* !#C$81$<LXBWXr   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.
r=   )get_parent_subgraph_nodesr   get_initializerr   to_arrayr$   shaper:   )r   current_nodeparent_nodesinitializersr&   r   initializertemps           r   )get_2d_initializers_from_parent_subgraphs9BertOnnxModelTF.get_2d_initializers_from_parent_subgraphs   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 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 )Nr4   r   T	recursivez(Found multiple candidates of segment_idsinput_name_to_nodesr$   get_graph_inputsrd   )r   segment_embeddingr   r   graph_inputss        r   find_segment_ids BertOnnxModelTF.find_segment_ids   sn    "6687#6u:?,,U1X,F|!?"8,G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 )Nr4   r   Tr|   z&Found multiple candidates of input_idsr~   )r   word_embeddingr   r   r   s        r   find_input_idsBertOnnxModelTF.find_input_ids   sn    "6684#3u:?,,U1X,F|!?"6Er   c                    U R                  5        H  nUR                  S:X  d  M  U R                  U/ SQ/ SQ5      nUc  M1  Uu  pEnU R                  US5      (       d  MO  U R                  US5      (       d  Mh  U R	                  USS9nU Vs/ s H  oU;  d  M
  UPM     n	n[        U	5      S:X  d  M  U	S   s  $    g s  snf )	NSoftmax)Addr,   r*   )r   r4   Nr]   r4   Tr|   r   )r   r   r?   ra   r   r$   )
r   excluded_graph_inputsr&   rj   rD   rl   rk   r   r   r8   s
             r   find_mask_inputBertOnnxModelTF.find_mask_input   s    JJLD||y( 2249NP\]	$/8,H**8V<<AXAXYacdAeAe#'#8#8T#8#RL1=dNcAceFd6{a'%ay( ! 	 es   	B=B=c                 z   U R                  U5      nUc  [        R                  S5        gU R                  U5      nUc  [        R                  S5        gU R	                  XV/5      nUc  [        R                  S5        gXeU/U l        U R                  S5      nU R                  R                  Xx5        U R                  U5      R                  R                  R                  [        R                  :w  a  U R                  U5      u  pU R                  U5      R                  R                  R                  [        R                  :w  a  U R                  U5      u  pU R                  U5      R                  R                  R                  [        R                  :w  a  U R                  U5      u  pU 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R,                  S   U
5        U R/                  U5        g )Nz8Failed to find segment_ids. Cannot fuse embedding layer.Fz6Failed to find input_ids. Cannot fuse embedding layer.z7Failed to find input_mask. Cannot fuse embedding layer.
mask_indexembed_outputEmbedLayerNormalizationr4   r=   
EmbedLayer)r8   r9   r:   zcom.microsoftr   )r   r"   r#   r   r   bert_inputscreate_node_namerb   set_mask_indicefind_graph_inputtypetensor_type	elem_typer   INT32cast_graph_input_to_int32r@   rA   rB   r   domainr   r   rD   )r   normalize_noder   r   position_embeddingsegment_ids	input_ids
mask_inputr   castedr   
embed_nodes               r   create_embedding_subgraph)BertOnnxModelTF.create_embedding_subgraph   s   ++,=>KKRS''7	KKPQ));*BC
KKQR%J?**<8
++JC  +00<<FF+J[J[[ $ > >y IF  -22>>HHKL]L]]"&"@"@"MF  ,11==GG;K\K\\!%!?!?
!KF,,^<[[**%"!$$Q'$$Q'	 ". + 
 ,
''(=(=a(@,Oj!r   c                    [         R                  S5        U R                  5       nU R                  S5      nU GH5  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  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c  GM  UR                  S
:X  d  GM%  U R                  U5      n[        U5      S:w  a&  [         R!                  SR                  U5      5          gSnSnUR#                  5        H_  u  nnUS   S:X  a)  Un[         R                  SR                  UU5      5        M8  Un[         R                  SR                  UU5      5        Ma     Ub  Uc  [         R                  S5          g[         R                  S5        U R%                  X=X5        U R'                  5           g   g)z=
Automatically detect word, segment and position embeddings.
z#start processing embedding layer...LayerNormalization)r   r+   r2   )r   r4   r   Nr   r=   z+Found position embedding. name:{}, shape:{}z4Failed to find position embedding. name:{}, shape:{}r   zHFailed to find two embeddings (word and segment) from Add node. Found {}z*Found segment embedding. name:{}, shape:{}z(Found words embedding. name:{}, shape:{}z.Failed to find both word and segment embeddingzCreate Embedding node)r"   r#   output_name_to_nodeget_nodes_by_op_typer?   rq   r   r   rr   r$   rs   re   r:   
get_parentr   ry   warningitemsr   ri   )r   r   layer_norm_nodeslayer_norm_nodepos_embed_pathrD   reshape_noderO   rw   rx   r   first_parent
embeddingsr   r   r:   rs   s                    r   process_embedding!BertOnnxModelTF.process_embedding   s+    	9:"668445IJ/O!33OE`bk4GIN%1?.HJ..z/?/?/BCK"((5D4::!#IPPQ\QaQacgcmcmno%0%5%5"RYYZeZjZjlplvlvwx??8Q8KLL'L,@,@E,I!KKLY
z?a'NNbiijtuw!%$(!#-#3#3#5KD%Qx1},0)$P$W$WX\^c$de)-$N$U$UVZ\a$bc $6 ")->-FKK PQ34..Pav  "[  0r   c                 d    U R                  5         U R                  5         U R                  5         g r   )r'   r   rZ   r   s    r   
preprocessBertOnnxModelTF.preprocess*  s$     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_nodesattention_nodepathreshapeembeds         r   %remove_reshape_before_first_attention5BertOnnxModelTF.remove_reshape_before_first_attention0  s    33K@-N)).9F_:`cdfgbhiD|KKEF!NG++GNN1,=w}}Q?OPW% .r   c                 D    U R                  5         U R                  5         g r   )r   ri   r   s    r   postprocessBertOnnxModelTF.postprocess<  s    224r   )r   )__name__
__module____qualname____firstlineno___BertOnnxModelTF__initr'   rZ   rE   ry   r   r   r   r   r   r   r   r   __static_attributes____classcell__)r   s   @r   r	   r	      sO    8G2h-Y^   /"b5n
 r   r	   )loggingr@   sysargparsenumpynpcollectionsr   r   r   r   r   	getLoggerr   r"   r	    r   r   <module>r      s@      
    6 6 '			8	$lm lr   