+
    9iM                        ^ RI Ht ^ RIHt ^ RIHtHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHtHt ^ RIHtHt ^ RIHt ^ RIH t  ^ RI!H"t" ^ RI#H$t$ ^ RI%H&t& ^ RI'H(t( ^ RI)H*t* ^ RI+H,t, ^ RI-H.t.H/t/ ^ RI0H1t1H2t2 ^ RI3H4t4 ^ RI5H6t6H7t7H8t8 ^ RI9H:t: ]! ];4      t< ! R R]:4      t=R# )    )	getLogger)PackingMode)AttentionMaskFusionAttention)FusionBartAttention)FusionBiasGelu)FusionConstantFold)FusionEmbedLayerNormalization)FusionFastGelu)
FusionGelu)FusionGeluApproximation)FusionGemmFastGelu)FusionLayerNormalizationFusionLayerNormalizationTF)AttentionMaskFormatFusionOptions)FusionQOrderedAttention)FusionQOrderedGelu) FusionQOrderedLayerNormalization)FusionQOrderedMatMul)FusionQuickGelu)FusionReshape)FusionRotaryEmbeddings)FusionShape)"FusionSimplifiedLayerNormalization&FusionSkipSimplifiedLayerNormalization) FusionBiasSkipLayerNormalizationFusionSkipLayerNormalization)FusionUtils)
ModelProtoTensorProtohelper)	OnnxModelc                   .  a a ] tR t^%t oR$V3R lV 3R llltR tR tR tR tR t	R t
R	 tR
 tR tR tR tR tR%R ltR tR tR tV3R lR ltV3R lR ltR tR&R ltR tR tR tR tR'V3R lR lltR tR(R  ltR)V3R! lR" llt R#t!Vt"V ;t## )*BertOnnxModelc                ,   < V ^8  d   QhRS[ RS[RS[/# )   model	num_headshidden_size)r    int)format__classdict__s   "f/var/www/html/photoedit/myenv/lib/python3.14/site-packages/onnxruntime/transformers/onnx_model_bert.py__annotate__BertOnnxModel.__annotate__&   s"     ' 'j 'S '3 '    c                  < V^ 8X  d   V^ 8X  g   V^ 8  d   W2,          ^ 8X  g   Q h\         SV `  V4       W n        W0n        \	        V 4      V n        \        W P                  V P                  V P
                  4      V n        \        W P                  V P                  V P
                  4      V n	        \        V 4      V n        R# )a  Initialize BERT ONNX Model.

Args:
    model (ModelProto): the ONNX model
    num_heads (int, optional): number of attention heads. Defaults to 0 (detect the parameter automatically).
    hidden_size (int, optional): hidden dimension. Defaults to 0 (detect the parameter automatically).
N)super__init__r)   r*   r   attention_maskr   attention_fusionr   qordered_attention_fusionr   utils)selfr(   r)   r*   	__class__s   &&&&r.   r4   BertOnnxModel.__init__&   s     Q;!#3Q;KbfgKghh"&+D1 /6F6FX\XkXk l)@""DNND4G4G*
& !&
r1   c                <    \        V 4      pVP                  4        R # N)r	   applyr9   fusions   & r.   fuse_constant_fold BertOnnxModel.fuse_constant_fold;       #D)r1   c                n    V P                   P                  4        V P                  P                  4        R # r=   )r6   r>   r7   r9   s   &r.   fuse_attentionBertOnnxModel.fuse_attention?   s&    ##%&&,,.r1   c                    \        V 4      pVP                  4        \        V 4      pVP                  4        \        V 4      pVP                  4        \	        V 4      pVP                  4        R # r=   )r   r>   r   r   r   r?   s   & r.   	fuse_geluBertOnnxModel.fuse_geluD   sN    D!% &#D)r1   c                <    \        W4      pVP                  4        R # r=   )r   r>   )r9   is_fastgelur@   s   && r.   fuse_bias_geluBertOnnxModel.fuse_bias_geluO   s    2r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   gelu_approximation BertOnnxModel.gelu_approximationS   s    (.r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_gemm_fast_gelu!BertOnnxModel.fuse_gemm_fast_geluW   rC   r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_add_bias_skip_layer_norm+BertOnnxModel.fuse_add_bias_skip_layer_norm[   s    1$7r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_reshapeBertOnnxModel.fuse_reshape_   s    t$r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   
fuse_shapeBertOnnxModel.fuse_shapec   s    T"r1   c                <    \        W4      pVP                  4        R # r=   )r
   r>   )r9   use_mask_indexr@   s   && r.   fuse_embed_layerBertOnnxModel.fuse_embed_layerg   s    .tDr1   c                    \        V 4      pVP                  4        \        V 4      pVP                  4        \        V 4      pVP                  4        R # r=   )r   r>   r   r   r?   s   & r.   fuse_layer_normBertOnnxModel.fuse_layer_normk   s=    )$/+D1 2$7r1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_simplified_layer_norm(BertOnnxModel.fuse_simplified_layer_normv   s    3D9r1   c                >    \        WR 7      pVP                  4        R# ))shape_inferN)r   r>   )r9   ri   r@   s   && r.   fuse_skip_layer_norm"BertOnnxModel.fuse_skip_layer_normz   s    -dLr1   c                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_skip_simplified_layer_norm-BertOnnxModel.fuse_skip_simplified_layer_norm~   s    7=r1   c                   \        V 4      pVP                  4        \        \        R  V P                  P
                  P                  4      4      pV Uu0 uF  q3P                  kK  	  pp^ pV\        V P                  P                  4      8  dr   V P                  P                  V,          pRVP                  9   d9   VP                  V9  d(   V P                  P                  P                  V4       K  V^,          pK  R# u upi )c                 N    V P                   R 8H  ;'       d    V P                  R8g  # )RotaryEmbeddingcom.microsoft)op_typedomain)nodes   &r.   <lambda>6BertOnnxModel.fuse_rotary_embeddings.<locals>.<lambda>   s#    T\\->>aa4;;RaCaar1   rq   N)r   r>   listfilterr(   graphru   rt   len	functionsnameremove)r9   r@   rot_emb_nodesru   non_ms_domains_to_keepifns   &      r.   fuse_rotary_embeddings$BertOnnxModel.fuse_rotary_embeddings   s    '-a

  %%
 ;H!H-$++-!H#djj**++%%a(B BGG+		AW0W

$$++B/Q , "Is   Dc                <    \        V 4      pVP                  4        R # r=   )r   r>   r?   s   & r.   fuse_qordered_mamtul"BertOnnxModel.fuse_qordered_mamtul   s    %d+r1   c                <   < V ^8  d   QhRS[ RS[S[,          RS[/# )r'   rs   input_indicescasted)strrx   r+   bool)r,   r-   s   "r.   r/   r0      s(      s 4PS9 ^b r1   c                F   . pV P                  4       pV P                  V4      pV F  pV Uu. uF1  q\        VP                  4      8  g   K  VP                  V,          NK3  	  p	pV	 F  p
V P	                  V
4      '       d   V'       g   VP                  V
4       K5  K7  W9   g   K?  WZ,          pVP                  R8X  g   KZ  V P	                  VP                  ^ ,          4      f   K  V'       g   K  VP                  VP                  ^ ,          4       K  	  K  	  V# u upi )z
Get graph inputs that feed into node type (like EmbedLayerNormalization or Attention).
Returns a list of the graph input names based on the filter whether it is casted or not.
Cast)output_name_to_nodeget_nodes_by_op_typer{   inputfind_graph_inputappendrs   )r9   rs   r   r   graph_inputsr   nodesru   r   bert_inputs
bert_inputparents   &&&&        r.   get_graph_inputs_from_node_type-BertOnnxModel.get_graph_inputs_from_node_type   s    
 "668))'2D2?W-Qs4::CV=4::a==-KW)
((44!$++J7 "60<F~~/D4I4I&,,WX/4Z4f!6(//Q@ *   Xs   DDc                    < V ^8  d   QhRS[ /# )r'   r   r   )r,   r-   s   "r.   r/   r0      s       r1   c                d    V P                  R . ROV4      pW P                  R^.V4      ,          pV# )EmbedLayerNormalization	Attention)r         )r   )r9   r   inputss   && r.   !get_graph_inputs_from_fused_nodes/BertOnnxModel.get_graph_inputs_from_fused_nodes   s7    556OQZ\bc66{QCPPr1   c                   V P                  4       p^ p^ pVP                   FH  pV P                  V\        P                  4      w  rVV'       d
   V^,          pV\        V4      ,          pKJ  	  \        P                  RV RV R24       R# )zPChange data type of all graph inputs to int32 type, and add Cast node if needed.z)Graph inputs are changed to int32. Added z Cast nodes, and removed z Cast nodes.N)rz   r   change_graph_input_typer!   INT32r{   loggerinfo)r9   rz   add_cast_countremove_cast_countgraph_inputnew_noderemoved_nodess   &      r.   change_graph_inputs_to_int32*BertOnnxModel.change_graph_inputs_to_int32   s    

 ;;K&*&B&B;P[PaPa&b#H!#]!33	 '
 	77GG`ar`ss  A	
r1   c                p   V P                  RR7      V P                  RR7      ,           pV P                  P                  P                   F  pVP                  V9   g   K  VP
                  P                  P                  P                  ^ ,          pWn	        Vf   KS  VP
                  P                  P                  P                  ^,          pW%n	        K  	  V P                  P                  P                   F:  pVP
                  P                  P                  P                  ^ ,          pWn	        K<  	  R# )z4
Update input and output shape to use dynamic axes.
T)r   FN)r   r(   rz   r   r}   typetensor_typeshapedim	dim_paramoutput)r9   dynamic_batch_dimdynamic_seq_lenbert_graph_inputsr   	dim_protor   s   &&&    r.   use_dynamic_axesBertOnnxModel.use_dynamic_axes   s     !BB C 
22%2@A ZZ%%++Ezz..!JJ2288<<Q?	&7#". %

 6 6 < < @ @ CI*9' , jj&&--F//5599!<I"3 .r1   c                &    V P                  4        R # r=   )adjust_reshape_and_expandrE   s   &r.   
preprocessBertOnnxModel.preprocess   s    &&(r1   c                   . pV P                  4        EF  pVP                  R 8X  g   K  V P                  VP                  ^,          4      pVeZ   VP                  ^ 8X  dI   VP                  V.4       V P                  VP                  ^ ,          VP                  ^ ,          4       K  V P                  V. RO. ROV P                  4       4      pVf   K  VR,          pV P                  VP                  ^,          4      pVR,          pV P                  VP                  ^,          4      pVR,          p	Vf   EK(  Vf   EK/  \        V4      ^8X  g   EKB  \        V4      ^8X  g   EKU  V^,          V^ ,          8X  g   EKm  V	P                  ^ ,          VP                  ^ &   EK  	  V'       d5   V P                  V4       \        P                  R\        V4       24       R# R# )ReshapeNz"Removed Reshape and Expand count: )Expandr   r   Slice)r   r   r   r   )r   rs   get_constant_valuer   sizeextendreplace_input_of_all_nodesr   match_parent_pathr   r{   remove_nodesr   r   )
r9   nodes_to_removeru   reshape_shapereshape_pathexpand_nodeexpand_shape_valuereshape_before_expandshape_value
slice_nodes
   &         r.   r   'BertOnnxModel.adjust_reshape_and_expand   s   JJLD||y( !% 7 7

1 F ,1C1Cq1H#**D6233DKKNDJJqMR  $55< ,,.	   +".r"2K)-)@)@ARARSTAU)V&,8,<)"&"9"9:O:U:UVW:X"YK!-b!1J*6'3 23q8,1.q1[^C(2(9(9!(<

1C !F o.KK<S=Q<RST r1   c                   V P                  4       p. pV P                  4        EF  pR ^R^ R^/pVP                  V9   d   WCP                  ,          pV P                  V. ROV^ ^ ^ ^ ^ .V4      pVe{   Vw  ppp	p
ppVP                  ^ ,          V P                  4       P                  ^ ,          P                  8X  d1   VP                  ^ ,          VP                  ^ &   V P                  4       pVP                  R8X  g   K  V P                  V. R	O. R
OV4      pVf   K  VR,          P                  ^ ,          V P                  4       P                  ^ ,          P                  8X  g   EKE  \        P                  ! RVP                  ^ \        VP                  4      ^,
           VP                  VP                  R,           R7      pRVn        VP                  P                  \        P                  ! RV P                  4      .4       V P!                  WP#                  V4      P                  4       VP%                  V4       EK!  	  V P'                  V4       R# )r   	ReduceSumr   N_remove_mask)r   outputsr}   rr   r)   )r   ConstantOfShapeConcat	UnsqueezeGatherShape)r   r   r   r   )   r   r   r   r   )r   r   rs   r   r   rz   r}   r   r"   	make_noder{   rt   	attributer   make_attributer)   add_nodeget_graph_by_noder   r   )r9   r   r   ru   op_input_idr   parent_nodescastconstantOfShapeconcat	unsqueezegatherr   attention_nodes   &             r.   clean_graphBertOnnxModel.clean_graph  s   "668JJLD 5aaVWXK||{*-#55 1aA&'   + %'!{{1~););A)>)C)CC38<<?--a0.2.F.F.H+||{*
  $55E '	   +#B'--a0DJJL4F4Fq4I4N4NN)/)9)9'#'::a#djj/A2E#F$(KK!%^!;	* 1@-&00779N9N{\`\j\j9k8lmn6L6L^6\6a6ab'..t4y !z 	/*r1   c                F    V P                  4        V P                  4        R # r=   )r   prune_graphrE   s   &r.   postprocessBertOnnxModel.postprocessF  s    r1   c                4   < V ^8  d   QhRS[ R,          RS[/# )r'   optionsNadd_dynamic_axes)r   r   )r,   r-   s   "r.   r/   r0   J  s(     RB RB 4 RBt RBr1   c                V   Ve#   VP                   '       g   V P                  4        V P                  P                  4        V P                  P	                  4        V P                  4        Ve   VP                  '       d!   V P                  4        V P                  4        Ve   VP                  '       d   V P                  4        V P                  4        V P                  4        Ve   VP                  '       d,   V P                  VP                   4       V P                  4        Ve   VP                   '       d   V P#                  4        Ve   V P$                  P'                  VP(                  4       VP*                  '       d]   \-        V P.                  \0        4      '       g=   \3        V V P4                  V P6                  V P$                  VP*                  4      V n        Ve   VP8                  '       d   V P;                  4        Ve   VP<                  '       d   V P?                  4        V PA                  4        Ve   VPB                  '       d/   VP(                  \D        PF                  8H  pV PI                  V4       V P                  PK                  4        V PM                  4        Ve   VPN                  '       d%   V PQ                  RR7       V PQ                  RR7       Ve   VPR                  '       d   V PU                  4        Ve#   VPV                  '       d   V PY                  4        Ve#   VPZ                  '       d   V P]                  4        V P_                  4        V'       d   V Pa                  4        \b        Pe                  RV Pg                  4        24       R # )NT)rL   Fzopset version: )4enable_shape_inferencedisable_shape_inferencer8   remove_identity_nodesremove_useless_cast_nodesrA   enable_layer_normrc   rf   enable_gelurI   r   rY   enable_skip_layer_normrj   rm   enable_rotary_embeddingsr   r5   set_mask_formatattention_mask_formatuse_multi_head_attention
isinstancer6   r   r   r*   r)   enable_attentionrF   enable_qordered_matmulr   r\   enable_embed_layer_normr   MaskIndexEndr`   remove_useless_reshape_nodesr   enable_bias_gelurM   enable_bias_skip_layer_normrV   enable_gelu_approximationrP   enable_gemm_fast_gelurS   remove_unused_constantr   r   r   get_opset_version)r9   r   r   r_   s   &&& r.   optimizeBertOnnxModel.optimizeJ  s   )G)G)G((*

((* 	

,,. 	!O 9 9 9  "++-O 3 3 3NNO > > >%%g&D&DE002O @ @ @'')//0M0MN///
4CXCXZm8n8n(7$$NN''44)% O 8 8 8! O > > >%%'O ? ? ?$::>Q>^>^^N!!.1 	

//1 O 8 8 8D1E2O C C C..07#D#D#D##%7#@#@#@$$&##% !!#od&<&<&>%?@Ar1   c                    / p. ROp. ROpW#,            F!  pV P                  V4      p\        V4      W&   K#  	  \        P                  RV 24       V# )z(
Returns node count of fused operators.
zOptimized operators: )r   r   MultiHeadAttentionGeluFastGeluBiasGeluGemmFastGeluLayerNormalizationSimplifiedLayerNormalizationSkipLayerNormalization SkipSimplifiedLayerNormalizationrq   )QOrderedAttentionQOrderedGeluQOrderedLayerNormalizationQOrderedMatMul)r   r{   r   r   )r9   op_countopsq_opsopr   s   &     r.   get_fused_operator_statistics+BertOnnxModel.get_fused_operator_statistics  s]     

 ++B--b1Eu:HL  	+H:67r1   c                  a Sf   V P                  4       oR V3R llpV! R4      pV! R4      V! R4      ,           V! R4      ,           pV! R4      V! R4      ,           V! R	4      ,           pV! R
