
    h                         S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  \ R                  " \5      r " S S	\5      rg)
    N)FusionLayerNormalization)FusionMultiHeadAttentionMMDit)FusionOptions)is_installed)
ModelProto)BertOnnxModelc                      ^  \ rS rSrSS\S\S\4U 4S jjjrS rS rS r	SS
\
S	-  S\4S jjrSS
\
S	-  4S jjrS rSrU =r$ )MmditOnnxModel   model	num_headshidden_sizec                 Z   > US:X  a  US:X  d  US:  a  X2-  S:X  d   e[         TU ]  XUS9  g)aC  Initialize Multimodal Diffusion Transformer (MMDiT) 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).
r   )r   r   N)super__init__)selfr   r   r   	__class__s       c/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/onnx_model_mmdit.pyr   MmditOnnxModel.__init__   s<     Q;!#3Q;KbfgKghhM    c                 D    U R                  5         U R                  5         g N)prune_graphremove_unused_constant)r   s    r   postprocessMmditOnnxModel.postprocess   s    ##%r   c                 p    Sn[         R                  S5        [        X(       + SS9nUR                  5         g )NTzwThe optimized model requires LayerNormalization with broadcast support. Please use onnxruntime-gpu>=1.21 for inference.)check_constant_and_dimensionforce)loggerwarningr   apply)r   layernorm_support_broadcastfusions      r   fuse_layer_normMmditOnnxModel.fuse_layer_norm"   s9    &*#>	
 */NVZ
 	r   c                 :    [        U 5      nUR                  5         g r   )r   r"   )r   r$   s     r   fuse_multi_head_attention(MmditOnnxModel.fuse_multi_head_attention-   s    .t4r   Noptionsadd_dynamic_axesc                 6   U(       a   e[        S5      (       aH  SS KnSSKJn  U" 5          SnUR                  [	        U5      SSS9nU R                  X5        S S S 5        g [        R                  S5        U R                  US 5        g ! , (       d  f       g = f)Ntqdmr   )logging_redirect_tqdm   r$   )initialdescz<tqdm is not installed. Run optimization without progress bar)r   r-   tqdm.contrib.loggingr.   range	_optimizer    info)r   r*   r+   r-   r.   stepsprogress_bars          r   optimizeMmditOnnxModel.optimize1   sy    ###B&(#yyuqxyPw5 )(
 KKVWNN7D) )(s   .B


Bc                    Ub!  UR                   (       d  U R                  5         U R                  R                  5         U(       a  UR	                  S5        Ub  UR
                  (       a   U R                  5         U R                  5         U(       a  UR	                  S5        Ub  UR                  (       a  U R                  5         U(       a  UR	                  S5        Ub  UR                  (       a  U R                  5         U(       a  UR	                  S5        U R                  5         U(       a  UR	                  S5        [        R                  SU R                  5        35        g )N   zopset version: )enable_shape_inferencedisable_shape_inferenceutilsremove_useless_cast_nodesupdateenable_layer_normr%   fuse_simplified_layer_normenable_gelu	fuse_geluenable_attentionr(   r   r    r5   get_opset_version)r   r*   r7   s      r   r4   MmditOnnxModel._optimize@   s   )G)G((* 	

,,."O 9 9  "++-"O 3 3NN"O 8 8**,""od&<&<&>%?@Ar   c                     0 n/ SQnU H!  nU R                  U5      n[        U5      X'   M#     [        R                  SU 35        U$ )z(
Returns node count of fused operators.
)FastGeluMultiHeadAttentionLayerNormalizationSimplifiedLayerNormalizationzOptimized operators:)get_nodes_by_op_typelenr    r5   )r   op_countopsopnodess        r   get_fused_operator_statistics,MmditOnnxModel.get_fused_operator_statistics_   sR     
 B--b1Eu:HL  	*8*56r    )r   r   )NF)NN)__name__
__module____qualname____firstlineno__r   intr   r   r%   r(   r   boolr8   r4   rS   __static_attributes____classcell__)r   s   @r   r
   r
      sk    	Nj 	NS 	N3 	N 	N&	* 4 *t *B!5 B> r   r
   )loggingfusion_layernormr   fusion_mha_mmditr   fusion_optionsr   import_utilsr   onnxr   onnx_model_bertr   	getLoggerrV   r    r
   rU   r   r   <module>rf      s:     5 : ( %  )			8	$^] ^r   