
    fPi                      t    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e          ZdS )	    )	getLogger)Fusion)FusionUtils)helper)	OnnxModelc                   4     e Zd Zdef fdZdedefdZ xZS )FusionQOrderedMatMulmodelc                 N    t                                          |dd           d S )NQOrderedMatMulMatMul)super__init__)selfr
   	__class__s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/transformers/fusion_qordered_matmul.pyr   zFusionQOrderedMatMul.__init__   s&     0(;;;;;    input_name_to_nodesoutput_name_to_nodec           	         | j                             ||          }t          |          dk    s|d         j        dk    rd S |d         }d}| j                             |j        d                   '| j                             |j        d                   d S | j                             |j        d                   d}| j                             ||          }t          |          dk    rd S |d         }d }	d }
|j        dk    rL|}	| j                             |	|          }t          |          dk    s|d         j        dk    rd S |d         }
n|j        dk    r|}
nd S t          j        |
| j                   sd S | j                             |dgdgfg|          \  }}}d }d }|dk     rH| j                             |g dg dfg|          \  }}}|dk     rd S |d         }|d         }|d         }n|d         }t          j        || j                   sd S d }d	}| j                             |g d
g dfg|          \  }}}|dk     r6| j                             |dgdgfg|          \  }}}|dk     rd S |d         }n
d}|d         }| j                             |j        d                   d S t          j        || j         d          sd S d }|	5| j                             |	dgdgfg|          \  }}}|dk     rd S |d         }|t          j        || j                   sd S ||g}|	|	                    |	g           |	                    |           |	                    |
g           | j         
                    ||
j        ||          st                              d           d S |2| j                             ||j        d         |j        d                    ||j        d         n|j        d         |j        d         |j        d         |j        d         |
j        d         |j        |         g}|	@|                    |j        d                    |                    |j        d                    |r9| j                             |j        d                   }t          j        |           t%          j        d||
j        d         g| j                             dd                    }|j        	                    t%          j        dd          g           |j        	                    t%          j        dd          g           |j        	                    t%          j        dd          g           d|_        | j        	                    |           | j                            |           | j        | j        |j        <   d S )N   r   AddQuantizeLinearDequantizeLinear)Reshape	Transposer   r   )r   r   r   r      T)r   r   r   r   )r   r   r   r   F   z0It is not safe to fuse QOrderedMatMul node. Skipr   )name_prefix)inputsoutputsnameorder_Aorder_Border_Yzcom.microsoft)r
   get_childrenlenop_typeget_constant_valueinputr   check_qdq_node_for_fusionmatch_parent_pathsextendis_safe_to_fuse_nodesoutputloggerdebugreplace_node_inputappendget_initializertranspose_2d_int8_tensorr   	make_nodecreate_node_name	attributemake_attributedomainnodes_to_removenodes_to_addthis_graph_namenode_name_to_graph_namer"   )r   noder   r   matmul_childrenbias_add_nodebias_add_node_indexbias_add_childrenbias_add_childresidual_add_nodedownstream_quantize_noderesidual_add_childrenfirst_path_idfirst_input_parent_nodes_reshape_node_0transpose_node_0dequantize_node_0dequantize_node_1is_weight_transpose_requiredweight_path_idweight_nodesresidual_add_dequantize_noderesidual_path_idresidual_input_parent_nodessubgraph_nodesfused_node_inputsweight_tensor
fused_nodes                                r   fusezFusionQOrderedMatMul.fuse   s   *11$8KLL 1$$(:(Be(K(KF'*  J))-*=a*@AAI
--m.A!.DEEMF:(()<Q)?@@H"# J33MCVWW !!Q&&F*1- !#' !U** .$(J$;$;<MOb$c$c!())Q..2G2J2RVf2f2f'<Q'?$$#'777'5$$ F 45MtzZZ 	F 6:Z5R5R!"QC()6
 6
2/ 19=9V9VPPPR^R^R^_`#: :6M3Q q  5a8N7: 8 ; 8 ; 45F
SS 	F !'+$*.**G*GUUUWcWcWcde+
 +
'a A.2j.K.K%&,-#/ /+NL! !! ,Q+0( ,Q :(():)@)CDDLF 45F
TYZZ 	F (,$(?Cz?\?\!()A3/ $@ @<91  !##+Fq+I( (3K<a($*=
 =
3 F .(!!#4"5666l+++78999z//4;=PRe
 
 	 LLKLLLF 'J))*:<L<RST<UWhWnopWqrrr )7(BN!!$$HYH_`aHb#A&#A&#A&$*1- 34
 ($$%A%G%JKKK$$%A%G%JKKK ( 	@ J667H7Nq7QRRM0???%$-4Q78,,-=K[,\\	
 
 

 	##V%:9a%H%H$IJJJ##V%:9a%H%H$IJJJ##V%:9a%H%H$IJJJ+
##N333  ,,,8<8L$Z_555r   )__name__
__module____qualname__r   r   dictrY   __classcell__)r   s   @r   r	   r	      sz        <i < < < < < <DMd DM DM DM DM DM DM DM DM DMr   r	   N)loggingr   fusion_baser   fusion_utilsr   onnxr   
onnx_modelr   rZ   r0   r	    r   r   <module>re      s                $ $ $ $ $ $                  	8		HM HM HM HM HM6 HM HM HM HM HMr   