4      V! R4      ,           pV! R4      V! R4      ,           pV^ 8  ;'       d:    V^ 8  ;'       d-    WE8H  ;'       d!    V^V,          8  ;'       g    V^V,          8  pV^ 8X  d   \        P                  R4       V^ 8X  d   \        P                  R4       V^ 8X  d   \        P                  R4       V^ 8X  d   \        P                  R4       V^ 8X  d   \        P                  R4       V# )z1
Returns True when the model is fully optimized.
c                $    V ^8  d   QhR\         /# )r'   op_name)r   )r,   s   "r.   r/   6BertOnnxModel.is_fully_optimized.<locals>.__annotate__  s     	4 	4c 	4r1   c                 :   < SP                  V 4      ;'       g    ^ # )r   )get)r(  fused_op_counts   &r.   r   2BertOnnxModel.is_fully_optimized.<locals>.op_count  s    !%%g.33!3r1   r   r   r  r  r  r  r  r  r  r  r  zLayer Normalization not fusedz$Simple Layer Normalization not fusedzGelu (or FastGelu) not fusedz!EmbedLayerNormalization not fusedz+Attention (or MultiHeadAttention) not fused)r$  r   debugwarning)	r9   r,  r   embed	attentiongelu
layer_normsimple_layer_norm
is_perfects	   &f       r.   is_fully_optimized BertOnnxModel.is_fully_optimized  sg    !!??AN	4 	4 23[)H5I,JJXViMjj	(:"66*9MM23h?W6XX
$%CDxPrGss QY X XQX X"X X I-VV3DI3U	 	 ?LL89!LL?@19LL78A:LL<=>NNHIr1   c                    < V ^8  d   QhRS[ /# )r'   use_symbolic_shape_inferr   )r,   r-   s   "r.   r/   r0     s     7 7 7r1   c                >    \        V 4      pVP                  V4       R # r=   )r   convert)r9   r9  packing_modes   && r.   convert_to_packing_mode%BertOnnxModel.convert_to_packing_mode  s    "4(56r1   )r6   r5   r*   r)   r7   r8   )r   r   )T)
batch_sizemax_seq_len)NFr=   )F)$__name__
__module____qualname____firstlineno__r4   rA   rF   rI   rM   rP   rS   rV   rY   r\   r`   rc   rf   rj   rm   r   r   r   r   r   r   r   r   r   r   r  r$  r6  r=  __static_attributes____classdictcell____classcell__)r:   r-   s   @@r.   r%   r%   %   s     ' '*/
		( , 

4('UR@+DRB RBh@&P7 7 7r1   r%   N)>loggingr   r=  r   fusion_attentionr   r   fusion_bart_attentionr   fusion_biasgelur   fusion_constant_foldr	   fusion_embedlayerr
   fusion_fastgelur   fusion_gelur   fusion_gelu_approximationr   fusion_gemmfastgelur   fusion_layernormr   r   fusion_optionsr   r   fusion_qordered_attentionr   fusion_qordered_gelur   fusion_qordered_layernormr   fusion_qordered_matmulr   fusion_quickgelur   fusion_reshaper   fusion_rotary_attentionr   fusion_shaper   fusion_simplified_layernormr   r   fusion_skiplayernormr   r   fusion_utilsr   onnxr    r!   r"   
onnx_modelr#   rA  r   r%    r1   r.   <module>rb     sp     / ; 5 * 3 ; * " = 2 Q = = 3 F 7 , ( : $ r _ $ 0 0  	8	C7I C7r1   