Ë
    cQêj2)  ã                   ó(  — 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                  | «      }d}|j                  D �cg c]   }|j                  r|j                  dk(  sŒ|‘Œ" }}t        |«      dk(  rd|d	   j                  }|d
k  rPt        t        |«      |«      j                  «        t
        j                  j!                  |d«      }t#        |«      }d}|s3	 d	dlm} t*        j-                  d«       |j/                  |||||«      }d}|�sN| }|rBt1        |dz  «      }|	rt        j2                  ||d|
|d¬«       nt        j4                  ||«       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¬«      }~|rt        j                  |«      }nd}|} |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                  |«      }|€1t	        | t
        j                  «      r| nt        j                  | «      }ddd«       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.)ÚprefixFzai.onnxr   r   é
   é   T)Ú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preprocessed.onnx)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symbolic_shape_inferred.onnxz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Úmodel_updatedÚopsetÚai_onnx_domainÚopset_versionr   ÚeÚoptimization_input_modelÚopt_model_pathÚsess_optionÚexternal_namesÚexternal_valuesÚsessÚinferred_model_paths                                 úm/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/onnxruntime/quantization/shape_inference.pyÚquant_pre_processr]      sW  € ðV ÐØ'×+Ñ+Ð,>ÀÓEˆØÐ"Ð"Ð"àÐ(ÐJÐ*JÓJÐ(ä	×	$Ñ	$¨LÔ	9ð Bh¸]Ü˜Ó'ˆ	Ü)¨+´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ð !ˆMâ à'2Ð$Ùä+.¨yÐ;NÑ/NÓ+OÐ(Ù(Ü—O‘OØØ0Ø.2Ø0GØ'CØ*/öô —I‘I˜eÐ%=Ô>ä  Ð-=Ñ!=Ó>ˆNð&5Ü)×8Ñ8Ó:�Ø7E�Ô4Ü7B×7YÑ7Y×7jÑ7j�Ô4äÐ6¼¿¹ÔHÜ(Ð)AÔBÜ(ðióð ô
 7RÐRjÓ6kÑ3�N OØ×9Ñ9¼$¸~Ó:NÔPTÐUdÓPeÔfØ/G×/YÑ/YÓ/[Ñ,ñ )Ñ-BØ×8Ñ8ØQÐShôô #×3Ñ3Ø,¨kÐF\ÐE]ô�ð
 Ù"ä ŸI™I nÓ5‘Eà �EØ"0�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àˆ=Ü#-¨k¼4¿?¹?Ô#K‘KÔQU×QZÑQZÐ[fÓQgˆE÷EBhôH ˜UÔ#áÜ�‰ØØØ"&Ø$;Ø+Ø7Ø#ö	
ô 	�	‰	�%Ð*Õ+ùòS røô ò Ü!ðqóð ðûðûôH ò 5Ü—‘Øoôô —‘œY×1Ñ1Ó3×4ð	5ú÷qBhð Bhúsq   ¸AQ%Â O7Â&O7Â*A6Q%Ä!O<Ä'BQ%Æ+C9PÊ$D
Q%Ï7Q%Ï<	PÐPÐPÐQ%ÐA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]   © ó    r\   ú<module>rj      s  ðó Û Û Ý ã ã ß ^å .Ý !ß Yà	ˆ×	Ñ	˜8Ó	$€ð 8<Ø+/Ø#Ø!Ø %ØØØ#ØØ"'Ø$)Ø)-Ø(,ñB,Ø�t‘˜dŸo™oÑ-°Ñ4ðB,à˜T‘z DÑ(ðB,ð ðB,ð ð	B,ð
 ðB,ð ðB,ð ðB,ð ðB,ð ðB,ð  ðB,ð "ðB,ð   $™JðB,ð #&ðB,ð 
ôB,ri   