o
    XiD                     @  sv   d dl mZ d dlZd dlZd dlmZmZmZ d dlZd dl	Z
d dlmZmZ edZejZG dd de
jZdS )    )annotationsN)AnySequenceUnion)type_annotationvalues
onnxscriptc                      s|   e Zd ZdZd$d% fdd	Zed&ddZed'ddZd(ddZd)ddZ	d*ddZ
d+ddZd,dd Zd-d"d#Z  ZS ).
IRFunctionz Represents a function in the IR. namestrdomainreturnNonec                   s<   t jg g g |d}t j|||g d g | _i | _i | _d S )N)inputsoutputsnodesr   )graph
attributes)irGraphsuper__init__ordered_inputs_and_attrsnested_functionsouter_scope_variables)selfr   r   r   	__class__ R/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/_internal/irbuilder.pyr      s
   

zIRFunction.__init__Sequence[str]c                 C  s   dd | D S )z;Returns the list of variables assigned to by this function.c                 S  s   g | ]}|j D ]}|jqqS r   )r   r   ).0nvr   r   r    
<listcomp>,   s    z-IRFunction.assigned_names.<locals>.<listcomp>r   r   r   r   r    assigned_names)   s   zIRFunction.assigned_namesSequence[ir.Attr]c                 C  s   dd | j D S )Nc                 S  s   g | ]
}t |tjr|qS r   )
isinstancer   Attr)r"   attrr   r   r    r%   0   s    z$IRFunction.attrs.<locals>.<listcomp>)r   r&   r   r   r    attrs.   s   zIRFunction.attrsnodeir.Nodec                 C  s   t | }d| |_| | |j}|j}|| jvr!|| j|< d S | j| }||kr>tjd|d| d| dtdd d S d S )Nr#   zVersion conflict: domain: z, versions z and z used.   )category
stacklevel)	lenr   appendr   versionopset_importswarningswarnUserWarning)r   r-   countr   r4   existing_versionr   r   r    append_node2   s&   



zIRFunction.append_node	parameterir.Value | ir.Attrc                 C  sV   | j | t|tjr| j| d S t|tjs#tdt| | j	
| d S )Nz"Expected ir.Value or ir.Attr, got )r   r3   r)   r   Valuer   r*   	TypeErrortyper   add)r   r<   r   r   r    append_parameterD   s   zIRFunction.append_parameterfunc                 C  s   || j |j< d S N)r   r   )r   rC   r   r   r    add_nested_functionM   s   zIRFunction.add_nested_functiondict[str, values.OnnxFunction]c                   s.   i d	 fddd
fdd |  S )Nfunction_irr	   c                   s:   t j| jD ]}|jdd }t|tjr | qd S )Ncallee)	r   	traversalRecursiveGraphIteratorr   metagetr)   r   OnnxFunction)rG   r-   rH   )rA   r   r    visitS   s   z.IRFunction.get_called_functions.<locals>.visitfvalues.OnnxFunctionc                   s&   | j  v rd S |  | j < | j d S rD   )r   rG   )rO   )called_functionsrN   r   r    rA   Y   s   

z,IRFunction.get_called_functions.<locals>.add)rG   r	   )rO   rP   r   r&   r   )rA   rQ   rN   r    get_called_functionsP   s
   zIRFunction.get_called_functionsonnx.GraphProtoc                 C  s   t j| jS )z0Converts this instance into a `onnx.GraphProto`.)r   serdeserialize_graphr   r&   r   r   r    to_graph_protoc   s   zIRFunction.to_graph_protoonnx.FunctionProtoc                 C  s   t j| S )z3Converts this instance into a `onnx.FunctionProto`.)r   rT   serialize_functionr&   r   r   r    to_function_protog   s   zIRFunction.to_function_proto)r
   )r   r   r   r   r   r   )r   r!   )r   r(   )r-   r.   r   r   )r<   r=   r   r   )rC   r	   r   r   )r   rF   )r   rS   )r   rW   )__name__
__module____qualname____doc__r   propertyr'   r,   r;   rB   rE   rR   rV   rY   __classcell__r   r   r   r    r	      s    


	

r	   )
__future__r   loggingr6   typingr   r   r   onnxonnx_irr   onnxscript._internalr   r   	getLoggerloggerTypeAnnotationValueFunctionr	   r   r   r   r    <module>   s   
