o
    °ƒðjü&  ã                   @   sþ   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 ddlmZ ddlmZmZ e  e¡Z											
			 						ddeeB ejB dB deeB dB dededededededededededB deddfdd„ZdS )é    N)ÚPath)ÚSymbolicShapeInference)Úextract_raw_data_from_modelÚhas_external_dataé   )ÚReplaceUpsampleWithResize)Ú	ONNXModel)Úadd_pre_process_metadataÚ&save_and_reload_model_with_shape_inferFéÿÿÿé   Ú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              	   K   s8  | du r
|  dd¡} | dusJ ‚|dusJ dƒ‚tjdd���D}t|ƒ}d}|sCt d¡ t| tjƒr5| nt 	| ¡}t
 |||||¡}|r†dd„ |jD ƒ}t|ƒd	kr†|d
 j}|dk r†tt|ƒ|ƒ ¡  |j |d
 ¡ d}|j tj d|¡g¡ tj ||¡}t|ƒ}|�s|s§t|d ƒ} |	rŸtj|| d|
|dd� nt || ¡ d}t|d ƒ}zEt ¡ }||_tjj |_!t| tjƒrÝt"| ƒrÈt#dƒ‚t$| ƒ\}}| %t&|ƒt&|ƒ¡ |  '¡ } n
|rç|	rç| (dd¡ tj)| |dgd�}~W n t*�y   t +d¡ t +t, -¡ ¡ Y nw |} |�s\|du�r0t|d ƒ} |	�r(tj|| d|
|dd� nt || ¡ d}t| tjƒ�rJtt|ƒd ƒ} tj|| d|
|dd� t|d ƒ}tj. /| |¡ t 	|¡}W d  ƒ n	1 �sgw   Y  |du �rt| tjƒ�rz| nt 	| ¡}t0|ƒ |	�r”tj||d|
||dd� dS t ||¡ 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...c                 S   s    g | ]}|j r|j d kr|‘qS )zai.onnx)Údomain)Ú.0Úopset© r    úª/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/quantization/shape_inference.pyÚ
<listcomp>^   s     z%quant_pre_process.<locals>.<listcomp>r   r   é
   é   Ú 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'   )1ÚpopÚtempfileÚTemporaryDirectoryr   ÚloggerÚinfoÚ
isinstanceÚonnxÚ
ModelProtoÚloadr   Úinfer_shapesÚopset_importÚlenÚversionr   r   ÚapplyÚremoveÚextendÚhelperÚmake_opsetidÚversion_converterÚconvert_versionr
   Ú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Úai_onnx_domainÚopset_versionÚopt_model_pathÚsess_optionÚexternal_namesÚexternal_valuesÚsessÚinferred_model_pathr    r    r!   Úquant_pre_process   sÞ   +
û
ú	
ÿ
ÿÿü
ú	ú	
€Œ
v
ù
ra   )NNFFFFr   Fr   FFNr   )Úloggingr,   rP   Úpathlibr   r1   rB   Ú&onnxruntime.tools.symbolic_shape_inferr   Ú#onnxruntime.transformers.onnx_utilsr   r   Úfusionsr   Ú
onnx_modelr   Úquant_utilsr	   r
   Ú	getLoggerÚ__name__r.   r?   r2   ÚboolÚintra   r    r    r    r!   Ú<module>   sl   
óÿ
þýüûúùø	÷
öõôóñ