o
    öXœi
  ã                   @  s@   d Z ddlmZ dgZddlZdd	d„Zddd„Zddd„ZdS )z4Identify implicit uses of values in ONNX sub-graphs.é    )ÚannotationsÚanalyze_implicit_usageNÚgraphúir.GraphÚreturnúdict[ir.Graph, set[ir.Value]]c                 C  s$   | g}i }| D ]}t |||ƒ q|S )ax  Analyze implicit usage of values in sub-graphs.

    This function returns a mapping from each sub-graph to a set of
    :class:`~onnx_ir.Value`s that are captured from outer scopes (i.e., not defined
    within the sub-graph itself).

    Args:
        graph: The graph to analyze.

    Returns:
        A dictionary mapping sub-graphs to sets of implicitly used values.
    )Ú_process_node)r   Úgraph_stackÚimplicit_usagesÚnode© r   úT/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/analysis/_implicit_usage.pyr      s
   r   úir.NodeÚsubgraphr	   úlist[ir.Graph]r
   ÚNonec                 C  sN   | j D ]!}|d u s|j|u rqt|ƒD ]}||ju r n||  |¡ qqd S )N)Úinputsr   ÚreversedÚadd)r   r   r	   r
   ÚinpÚgr   r   r   Ú_collect_implicit_usages"   s   

€ùr   c                 C  sØ   | j  ¡ D ]d}|jtjjkr7| ¡ }| |¡ ||vr tƒ ||< |D ]} t	| |||ƒ t
| ||ƒ q"| ¡  q|jtjjkri| ¡ D ]&}| |¡ ||vrRtƒ ||< |D ]} t	| |||ƒ t
| ||ƒ qT| ¡  qBqdS )z7Perform a DFS to find all implicit usages in subgraphs.N)Ú
attributesÚvaluesÚtypeÚirÚAttributeTypeÚGRAPHÚas_graphÚappendÚsetr   r   ÚpopÚGRAPHSÚ	as_graphs)r   r
   r	   Úattrr   r   r   r   r   2   s*   





€îr   )r   r   r   r   )
r   r   r   r   r	   r   r
   r   r   r   )r   r   r
   r   r	   r   r   r   )	Ú__doc__Ú
__future__r   Ú__all__Úonnx_irr   r   r   r   r   r   r   r   Ú<module>   s   ÿ

