o
    Xi                     @  s.   d Z ddlmZ ddlmZ G dd dZdS )z-Auxiliary class for managing names in the IR.    )annotations)_corec                   @  s@   e Zd ZdZdd ZdddZdd	d
ZdddZdddZdS )NameAuthoritya  Class for giving names to values and nodes in the IR.

    The names are generated in the format ``val_{value_counter}`` for values and
    ``node_{op_type}_{node_counter}`` for nodes. The counter is incremented each time
    a new value or node is named.

    This class keeps tracks of the names it has generated and existing names
    in the graph to prevent producing duplicated names.

    .. note::
        Once a name is tracked, it will not be made available even if the node/value
        is removed from the graph. It is possible to improve this behavior by keeping
        track of the names that are no longer used, but it is not implemented yet.

    However, if a value/node is already named when added to the graph,
    the name authority will not change its name.
    It is the responsibility of the user to ensure that the names are unique
    (typically by running a name-fixing pass on the graph).

    TODO(justichuby): Describe the pass when we have a reference implementation.
    c                 C  s    d| _ d| _t | _t | _d S )Nr   )_value_counter_node_counterset_value_names_node_names)self r   K/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/_name_authority.py__init__!   s   zNameAuthority.__init__returnstrc                 C  s,   	 d| j  }|  j d7  _ || jvr|S q)z#Generate a unique name for a value.Tval_   )r   r   )r
   namer   r   r   _unique_value_name'   s   
z NameAuthority._unique_value_nameop_typec                 C  s2   	 d| d| j  }|  j d7  _ || jvr|S q)z"Generate a unique name for a node.Tnode__r   )r   r	   )r
   r   r   r   r   r   _unique_node_name/   s   
zNameAuthority._unique_node_namevalue_core.ValueNonec                 C  s&   |j d u r
|  |_ | j|j  d S N)r   r   r   add)r
   r   r   r   r   register_or_name_value7   s   

z$NameAuthority.register_or_name_valuenode
_core.Nodec                 C  s*   |j d u r| |j|_ | j|j  d S r   )r   r   r   r	   r   )r
   r   r   r   r   register_or_name_nodeA   s   
z#NameAuthority.register_or_name_nodeN)r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )	__name__
__module____qualname____doc__r   r   r   r   r    r   r   r   r   r   
   s    



r   N)r$   
__future__r   onnx_irr   r   r   r   r   r   <module>   s   