o
    Xi                     @  s   d dl mZ ddgZd dlZd dlZd dlm  mZ d dl	m
Z
 d dlmZmZ eeZG dd de
jjZG dd	 d	e
jjZ	ddddZdS )    )annotationsConvertVersionPassconvert_versionN)ir)_c_api_utils_version_converterc                      s.   e Zd ZdZdd fd	d
ZdddZ  ZS )r   a  Convert the model to the specified ONNX opset version.

    This pass leverages the onnxscript version converter to convert the model. If
    the conversion is not supported, it falls back to the onnx C API to convert
    the model. This pass is in-place.

    The pass is an no-op if the c-api fails.

    Attributes:
        target_version: The target ONNX opset version to convert the model to.
        fallback: Whether to fallback to the onnx version converter if the
            target version is not supported. Default is False.
    Ftarget_versionintfallbackboolreturnNonec                   sB   t    || _|| _tjt||dt	 t
 t | _d S )Nr   r
   )super__init__r   r
   r   passes
Sequential_ConvertVersionPasscommon_passesRemoveUnusedNodesPassRemoveUnusedFunctionsPassRemoveUnusedOpsetsPassconvert_passselfr   r
   	__class__ Y/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/version_converter/__init__.pyr   $   s   

zConvertVersionPass.__init__modelir.Modelir.passes.PassResultc                 C  s
   |  |S N)r   )r   r   r   r   r   call2   s   
zConvertVersionPass.call)Fr   r	   r
   r   r   r   r   r    r   r!   __name__
__module____qualname____doc__r   r#   __classcell__r   r   r   r   r      s    c                      s,   e Zd ZdZd fdd	ZdddZ  ZS )r   a
  Convert the model to the specified ONNX opset version.

    This pass leverages the onnxscript version converter to convert the model. If
    the conversion is not supported, it falls back to the onnx C API to convert
    the model. This pass is in-place.

    The pass is an no-op if the c-api fails.

    Attributes:
        target_version: The target ONNX opset version to convert the model to.
        fallback: Whether to fallback to the onnx version converter if the
            target version is not supported.
    r   r	   r
   r   r   r   c                   s   t    || _|| _d S r"   )r   r   r   r
   r   r   r   r   r   E   s   

z_ConvertVersionPass.__init__r   r    r!   c           	   
     s  d|j jv r|j jd }| jkrtj|dS  jr"t| jr1tj	| jd tj|dS  jsBt
d j tj|dS t
d j d fd
d}z	tj||d}W n" ty{ } zt
jd j|d tj|dW  Y d }~S d }~ww t|}|j jD ]}|j|j jv r|j j|j j|_|j | q|j jd t|j j }|j j  |j j| |j |_ tj|dS )N Fr   TzThe model version conversion is not supported by the onnxscript version converter and fallback is disabled. The model was not modified (target version: %d). Set fallback=True to enable fallback to the onnx c-api version converter.zThe model version conversion is not supported by the onnxscript version converter and fallback is enabled. The model will be converted using the onnx C API (target version: %d).protoonnx.ModelProtor   c                   s   t jj|  jdS )z$Partial function to check the model.r-   )onnxversion_converterr   r   )r.   r   r   r   _partial_convert_versionq   s   z:_ConvertVersionPass.call.<locals>._partial_convert_version)funcr   zeFailed to convert the model to the target version %d using the ONNX C API. The model was not modified)exc_info)r.   r/   r   r/   )graphopset_importsr   r   r   
PassResultr
   r   version_supportedr   loggerwarningr   call_onnx_api	Exception
from_protoinputsnameinitializersconst_valueregister_initializerlenclearextend)	r   r   onnx_opset_versionr3   converted_protoeconverted_modelinputuser_inputsr   r2   r   r#   J   s^   

	

	z_ConvertVersionPass.callr$   r%   r&   r   r   r   r   r   6   s    r   r   ir.Model | onnx.ModelProtor   r	   r   r   c                 C  sz   t | tjr| }t| } nd}t | tjsJ t||d|  |dur;|j  |j	dd= |j
t| j dS dS )a,  Convert the model to the specified ONNX opset version.

    Args:
        model: The model to convert.
        target_version: The target ONNX opset version.
        fallback: Whether to fallback to the onnx version converter if the
            target version is not supported. Default is False.
    Nr   )
isinstancer0   
ModelProtor   r>   Modelr   r6   Clear	functionsCopyFromto_proto)r   r   r
   model_protor   r   r   r      s   
r"   )r   rM   r   r	   r   r   )
__future__r   __all__loggingr0   onnx_ir.passes.commonr   commonr   
onnxscriptr   onnxscript.version_converterr   r   	getLoggerr'   r:   InPlacePassr   r   r   r   r   r   r   <module>   s   
!_