o
    ;ήc~                     @   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
 e eZ							 					dd
edededededededededededededdfddZdS )    N)Path)SymbolicShapeInference   )add_pre_process_metadataF./   input_model_path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                 C   s  t jdd}t|}d}|s td tt| ||||}|sp|s2t	|d } t
||  d}t	|d }zt }||_tjj|_tj| |dgd}W n tym } ztd	 tt  W Y d}~nd}~ww |} |s|durt	|d } |	rtj|| d
|
|dd nt
||  d}t	|d }tj| | t|}W d   n1 sw   Y  |du rt| }t| |	rtj||d
|
||dd dS t
|| dS )a  Shape inference and model optimization, in preparation for quantization.

    Args:
        input_model_path: Path to the input model file")
        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
    z
pre.quant.)prefixNz&Performing symbolic shape inference...zsymbolic_shape_inferred.onnxzoptimized.onnxCPUExecutionProvider)	providerszYONNX Runtime Model Optimization Failed! Consider rerun with option `--skip_optimization'.TF)r   r   size_thresholdconvert_attributezonnx_shape_inferred.onnx)r   r   locationr   r   )tempfileTemporaryDirectoryr   loggerinfor   infer_shapesonnxloadstrsaveonnxruntimeSessionOptionsoptimized_model_filepathGraphOptimizationLevelORT_ENABLE_BASICgraph_optimization_levelInferenceSession	Exceptionerror	traceback
format_exc
save_modelshape_inferenceinfer_shapes_pathr   )r	   r
   r   r   r   r   r   r   r   r   r   r   r   quant_tmp_dir	temp_pathmodelopt_model_pathsess_option_einferred_model_path r<   O/tmp/pip-target-vg8gfxp4/lib/python/onnxruntime/quantization/shape_inference.pyquant_pre_process   s   )

	
<


r>   )FFFFr   Fr   FFr   r   )loggingr   r/   pathlibr   r"   r&   &onnxruntime.tools.symbolic_shape_inferr   quant_utilsr   	getLogger__name__r   r$   boolintr>   r<   r<   r<   r=   <module>   sb   
	
