o
    °ƒðj<2  ã                   @   s|   d dl mZ d dlZd dlmZmZ d dlmZmZmZm	Z	 d dlm
Z d dlmZ eeƒZG dd„ dƒZG d	d
„ d
ƒZdS )é    )Ú	getLoggerN)Úarray_equalÚndarray)Ú	NodeProtoÚTensorProtoÚhelperÚnumpy_helper)Úonnx_pb)Ú	OnnxModelc                
   @   s.  e Zd Zdefdd„Zdedeeef fdd„Zd1defd	d
„Z				d2dede
dedB dedB fdd„Zdefdd„Zdefdd„Zedd„ ƒZed3defdd„ƒZdededB fdd„Zed4defdd„ƒZed ejfd!d"„ƒZed5dedefd$d%„ƒZd&e
fd'd(„Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ ZdS )6ÚFusionUtilsÚmodelc                 C   s
   || _ d S ©N)r   )Úselfr   © r   ú§/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/transformers/fusion_utils.pyÚ__init__   s   
zFusionUtils.__init__Ú
input_nameÚreturnc                 C   sn   | j  |¡}|d ur&|jjjtjkr&|  |¡\}}t 	d|› d�¡ d|fS t 	d|› d|d u› �¡ d|fS )NzCasted graph input z	 to int32TzDid not cast graph input z to int32: found F)
r   Úfind_graph_inputÚtypeÚtensor_typeÚ	elem_typer   ÚINT32Úcast_input_to_int32ÚloggerÚdebug)r   r   Úgraph_inputÚcast_outputÚ	cast_noder   r   r   Úcast_graph_input_to_int32   s   z%FusionUtils.cast_graph_input_to_int32Úint32c                 C   sf   |d | }|dkrt tjƒ}n|dkrt tjƒ}n|dkr$t tjƒ}ntdƒ‚|  |||¡}||fS )NÚ_r    Úfloat32Úfloat16z"Invalid target_type: {target_type})Úintr   r   ÚFLOATÚFLOAT16Ú
ValueErrorÚadd_cast_node)r   r   Útarget_typeÚoutput_nameÚto_typer   r   r   r   Ú
cast_input   s   zFusionUtils.cast_inputNr+   r*   Ú
graph_namec           	      C   s”   |d u r|d|› � }|g}|d u r| j  ¡ }||v r,|| }|r,|jdkr,|jd g}tjd||gd�}|j t d|¡g¡ | j j	||d� |S )NÚ	_cast_to_ÚCastr   )ÚinputsÚoutputsÚto)r-   )
r   Úoutput_name_to_nodeÚop_typeÚinputr   Ú	make_nodeÚ	attributeÚextendÚmake_attributeÚadd_node)	r   r   r+   r*   r3   r-   r0   Úparent_noder   r   r   r   r(   .   s   
zFusionUtils.add_cast_nodec                 C   s   |   |d¡S )Nr    )r,   )r   r   r   r   r   r   I   s   zFusionUtils.cast_input_to_int32c                 C   s„   | j  ¡ }|| }|D ]4}|jdkr?d}|jD ]}|jdkr*|jttjƒkr*d} nq|r?|j	d }| j  
|¡ | j  ||¡ qd S )Nr/   Fr2   Tr   )r   Úinput_name_to_nodesr4   r7   ÚnameÚir$   r   r   ÚoutputÚremove_nodeÚreplace_input_of_all_nodes)r   r   r<   ÚnodesÚnodeÚis_int32Úattr*   r   r   r   Úremove_cast_int32L   s    


€
€özFusionUtils.remove_cast_int32c                 C   s|   d}| j | |v r%| || j |  v r%|| j |   | ¡ t|| j |  ƒ}|| j |< ||v r7||  | ¡ |S | g||< |S )Nr   )r5   ÚremoveÚlenÚappend)rC   r>   Únew_input_namer<   Úold_input_referencer   r   r   Úupdate_node_input[   s    

þzFusionUtils.update_node_inputr   c           
      C   s<   |j | }|j | }t ||||¡}|dko|  |¡ }	|	S )a  
        Before:
              (input)-->parent-->node-->(output)
        After:
              (input)-->parent-->
                |
                +----->node-->(output)

        This function returns a flag whether the parent node can be removed.
        r   )r5   r   rL   Úfind_graph_output)
