U
    T?hm                     @   s"   d dl Z d dlZG dd dZdS )    Nc                   @   sp   e Zd ZdZejdddZeddddZe	dd	d
dZ
e	eeddddZddddZddddZdS )DynamoOnnxHelperzK
    Helper class for processing ONNX models exported by torch Dynamo.
    modelc                 C   s
   || _ d S )Nr   )selfr    r   ]/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/transformers/dynamo_onnx_helper.py__init__   s    zDynamoOnnxHelper.__init__N)edge_mappingreturnc                 C   s   | j jjD ]p}tt|jD ]&}|j| |kr||j|  |j|< qtt|jD ]&}|j| |krR||j|  |j|< qRq
| j jjD ]}|j|kr||j |_q| j jjD ]}|j|kr||j |_qd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   iZgraph_inputZgraph_outputr   r   r   update_edges   s    

zDynamoOnnxHelper.update_edges)	func_namer
   c                 C   sN  t d| d g }g }g }g }| jjjD ]2}|j|kr,|| |t|j	t|j
  q,d}| jjD ]<}|j|krl|t|j |t|j	t|j
  |}qlt|t|kst|D ]}| jjj| q|D ]}| jjj| q|dk	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   Z	functionsr   r   AssertionErrorremover   r   )r   r   nodes_to_removeZnodes_to_addZedges_to_removeZedges_to_addr   Zfunc_to_removefr	   r   kvr   r   r   unroll_function%   s:    




z DynamoOnnxHelper.unroll_function)r   input_id	output_idr
   c                 C   sn   i }g }| j jjD ]2}|j|dkr|j| ||j| < || q|D ]}| j jj| qJ| 	| 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_function)r
   c                 C   s   t d | ddd dS )z9
        Removes the dropout layer in the model.
        zRemoving dropout layer...Z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...ZLinear_lm_head   r   Nr'   r(   r   r   r   remove_lm_head_layerb   s    
z%DynamoOnnxHelper.remove_lm_head_layer)__name__
__module____qualname____doc__onnxZ
ModelProtor   dictr   strr!   intr&   r)   r+   r   r   r   r   r   
   s   'r   )r   r0   r   r   r   r   r   <module>   s   