§
    û¸¸jÅ"  ã                   ó  — d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ ddlmZ  e j        e¦  «        Z	 	 	 	 	 	 	 	 	 	 	 	 	 dd
eez  ej        z  dz  deez  dz  dededededededededededz  deddfd„ZdS )é    N)ÚPath)ÚSymbolicShapeInference)Úextract_raw_data_from_modelÚhas_external_dataé   )Úadd_pre_process_metadataFéÿÿÿé   Úinput_modelÚoutput_model_pathÚskip_optimizationÚskip_onnx_shapeÚskip_symbolic_shapeÚ
auto_mergeÚint_maxÚguess_output_rankÚverboseÚsave_as_external_dataÚall_tensors_to_one_fileÚexternal_data_locationÚexternal_data_size_thresholdÚreturnc           	      óš  — | €|                      dd¦  «        } | €J ‚|€
J d¦   «         ‚t          j        d¬¦  «        5 }t          |¦  «        }d}|sbt                               d¦  «         t          | t          j        ¦  «        r| nt          j	        | ¦  «        }t          j        |||||¦  «        }|�s¥|sFt          |dz  ¦  «        } |	rt          j        || d|
|d	¬
¦  «         nt          j        || ¦  «         d}t          |dz  ¦  «        }	 t          j        ¦   «         }||_        t          j        j        |_        t          | t          j        ¦  «        rut+          | ¦  «        rt-          d¦  «        ‚t/          | ¦  «        \  }}|                     t3          |¦  «        t3          |¦  «        ¦  «         |                      ¦   «         } n|r|	r|                     dd¦  «         t          j        | |dg¬¦  «        }~nU# t:          $ rH t                               d¦  «         t                               t?          j         ¦   «         ¦  «         Y nw xY w|} |sá|�Ft          |dz  ¦  «        } |	rt          j        || d|
|d	¬
¦  «         nt          j        || ¦  «         d}t          | t          j        ¦  «        r9t          t          |¦  «        dz  ¦  «        } t          j        || d|
|d	¬
¦  «         t          |dz  ¦  «        }t          j!         "                    | |¦  «         t          j	        |¦  «        }ddd¦  «         n# 1 swxY w Y   |€0t          | t          j        ¦  «        r| nt          j	        | ¦  «        }tG          |¦  «         |	rt          j        ||d|
||d	¬¦  «         dS t          j        ||¦  «         dS )aˆ  Shape inference and model optimization, in preparation for quantization.

    Args:
        input_model: Path to the input model file or ModelProto
        output_model_path: Path to the output model file
        skip_optimization: Skip model optimization step if true. This may result in ONNX shape
            inference failure for some models.
        skip_onnx_shape: Skip ONNX shape inference. Symbolic shape inference is most effective
            with transformer based models. Skipping all shape inferences may
            reduce the effectiveness of quantization, as a tensor with unknown
            shape can not be quantized.
        skip_symbolic_shape: Skip symbolic shape inference. Symbolic shape inference is most
            effective with transformer based models. Skipping all shape
            inferences may reduce the effectiveness of quantization, as a tensor
            with unknown shape can not be quantized.
        auto_merge: For symbolic shape inference, automatically merge symbolic dims when
            conflict happens.
        int_max: For symbolic shape inference, specify the maximum value for integer to be
            treated as boundless for ops like slice
        guess_output_rank: Guess output rank to be the same as input 0 for unknown ops
        verbose: Logs detailed info of inference, 0: turn off, 1: warnings, 3: detailed
        save_as_external_data: Saving an ONNX model to external data
        all_tensors_to_one_file: Saving all the external data to one file
        external_data_location: The file location to save the external file
        external_data_size_threshold: The size threshold for external data
    NÚinput_model_pathzoutput_model_path is required.z
