o
    ˜º¥gm  ã                   @   s"   d dl Z d dlZG dd„ dƒZdS )é    Nc                   @   sr   e Zd ZdZdejfdd„Zdeddfdd	„Zd
e	ddfdd„Z
d
e	dededdfdd„Zddd„Zddd„ZdS )ÚDynamoOnnxHelperzK
    Helper class for processing ONNX models exported by torch Dynamo.
    Úmodelc                 C   s
   || _ d S )N)r   )Úselfr   © r   úb/var/www/visachat/venv/lib/python3.10/site-packages/onnxruntime/transformers/dynamo_onnx_helper.pyÚ__init__   s   
zDynamoOnnxHelper.__init__Úedge_mappingÚreturnNc                 C   sÌ   | j jjD ]8}tt|jƒƒD ]}|j| |v r!||j|  |j|< qtt|jƒƒD ]}|j| |v r<||j|  |j|< q)q| j jjD ]}|j|v rP||j |_qC| j jjD ]}|j|v rc||j |_qVdS )zP
        Updates the edges in the model according to the given mapping.
        N)r   ÚgraphÚnodeÚrangeÚlenÚinputÚoutputÚname)r   r   r   ÚiÚgraph_inputÚgraph_outputr   r   r   Úupdate_edges   s&   €€þ
€
€þzDynamoOnnxHelper.update_edgesÚ	func_namec                 C   sH  t  d|› d¡ g }g }g }g }| jjjD ]}|j|kr/| |¡ | t|j	ƒt|j
ƒ ¡ qd}| jjD ]}|j|krT| t|jƒ¡ | t|j	ƒt|j
ƒ ¡ |}q6t|ƒt|ƒks_J ‚|D ]
}| jjj |¡ qa|D ]
}| jjj |¡ qn|dur„| jj |¡ i }	tt|ƒƒD ]}
||
 }||
 }||krž||	|< qŒ|  |	¡S )zH
        Unrolls the function with the given name in the model.
        zUnrolling function z...N)ÚloggingÚinfor   r
   r   Úop_typeÚappendÚextendÚlistr   r   Ú	functionsr   r   Úremover   r   )r   r   Únodes_to_removeÚnodes_to_addÚedges_to_removeÚedges_to_addr   Úfunc_to_removeÚfr   r   ÚkÚvr   r   r   Úunroll_function%   s@   

€
€€
z DynamoOnnxHelper.unroll_functionÚinput_idÚ	output_idc                 C   sn   i }g }| j jjD ]}|j |¡dkr"|j| ||j| < | |¡ q	|D ]
}| j jj |¡ q%|  	|¡ dS )z4
        Removes the function in the model.
        éÿÿÿÿN)
r   r
   r   r   Úfindr   r   r   r   r   )r   r   r'   r(   r   r   r   r   r   r   Úremove_functionL   s   
€z DynamoOnnxHelper.remove_functionc                 C   s   t  d¡ |  ddd¡ dS )z9
        Removes the dropout layer in the model.
        zRemoving dropout layer...ÚDropoutr   N©r   r   r+   ©r   r   r   r   Úremove_dropout_layer[   s   
z%DynamoOnnxHelper.remove_dropout_layerc                 C   s   t  d¡ |  ddd¡ dS )z9
        Removes the LM head layer in the model.
        zRemoving LM head layer...ÚLinear_lm_headé   r   Nr-   r.   r   r   r   Úremove_lm_head_layerb   s   
z%DynamoOnnxHelper.remove_lm_head_layer)r	   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚonnxÚ
ModelProtor   Údictr   Ústrr&   Úintr+   r/   r2   r   r   r   r   r   
   s    '
r   )r   r7   r   r   r   r   r   Ú<module>   s   