
    Bvh]                        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  e j                  e      Z G d de      Zy)    Nz ../tools/symbolic_shape_infer.pyz../toolsz..)SymbolicShapeInferenceget_shape_from_type_protosympyc                   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                 b    t         |   ||||       || _        d| _        d| _        i | _        y )NF)super__init__model_all_shapes_inferred_is_inferred_dynamic_axis_mapping_)selfmodelverboseint_max
auto_mergeguess_output_rank	__class__s         U/RAG/venv/lib/python3.12/site-packages/onnxruntime/transformers/shape_infer_helper.pyr
   z%SymbolicShapeInferenceHelper.__init__   s6    *.?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.infer   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
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+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_protor.   ir/   s         r   get_edge_shapez+SymbolicShapeInferenceHelper.get_edge_shapeO   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   r9   	Exception)r   r6   
edge_otherr.   shape_others        r   compare_shapez*SymbolicShapeInferenceHelper.compare_shapeg   sT     ((((##D)))*5=K/OPP##r   )r   iTF)   )__name__
__module____qualname__r
   dictr'   intr"   r0   r9   r>   __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   	getLoggerr@   r   r    r   r   <module>rR      s     	 
 GGOOH%	77>>"'',,y*LMNHHOOBGGLLJ78HHOOBGGLLD12 Y Y			8	$c$#9 c$r   