o
    Xi8                     @  s   d Z ddlmZ ddgZddlZddlZddlmZmZ ddl	m
Z
mZ ddlZddlmZmZ ed	ZeeZG d
d dejd ZG dd deZG dd deZG dd dejedf ZG dd dejedf ZdS )zTracked containers for graph.    )annotationsGraphInputsGraphOutputsN)IterableSequence)SupportsIndexTypeVar)_core
_protocolsTc                      s   e Zd ZdZd*d+ fddZd,d	d
Zd-ddZd-ddZd. fddZd, fddZ	d/ fddZ
d0d1 fddZd. fddZd, fdd Zd2d"d#Zd, fd$d%Zd&d' Zd(d) ZeZeZeZeZeZ  ZS )3_GraphIOz"The inputs and outputs of a Graph.Ngraph_core.Graphc                   sL   || _ t | _|d urt|}|D ]}| | qt | |   d S N)	_graphcollectionsCounter_ref_countertuple
_set_graphsuper__init___check_invariance)selfr   initlistvalue	__class__ M/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/_graph_containers.pyr      s   
z_GraphIO.__init__returnNonec                 C     t )"Check the invariance of the graph.NotImplementedErrorr   r   r   r   r   .      z_GraphIO._check_invariancer   _core.Valuec                 C  r"   )Set the graph for the value.r$   r   r   r   r   r   r   2   r'   z_GraphIO._set_graphc                 C  r"   )Unset the graph for the value.r$   r*   r   r   r   _maybe_unset_graph6   r'   z_GraphIO._maybe_unset_graphitemc                   s"   |  | t | |   dS )zAdd a new input to the graph.N)r   r   appendr   r   r-   r   r   r   r.   :   s   
z_GraphIO.appendc                   s,   t |}|D ]}| | qt | dS )z%Extend the list of inputs or outputs.N)r   r   r   extend)r   otherr-   r   r   r   r0   A   s   z_GraphIO.extendiintc                   s$   t  || | | |   dS )z$Insert an input/output to the graph.N)r   insertr   r   )r   r2   r-   r   r   r   r4   H   s   
z_GraphIO.insertc                   s"   t  |}| | |   |S )&Remove an input/output from the graph.)r   popr,   r   )r   r2   r   r   r   r   r7   N   s   
z_GraphIO.popc                   s"   t  | | | |   dS )r6   N)r   remover,   r   r/   r   r   r   r8   U   s   
z_GraphIO.removec                   s$   | j D ]}| | qt   dS )zClear the list.N)datar,   r   clearr*   r   r   r   r:   [   s   
z_GraphIO.clearlist[_core.Value]c                 C  s
   | j  S )z"Return a shallow copy of the list.)r9   copyr&   r   r   r   r<   a   s   
z_GraphIO.copyc                   s   t |tr.t |tr.| j| D ]}| | q|D ]}| | qt || |   dS t |t	rM| | j|  | | t || |   dS t
dt| dt| )z$Replace an input/output to the node.NzInvalid types for __setitem__: z and )
isinstancer   slicer9   r,   r   r   __setitem__r   r   	TypeErrortype)r   r2   r-   r   r   r   r   r?   f   s   

z_GraphIO.__setitem__c                 C  s
   | j | S )z#Get an input/output from the graph.)r9   )r   r2   r   r   r   __getitem__{   s   
z_GraphIO.__getitem__c                 O  s   t d)zUnimplemented method.zMethod is not supported)RuntimeError)r   _args_kwargsr   r   r   _unimplemented   s   z_GraphIO._unimplementedr   r   r   r    r!   r   r(   r    r!   )r-   r(   r    r!   )r2   r3   r-   r(   r    r!   )r5   )r2   r3   r    r(   )r    r;   )__name__
__module____qualname____doc__r   r   r   r,   r.   r0   r4   r7   r8   r:   r<   r?   rB   rF   __add____radd____iadd____mul____rmul____classcell__r   r   r   r   r      s*    



