o
    Xi                     @  s|   d Z ddlmZ ddgZddlZddlZddlZddlm	Z	 e
eZdddZG dd dejjZdddddddZdS )z0Shape inference pass using onnx.shape_inference.    )annotationsShapeInferencePassinfer_shapesN)_c_api_utilsmodelir.Modelinferred_protoonnx.ModelProtoreturnboolc                 C  s   t j|}d}t|  | D ]L\}}t j|}t j|}| D ]7\}}	||v rT|| }
|	j|
jkrB|
jdurB|
j|	_d}|	j	|
j	krS|
j	durS|
j	|	_	d}q%t
d||j q%q|S )a)  Merge the shape inferred model with the original model.

    Args:
        model: The original IR model.
        inferred_proto: The ONNX model with shapes and types inferred.

    Returns:
        A tuple containing the modified model and a boolean indicating whether the model was modified.
    FNTz'Value %s not found in inferred graph %s)irserdedeserialize_modelzipgraphsconveniencecreate_value_mappingitemsshapedtypeloggerwarningname)r   r   inferred_modelmodifiedoriginal_graphinferred_graphoriginal_valuesinferred_valuesr   valueinferred_value r!   Y/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/passes/common/shape_inference.py_merge_func   s(   
r#   c                      s0   e Zd ZdZ	dd fd	d
ZdddZ  ZS )r   z0This pass performs shape inference on the graph.T
check_typer   strict_mode	data_propr
   Nonec                   s    t    || _|| _|| _dS )aS  Initialize the shape inference pass.

        If inference fails, the model is left unchanged.

        Args:
            check_type: If True, check the types of the inputs and outputs.
            strict_mode: If True, use strict mode for shape inference.
            data_prop: If True, use data propagation for shape inference.
        N)super__init__r$   r%   r&   )selfr$   r%   r&   	__class__r!   r"   r)   8   s   

zShapeInferencePass.__init__r   r   ir.passes.PassResultc              
     sz   d
 fdd}zt ||}W n  ty/ } ztjd|d tj|dW  Y d }~S d }~ww t||}tjj||d	S )Nprotor	   r
   c                   s   t jj|  j j jdS )Nr$   r%   r&   )onnxshape_inferencer   r$   r%   r&   )r.   r*   r!   r"   partial_infer_shapesJ   s   z5ShapeInferencePass.call.<locals>.partial_infer_shapesz3Shape inference failed: %s. Model is left unchanged)exc_infoF)r   )r.   r	   r
   r	   )	r   call_onnx_api	Exceptionr   r   r   passes
PassResultr#   )r*   r   r3   inferred_model_protoer   r!   r2   r"   callI   s   
zShapeInferencePass.call)TTT)r$   r   r%   r   r&   r   r
   r'   )r   r   r
   r-   )__name__
__module____qualname____doc__r)   r;   __classcell__r!   r!   r+   r"   r   5   s
    Tr/   r$   r%   r&   c                C  s   t |||d| jS )a|  Perform shape inference on the model.

    Args:
        model: The model to perform shape inference on.
        check_type: If True, check the types of the inputs and outputs.
        strict_mode: If True, use strict mode for shape inference.
        data_prop: If True, use data propagation for shape inference.

    Returns:
        The model with shape inference applied.
    r/   )r   r   )r   r$   r%   r&   r!   r!   r"   r   \   s   )r   r   r   r	   r
   r   )
r   r   r$   r   r%   r   r&   r   r
   r   )r?   
__future__r   __all__loggingr0   onnx_irr   onnx_ir.passes.commonr   	getLoggerr<   r   r#   r7   InPlacePassr   r   r!   r!   r!   r"   <module>   s    

*