r   rC   r;   r<   Únode_input_indexÚparent_input_indexÚold_input_namerJ   rK   Úparent_can_be_removedr   r   r   Úskip_parentk   s
   

zFusionUtils.skip_parentrC   c                 C   sV   |j dv sJ ‚t|jƒdkr| j |jd ¡S d }|jD ]}|jdkr(t |¡}q|S )N)ÚSqueezeÚ	Unsqueezeé   Úaxes)	r4   rH   r5   r   Úget_constant_valuer7   r=   r   Úget_attribute_value)r   rC   rV   Úattrr   r   r   Úget_squeeze_or_unsqueeze_axes�   s   


€z)FusionUtils.get_squeeze_or_unsqueeze_axesÚattribute_namec                 C   sR   |}| j D ]}|j|krt |¡}qt|tƒr%t|ttfƒo$t||dd�S ||kS )a¦  Verify that a node has expected value for an attribute.

        Args:
            node (NodeProto): a node to check
            attribute_name (str): name of attribute
            expected_value (Any): expected value of the attribute
            default_value (Any, optional): default value if the attribute does not exist. Defaults to None.

        Returns:
            bool: whether the check is passed or not
        F©Ú	equal_nan)r7   r=   r   rX   Ú
isinstanceÚlistr   r   )rC   r[   Úexpected_valueÚdefault_valueÚvaluerY   r   r   r   Úcheck_node_attributeŽ   s   


€
z FusionUtils.check_node_attributeÚtensorc                 C   sˆ   t | tjƒstdt| ƒ› �ƒ‚t| jƒdks| jtjjkr!tdƒ‚| j	r@t
 t
j| j	dd�| j¡}t
 |ddg¡}| ¡ | _	| S tdƒ‚)	z¶Transpose a 2-D INT8 TensorProto
        Args:
            tensor (TensorProto): tensor to be transposed
        Returns:
            tensor (TensorProto): transposed tensor
        z3Expected input type is an ONNX TensorProto but got é   z'Only INT8 2-D tensors can be transposedÚint8)ÚdtyperU   r   zonly raw buffer supported)r^   Ú
onnx_protor   r'   r   rH   ÚdimsÚ	data_typeÚINT8Úraw_dataÚnumpyÚreshapeÚ
frombufferÚ	transposeÚtobytes)rd   Ú
int32_dataÚint32_transposed_datar   r   r   Útranspose_2d_int8_tensor¥   s   
þz$FusionUtils.transpose_2d_int8_tensorTc                 C   s²   | j dvrt d| j › �¡ | | jd ¡}|du rdS |jdkp,|jdko,|jd dk}|r3|s3dS t| jƒdkr<dS | | jd ¡}|j|jkrLdS |du rRdS t 	|dk¡S )	a  Verify if a provided QuantizeLinear (Q) / DequantizeLinear (DQ) node is a good candidate for fusion.
           It is a good candidate for fusion if:
           (1) The Q/DQ node is for per-tensor quantization if allow_per_tensor_quantization_only is `True`
           (2) The Q/DQ node should have constant scale
           (3) The Q/DQ node should have a zero point of 0
        Args:
            node (NodeProto): a Q/DQ node to check
        Returns:
            bool: whether the check is passed or not
        >   ÚQuantizeLinearÚDequantizeLinearz+Provided node is not a Q/DQ node. Op Type: rU   NFr   re   T)
