§
    û¸¸j=6  ã                  ó˜   — d dl mZ d dlZd dlmZ d dlZddlmZmZ ddl	m
Z
 ddlmZ 	 	 	 	 	 	 	 	 d(d)d„Z G d„ d¦  «        Z	 	 d*d+d'„ZdS ),é    )ÚannotationsN)ÚPathé   )Ú
FusionGeluÚFusionLayerNormalization)Ú	ONNXModelé   )ÚFusionLpNormalizationFé   Úmodel_inputústr | Path | onnx.ModelProtoÚmodel_outputú
str | PathÚfuse_layernormÚboolÚsave_as_external_dataÚall_tensors_to_one_fileÚexternal_data_locationú
str | NoneÚexternal_data_size_thresholdÚintÚexternal_data_convert_attributeÚinputs_to_make_channel_lastúlist[str] | NoneÚoutputs_to_make_channel_lastÚreturnc
           	     ó´  — d}
t          | t          j        ¦  «        r| nt          j        | ¦  «        }t	          |¦  «        }t          |¦  «        }|                     ¦   «         rd}
t          |¦  «        }|                     ¦   «         rd}
|rlt          d„ |j	        D ¦   «         ¦  «        }|j
        dk     rt          j        d|j
        › d�¦  «         n%t          |¦  «        }|                     ¦   «         rd}
|s|	r5d}|                     |¦  «        dz   }t          |j        ||	||¬	¦  «         d}
d
}|                     |¦  «        dz   }|j        j        j        D ]H}|j        dk    r;|j        s4|› |›�}|dz  }||_        d}
t          j        d|j        › d|› d�¦  «         ŒI|
r/|                     ¦   «          t          j        |||||||¬¦  «         |
S )a‰  
    If necessary, this method creates a new "pre-processed" model in preparation for
    quantization of a model to be used in QNN EP. Returns true if a new model was created.

    This method perfoms the following operations:
    - Fuse Erf sequence into a single Gelu node.
    - Fuse ReduceL2 sequence into a single LpNormalization node (p == 2).
    - (Optional) Fuse ReduceMean sequence into a single LayerNormalization node.

    Args:
        model_input: Path to the input model file or ModelProto.
        model_output: Path the output model file, which is only created if this method returns True.
        fuse_layernorm: True if ReduceMean sequences should be fused into LayerNormalization nodes.
            Defaults to False.
        save_as_external_data: True if output model should be saved with external data. Defaults to false.
        all_tensors_to_one_file: Effective only if save_as_external_data is true. Defaults to false.
            If true, save all tensors to one external file specified by external_data_location.
            If false, save each tensor to a file named with the tensor name.
        external_data_location: Effective only if save_as_external_data is true. Defaults to None.
            Specify the external file to which all tensors are saved. Path is relative
            to the model path. If not specified, the model's name is used.
        external_data_size_threshold: Effective only if save_as_external_data is true. Defaults to 1024.
            Tensors with a data size >= external_data_size_threshold are converted to external data.
            To convert every tensor with raw data to external data, set to 0.
        external_data_convert_attribute: Effective only if save_as_external_data is true. Defaults to false.
            If true, convert all tensors to external data.
            If false, convert only non-attribute tensors to external data.
        inputs_to_make_channel_last: List of graph input names to transpose to be "channel-last". For example,
            if "input0" originally has the shape (N, C, D1, D2, ..., Dn), the resulting model will change input0's
            shape to (N, D1, D2, ..., Dn, C) and add a transpose node after it.

            Original:
                input0 (N, C, D1, D2, ..., Dn) --> <Nodes>

            Updated:
                input0 (N, D1, D2, ..., Dn, C) --> Transpose --> input0_chanfirst (N, C, D1, D2, ..., Dn) --> <Nodes>

            This can potentially improve inference latency for QDQ models running on QNN EP because the
            additional transpose node may allow other transpose nodes inserted during ORT layout transformation
            to cancel out.
        outputs_to_make_channel_last: List of graph output names to transpose to be "channel-last". For example,
            if "output0" originally has the shape (N, C, D1, D2, ..., Dn), the resulting model will change output0's
            shape to (N, D1, D2, ..., Dn, C) and add a transpose node before it.

            Original:
                <Nodes> --> output0 (N, C, D1, D2, ..., Dn)

            Updated:
                <Nodes> --> output0_chanfirst (N, C, D1, D2, ..., Dn) --> Transpose --> output0 (N, D1, D2, ..., Dn, C)

            This can potentially improve inference latency for QDQ models running on QNN EP because the
            additional transpose node may allow other transpose nodes inserted during ORT layout transformation
            to cancel out.
    FTc              3  óF   K  — | ]}|j         d k    s|j         dk    ¯|V — ŒdS )Ú zai.onnxN)Údomain)Ú.0Úxs     ú‹/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/onnxruntime/quantization/execution_providers/qnn/preprocess.pyú	<genexpr>z'qnn_preprocess_model.<locals>.<genexpr>d   s9   è è € ÐcÐc ¸1¼8Àrº>¸>ÈQÌXÐYbÒMbÐMb˜!ÐMbÐMbÐMbÐMbÐcÐcó    é   zœUnable to fuse ReduceMean sequence into a LayerNormalization node. ONNX model must use an opset >= 17 in order to use LayerNormalization, but found version z9. Please use onnx.version_converter to update your model.ÚTranspose_channel_r	   )Útranspose_node_name_prefixÚ transpose_node_name_start_suffixÚqnn_preproc_node_ÚConstantzNode of type z" does not have a name. Renamed to ú.)r   r   ÚlocationÚsize_thresholdÚconvert_attribute)Ú
