o
    öXœiS  ã                   @   st   d Z ddlmZ ddlZddlZddlm  mZ	 dej
deej ddfdd„Zd	ejd
eej dejfdd„ZdS )zPA utility function to replace custom operations in a model with their expansionsé    )ÚSequenceNÚirmodelÚirfunctionsÚreturnc                 C   sL   | j }t|ƒdkrtdƒ‚|D ]}||| ¡ < qt ¡ | ƒ t ¡ | ƒ dS )a6  A utility function to replace custom operations in a model with their expansions:

    The model is updated in-place.

    Args:
        irmodel: An ONNX model possibly containing calls to custom operations.
        irfunctions: A sequence of functions defining the expansions for the custom operations.


    r   z.Input model cannot have model-local functions.N)Ú	functionsÚlenÚ
ValueErrorÚ
identifierÚcommon_passesÚ
InlinePassÚRemoveUnusedOpsetsPass)r   r   Úmodel_functionsÚfunc© r   úL/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/utils/replace.pyÚreplace_functions_inplace   s   r   Úmodelr   c                 C   s,   t  | ¡}dd„ |D ƒ}t||ƒ t  |¡S )au  A utility function to replace custom operations in a model with their expansions:
    Args:
        model: An ONNX ModelProto possibly containing calls to custom operations.
        functions: A sequence of FunctionProto defining the expansions for the custom operations.

    Returns:
        An updated ModelProto with custom operations replaced by their expansions.
    c                 S   s   g | ]}t  |¡‘qS r   )ÚirÚ
from_proto)Ú.0r   r   r   r   Ú
<listcomp>/   s    z%replace_functions.<locals>.<listcomp>)r   r   r   Úto_proto)r   r   r   r   r   r   r   Úreplace_functions#   s   


r   )Ú__doc__Útypingr   ÚonnxÚonnx_irr   Úonnx_ir.passes.commonÚpassesÚcommonr
   ÚModelÚFunctionr   Ú
ModelProtoÚFunctionProtor   r   r   r   r   Ú<module>   s   ÿÿþ