r4   r   r   rW   r5   ÚndimÚshaperH   rm   Úall)rC   r   Ú"allow_per_tensor_quantization_onlyÚscaleÚscale_has_single_elementÚ
zero_pointr   r   r   Úcheck_qdq_node_for_fusion½   s    
"z%FusionUtils.check_qdq_node_for_fusionÚinput_indexc                 C   sR   t |jƒ|ks	J ‚| j |j| ¡}t|tƒr%t|ttfƒo$t||dd�S ||kS )a7  Verify that a node has expected input value

        Args:
            node (NodeProto): a node to check
            input_index (int): index of its input to be verified
            expected_value (Any): expected value of the input

        Returns:
            bool: whether the check is passed or not
        Fr\   )rH   r5   r   rW   r^   r_   r   r   )r   rC   r   r`   rb   r   r   r   Úcheck_node_input_valueè   s
   
z"FusionUtils.check_node_input_valuec                 C   sˆ   g }| j  ¡ }| j  ¡ D ] }|jdkr,|jd |vr,| j  |jd |jd ¡ | |¡ q|rB| j  |¡ t	 
dt|ƒ› d�¡ dS dS )z>Remove Identity nodes, except those right before graph output.ÚIdentityr   zRemoved z Identity nodesN)r   Úget_graphs_output_namesrB   r4   r?   rA   r5   rI   Úremove_nodesr   ÚinforH   )r   Únodes_to_removeÚgraph_output_namesrC   r   r   r   Úremove_identity_nodesü   s   


€þz!FusionUtils.remove_identity_nodesc                 C   ó   | j  ¡  d S r   )r   Úremove_cascaded_cast_nodes©r   r   r   r   r‰   
  ó   z&FusionUtils.remove_cascaded_cast_nodesc                 C   rˆ   r   )r   Úremove_useless_cast_nodesrŠ   r   r   r   rŒ     r‹   z%FusionUtils.remove_useless_cast_nodesc                 C   s@  | j jdd�}|du rdS g }| j  ¡ D ]0}|jdkrD| |jd ¡}| |jd ¡}|rD|rD||krDt d|j	› d|› �¡ | 
|¡ q|rœt| j  ¡ ƒ}t| j  ¡ ƒ}|D ]F}tt|jƒ|@ ƒrˆtt|jƒ|@ ƒs‡t| j  ¡ |jd  ƒdkr‡| j  |jd |jd ¡ nqW| j  |jd |jd ¡ | j  |¡ qWdS dS )	ziRemove reshape node that is not needed based on symbolic shape inference: input and output has same shapeT)ÚupdateNÚReshaper   zRemove reshape node z* since its input shape is same as output: rU   )r   Úinfer_runtime_shaperB   r4   Úget_edge_shaper5   r?   r   r„   r=   rI   ÚsetÚget_graphs_input_namesr‚   ÚboolrH   r<   Úreplace_output_of_all_nodesrA   r@   )r   Úshape_inferr…   rC   Úinput_shapeÚoutput_shapeÚgraph_input_namesr†   r   r   r   Úremove_useless_reshape_nodes  s8   
ÿ
€ÿòz(FusionUtils.remove_useless_reshape_nodes)r    )NNN)r   r   r   )T)Ú__name__Ú
__module__Ú__qualname__r
   r   ÚstrÚtupler“   r   r,   r$   r(   r   rF   ÚstaticmethodrL   rR   r   r   rZ   rc   rh   r   rt   r~   r€   r‡   r‰   rŒ   r™   r   r   r   r   r      sD    
úþýü
ú
*r   c                   @   s(   e Zd Zeddededefdd„ƒZdS )	ÚNumpyHelperFrd   Ú
fill_zerosr   c                 C   s$   |rt | jt | j¡d�S t | ¡S )N)rx   rg   )r   ri   r   Útensor_dtype_to_np_dtyperj   r   Úto_array)rd   r¡   r   r   r   r£   3  s   
þ
zNumpyHelper.to_arrayN)F)rš   r›   rœ   rŸ   r   r“   r   r£   r   r   r   r   r    2  s    r    )Úloggingr   rm   r   r   Úonnxr   r   r   r   r	   rh   Ú
onnx_modelr
   rš   r   r   r    r   r   r   r   Ú<module>   s     $