
    \j                     4   U d dl Z d dlZd dlZej                  j	                  e      Zej                  j                  ej                  j                  ed            r6ej                  j                  ej                  j                  ed             n5ej                  j                  ej                  j                  ed             	 d dl
mZmZmZ dZdZedz  ed<    e j(                  e      Z G d	 d
e      Zy# e$ rZeZdZdZdZeZY dZ[2dZ[ww xY w)    Nz ../tools/symbolic_shape_infer.pyz../toolsz..)SymbolicShapeInferenceget_shape_from_type_protosympyT"_symbolic_shape_infer_import_errorFc                   N     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 )
SymbolicShapeInferenceHelperc                     t         s+t        }|dt        |      v rdnd|}t        d|       |t        |   ||||       || _        d| _        d| _        i | _	        y )Nr   z1missing 'sympy' (install with: pip install sympy)z'failed to import symbolic_shape_infer: u0   SymbolicShapeInferenceHelper is unavailable — F)
_symbolic_shape_infer_availabler   strImportErrorsuper__init__model_all_shapes_inferred_is_inferred_dynamic_axis_mapping_)	selfmodelverboseint_max
auto_mergeguess_output_rankerrcause	__class__s	           ^/root/.hermes/venv/lib/python3.12/site-packages/onnxruntime/transformers/shape_infer_helper.pyr   z%SymbolicShapeInferenceHelper.__init__!   s    .4C ?w#c(': D>sgF 
  PQVPWXY_bb*.?I*/!"'57"    dynamic_axis_mappingmax_runsc                 r   |J | j                   r| j                  |k(  r| j                  S || _        | j                  | j                         d}| j
                  rJt        j                  d|        | j                         | _        |dz  }|dkD  r||k\  rn| j
                  rJd| _         | 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.
        r   zshape infer run    T)	r   r   r   _preprocessr   run_loggerdebug_infer_impl)r   r   r   counts       r   inferz"SymbolicShapeInferenceHelper.infer0   s     $///!;!;?S!S,,,%9"%iiLL+E734(,(8(8(:D%QJE!| 1 ii !(((r   c                    g }| j                  ||      }|r|D ]  }t        |t              r|| j                  v r|j	                  | j                  |          @|| j
                  v r|j	                  | j
                  |          m|j	                  t        j                  |d             |J |j	                  |        |S )zQOverride it to ensure shape inference by giving the actual value of dynamic axis.T)integer)
_get_shape
isinstancer   r   appendsymbolic_dims_r   Symbol)r   nodeidxsympy_shapeshapedims         r   _get_sympy_shapez-SymbolicShapeInferenceHelper._get_sympy_shapeN   s    c* 
,c3'd888#**4+E+Ec+JK 3 33#**4+>+>s+CD#**5<<T+JK?*?&&s+
, r   c                 D   | j                   sJ || j                  vrt        dt        |      z          y| j                  |   j                  }t        |      }|Et        |      D ]7  \  }}t        |t              s|| j                  v s&| j                  |   ||<   9 |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_protor3   ir4   s         r   get_edge_shapez+SymbolicShapeInferenceHelper.get_edge_shapea   s     ((((t~~%1CI=>^^D)..
)*5#E* ?3c3'C43M3M,M#99#>E!H? r   c                     | j                   sJ | j                  |      }| 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
        z1At least one shape is missed for edges to compare)r   r>   	Exception)r   r;   
edge_otherr3   shape_others        r   compare_shapez*SymbolicShapeInferenceHelper.compare_shapey   sT     ((((##D)))*5=K/OPP##r   )r   iTF)   )__name__
__module____qualname__r   dictr   intr(   r5   r>   rC   __classcell__)r   s   @r   r   r       s2    8)$sCx. )C )<&0$r   r   )loggingossyspathdirname__file__	file_pathexistsjoinr-   symbolic_shape_inferr   r   r   r
   r   r   __annotations__excobject	getLoggerrE   r$   r    r   r   <module>rZ      s     	 
 GGOOH%	77>>"'',,y*LMNHHOOBGGLLJ78HHOOBGGLLD12
-]]&*#=A&d(:A 
		8	$k$#9 k$  -# $E&+#),&-s   
C> >D
DD