
     Ngu                        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                    r4ej                            ej        
                    ed                     n3ej                            ej        
                    ed                     d dlmZmZmZ  e j        e          Z G d de          ZdS )	    N)Dictz ../tools/symbolic_shape_infer.pyz../toolsz..)SymbolicShapeInferenceget_shape_from_type_protosympyc                   T     e Zd Zd fd	Zddeeef         defd	Zd
 Zd Z	d Z
 xZS )SymbolicShapeInferenceHelperr   TFc                     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__s         g/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/transformers/shape_infer_helper.pyr   z%SymbolicShapeInferenceHelper.__init__   sG    *.?III*/!"'57"""       dynamic_axis_mappingmax_runsc                 J   |J | j         r| j        |k    r| j        S || _        |                     | j                   d}| j        rOt                              d|            |                                 | _        |dz  }|dk    r||k    rn| j        Od| _         | 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   counts       r   inferz"SymbolicShapeInferenceHelper.infer   s     $/// 	-!;?S!S!S,,%9"%%%i 	LL3E33444(,(8(8(:(:D%QJE!|| 1 1 i 	 !((r   c                    g }|                      ||          }|r|D ]}t          |t                    r~|| j        v r!|                    | j        |                    A|| j        v r!|                    | j        |                    k|                    t          j        |d                     |J |                    |           |S )zQOverride it to ensure shape inference by giving the actual value of dynamic axis.T)integer)
_get_shape
isinstancestrr   appendsymbolic_dims_r   Symbol)r   nodeidxsympy_shapeshapedims         r   _get_sympy_shapez-SymbolicShapeInferenceHelper._get_sympy_shape=   s    c** 	, 
, 
,c3'' 	,d888#**4+Ec+JKKKK 333#**4+>s+CDDDD#**5<T+J+J+JKKKK???&&s++++r   c                 8   | j         sJ || j        vr!t          dt          |          z              dS | j        |         j        }t          |          }|Ct          |          D ]3\  }}t          |t                    r|| j        v r| j        |         ||<   4|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_protor1   ir2   s         r   get_edge_shapez+SymbolicShapeInferenceHelper.get_edge_shapeP   s     ((((t~%%1CII=>>>4^D).
)*55#E** ? ?3c3'' ?C43M,M,M#9#>E!Hr   c                     | j         sJ |                     |          }|                     |          }||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   r<   	Exception)r   r9   
edge_otherr1   shape_others        r   compare_shapez*SymbolicShapeInferenceHelper.compare_shapeh   s]     ((((##D))))*55=K/OPPP##r   )r   r	   TF)r   )__name__
__module____qualname__r   r   r*   intr%   r3   r<   rA   __classcell__)r   s   @r   r   r      s        8 8 8 8 8 8) )$sCx. )C ) ) ) )<  &  0$ $ $ $ $ $ $r   r   )loggingossystypingr   pathdirname__file__	file_pathexistsjoinr+   symbolic_shape_inferr   r   r   	getLoggerrB   r!   r    r   r   <module>rT      s*    				 



       GOOH%%	7>>"',,y*LMMNN 3HOOBGLLJ778888HOOBGLLD11222 Y Y Y Y Y Y Y Y Y Y		8	$	$c$ c$ c$ c$ c$#9 c$ c$ c$ c$ c$r   