U
    U?hL!                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ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 eZdeeeeejf  eeeef  eeeeeeeeeee eddddZdS )    N)Path)OptionalUnion)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              	   K   s  | dkr| dd} | dk	s t|dk	s0tdtjdd}t|}d}|std t| tj	rj| nt
| }t|||||}|s|st|d } |	rtj|| d|
|d	d
 nt||  d}t|d }zxt }||_tjj|_t| tj	r6t| rtdt| \}}|t|t| |  } tj| |dgd}~W n. tk
rz   td tt   Y nX |} |s"|dk	rt|d } |	rtj|| d|
|d	d
 nt||  d}t| tj	rtt|d } tj|| d|
|d	d
 t|d }tj!"| | t
|}W 5 Q R X |dkrRt| tj	rH| nt
| }t#| |	rztj||d|
||d	d n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
    NZ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.onnxzModelProto 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.Z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AssertionErrortempfileTemporaryDirectoryr   loggerinfo
isinstanceonnx
ModelProtoloadr   Zinfer_shapesstrZ
save_modelsaveonnxruntimeZSessionOptionsZoptimized_model_filepathZGraphOptimizationLevelZORT_ENABLE_BASICZgraph_optimization_levelr   
ValueErrorr   Zadd_external_initializerslistZSerializeToStringZInferenceSession	Exceptionerror	traceback
format_excZshape_inferenceZinfer_shapes_pathr	   )r   r   r   r   r   r   r   r   r   r   r   r   r   Zdeprecated_kwargsZquant_tmp_dirZ	temp_pathmodelZloaded_modelZopt_model_pathZsess_optionZexternal_namesZexternal_valuessessZinferred_model_path r4   Z/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/quantization/shape_inference.pyquant_pre_process   s    +
	


		

r6   )NNFFFFr
   Fr   FFNr   )loggingr!   r0   pathlibr   typingr   r   r&   r+   Z&onnxruntime.tools.symbolic_shape_inferr   Z#onnxruntime.transformers.onnx_utilsr   r   Zquant_utilsr	   	getLogger__name__r#   r)   r'   boolintr6   r4   r4   r4   r5   <module>   sN   
             