
     Ngm                     0    d dl Z d dlZ G d d          ZdS )    Nc                   p    e Zd ZdZdej        fdZdeddfdZde	ddfd	Z
de	d
ededdfdZddZddZdS )DynamoOnnxHelperzK
    Helper class for processing ONNX models exported by torch Dynamo.
    modelc                     || _         d S )N)r   )selfr   s     g/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/transformers/dynamo_onnx_helper.py__init__zDynamoOnnxHelper.__init__   s    


    edge_mappingreturnNc                 "   | j         j        j        D ]}t          t	          |j                            D ],}|j        |         |v r||j        |                  |j        |<   -t          t	          |j                            D ],}|j        |         |v r||j        |                  |j        |<   -| j         j        j        D ]}|j        |v r||j                 |_        | j         j        j        D ]}|j        |v r||j                 |_        d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_outputs         r   update_edgeszDynamoOnnxHelper.update_edges   s5    J$) 	B 	BD3tz??++ @ @:a=L00$0A$?DJqM3t{++,, B B;q>\11%1$+a.%ADKNB  :+1 	B 	BK<//#/0@#A  J,3 	D 	DL L00$01B$C!	D 	Dr
   	func_namec                    t          j        d| d           g }g }g }g }| j        j        j        D ]^}|j        |k    rQ|                    |           |                    t          |j	                  t          |j
                  z              _d}| j        j        D ]r}|j        |k    re|                    t          |j                             |                    t          |j	                  t          |j
                  z              |}st          |          t          |          k    sJ |D ]&}| j        j        j                            |           '|D ]&}| j        j        j                            |           '|| j        j                            |           i }	t          t          |                    D ]}
||
         }||
         }||k    r||	|<   |                     |	          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vs                r   unroll_functionz DynamoOnnxHelper.unroll_function%   s    	99999:::J$) 	M 	MD|y((&&t,,,&&tDJ'7'7$t{:K:K'KLLL% 	# 	#Av""##DLL111##DMMDNN$BCCC!"?##s<'8'88888# 	/ 	/DJ!((....  	/ 	/DJ!((....%J ''777s?++,, 	$ 	$A"AQAAvv"#Q  ...r
   input_id	output_idc                 N   i }g }| j         j        j        D ]P}|j                            |          dk    r0|j        |         ||j        |         <   |                    |           Q|D ]&}| j         j        j                            |           '| 	                    |           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   s          r   remove_functionz DynamoOnnxHelper.remove_functionL   s     J$) 	- 	-D|  ++r1159[5KTZ12&&t,,,# 	/ 	/DJ!((....,'''''r
   c                 \    t          j        d           |                     ddd           dS )z9
        Removes the dropout layer in the model.
        zRemoving dropout layer...Dropoutr   Nr   r   r1   r   s    r   remove_dropout_layerz%DynamoOnnxHelper.remove_dropout_layer[   s3     	0111Y1-----r
   c                 \    t          j        d           |                     ddd           dS )z9
        Removes the LM head layer in the model.
        zRemoving LM head layer...Linear_lm_head   r   Nr4   r5   s    r   remove_lm_head_layerz%DynamoOnnxHelper.remove_lm_head_layerb   s4     	0111-q!44444r
   )r   N)__name__
__module____qualname____doc__onnx
ModelProtor	   dictr   strr+   intr1   r6   r:    r
   r   r   r   
   s         do    D D$ D D D D&%/ %/ %/ %/ %/ %/N( ( ( (PT ( ( ( (. . . .5 5 5 5 5 5r
   r   )r   r?   r   rD   r
   r   <module>rE      sU   
  ^5 ^5 ^5 ^5 ^5 ^5 ^5 ^5 ^5 ^5r
   