
     NgL!                     >   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 j        e          Z	 	 	 	 	 	 	 	 	 	 	 	 	 ddeeeeej        f                  deeeef                  dedededededededededee         deddf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           	      d   | |                     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                  rtt+          |           rt-          d          t/          |           \  }}|                    t3          |          t3          |                     |                                 } t          j        | |dg          }~nU# t8          $ 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	        |           }tE          |           |	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.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.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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                           d/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/quantization/shape_inference.pyquant_pre_processrM      s   V '++,>EE"""((*J(((		$L	9	9	9 _3]''	" 		KK@AAA*4[$/*R*Rn;;X\XabmXnXnL*7! E ! -	)& !).L"LMM( 
2O#.20G'C*/     Ie[111 -=!=>>N5)8::7E47B7Y7j4k4?;; 	B(55 (i  
 7RR]6^6^3NO99$~:N:NPTUdPePefff"-"?"?"A"AK"3KYoXp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#.20G'C*/     Ie[111+t77 	!$}"5"58J"JKK*.,C#?&+    #&i2L&L"M"M 22;@STTTI122E_3 _3 _3 _3 _3 _3 _3 _3 _3 _3 _3 _3 _3 _3 _3B })+tGGcTYWbMcMcU### ,"&$;+7#	
 	
 	
 	
 	
 	
 		%*+++++s9   CL0CG*)L0*AH<9L0;H<<C(L00L47L4)NNFFFFr   Fr   FFNr   )loggingr$   r=   pathlibr   typingr   r   r)   r0   &onnxruntime.tools.symbolic_shape_inferr   #onnxruntime.transformers.onnx_utilsr   r   quant_utilsr
   	getLogger__name__r&   r-   r*   boolintrM        rL   <module>rZ      s               " " " " " " " "      I I I I I I ^ ^ ^ ^ ^ ^ ^ ^ 1 1 1 1 1 1		8	$	$ @D48#! %#"'$),0(,b, b,%T4? :;<b,c4i 01b, b, 	b,
 b, b, b, b, b,  b, "b, %SMb, #&b, 
b, b, b, b, b, b,rY   