o
    vi&                     @   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
|||||}|rdd |jD }t|d	kr|d
 j}|dk rtt||  |j|d
  d}|jtjd|g tj||}t|}|s|st|d } |	rtj|| d|
|dd nt||  d}t|d }zEt }||_tjj |_!t| tjrt"| rt#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| |	rt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    g/home/air/biblejyuku/back/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.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.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   

	
