o
    gu                     @   s   d dl Z d dlZd dlZd dlmZ ejeZej	ej
edr/ejej
ed nejej
ed d dlmZmZmZ e eZG dd deZdS )	    N)Dictz ../tools/symbolic_shape_infer.pyz../toolsz..)SymbolicShapeInferenceget_shape_from_type_protosympyc                       sR   e Zd Zd fdd	Zddeeef d	efd
dZdd Zdd Z	dd Z
  ZS )SymbolicShapeInferenceHelperr   TFc                    s.   t  |||| || _d| _d| _i | _d S )NF)super__init__model_all_shapes_inferred_is_inferred_dynamic_axis_mapping_)selfmodelverboseint_max
auto_mergeguess_output_rank	__class__ b/var/www/visachat/venv/lib/python3.10/site-packages/onnxruntime/transformers/shape_infer_helper.pyr	      s
   
z%SymbolicShapeInferenceHelper.__init__   dynamic_axis_mappingmax_runsc                 C   s   |dusJ | j r| j|kr| jS || _| | j d}| jr<td|  |  | _|d7 }|dkr9||kr9n| jsd| _ | jS )a  Run shape inference, and try replace dynamic axis from string to integer when mapping is provided.

        Args:
            dynamic_axis_mapping (_type_): a dictionary with name of dynamic axis as key, like {"batch_size" : 4}
            max_runs (int, optional): limit maximum number of runs to avoid infinite loop. Defaults to 200.

        Returns:
            bool: whether all shapes has been inferred or not.
        Nr   zshape infer run    T)	r   r   r   _preprocessr
   run_loggerdebug_infer_impl)r   r   r   countr   r   r   infer   s   

z"SymbolicShapeInferenceHelper.inferc                 C   s   g }|  ||}|rF|D ]9}t|tr:|| jv r!|| j|  q|| jv r/|| j|  q|tj|dd q|dus@J || q|S )zQOverride it to ensure shape inference by giving the actual value of dynamic axis.T)integerN)
_get_shape
isinstancestrr   appendsymbolic_dims_r   Symbol)r   nodeidxsympy_shapeshapedimr   r   r   _get_sympy_shape=   s   


z-SymbolicShapeInferenceHelper._get_sympy_shapec                 C   s|   | j sJ || jvrtdt|  dS | j| j}t|}|dur<t|D ]\}}t|tr;|| jv r;| j| ||< q&|S )zGet shape of an edge.

        Args:
            edge (str): name of edge

        Returns:
            Optional[List[int]]: the shape, or None if shape is unknown
        zCannot retrieve the shape of N)	r   	known_vi_printr&   typer   	enumerater%   r   )r   edge
type_protor-   ir.   r   r   r   get_edge_shapeP   s   
	
z+SymbolicShapeInferenceHelper.get_edge_shapec                 C   s>   | j sJ | |}| |}|du s|du rtd||kS )a*  Compare shape of two edges.

        Args:
            edge (str): name of edge
            edge_other (str): name of another edge

        Raises:
            Exception: At least one shape is missed for edges to compare

        Returns:
            bool: whether the shape is same or not
        Nz1At least one shape is missed for edges to compare)r   r7   	Exception)r   r4   
edge_otherr-   shape_otherr   r   r   compare_shapeh   s   


z*SymbolicShapeInferenceHelper.compare_shape)r   r   TF)r   )__name__
__module____qualname__r	   r   r&   intr"   r/   r7   r;   __classcell__r   r   r   r   r      s    r   )loggingossystypingr   pathdirname__file__	file_pathexistsjoinr'   symbolic_shape_inferr   r   r   	getLoggerr<   r   r   r   r   r   r   <module>   s   
