Ë
    ÓGìj"  ã                   ó,  — 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y)é    N)ÚPath)ÚOptionalÚUnion)ÚSymbolicShapeInference)Úextract_raw_data_from_modelÚhas_external_dataé   )Úadd_pre_process_metadataÚ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        |«      }d}|s_t        j                  d«       t        | t        j                  «      r| nt        j                  | «      }t        j                  |||||«      }|�s|sDt        |dz  «      } |	rt        j                  || d|
|d	¬
«       nt        j                  || «       d}t        |dz  «      }	 t        j                   «       }||_        t        j$                  j&                  |_        t        | t        j                  «      rXt+        | «      rt-        d«      ‚t/        | «      \  }}|j1                  t3        |«      t3        |«      «       | j5                  «       } t        j6                  | |dg¬«      }~|} |sÕ|�Dt        |dz  «      } |	rt        j                  || d|
|d	¬
«       nt        j                  || «       d}t        | t        j                  «      r2t        t        |«      dz  «      } t        j                  || d|
|d	¬
«       t        |dz  «      }t        j@                  jC                  | |«       t        j                  |«      }ddd«       €1t        | t        j                  «      r| nt        j                  | «      }tE        |«       |	rt        j                  ||d|
||d	¬«       yt        j                  ||«       y# t8        $ r@ t        j;                  d«       t        j;                  t=        j>                  «       «       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&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.Ú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                           úlC:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\onnxruntime/quantization/shape_inference.pyÚquant_pre_processrK      s8  € ðV ÐØ'×+Ñ+Ð,>ÀÓEˆØÐ"Ð"Ð"àÐ(ÐJÐ*JÓJÐ(ä	×	$Ñ	$¨LÕ	9¸]Ü˜Ó'ˆ	Øˆá"Ü�K‰KÐ@ÔAÜ*4°[Ä$Ç/Á/Ô*R™;ÔX\×XaÑXaÐbmÓXnˆLÜ*×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ä"×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÷ 
:ðB €}Ü)¨+´t·±ÔG‘ÌTÏYÉYÐWbÓMcˆä˜UÔ#áÜ�‰ØØØ"&Ø$;Ø+Ø7Ø#ö	
ô 	�	‰	�%Ð*Õ+øôw ò 5Ü—‘Øoôô —‘œY×1Ñ1Ó3×4ð	5ú÷m 
:Ð	9ús3   ¸CM&Ã?CLÇCM&ÌAM#ÍM&Í"M#Í#M&Í&M/)NNFFFFiÿÿÿFr   FFNi   )Úloggingr"   r;   Úpathlibr   Útypingr   r   r'   r.   Ú&onnxruntime.tools.symbolic_shape_inferr   Ú#onnxruntime.transformers.onnx_utilsr   r   Úquant_utilsr
   Ú	getLoggerÚ__name__r$   r+   r(   ÚboolÚintrK   © ó    rJ   Ú<module>rX      s%  ðó Û Û Ý ß "ã ã Ý Iß ^å 1à	ˆ×	Ñ	˜8Ó	$€ð @DØ48Ø#Ø!Ø %ØØØ#ØØ"'Ø$)Ø,0Ø(,ñb,Ø˜%  T¨4¯?©?Ð :Ñ;Ñ<ðb,à  c¨4 iÑ 0Ñ1ðb,ð ðb,ð ð	b,ð
 ðb,ð ðb,ð ðb,ð ðb,ð ðb,ð  ðb,ð "ðb,ð % S™Mðb,ð #&ðb,ð 
ôb,rW   