
    `i-&                         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                   :   ^  \ rS rSrU 4S jrS rS rS rSrU =r	$ )Gpt2OnnxModel   c                 &   > [         TU ]  XU5        g )N)super__init__)selfmodel	num_headshidden_size	__class__s       k/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxruntime_tools/transformers/Gpt2OnnxModel.py__initGpt2OnnxModel.__init   s    ;7    c           	         [         R                  S5        U R                  5       nU R                  5       nSnU R	                  S5       GH  n/ nU R                  U/ SQ/ SQUUS9nUc  M"  Uu  pxppnUR                  SUS   -
     nU R                  U/ S	Q/ S
Q5      nUc  [         R                  S5        Mo  Uu  nnnnnnU R                  USU5      nUb  UR                  S:w  a$  [         R                  SUR                   35        M  UUR                  ;  a  [         R                  S5        M  U R                  U/ SQ/ SQ5      nUb^  Uu  nnnnnU R                  U/ SQ/ SQ5      nUc  [         R                  S5        GM=  US   nUU:w  a  [         R                  S5        GM`  OU 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c  [         R                  S5        GM  US   nUU:w  a  [         R                  S5        GM  U R                  U/ SQ/ SQ5      n U c  [         R                  S5        GM!  U u  n!n"n#UU#:w  a  [         R                  S5        GME  U R                  U/ SQ/ SQ5      n$U$c  [         R                  S5        GMw  U$u  n%n&n'UU':w  a  [         R                  S 5        GM  U R                  UU	UR                  S   UR                  S   US:H  5        US-  nGM     U R                  5         [         R                  S!U 35        g)"z2
Fuse Attention subgraph into one Attention node.
zstart attention fusion...r   LayerNormalization)AddReshapeGemmr   r   	TransposeMatMul)r   Nr   r   r   r   r   )output_name_to_nodereturn_indiceN   )r   r   Splitr   r   r   )r    r   r   r   r   r   z&fuse_attention: failed to match v pathz)failed to get layernorm before gemm. Got z4Add and LayerNormalization shall have one same input)SoftmaxSubMulDivr   )r   r   r   r   r   )
r$   r#   Slicer&   	Unsqueezer#   Squeezer&   Shaper%   )
r    r   r    r   r    r   r   r   r   r   z)fuse_attention: failed to match mask pathz-fuse_attention: skip since div_qk != div_mask)r"   Wherer%   r   )r   r   r    r   z'fuse_attention: failed to match qk path)	Castr&   r&   r'   r#   r(   r&   r)   r%   )	r   r   r   r    r   r   r   r   r   )r   r   r!   )r   r   r   z&fuse_attention: failed to match q pathz-fuse_attention: skip since split_v != split_q)r    r   r   z&fuse_attention: failed to match k pathz-fuse_attention: skip since split_v != split_kzFused Attention count:)loggerdebuginput_name_to_nodesr   get_nodes_by_op_typematch_parent_pathinput
get_parentop_typecreate_attention_nodeoutputprune_graphinfo)(r   r/   r   attention_countnormalize_noder   	qkv_nodesadd_qkvreshape_qkvgemm_qkv	reshape_1	reshape_2transpose_qkv
matmul_qkvanother_inputv_nodestranspose_v	reshape_vsplit_vreshape_after_gemmgemmreshape_before_gemmlayernorm_before_attentionqk_nodes
softmax_qksub_qkmul_qkdiv_qk	matmul_qk
mask_nodesdiv_maskwhere_qkq_nodestranspose_q	reshape_qsplit_qk_nodestranspose_k	reshape_ksplit_ks(                                           r   fuse_attentionGpt2OnnxModel.fuse_attention   sl    	02"668"668"778LMNM..W($7+ / I  `i]W8	R\#MM!mA.>*>?M,,O"$G EF_f\[)W.@$H[)-9LaQd)e&)15O5W5W[o5oHIcIkIkHlmn $>$D$DDST--j:dfuvH#BJ?VVVY!33lik
 %LL!LM%b>X%LL!PQ &
  11*>ceqr#LL!JK<D9Xvy!33fbd
 %LL!LM%b>X%LL!PQ,,Y8Y[deGEF07-[)W'!LM,,Y8Y[deGEF07-[)W'!LM&&tX7Q7X7XYZ7[]h]o]opq]r'6!';= q Ou Nx 	,_,=>?r   c                    U R                  S5      n[        R                  R                  SX1R                  S   UR                  S   /US-   /US9nSUl        UR                  R                  [        R                  R                  SU R                  5      [        R                  R                  SS5      /5        [        R                  R                  S	US-   UR                  S   /US
-   /US-   S9n[        R                  R                  SUS
-   UR                  S   /U/US-   S9n	U R                  U5        U R                  U5        U R                  U	5        g )N	Attentionr       _outputinputsoutputsnamezcom.microsoftr   unidirectionalr   _matmul_output_matmulr   _add)create_node_nameonnxhelper	make_noder2   domain	attributeextendmake_attributer   add_node)
r   rI   r>   r2   r6   add_graph_inputattention_node_nameattention_nodematmul_noders   s
             r   r5   #Gpt2OnnxModel.create_attention_node   s^   "33K@..{7<jjmTZZXY]6[8Ki8W7X4G / I !0  ''[[''T^^D[[''(8!<>	? kk++H4G)4SU]UcUcdeUf3g5HK[5[4\1Dy1P , R
 ;;((1DGW1WYaYgYghiYj0k28.AF.J ) L
 	n%k"hr   c                 l   [         R                  S5        U R                  5       nU R                  5       nSnU R	                  S5       GH?  nU R                  USUSS9n/ nU R                  USS/SS/U5      nUc  U R                  USS	/SS/U5      nUc  MP  Uu  pU R                  S
S5      n
[        R                  R                  S
U
S-   UR                  S   /U
S-   /U
S9nU R                  SS5      n[        R                  R                  SU
S-   UR                  S   /US-   /US9nU
S-   U	R                  S'   U R                  UR                  S   US-   5        U R                  U5        U R                  U5        US-  nGMB     U R                  5         [         R!                  SU 35        g)z
Remove extra reshape nodes.
zstart postprocessing...r   r   r   F)	recursiveFastGeluNr   r   FullyConnect_MatMul_inputr    rb   rc   r   FullyConnect_Addra   z"postprocess: remove Reshape count:)r-   r.   r/   r   r0   find_first_child_by_typer1   rk   rl   rm   rn   r2   r6   replace_input_of_all_nodesrs   r7   r8   )r   r/   r   reshape_count	gemm_noderH   r   nodesrJ   	root_nodematmul_node_namerw   add_node_namers   s                 r   postprocessGpt2OnnxModel.postprocess   s    	.0"668"668226:I!%!>!>y?H?RIN "? "P
 M**9y*6MPQSTvWjkE}..y9FZ:[^_ab]c/BD=/4, #44X?TU++//8H88SU^UdUdefUg7h9II9U8V5E 0 GK
 !11%9KLM{{,,U5E	5QS\SbSbcdSe4f6Ci6O5P2? - AH
 #3X"=IQ++,>,E,Ea,H-ZcJcdMM+&MM(#QMC ;F 	8HIr    )
__name__
__module____qualname____firstlineno___Gpt2OnnxModel__initr]   r5   r   __static_attributes____classcell__)r   s   @r   r	   r	      s!    8h@T 2.J .Jr   r	   )loggingrl   sysargparsenumpynpcollectionsr   r   r   r   r   	getLoggerr   r-   r	   r   r   r   <module>r      sB   
   
    6 6 '			8	$uJM uJr   