o
    Xi                     @  sr   d Z ddlmZ dgZddlZddlmZ ddlZe	e
ZG dd dejjZdddZdddZdddZdS ).Eliminate common subexpression in ONNX graphs.    )annotations"CommonSubexpressionEliminationPassN)Sequencec                      s8   e Zd ZdZdd fddZdddZdddZ  ZS )r   a  Eliminate common subexpression in ONNX graphs.

    .. versionadded:: 0.1.1

    .. versionchanged:: 0.1.3
        Constant nodes with values smaller than ``size_limit`` will be CSE'd.

    Attributes:
        size_limit: The maximum size of the tensor to be csed. If the tensor contains
            number of elements larger than size_limit, it will not be cse'd. Default is 10.

    
   
size_limitintc                   s   t    || _dS )z2Initialize the CommonSubexpressionEliminationPass.N)super__init__r   )selfr   	__class__ j/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/passes/common/common_subexpression_elimination.pyr
   !   s   

z+CommonSubexpressionEliminationPass.__init__modelir.Modelreturnir.passes.PassResultc                 C  s    |j }| |}tjj||dS )z;Return the same ir.Model but with CSE applied to the graph.)modified)graph_eliminate_common_subexpressionirpasses
PassResult)r   r   r   r   r   r   r   call&   s   
z'CommonSubexpressionEliminationPass.callr   ir.Graphboolc                 C  sr  d}i }|D ]}d}d}i }|j  D ]M\}}	|	jtjjtjjfv r&d} n;|	j}
|	jtjjtjj	tjj
fv r<t|
}
n |	jtjju r\|
j| jkrMd} n|
 }|jt|j| f}
|
||< q|rjtd| q|rstd| qt|r~td| q| t|jtdd |jD tt| f}||v rd}|| }t|||j|jd td	| q|||< q|S )
r   FTzSkipping control flow op %sz Skipping large tensor in node %sz Skipping non-deterministic op %sc                 s  s    | ]}t |V  qd S N)id).0inputr   r   r   	<genexpr>s       zUCommonSubexpressionEliminationPass._eliminate_common_subexpression.<locals>.<genexpr>)remove_noderemove_values
new_valueszReusing node %s)
attributesitemstyper   AttributeTypeGRAPHGRAPHSvalueINTSFLOATSSTRINGStupleTENSORsizer   numpyshapestrdtypetobytesloggerdebug_is_non_deterministic_opop_identifierlenoutputsinputssorted_remove_node_and_replace_values)r   r   r   existing_node_info_to_the_nodenodecontrol_flow_oplarge_tensorr&   kvr,   np_value	node_infoexisting_noder   r   r   r   0   sf   




zBCommonSubexpressionEliminationPass._eliminate_common_subexpression)r   )r   r   )r   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r
   r   r   __classcell__r   r   r   r   r      s
    

r#   ir.Noder$   Sequence[ir.Value]r%   r   r   r   Nonec          	   	   C  s   t dd |D rUtt||}t| jD ]?\}}||v rT|| }| s)| rKtjd|gtj	|j
|j|jdgd}|jd | j|< | || q|j
|_
|| j|< qtj|| | j|dd d	S )
a  Replaces nodes and values in the graph or function.

    Args:
        graph: The graph to replace nodes and values in.
        remove_node: The node to remove.
        remove_values: The values to replace.
        new_values: The values to replace with.
    c                 s  s    | ]}|  V  qd S r   )is_graph_output)r   remove_valuer   r   r   r!      r"   z2_remove_node_and_replace_values.<locals>.<genexpr>Identity)namer(   r4   )r>   r=   r   T)safeN)anydictzip	enumerater=   rR   is_graph_inputr   rB   ValuerU   r(   r4   insert_beforeconveniencereplace_all_uses_withremove)	r   r#   r$   r%   replacement_mappingidxgraph_output	new_valueidentity_noder   r   r   r@      s4   
r@   rB   r   c                 C  s    t h d}| j|v ot| jS )N>   MultinomialRandomNormalRandomUniformRandomNormalLikeRandomUniformLike)	frozensetop_type_is_onnx_domaindomain)rB   non_deterministic_opsr   r   r   r:      s   	r:   dr5   c                 C  s   | dkS )z'Check if the domain is the ONNX domain. r   )rp   r   r   r   rm      s   rm   )
r#   rO   r$   rP   r%   rP   r   r   r   rQ   )rB   rO   r   r   )rp   r5   r   r   )rM   
__future__r   __all__loggingcollections.abcr   onnx_irr   	getLoggerrJ   r8   r   InPlacePassr   r@   r:   rm   r   r   r   r   <module>   s   

w
6