o
    s::j®  ã                   @  sb  U d dl mZ d dlZd dlmZmZmZ g d¢Zd dlZd dl	m
  mZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZmZmZmZ d d	lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( e )e*¡Z+ed
ej,ej-ƒZ.g e&j/¢ej/¢e j/¢e!j/¢e%j/¢e$j/¢e 0¡ ¢e'j/¢e#j/¢e"j/¢e(j/¢R Z1de2d< G dd„ dej
j3ƒZ4	dddd„Z5dS )é    )ÚannotationsN)ÚSequenceÚTypeVarÚUnion)Úmerge_metadataÚpatternÚrewriteÚRewritePassÚMatchResultÚMatchContextÚRewriteRuleÚRewriteRuleClassBaseÚRewriteRuleSetÚRewriterContextÚMatchingTracerÚMatchStatusÚRULE_NAME_TAG)Ú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                      s(   e Zd Zd‡ fdd„Zdd
d„Z‡  ZS )r	   Úrulesú6Sequence[pattern.RewriteRule] | pattern.RewriteRuleSetÚreturnÚNonec                  sD   t ƒ  ¡  t|tƒr|stdƒ‚t |¡}t|tjƒsJ ‚|| _d S )Nzrules must not be empty)ÚsuperÚ__init__Ú
isinstancer   Ú
ValueErrorr   r   r!   )Úselfr!   ©Ú	__class__© úc/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnxscript/rewriter/__init__.pyr&   G   s   



zRewritePass.__init__Úmodelúir.Modelúir.passes.PassResultc                 C  s.   | j  |¡}|rt d|¡ tj |t|ƒ¡S )Nz,Applied %s of general pattern rewrite rules.)r!   Úapply_to_modelÚloggerÚinfor   ÚpassesÚ
PassResultÚbool)r)   r.   Úcountr,   r,   r-   ÚcallU   s   zRewritePass.call)r!   r"   r#   r$   )r.   r/   r#   r0   )Ú__name__Ú
__module__Ú__qualname__r&   r8   Ú__classcell__r,   r,   r*   r-   r	   F   s    r	   r.   Úpattern_rewrite_rulesúCUnion[Sequence[pattern.RewriteRule], pattern.RewriteRuleSet] | Noner#   c                 C  s~   |du rt }n|s| S t| tjƒrtj | ¡}d}n| }d}tj t	|ƒt
 ¡ t
 ¡ t
 ¡ f¡}||ƒj}|r=tj |¡S |S )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.
    NTF)r    r'   ÚonnxÚ
ModelProtor   ÚserdeÚdeserialize_modelr4   ÚPassManagerr	   Úcommon_passesÚRemoveUnusedNodesPassÚRemoveUnusedFunctionsPassÚRemoveUnusedOpsetsPassr.   Úserialize_model)r.   r=   Úmodel_irÚprotoÚrewrite_passr,   r,   r-   r   ]   s(   üÿ
r   )N)r.   r   r=   r>   r#   r   )6Ú
__future__r   ÚloggingÚtypingr   r   r   Ú__all__r?   Úonnx_ir.passes.commonr4   ÚcommonrD   Ú
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   Ú	getLoggerr9   r2   r@   ÚModelr   r!   Úbasic_optimization_rulesr    Ú__annotations__ÚInPlacePassr	   r   r,   r,   r,   r-   Ú<module>   sN    4
ÿþýüûúùø	÷
öõý