
     Tiq                       % S SK Jr  S SKJrJrJr  / SQrS SKrS SKJ	s  J
r  S SKJr  S SKJr  S SKJrJrJrJr  S SKJrJrJrJrJrJr  S S	KJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'  \" S
\RP                  \RR                  5      r*/ \%RV                  Q\RV                  Q\RV                  Q\ RV                  Q\$RV                  Q\#RV                  Q\RX                  " 5       Q\&RV                  Q\"RV                  Q\!RV                  Q\'RV                  Q7r-S\.S'    " S S\R                  R^                  5      r0 S     SS jjr1g)    )annotations)SequenceTypeVarUnion)merge_metadatapatternrewriteRewritePassMatchResultMatchContextRewriteRuleRewriteRuleClassBaseRewriteRuleSetRewriterContextMatchingTracerMatchStatusRULE_NAME_TAGN)ir)r   )r   r   r   r   )r   r   r   r   r   r   )_basic_rules_broadcast_to_matmul_cast_constant_of_shape_collapse_slices_fuse_batchnorm_fuse_pad_into_conv_fuse_relus_clips_min_max_to_clip_no_op_redundant_scatter_nd_remove_optional_bias_ModelProtoOrIrztuple[pattern.RewriteRule, ...]_DEFAULT_REWRITE_RULESc                  >   ^  \ rS rSr    SU 4S jjrSS jrSrU =r$ )r
   C   c                  > [         TU ]  5         [        U[        5      (       a(  U(       d  [	        S5      e[
        R                  " U5      n[        U[
        R                  5      (       d   eXl        g )Nzrules must not be empty)super__init__
isinstancer   
ValueErrorr   r   rules)selfr)   	__class__s     [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/rewriter/__init__.pyr&   RewritePass.__init__D   sZ    
 	eX&& !:;;**51E%!7!78888-2
    c                    U R                   R                  U5      nU(       a  [        SU S35        [        R                  R                  U[        U5      5      $ )NzApplied z" of general pattern rewrite rules.)r)   apply_to_modelprintr   passes
PassResultbool)r*   modelcounts      r,   callRewritePass.callR   sF    

))%0HUG#EFGyy##E4;77r.   )r)   )r)   z6Sequence[pattern.RewriteRule] | pattern.RewriteRuleSetreturnNone)r5   zir.Modelr9   zir.passes.PassResult)__name__
__module____qualname____firstlineno__r&   r7   __static_attributes____classcell__)r+   s   @r,   r
   r
   C   s$    3E3 
	38 8r.   r
   c                   Uc  [         nO	U(       d  U $ [        U [        R                  5      (       a"  [        R
                  R                  U 5      nSnOU nSn[        R                  R                  [        U5      [        R                  " 5       [        R                  " 5       [        R                  " 5       45      nU" U5      R                  nU(       a  [        R
                  R                  U5      $ U$ )a  Rewrite the model using the provided pattern rewrite rules.

Unused nodes, functions, and opsets will be removed after the rewrite.

Args:
    model: The model to be rewritten. Can be an ONNX ModelProto or an ir.Model.
    pattern_rewrite_rules: A sequence of pattern rewrite rules or a RewriteRuleSet.
        If not provided, default rules will be applied. If empty, no rules will be applied
        and the original model will be returned.

Returns:
    The rewritten model as the same type as the input model.
TF)r!   r'   onnx
ModelProtor   serdedeserialize_modelr2   PassManagerr
   common_passesRemoveUnusedNodesPassRemoveUnusedFunctionsPassRemoveUnusedOpsetsPassr5   serialize_model)r5   pattern_rewrite_rulesmodel_irprotorewrite_passs        r,   r	   r	   Y   s    $ $ 6"%))88--e499((-.//1335002		
L H%++Hxx''11Or.   )N)r5   r    rL   zCUnion[Sequence[pattern.RewriteRule], pattern.RewriteRuleSet] | Noner9   r    )2
__future__r   typingr   r   r   __all__rB   onnx_ir.passes.commonr2   commonrG   
onnxscriptr   onnxscript.rewriterr   onnxscript.rewriter._basicsr   r   r   r   !onnxscript.rewriter._rewrite_ruler   r   r   r   r   r    onnxscript.rewriter.rules.commonr   r   r   r   r   r   r   r   r   r   r   rC   Modelr    r)   basic_optimization_rulesr!   __annotations__InPlacePassr
   r	    r.   r,   <module>r_      s^   # + +   - -  ' ^ ^     +T__bhhG;\\;; ""; 	;
 ; ; **,;   ; ; ;   ; 7 8"))'' 82 ))) 	)r.   