pre.quant.)Úprefixz&Performing symbolic shape inference...zsymbolic_shape_inferred.onnxTF)r   r   Úsize_thresholdÚconvert_attributezoptimized.onnxzÒModelProto has external data not loaded into memory, ORT cannot create session. Please load external data before calling this function. See https://onnx.ai/onnx/repo-docs/ExternalData.html for more information.z7session.optimized_model_external_initializers_file_namezoptimized.onnx.dataÚCPUExecutionProvider)Ú	providerszYONNX Runtime Model Optimization Failed! Consider rerun with option `--skip_optimization'.zmodel_input.onnxzonnx_shape_inferred.onnx)r   r   Úlocationr   r   )$ÚpopÚtempfileÚTemporaryDirectoryr   ÚloggerÚinfoÚ
isinstanceÚonnxÚ
ModelProtoÚloadr   Úinfer_shapesÚstrÚ
save_modelÚsaveÚonnxruntimeÚSessionOptionsÚoptimized_model_filepathÚGraphOptimizationLevelÚORT_ENABLE_BASICÚgraph_optimization_levelr   Ú
ValueErrorr   Úadd_external_initializersÚlistÚSerializeToStringÚadd_session_config_entryÚInferenceSessionÚ	ExceptionÚerrorÚ	tracebackÚ
format_excÚshape_inferenceÚinfer_shapes_pathr   )r   r   r   r   r   r   r   r   r   r   r   r   r   Údeprecated_kwargsÚquant_tmp_dirÚ	temp_pathÚmodelÚloaded_modelÚopt_model_pathÚsess_optionÚexternal_namesÚexternal_valuesÚsessÚinferred_model_paths                           úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/onnxruntime/quantization/shape_inference.pyÚquant_pre_processrL      sÃ  € ðV ÐØ'×+Ò+Ð,>ÀÑEÔEˆØÐ"Ð"Ð"àÐ(Ð(Ð*JÑ(Ô(Ð(å	Ô	$¨LÐ	9Ñ	9Ô	9ð e3¸]Ý˜Ñ'Ô'ˆ	Øˆà"ð 		Ý�KŠKÐ@ÑAÔAÐAÝ*4°[Å$Ä/Ñ*RÔ*RÐn˜;˜;ÕX\ÔXaÐbmÑXnÔXnˆLÝ*Ô7ØØØØ!Øñô ˆEð !ñ 3	)à&ð å! )Ð.LÑ"LÑMÔM�Ø(ð 
2Ý”OØØ#Ø.2Ø0GØ'CØ*/ðñ ô ð ð õ ”I˜e [Ñ1Ô1Ð1Ø�å  Ð-=Ñ!=Ñ>Ô>ˆNð5Ý)Ô8Ñ:Ô:�Ø7E�Ô4Ý7BÔ7YÔ7j�Ô4å˜k­4¬?Ñ;Ô;ð Ý(¨Ñ5Ô5ð Ý(ðiñô ð õ
 7RÐR]Ñ6^Ô6^Ñ3�N OØ×9Ò9½$¸~Ñ:NÔ:NÕPTÐUdÑPeÔPeÑfÔfÐfØ"-×"?Ò"?Ñ"AÔ"A�K�Kð )ð Ð-Bð Ø×8Ò8ØQÐShñô ð õ #Ô3°KÀÐYoÐXpÐqÑqÔq�ð �DøÝð 5ð 5ð 5Ý—’Øoñô ð õ —’�YÔ1Ñ3Ô3Ñ4Ô4Ð4Ð4Ð4ð	5øøøð )ˆKàð !	3ð
 Ð Ý! )Ð.LÑ"LÑMÔM�Ø(ð 
2Ý”OØØ#Ø.2Ø0GØ'CØ*/ðñ ô ð ð õ ”I˜e [Ñ1Ô1Ð1Ø�å˜+¥t¤Ñ7Ô7ð 	Ý!¥$ }Ñ"5Ô"5Ð8JÑ"JÑKÔK�Ý”ØØØ*.Ø,CØ#?Ø&+ðñ ô ð õ #& iÐ2LÑ&LÑ"MÔ"MÐÝÔ ×2Ò2°;Ð@SÑTÔTÐTÝ”IÐ1Ñ2Ô2ˆEðKe3ð e3ð e3ñ e3ô e3ð e3ð e3ð e3ð e3ð e3ð e3øøøð e3ð e3ð e3ð e3ðN €}Ý)¨+µt´ÑGÔGÐc��ÍTÌYÐWbÑMcÔMcˆå˜UÑ#Ô#Ð#àð ,ÝŒØØØ"&Ø$;Ø+Ø7Ø#ð	
ñ 	
ô 	
ð 	
ð 	
ð 	
õ 	Œ	�%Ð*Ñ+Ô+Ð+Ð+Ð+s9   ¾CMÄC2HÈMÈAIÉMÉIÉC(MÍMÍM)NNFFFFr	   Fr   FFNr
   )Úloggingr"   r<   Úpathlibr   r'   r.   Ú&onnxruntime.tools.symbolic_shape_inferr   Ú#onnxruntime.transformers.onnx_utilsr   r   Úquant_utilsr   Ú	getLoggerÚ__name__r$   r+   r(   ÚboolÚintrL   © ó    rK   ú<module>rX      s   ðð €€€Ø €€€Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€à Ð Ð Ð Ø IÐ IÐ IÐ IÐ IÐ IØ ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^à 1Ð 1Ð 1Ð 1Ð 1Ð 1à	ˆÔ	˜8Ñ	$Ô	$€ð 8<Ø+/Ø#Ø!Ø %ØØØ#ØØ"'Ø$)Ø)-Ø(,ðh,ð h,Ø�t‘˜dœoÑ-°Ñ4ðh,à˜T‘z DÑ(ðh,ð ðh,ð ð	h,ð
 ðh,ð ðh,ð ðh,ð ðh,ð ðh,ð  ðh,ð "ðh,ð   $™Jðh,ð #&ðh,ð 
ðh,ð h,ð h,ð h,ð h,ð h,rW   