r   r(   c                   @  .   e Zd ZdZdddZddd	Zdd
dZdS )r   zThe inputs of a Graph.r    r!   c                 C  <   t jsdS | jD ]}|j| ju rqtd| d| jdS )r#   NInvariance error: Value 'z ' is not an input of the graph: onnx_irDEBUGr9   r   
ValueErrorr*   r   r   r   r         
zGraphInputs._check_invariancer   r(   c                 C  sf   |j dur|j | j urtd| d| dur!td| d| j|  d7  < d|_| j |_ dS )r)   NValue 'z^' is already owned by a different graph. Please remove the value from the previous graph firstzf' is produced by a node and cannot be an input to the graph. Please create new Values for graph inputs   T)r   rZ   producerr   _is_graph_inputr*   r   r   r   r      s   

zGraphInputs._set_graphc                 C  T   |j | j u s
J d| j|  d8  < | j| dkrdS d|_| r%dS d|_ dS r+   'Bug: value does not belong to the graphr]   r   NF)r   r   r_   _owned_by_graphr*   r   r   r   r,         
zGraphInputs._maybe_unset_graphNrH   rI   rJ   rK   rL   rM   r   r   r,   r   r   r   r   r      s
    

c                   @  rT   )r   zThe outputs of a Graph.r    r!   c                 C  rU   )r#   NrV   z!' is not an output of the graph: rW   r*   r   r   r   r      r[   zGraphOutputs._check_invariancer   r(   c                 C  sJ   |j dur|j | j urtd| d| j|  d7  < d|_| j |_ dS )r)   Nr\   zb' is already an output of a different graph. Please remove the value from the previous graph firstr]   T)r   rZ   r   _is_graph_outputr*   r   r   r   r      s   
zGraphOutputs._set_graphc                 C  r`   ra   )r   r   rf   rc   r*   r   r   r   r,      rd   zGraphOutputs._maybe_unset_graphNrH   rI   re   r   r   r   r   r      s
    


c                      s^   e Zd ZdZdd fddZdddZdddZd fddZd fddZdddZ	  Z
S )GraphInitializerszUThe initializers of a Graph as ``dict[str, Value]`` with additional mutation methods.Nr   r   c                  sR   i }|d ur| | |r| | || _| D ]}| | qt | d S r   )updater   valuesr   r   r   )r   r   dictkwargsr9   r   r   r   r   r      s   

zGraphInitializers.__init__r   r(   r    r!   c                 C  s8   |j dur|j | j urtd| dd|_| j |_ dS )r)   Nr\   zg' is already an initializer of a different graph. Please remove the value from the previous graph firstT)r   rZ   _is_initializerr*   r   r   r   r      s   
zGraphInitializers._set_graphc                 C  s0   |j | j u s
J dd|_| rdS d|_ dS )r+   rb   FN)r   rl   rc   r*   r   r   r   r,      s
   
z$GraphInitializers._maybe_unset_graphkeystrc                   s   t |tjstdt| t |tstdt| |dkr%td|js3t	d|| ||_n||jkrDtd| d|j d|
 d	urRtd
| d|| jv ra| j| }| | | | t || d	S )z!Set an initializer for the graph.z"value must be a Value object, not z!Value name must be a string, not  z$Value name cannot be an empty stringz1Value %s does not have a name, setting it to '%s'zKey 'z(' does not match the name of the value 'z('. Please use the value.name as the key.Nr\   z9' is produced by a node and cannot be a graph initializer)r=   r	   Valuer@   rA   rn   rZ   nameloggerinfor^   r9   r,   r   r   r?   )r   rm   r   	old_valuer   r   r   r?      s,   






zGraphInitializers.__setitem__c                   s$   | j | }| | t | dS )z%Delete an initializer from the graph.N)r9   r,   r   __delitem__r   rm   r   r   r   r   ru     s   

zGraphInitializers.__delitem__c                 C     || |j < dS )z Add an initializer to the graph.Nrq   r*   r   r   r   add      zGraphInitializers.addr   rG   rI   )rm   rn   r   r(   r    r!   )rm   rn   r    r!   )rJ   rK   rL   rM   r   r   r,   r?   ru   ry   rS   r   r   r   r   rg      s    

		rg   c                      s   e Zd ZdZd3 fdd	Zd4 fddZd5ddZd6d7ddZd6d8ddZd6d9ddZ	d6d:dd Z
d6d;d"d#Zd6d<d%d&Zd6d=d(d)Zd6d>d+d,Z	d6d?d.d/Zd6d@d1d2Z  ZS )A
AttributeszOThe attributes of a Node as ``dict[str, Attr]`` with additional access methods.attrsIterable[_core.Attr]owner_core.Node | _core.Functionr    r!   c                   s    || _ t dd |D  d S )Nc                 S  s   i | ]}|j |qS r   rx   ).0attrr   r   r   
<dictcomp>,  s    z'Attributes.__init__.<locals>.<dictcomp>)_ownerr   r   )r   r|   r~   r   r   r   r   (  s   zAttributes.__init__rm   rn   r   
_core.Attrc                   sN   t |turtdt | t|tjstdt | t || dS )zSet an attribute for the node.zKey must be a string, not zValue must be an Attr, not N)rA   rn   r@   r=   r	   Attrr   r?   rv   r   r   r   r?   .  s
   zAttributes.__setitem__c                 C  rw   )zAdd an attribute to the node.Nrx   r*   r   r   r   ry   6  rz   zAttributes.addNdefaultr   int | Tc                 C     || v r
| |   S |S )z'Get the integer value of the attribute.)as_intr   rm   r   r   r   r   get_int:     zAttributes.get_int	float | Tc                 C  r   )z%Get the float value of the attribute.)as_floatr   r   r   r   	get_float@  r   zAttributes.get_floatstr | Tc                 C  r   )z&Get the string value of the attribute.)	as_stringr   r   r   r   
get_stringF  r   zAttributes.get_string_protocols.TensorProtocol | Tc                 C  r   )z&Get the tensor value of the attribute.)	as_tensorr   r   r   r   
get_tensorL  r   zAttributes.get_tensor_core.Graph | Tc                 C  r   )z%Get the graph value of the attribute.)as_graphr   r   r   r   	get_graphR  r   zAttributes.get_graphSequence[int] | Tc                 C  r   )z0Get the Sequence of integers from the attribute.)as_intsr   r   r   r   get_intsX  r   zAttributes.get_intsSequence[float] | Tc                 C  r   )z.Get the Sequence of floats from the attribute.)	as_floatsr   r   r   r   
get_floats^  r   zAttributes.get_floatsSequence[str] | Tc                 C  r   )z/Get the Sequence of strings from the attribute.)
as_stringsr   r   r   r   get_stringsd  r   zAttributes.get_strings'Sequence[_protocols.TensorProtocol] | Tc                 C  r   )z/Get the Sequence of tensors from the attribute.)
as_tensorsr   r   r   r   get_tensorsj  s   zAttributes.get_tensorsSequence[_core.Graph] | Tc                 C  r   )z.Get the Sequence of graphs from the attribute.)	as_graphsr   r   r   r   
get_graphst  r   zAttributes.get_graphs)r|   r}   r~   r   r    r!   )rm   rn   r   r   r    r!   )r   r   r    r!   r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rm   rn   r   r   r    r   )rJ   rK   rL   rM   r   r?   ry   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r{   %  s     
	
r{   r   )rM   
__future__r   __all__r   loggingcollections.abcr   r   typingr   r   rX   r	   r
   r   	getLoggerrJ   rr   UserListr   r   r   UserDictrn   rg   r{   r   r   r   r   <module>   s$   
o*&K