isinstanceÚonnxÚ
ModelProtoÚ
load_modelr   r   Úapplyr
   ÚnextÚopset_importÚversionÚloggingÚwarningr   Úget_largest_node_name_suffixÚupdate_io_to_channel_lastÚmodelÚgraphÚnodeÚop_typeÚnameÚtopological_sortÚ
save_model)r   r   r   r   r   r   r   r   r   r   Úmodifiedr<   Ú
onnx_modelÚfusion_geluÚfusion_lpnormÚ
onnx_opsetÚfusion_layernormÚtranspose_node_prefixÚtranspose_node_suffixÚunnamed_node_prefixÚavailable_suffixr>   Únew_node_names                          r#   Úqnn_preprocess_modelrN      sx  € ðD €HÝ% kµ4´?ÑCÔCÐeˆKˆKÍÌÐYdÑIeÔIe€EÝ˜5Ñ!Ô!€Jõ ˜ZÑ(Ô(€KØ×ÒÑÔð Øˆõ *¨*Ñ5Ô5€MØ×ÒÑÔð Øˆð ð  ÝÐcÐc UÔ%7ÐcÑcÔcÑcÔcˆ
ð Ô Ò"Ð"ÝŒOðsà%/Ô%7ðsð sð sñô ð ð õ  8¸
ÑCÔCÐØ×%Ò%Ñ'Ô'ð  Ø�ð #ð 
Ð&Bð 
Ø 4ÐØ%/×%LÒ%LÐMbÑ%cÔ%cÐfgÑ%gÐÝ!ØÔØ'Ø(Ø'<Ø-Bð	
ñ 	
ô 	
ð 	
ð ˆð .ÐØ!×>Ò>Ð?RÑSÔSÐVWÑWÐØÔ Ô&Ô+ð nð nˆØŒ<˜:Ò%Ð%¨d¬iÐ%Ø2ÐHÐ4DÐHÐHˆMØ Ñ!ÐØ%ˆDŒIØˆHÝŒOÐl¨D¬LÐlÐlÐ\iÐlÐlÐlÑmÔmÐmøàð 

