Ë
    ÝÍ:jA'  ã                   ó(  — 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m	Z	 ddl
mZ ddlmZ ddlmZmZ  e j"                  e«      Z	 	 	 	 	 	 	 	 	 	 	 	 	 ddeez  ej*                  z  dz  d	eez  dz  d
ededededededededededz  deddfd„Zy)é    N)ÚPath)Úextract_raw_data_from_modelÚhas_external_dataé   )ÚReplaceUpsampleWithResize)Ú	ONNXModel)Úadd_pre_process_metadataÚ&save_and_reload_model_with_shape_inferÚ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           	      ó¢  — | €|j                  dd«      } | €J ‚|€J d«       ‚t        j                  d¬«      5 }t        |«      }t	        | t
        j                  «      r| nt        j                  | «      }|j                  D �cg c]   }|j                  r|j                  dk(  sŒ|‘Œ" }}t        |«      dk(  rb|d   j                  }|d	k  rNt        t        |«      |«      j                  «        t
        j                  j!                  |d
«      }t#        |«      }|s1	 ddlm} t*        j-                  d«       |j/                  |||||«      }|�s4|sDt1        |dz  «      } |	rt        j2                  || d|
|d¬«       nt        j4                  || «       d}t1        |dz  «      }	 t7        j8                  «       }||_        t6        j<                  j>                  |_         t	        | t
        j                  «      rYtC        | «      rtE        d«      ‚tG        | «      \  }}|jI                  tK        |«      tK        |«      «       | jM                  «       } n|r|	r|jO                  dd«       t7        jP                  | |dg¬«      }~|} |sÕ|�Dt1        |dz  «      } |	rt        j2                  || d|
|d¬«       nt        j4                  || «       d}t	        | t
        j                  «      r2t1        t        |«      dz  «      } t        j2                  || d|
|d¬«       t1        |dz  «      }t
        jZ                  j]                  | |«       t        j                  |«      }ddd«       €1t	        | t
        j                  «      r| nt        j                  | «      }t_        |«       |	rt        j2                  ||d|
||d¬«       yt        j4                  ||«       yc c}w # t(        $ r}t)        d«      |‚d}~ww xY w# tR        $ r@ t*        jU                  d«       t*        jU                  tW        jX                  «       «       Y �Œ¿w xY w# 1 sw Y   ŒëxY w)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ai.onnxr   r   é
   é   )ÚSymbolicShapeInferencez¨sympy is required for symbolic shape inference in quantization preprocessing. Install with: 'pip install sympy' or pass skip_symbolic_shape=True to quant_pre_process().z&Performing symbolic shape inference...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    )0ÚpopÚtempfileÚTemporaryDirectoryr   Ú
isinstanceÚonnxÚ
ModelProtoÚloadÚopset_importÚdomainÚlenÚversionr   r   ÚapplyÚversion_converterÚconvert_versionr
   Ú&onnxruntime.tools.symbolic_shape_inferr   ÚImportErrorÚloggerÚinfoÚinfer_shapesÚ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ÚopsetÚai_onnx_domainÚopset_versionr   ÚeÚopt_model_pathÚsess_optionÚexternal_namesÚexternal_valuesÚsessÚinferred_model_paths                               ú}/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/onnxruntime/quantization/shape_inference.pyÚquant_pre_processr[      s"  € ðV ÐØ'×+Ñ+Ð,>ÀÓEˆØÐ"Ð"Ð"àÐ(ÐJÐ*JÓJÐ(ä	×	$Ñ	$¨LÔ	9ð v3¸]Ü˜Ó'ˆ	Ü)¨+´t·±ÔG‘ÌTÏYÉYÐWbÓMcˆð
 .3×-?Ñ-?Öq EÀuÇ|Â|ÐW\×WcÑWcÐgpÓWpš%ÐqˆÐqÜˆ~Ó !Ò#Ø*¨1Ñ-×5Ñ5ˆMØ Ò"Ü)¬)°EÓ*:¸MÓJ×PÑPÔRÜ×.Ñ.×>Ñ>¸uÀbÓI�Ü>¸uÓE�á"ðÝYô �K‰KÐ@ÔAØ*×7Ñ7ØØØØ!ØóˆEò !á&ä! )Ð.LÑ"LÓM�Ù(Ü—O‘OØØ#Ø.2Ø0GØ'CØ*/öô —I‘I˜e [Ô1Ø�ä  Ð-=Ñ!=Ó>ˆNð5Ü)×8Ñ8Ó:�Ø7E�Ô4Ü7B×7YÑ7Y×7jÑ7j�Ô4ä˜k¬4¯?©?Ô;Ü(¨Ô5Ü(ðióð ô
 7RÐR]Ó6^Ñ3�N OØ×9Ñ9¼$¸~Ó:NÔPTÐUdÓPeÔfØ"-×"?Ñ"?Ó"A‘Kñ )Ñ-BØ×8Ñ8ØQÐShôô #×3Ñ3°KÀÐYoÐXpÔq�ð ð )ˆKáð
 Ð Ü! )Ð.LÑ"LÓM�Ù(Ü—O‘OØØ#Ø.2Ø0GØ'CØ*/öô —I‘I˜e [Ô1Ø�ä˜+¤t§¡Ô7Ü!¤$ }Ó"5Ð8JÑ"JÓK�Ü—‘ØØØ*.Ø,CØ#?Ø&+õô #& iÐ2LÑ&LÓ"MÐÜ× Ñ ×2Ñ2°;Ð@SÔTÜ—I‘IÐ1Ó2ˆE÷mv3ðp €}Ü)¨+´t·±ÔG‘ÌTÏYÉYÐWbÓMcˆä˜UÔ#áÜ�‰ØØØ"&Ø$;Ø+Ø7Ø#ö	
ô 	�	‰	�%Ð*Õ+ùòC røô ò Ü!ðqóð ðûðûôv ò 5Ü—‘Øoôô —‘œY×1Ñ1Ó3×4ð	5ú÷[v3ð v3úsq   ¸AQÂ OÂ$OÂ(A4QÄOÄ#BQÆ%CO9ÊCQÏQÏ	O6Ï%O1Ï1O6Ï6QÏ9AQÐ>QÑQÑQÑQ)NNFFFFiÿÿÿFr   FFNi   )Úloggingr%   rH   Úpathlibr   r(   r:   Ú#onnxruntime.transformers.onnx_utilsr   r   Úfusionsr   Ú
onnx_modelr   Úquant_utilsr	   r
   Ú	getLoggerÚ__name__r4   r7   r)   ÚboolÚintr[   © ó    rZ   ú<module>rh      s  ðó Û Û Ý ã ã ß ^å .Ý !ß Yà	ˆ×	Ñ	˜8Ó	$€ð 8<Ø+/Ø#Ø!Ø %ØØØ#ØØ"'Ø$)Ø)-Ø(,ñy,Ø�t‘˜dŸo™oÑ-°Ñ4ðy,à˜T‘z DÑ(ðy,ð ðy,ð ð	y,ð
 ðy,ð ðy,ð ðy,ð ðy,ð ðy,ð  ðy,ð "ðy,ð   $™Jðy,ð #&ðy,ð 
ôy,rg   