o
    Xic                     @  s   d dl mZ d dlZd dlmZ d dlZd dlm  m	Z
 d dlmZ d dlmZ eeZ	dddejejdd	 dd
dddZdS )    )annotationsN)Callable)rewriter)_constant_folding   Tc                 C  s   d S )N )noder   r   S/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/optimizer/_optimizer.py<lambda>   s    r
   )onnx_shape_inferencestop_if_no_changeinput_size_limitoutput_size_limitshould_foldinlinemodelir.Modelnum_iterationsintr   boolr   r   r   r    Callable[[ir.Node], bool | None]r   returnNonec             	   C  s   t jjtj||||dttjt	 t
 t g||dt	 tjdddt t t t t g}|rAt g|}t jj| }	|	jsLJ |	| }
|
j| u sWJ d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
        stop_if_no_change: Stop the optimization loop if no change is detected in an iteration.
        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.
        should_fold: An optional function that takes a node and returns True if
            the node should be considered for folding.
            The function should return True/False value to indicate if this particular
            node should be folded, or None to use the default folding rules.
        inline: If True, inlines all functions in the model.
    )shape_inferencer   r   r   )steps
early_stopTr   )lift_all_constants
size_limitN)irpassesPassManagerr   FoldConstantsPassr   RewritePass_DEFAULT_REWRITE_RULEScommon_passesRemoveUnusedNodesPassRemoveUnusedFunctionsPassRemoveUnusedOpsetsPassLiftConstantsToInitializersPass'LiftSubgraphInitializersToMainGraphPassDeduplicateInitializersPass"CommonSubexpressionEliminationPassOutputFixPassNameFixPass
InlinePass
Sequentialin_placer   )r   r   r   r   r   r   r   r   r   optimizer_passresultr   r   r	   optimize_ir   s:   

r3   )r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )
__future__r   loggingtypingr   onnx_irr   onnx_ir.passes.commonr   commonr$   
onnxscriptr   onnxscript.optimizerr   	getLogger__name__logger&DEFAULT_CONSTANT_FOLD_INPUT_SIZE_LIMIT'DEFAULT_CONSTANT_FOLD_OUTPUT_SIZE_LIMITr3   r   r   r   r	   <module>   s"   