Ø×#Ò#Ñ%Ô%Ð%ÝŒØØØ"7Ø$;Ø+Ø7Ø=ð	
ñ 	
ô 	
ð 	
ð €Or%   c                  ó   — e Zd Zdd„Zdd	„Zd
S )ÚInputOutputNameMapÚorig_tensor_namesúset[str]Úorig_graph_inputsúdict[str, onnx.ValueInfoProto]Úorig_graph_outputsc                óL   — || _         || _        || _        i | _        g | _        d S ©N)rQ   rS   rU   Úupdated_io_namesÚnew_value_infos)ÚselfrQ   rS   rU   s       r#   Ú__init__zInputOutputNameMap.__init__š   s2   € ð "3ˆÔØ!2ˆÔØ"4ˆÔØ "ˆÔØ!ˆÔÐÐr%   Ú	orig_nameÚstrc                óX  — || j         v r| j         |         S |› d�}d}| j        D ]t}|                     |¦  «        r]|t          |¦  «        d …                              ¦   «         r4t          |t          |¦  «        d …         ¦  «        }t          ||¦  «        }Œu|dz  }|› |›�}| j                             |¦  «        p| j	        |         }t          j        ¦   «         }|                     |¦  «         ||_        | j                             |¦  «         || j         |<   | j         |         S )NÚ_channel_first_éÿÿÿÿr	   )rX   rQ   Ú
startswithÚlenÚisdigitr   ÚmaxrS   ÚgetrU   r1   ÚValueInfoProtoÚCopyFromr@   rY   Úappend)	rZ   r\   ÚprefixÚsuffixÚtensor_nameÚindexÚnew_nameÚorig_value_infoÚvalue_info_protos	            r#   Úget_new_namezInputOutputNameMap.get_new_name¦   sG  € Ø˜Ô-Ð-Ð-ØÔ(¨Ô3Ð3ð #Ð3Ð3Ð3ˆØˆØÔ1ð 	,ð 	,ˆKØ×%Ò% fÑ-Ô-ð ,°+½cÀ&¹k¼k¸m¸mÔ2L×2TÒ2TÑ2VÔ2Vð ,Ý˜K­¨F©¬¨¨Ô6Ñ7Ô7�Ý˜V UÑ+Ô+�øà�!‰ˆØÐ(˜fÐ(Ð(ˆð Ô0×4Ò4°YÑ?Ô?ÐeÀ4ÔCZÐ[dÔCeˆÝÔ.Ñ0Ô0ÐØ×!Ò! /Ñ2Ô2Ð2Ø (ÐÔØÔ×#Ò#Ð$4Ñ5Ô5Ð5à+3ˆÔ˜iÑ(ØÔ$ YÔ/Ð/r%   N)rQ   rR   rS   rT   rU   rT   )r\   r]   )Ú__name__Ú
__module__Ú__qualname__r[   rp   © r%   r#   rP   rP   ™   s<   € € € € € ð
"ð 
"ð 
"ð 
"ð0ð 0ð 0ð 0ð 0ð 0r%   rP   r'   r<   úonnx.ModelProtoÚinputs_to_updateÚoutputs_to_updater(   r]   r)   c           	     ó\  — t          |pg ¦  «        }t          |pg ¦  «        }|s|sd S | j        }d„ |j        D ¦   «         }d„ |j        D ¦   «         }|D ]}||vrt	          |› d�¦  «        ‚Œ|D ]}	|	|vrt	          |	› d�¦  «        ‚Œt          ¦   «         }
|
                     t          |¦  «        ¦  «         |
                     t          |¦  «        ¦  «         |
                     d„ |j        D ¦   «         ¦  «         t          |
||¦  «        }|j        D �]}t          t          |j        ¦  «        ¦  «        D ]‹}|j        |         r8|j        |         |v r)| 
                    |j        |         ¦  «        |j        |<   ŒG|j        |         r7|j        |         |v r(| 
                    |j        |         ¦  «        |j        |<   ŒŒt          t          |j        ¦  «        ¦  «        D ]9}|j        |         |v r(| 
                    |j        |         ¦  «        |j        |<   Œ:�Œ|D �]ñ}||         }|j                             d¦  «        r|j        j                             d¦  «        st	          d|j        › d	�¦  «        ‚|j        j        j        }t          |j        ¦  «        }|d
k     rt	          d|j        › d�¦  «        ‚t"          j                             ¦   «         }|                     |j        d         ¦  «         t          d|dz
  ¦  «        D ]0}|j        |                              |j        |dz            ¦  «         Œ1|j        |dz
                                |¦  «         t+          t          |¦  «        ¦  «        }t          |¦  «        D ]}|dk     r|n|dz
  ||<   Œ|dz
  |d<   t"          j                             d|› |›�|j        g| 
                    |j        ¦  «        g|¬¦  «        }|dz  }|j                             |g¦  «         �Œó|D �]ñ}||         }|j                             d¦  «        r|j        j                             d¦  «        st	          d|j        › d	�¦  «        ‚|j        j        j        }t          |j        ¦  «        }|d
