U
    T?hä  ã                   @   sj   d dl Z 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 e  e¡ZG dd„ deƒZdS )	é    N)ÚFusionGptAttention)ÚFusionGptAttentionMegatron)ÚFusionGptAttentionNoPast)ÚFusionRotaryAttention)ÚBertOnnxModelc                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚGpt2OnnxModelc                    s   t ƒ  |||¡ d S )N)ÚsuperÚ__init__)ÚselfÚmodelÚ	num_headsÚhidden_size©Ú	__class__© úZ/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/transformers/onnx_model_gpt2.pyr	      s    zGpt2OnnxModel.__init__c                 C   s~   t | jjjƒdks$t | jjjƒdkr:t| | jƒ}| ¡  n(t| | jƒ}| ¡  t	| | jƒ}| ¡  t
| | j| jƒ}| ¡  d S )Né   )Úlenr   ÚgraphÚinputÚoutputr   r   Úapplyr   r   r   r   )r
   Zfusionr   r   r   Úfuse_attention   s    $
zGpt2OnnxModel.fuse_attentionc                 C   sœ  t  d¡ |  ¡ }|  ¡ }d}|  d¡D ]T}| j|d|dd}|  |ddgddg|¡}|dkr¢|  |dd	gddg|¡}|dkr¢|  |dd
gddg|¡}|dkr¢q(|\}}|  dd¡}	tj	j
d|	d |jd g|	d g|	d}
|  dd¡}tj	j
d|	d |jd g|d g|d}|  |jd |d ¡ |  |jd |	d ¡ |	d |jd< |  |jd |d ¡ |  |
¡ |  |¡ |d7 }q(|  ¡  t  d|› ¡ dS )z-
        Remove extra reshape nodes.
        zstart postprocessing...r   ZGemmZReshapeF)Ú	recursiveZFastGeluNZLayerNormalizationZSkipLayerNormalizationZMatMulZFullyConnect_MatMulÚ_inputr   Ú_output)ÚinputsÚoutputsÚnameÚAddZFullyConnect_Addé   z#postprocess: remove Reshape count: )ÚloggerÚdebugÚinput_name_to_nodesÚoutput_name_to_nodeZget_nodes_by_op_typeZfind_first_child_by_typeZmatch_parent_pathZcreate_node_nameÚonnxÚhelperZ	make_noder   Zreplace_input_of_all_nodesr   Úadd_nodeZprune_graphÚinfo)r
   r#   r$   Zreshape_countZ	gemm_nodeZreshape_after_gemmZnodesZreshape_before_gemmZ	root_nodeZmatmul_node_nameZmatmul_nodeZadd_node_namer'   r   r   r   Úpostprocess"   sh    
   ÿüüüü


zGpt2OnnxModel.postprocess)Ú__name__Ú
__module__Ú__qualname__r	   r   r)   Ú__classcell__r   r   r   r   r      s   r   )Úloggingr%   Zfusion_gpt_attentionr   Zfusion_gpt_attention_megatronr   Zfusion_gpt_attention_no_pastr   Zfusion_rotary_attentionr   Zonnx_model_bertr   Ú	getLoggerr*   r!   r   r   r   r   r   Ú<module>   s   
