
     Ti"                    V   S SK Jr  S SKJr  / SQrS SKrS SKJs  Jr	  S SK
Js  Jr  S SKJr  S SK
JrJr  S SK
Jr  S SKJr  \" S	\R,                  \R.                  5      r SS
S
\R2                  \R4                  S
S.               SS jjjrSS jr    SS jrSS jrSS jrg)    )annotations)TypeVar)basic_constant_propagationfold_constants_irfold_constantsFOLDED_FROM_KEYinlineoptimize_iroptimizeremove_unused_nodesN)ir)r   r   )r   )r
   _ModelProtoOrIrT)onnx_shape_inferencestop_if_no_changeinput_size_limitoutput_size_limitr	   c          
     B   [        U [        R                  5      (       a  [        U UUUUUUS9  U $ [        U [        R
                  5      (       d   e[        R                  R                  U 5      n[        UUUUUUUS9  [        R                  R                  U5      nU$ )aP  Optimizes a model.

Args:
    model: The model to be optimized.
    num_iterations: Number of times the optimization loop is repeated.
    onnx_shape_inference: Applies node-level shape-inference as part of optimization
    input_size_limit: Will not apply constant folding to ops with any input of size
        greater than this. Does not apply to special ops like Shape() and Size().
    output_size_limit: Will not rewrite any foldable-op into a Constant op if the size
        of the output tensor is greater than this.
    stop_if_no_change: Stop the optimization loop if no change is detected in an iteration.
    inline: If True, inlines all functions in the model.

Returns:
    The optimized model. If the input was a ModelProto, the output will also be a
    ModelProto. If the input was an ir.Model, the output will also be an ir.Model.
)num_iterationsr   r   r   r   r	   )	
isinstancer   Modelr
   onnx
ModelProtoserdedeserialize_modelserialize_model)	modelr   r   r   r   r   r	   model_ir	new_protos	            \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/optimizer/__init__.pyr   r      s    6 %"" 	)!5/-/	
 %111188--e4)!5/-/	
 HH,,X6	    c                ^    U R                   (       a  [        R                  " 5       " U 5        gg)z5Inline all function calls (recursively) in the model.N)	functionscommon_passes
InlinePass)r   s    r   r	   r	   W   s      "5) r    c                   [        U [        R                  5      (       a  [        R                  " U /UQ70 UD6$ [        U [
        R                  5      (       d   eU n[        R                  R                  U5      n [        R                  " U /UQ70 UD6n[        R                  R                  U 5      nUR                  5         UR                  U5        U$ )z#Fold constants in a model in place.)r   r   r   constant_foldingr   r   r   r   r   r   ClearCopyFrom)r   argskwargsmodel_protoresultr   s         r   r   r   ]   s     %""..uFtFvFF%1111**;7!00HHHHH,,U3	Y'r    c                   [        U [        R                  5      (       a  [        R                  " 5       " U 5        g[        R
                  R                  U 5      n[        R                  " 5       " U5      R                  n[        R
                  R                  U5      nU R                  5         U R                  U5        g)z*Removes unused nodes from a model inplace.N)r   r   r   r#   RemoveUnusedNodesPassr   r   r   r   r'   r(   r   r   r   s      r   r   r   o   sz    %""++-e488--e4 668BHHHH,,X6	y!r    c                   [        U [        R                  5      (       a  [        R                  " 5       " U 5        g[        R
                  R                  U 5      n[        R                  " 5       " U5      R                  n[        R
                  R                  U5      nU R                  5         U R                  U5        g)z.Removes unused functions from a model inplace.N)r   r   r   r#   RemoveUnusedFunctionsPassr   r   r   r   r'   r(   r/   s      r   remove_unused_functionsr2   {   sz    %""//1%888--e4 ::<XFLLHH,,X6	y!r    )   )r   r   r   intr   boolr   r5   r   r4   r   r4   r	   r5   returnr   )r   zir.Modelr6   None)r   ir.Model | onnx.ModelProtor6   z$constant_folding.FoldConstantsResult)r   r8   r6   r7   )
__future__r   typingr   __all__r   onnx_ir.passes.commonpassescommonr#   &onnxscript.optimizer._constant_folding	optimizer_constant_foldingr&   
onnxscriptr   r   r   r   r   onnxscript.optimizer._optimizerr
   r   r   r   &DEFAULT_CONSTANT_FOLD_INPUT_SIZE_LIMIT'DEFAULT_CONSTANT_FOLD_OUTPUT_SIZE_LIMITr   r	   r   r2    r    r   <module>rG      s    # 	  - - A A  ^ V 7+T__bhhG
 6 "&",SS-UU666 	6
 6 6 6 6 6r*%)$	"	"r    