k     rt	          d|j        › d�¦  «        ‚t"          j                             ¦   «         }|                     |j        d         ¦  «         t          d|dz
  ¦  «        D ]0}|j        |                              |j        |dz            ¦  «         Œ1|j        |dz
                                |¦  «         t+          t          |¦  «        ¦  «        }t          |¦  «        D ]}|dk    r|n|dz   ||<   Œd||dz
  <   t"          j                             d|› |›�| 
                    |j        ¦  «        g|j        g|¬¦  «        }|dz  }|j                             |g¦  «         �Œó|j                             |j        ¦  «         d S )Nc                ó   — i | ]
}|j         |“ŒS rt   ©r@   )r!   Úginputs     r#   ú
<dictcomp>z-update_io_to_channel_last.<locals>.<dictcomp>Î   s   € ÐGÐGÐG°˜œ fÐGÐGÐGr%   c                ó   — i | ]
}|j         |“ŒS rt   rz   )r!   Úgoutputs     r#   r|   z-update_io_to_channel_last.<locals>.<dictcomp>Ï   s   € ÐLÐLÐL°G˜'œ,¨ÐLÐLÐLr%   z is not a graph inputz is not a graph outputc              3  ó2   K  — | ]}|j         D ]}|¯|V — Œ	Œd S rW   )Úinput)r!   r>   Ú
input_names      r#   r$   z,update_io_to_channel_last.<locals>.<genexpr>Ý   s9   è è € ÐjÐj¨DÐQUÔQ[ÐjÐjÀ:Ð_iÐj˜ZÐjÐjÐjÐjÐjÐjÐjr%   Útensor_typeÚshapezExpected input z# to have a tensor_type with a shaper   z to be of rank >= 3r	   Ú	Transpose)r@   ÚinputsÚoutputsÚpermzExpected output r   )Úsetr=   r€   ÚoutputÚ
ValueErrorÚupdater>   rP   Úrangerb   rp   ÚtypeÚHasFieldr‚   r@   rƒ   Údimr1   ÚTensorShapeProtoÚ	Dimensionrg   ÚlistÚhelperÚ	make_nodeÚextendÚ
value_inforY   )r<   rv   rw   r(   r)   r=   rS   rU   r�   Úoutput_namerQ   Úio_mapr>   ÚiÚg_input_nameÚg_inputÚinput_shapeÚ
input_rankÚchannel_dimÚtranspose_permÚtranspose_nodeÚg_output_nameÚg_outputÚoutput_shapeÚoutput_ranks                            r#   r;   r;   À   sÄ  € õ Ð+Ð1¨rÑ2Ô2ÐÝÐ-Ð3°Ñ4Ô4Ðàð Ð$5ð ØˆàŒK€EØGÐG¸5¼;ÐGÑGÔGÐØLÐL¸u¼|ÐLÑLÔLÐð 'ð Cð Cˆ
ØÐ.Ð.Ð.Ý 
ÐAÐAÐAÑBÔBÐBð /ð )ð Eð EˆØÐ0Ð0Ð0Ý ÐCÐCÐCÑDÔDÐDð 1õ ™œÐØ×Ò�SÐ!2Ñ3Ô3Ñ4Ô4Ð4Ø×Ò�SÐ!3Ñ4Ô4Ñ5Ô5Ð5Ø×ÒÐjÐj°E´JÐjÑjÔjÑjÔjÐjõ  Ð 1Ð3DÐFXÑYÔY€Fð ”
ð 	Eñ 	EˆÝ•s˜4œ:‘”Ñ'Ô'ð 	Cð 	CˆAØŒz˜!Œ}ð C ¤¨A¤Ð2BÐ!BÐ!BØ &× 3Ò 3°D´J¸q´MÑ BÔ B�”
˜1‘�Ø”˜A”ð C 4¤:¨a¤=Ð4EÐ#EÐ#EØ &× 3Ò 3°D´J¸q´MÑ BÔ B�”
˜1‘øå•s˜4œ;Ñ'Ô'Ñ(Ô(ð 	Eð 	EˆAØŒ{˜1Œ~Ð!2Ð2Ð2Ø!'×!4Ò!4°T´[À´^Ñ!DÔ!D�”˜A‘øñ	Eð
 )ð  ,ñ  ,ˆØ# LÔ1ˆàŒ|×$Ò$ ]Ñ3Ô3ð 	b¸7¼<Ô;S×;\Ò;\Ð]dÑ;eÔ;eð 	bÝÐ`¨w¬|Ð`Ð`Ð`ÑaÔaÐaà”lÔ.Ô4ˆÝ˜œÑ)Ô)ˆ
à˜Š>ˆ>ÝÐP¨w¬|ÐPÐPÐPÑQÔQÐQåÔ+×5Ò5Ñ7Ô7ˆØ×Ò˜[œ_¨QÔ/Ñ0Ô0Ð0Ý�q˜* q™.Ñ)Ô)ð 	@ð 	@ˆAØŒO˜AÔ×'Ò'¨¬¸¸A¹Ô(>Ñ?Ô?Ð?Ð?ØŒ˜
 Q™Ô'×0Ò0°Ñ=Ô=Ð=å�e JÑ/Ô/Ñ0Ô0ˆÝ�zÑ"Ô"ð 	6ð 	6ˆAØ%&¨¢U U  °°A±ˆN˜1ÑÐØ&¨™Nˆ�qÑåœ×.Ò.ØØ.ÐTÐ0PÐTÐTØ”L�>Ø×(Ò(¨¬Ñ6Ô6Ð7Øð /ñ 
ô 
ˆð 	)¨AÑ-Ð(àŒ
×Ò˜>Ð*Ñ+Ô+Ð+Ñ+ð +ð ,ñ ,ˆØ% mÔ4ˆØŒ}×%Ò% mÑ4Ô4ð 	d¸H¼MÔ<U×<^Ò<^Ð_fÑ<gÔ<gð 	dÝÐb°´ÐbÐbÐbÑcÔcÐcà”}Ô0Ô6ˆÝ˜,Ô*Ñ+Ô+ˆà˜Š?ˆ?ÝÐR°´ÐRÐRÐRÑSÔSÐSåÔ+×5Ò5Ñ7Ô7ˆØ×Ò˜\Ô-¨aÔ0Ñ1Ô1Ð1Ý�q˜+¨™/Ñ*Ô*ð 	Bð 	BˆAØÔ˜QÔ×(Ò(¨Ô)9¸!¸a¹%Ô)@ÑAÔAÐAÐAØÔ˜ q™Ô)×2Ò2°;Ñ?Ô?Ð?å�e KÑ0Ô0Ñ1Ô1ˆÝ�{Ñ#Ô#ð 	7ð 	7ˆAØ%&¨!¢V V  °°Q±ˆN˜1ÑÐØ*+ˆ�{ Q‘Ñ'åœ×.Ò.ØØ.ÐTÐ0PÐTÐTØ×'Ò'¨¬Ñ6Ô6Ð7Ø”]�OØð /ñ 
ô 
ˆð 	)¨AÑ-Ð(àŒ
×Ò˜>Ð*Ñ+Ô+Ð+Ñ+à	Ô×Ò˜FÔ2Ñ3Ô3Ð3Ð3Ð3r%   )FFFNr   FNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r'   r   )
r<   ru   rv   r   rw   r   r(   r]   r)   r   )Ú
__future__r   r8   Úpathlibr   r1   Úfusionsr   r   rD   r   rF   r
   rN   rP   r;   rt   r%   r#   ú<module>r¨      s  ðð #Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ð Ð à €€€à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø #Ð #Ð #Ð #Ð #Ð #Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0ð !Ø"'Ø$)Ø)-Ø(,Ø,1Ø48Ø59ðDð Dð Dð Dð DðN$0ð $0ð $0ð $0ð $0ñ $0ô $0ð $0ðV ';Ø,-ðs4ð s4ð s4ð s4ð s4ð s4ð s4r%   