o
    ¢Äi  ã                   @   sn   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ e  e¡ZG dd	„ d	eƒZdS )
é    N)ÚFusionLayerNormalization)ÚFusionMultiHeadAttentionMMDit)ÚFusionOptions)Úis_installed)Ú
ModelProto)ÚBertOnnxModelc                       sx   e Zd Zddededef‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdde	dB de
fdd„Zdde	dB fdd„Zdd„ Z‡  ZS )ÚMmditOnnxModelr   ÚmodelÚ	num_headsÚhidden_sizec                    s>   |dkr|dks|dkr|| dksJ ‚t ƒ j|||d dS )ak  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__© úc/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/transformers/onnx_model_mmdit.pyr      s   (zMmditOnnxModel.__init__c                 C   s   |   ¡  |  ¡  d S ©N)Zprune_graphZremove_unused_constant)r   r   r   r   Úpostprocess   ó   zMmditOnnxModel.postprocessc                 C   s*   d}t  d¡ t| | dd}| ¡  d S )NTzwThe optimized model requires LayerNormalization with broadcast support. Please use onnxruntime-gpu>=1.21 for inference.)Zcheck_constant_and_dimensionÚforce)ÚloggerÚwarningr   Úapply)r   Zlayernorm_support_broadcastÚfusionr   r   r   Úfuse_layer_norm"   s   ÿÿzMmditOnnxModel.fuse_layer_normc                 C   s   t | ƒ}| ¡  d S r   )r   r   )r   r   r   r   r   Úfuse_multi_head_attention-   r   z(MmditOnnxModel.fuse_multi_head_attentionNFÚoptionsÚadd_dynamic_axesc                 C   sŽ   |rJ ‚t dƒr:dd l}ddlm} |ƒ  d}|jt|ƒddd}|  ||¡ W d   ƒ d S 1 s3w   Y  d S t d¡ |  |d ¡ d S )NÚtqdmr   )Úlogging_redirect_tqdmé   r   )ÚinitialZdescz<tqdm is not installed. Run optimization without progress bar)r   r   Ztqdm.contrib.loggingr    ÚrangeÚ	_optimizer   Úinfo)r   r   r   r   r    ZstepsÚprogress_barr   r   r   Úoptimize1   s   "ý
zMmditOnnxModel.optimizec                 C   sÐ   |d ur|j s|  ¡  | j ¡  |r| d¡ |d u s|jr&|  ¡  |  ¡  |r-| d¡ |d u s4|jr8|  	¡  |r?| d¡ |d u sF|j
rJ|  ¡  |rQ| d¡ |  ¡  |r\| d¡ t d|  ¡ › ¡ d S )Né   zopset version: )Zenable_shape_inferenceZdisable_shape_inferenceÚutilsZremove_useless_cast_nodesÚupdateZenable_layer_normr   Zfuse_simplified_layer_normZenable_geluZ	fuse_geluZenable_attentionr   r   r   r%   Zget_opset_version)r   r   r&   r   r   r   r$   @   s,   





zMmditOnnxModel._optimizec                 C   s@   i }g d¢}|D ]}|   |¡}t|ƒ||< qt d|› ¡ |S )z8
        Returns node count of fused operators.
        )ZFastGeluZMultiHeadAttentionZLayerNormalizationZSimplifiedLayerNormalizationzOptimized operators:)Zget_nodes_by_op_typeÚlenr   r%   )r   Zop_countÚopsÚopÚnodesr   r   r   Úget_fused_operator_statistics_   s   
z,MmditOnnxModel.get_fused_operator_statistics)r   r   )NF)NN)Ú__name__Ú
__module__Ú__qualname__r   Úintr   r   r   r   r   Úboolr'   r$   r/   Ú__classcell__r   r   r   r   r      s    r   )ÚloggingZfusion_layernormr   Zfusion_mha_mmditr   Zfusion_optionsr   Zimport_utilsr   Zonnxr   Zonnx_model_bertr   Ú	getLoggerr0   r   r   r   r   r   r   Ú<module>   s   
