
    fPi                     h    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e          Z
dS )    )	getLogger)Fusion)NumpyHelper)	OnnxModelc                   @     e Zd Zdef fdZ fdZd Zd Zd Z xZ	S )FusionConstantFoldmodelc                 ^    t                                          |ddg           d| _        d S )N 	Transposer   )super__init__count)selfr	   	__class__s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/transformers/fusion_constant_fold.pyr   zFusionConstantFold.__init__   s,    [M222


    c                     t                                                       | j        dk    r$t                              d| j                    d S d S )Nr   zConstant Folded: )r   applyr   loggerinfo)r   r   s    r   r   zFusionConstantFold.apply   sI    :>>KK8DJ8899999 >r   c                 b    |                      |||           |                     |||           dS )zX
        Apply multiple fusions on Transpose nodes that can be constant folded.
        N)fuse_1fuse_2)r   nodeinput_name_to_nodesoutput_name_to_nodes       r   fusezFusionConstantFold.fuse   s:     	D-/BCCCD-/BCCCCCr   c                    t          |j                  dk    st          |j                  dk    rt                              d           dS | j                            |j        d                   }|t                              d           dS d}||j        d                  D ])}|j        dk    rt          |j                  dk    sd} n*|rt                              d	           dS ||j        d                  D ]}|j        d
k    s|j        dk    sd} n|rt                              d           dS t          j	        |          }t          |j
                  dk    rt                              d           dS |j        }|j        }	|                     |           |                     ||	|j
        d         |j
        d         g|j                   ||j        d                  D ]}t!          t          |j                            D ]}
|j        |
         |j        d         k    rm|j        d         |j        |
<   |j        d
k    rM|
dk    s|
dk    rA|
dk    rdnd}t#          |j                  D ]"\  }}|j        |k    rd|j        |         _        #| j                            |           | xj        dz  c_        dS )z
        Constant fold any initializer data representing a MatMul's
        weights that are stored in a Transpose op

        Ex: Transpose --> Gemm or Transpose --> MatMul
           :fuse_constant_fold: node has more than one input or outputNr   z8fuse_constant_fold: failed to identify initializer inputFr   TzAfuse_constant_fold: other non-Transpose nodes use the initializerGemmMatMulzOfuse_constant_fold: other non-Gemm and non-MatMul nodes use the transposed data   z7fuse_constant_fold: shape of initializer data is not 2D)name	data_typedimsvalstransAtransB)leninputoutputr   debugr	   get_initializerop_typer   to_arrayshaper%   r&   remove_initializeradd_initializerTrange	enumerate	attributeinodes_to_removeappendr   )r   r   r   r   protoskip
child_nodeweightr%   dtyper9   keyjattr_keys                 r   r   zFusionConstantFold.fuse_1    s    tz??a3t{#3#3q#8#8LLUVVVF 
**4:a=99=LLSTTTF -djm< 	 	J&+55#dj//Q:N:N ;O  	LL\]]]F .dk!n= 	 	J&&00J4F(4R4R 	LLjkkkF %e,,v|!!LLRSSSF z&&&,q/6<?3	 	 	
 	
 	
 .dk!n= 
	> 
	>J3z/0011 	> 	>#A&$+a.88*.*Q-J$Q'!)V33a166*+q&&hhh+4Z5I+J+J > >KAx'}33<=
 4Q 7 9	> 	##D)))

a



r   c                 r   t          |j                  dk    st          |j                  dk    rt                              d           dS | j                            |dd          }|t                              d           dS t          |j                  dk    st          |j                  dk    rt                              d           dS |j        d         j        }|j        d         j        }||k    rt                              d           dS |j        d         }||j        d                  }|D ]7}	t          |	j                  D ] \  }
}||j        d         k    r
||	j        |
<   !8| j
                            |           | j
                            |           | xj        dz  c_        dS )	z
        Constant fold any Transpose --> Transpose ops since the root input
        is the final result

        Ex: root_input --> Transpose --> Transpose --> next_node to root_input --> next_node
        r    r!   Nr   r   z<fuse_constant_fold: failed to identify parent Transpose nodezAfuse_constant_fold: parent node has more than one input or outputz@fuse_constant_fold: Transpose node permutations aren't identical)r+   r,   r-   r   r.   r	   match_parentr8   intsr7   r:   r;   r   )r   r   r   r   parent_node	node_permparent_node_perm
root_inputoutput_nodesoutput_noder9   input_s               r   r   zFusionConstantFold.fuse_2h   s    tz??a3t{#3#3q#8#8LLUVVVF j--dKCCLLWXXXF{ !!Q&&#k.@*A*AQ*F*FLL\]]]FN1%*	&038(((LL[\\\F !&q)
*4;q>:' 	6 	6K&{'899 6 6	6T[^+++5K%a(6
 	##D)))##K000

a



r   )
__name__
__module____qualname__r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s        i      : : : : :
D D DF F FP( ( ( ( ( ( (r   r   N)loggingr   fusion_baser   fusion_utilsr   
onnx_modelr   rN   r   r    r   r   <module>rW      s                $ $ $ $ $ $            	8		A A A A A A A A A Ar   