o
    Xi"                     @  s   d dl mZ d dlmZ g dZd dlZd dlm  mZ	 d dl
m  mZ d dlmZ d dl
mZmZ d dl
mZ d dlmZ ed	ejejZ	
d$ddejejddd%ddZd&ddZd'ddZd(d d!Zd(d"d#ZdS ))    )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	   _ModelProtoOrIr   T)onnx_shape_inferencestop_if_no_changeinput_size_limitoutput_size_limitr   modelnum_iterationsintr   boolr   r   r   r   returnc          	   	   C  sh   t | tjrt| ||||||d | S t | tjsJ tj| }t|||||||d tj|}|S )a  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.
    )r   r   r   r   r   r   )	
isinstancer   Modelr	   onnx
ModelProtoserdedeserialize_modelserialize_model)	r   r   r   r   r   r   r   model_ir	new_proto r!   Q/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/optimizer/__init__.pyr
      s0   	
r
   ir.ModelNonec                 C  s   | j rt |  dS dS )z5Inline all function calls (recursively) in the model.N)	functionscommon_passes
InlinePass)r   r!   r!   r"   r   W   s   ir.Model | onnx.ModelProto$constant_folding.FoldConstantsResultc                 O  s~   t | tjrtj| g|R i |S t | tjsJ | }tj|} tj| g|R i |}tj	| }|
  || |S )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    r!   r!   r"   r   ]   s   
r   c                 C  X   t | tjrt |  dS tj| }t |j}tj|}| 	  | 
| dS )z*Removes unused nodes from a model inplace.N)r   r   r   r&   RemoveUnusedNodesPassr   r   r   r   r+   r,   r   r   r    r!   r!   r"   r   o      r   c                 C  r1   )z.Removes unused functions from a model inplace.N)r   r   r   r&   RemoveUnusedFunctionsPassr   r   r   r   r+   r,   r3   r!   r!   r"   remove_unused_functions{   r4   r6   )r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r#   r   r$   )r   r(   r   r)   )r   r(   r   r$   )
__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   r6   r!   r!   r!   r"   <module>   s.   
9

