o
    XiT                     @  s  U d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZmZ ddlmZ ejrBddlZddlmZ eeeef Zd	ed
< ejG dd deZejG dd deZejG dd deeeZejG dd deZ ejG dd deZ!ejG dd deZ"ejG dd deZ#ejG dd deZ$ejG dd deZ%ejG dd deZ&ejG dd  d eZ'ejG d!d" d"eZ(ejG d#d$ d$eZ)ejG d%d& d&eZ*ejG d'd( d(eZ+ejG d)d* d*eZ,dS )+a  Protocols for the ONNX IR.

This file defines the interfaces for tools to interact with the IR. The interfaces
are designed such that tools leveraging the IR can be decoupled from the IR
implementation. This allows for the implementation to evolve independently of the
tools.
    )annotationsN)OrderedDict)
CollectionIterableIteratorMappingMutableMappingMutableSequenceSequence)AnyLiteralProtocol)_enums)	TypeAliasr   OperatorIdentifierc                   @  s   e Zd ZdZd	ddZdS )
ArrayCompatiblezProtocol for array-like objects.

    An example of an array-like object is a numpy ndarray or a PyTorch Tensor.
    Read more at https://numpy.org/devdocs/user/basics.interoperability.html
    dtyper   return
np.ndarrayc                 C     d S N selfr   r   r   F/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/_protocols.py	__array__D       zArrayCompatible.__array__Nr   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   <   s    r   c                   @  s*   e Zd ZdZdddddZdd	d
ZdS )DLPackCompatiblezProtocol for objects that can support dlpack.

    Computation backends can call __dlpack__ to obtain the underlying data in a
    tensor without copying the data. This allows use to use tensorflow tensors etc.
    without copying the data.
    .streamr$   r   r   c                C     dS zReturn PyCapsule.Nr   r   r$   r   r   r   
__dlpack__P      zDLPackCompatible.__dlpack__c                 C  r%   zReturn the device.Nr   r   r   r   r   __dlpack_device__T   r)   z"DLPackCompatible.__dlpack_device__Nr$   r   r   r   r   r   )r   r   r    r!   r(   r,   r   r   r   r   r"   G   s    r"   c                   @  s   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ed%ddZed%ddZd&ddZd'd(ddZ	ddd)ddZ
d*d d!Zd+d#d$ZdS ),TensorProtocola7  Concrete tensor backed by data.

    The protocol does not specify how the data is stored. That data is exposed
    through the :attr:`raw` attribute for examination, but accessing :attr:`raw`
    is typically not needed.

    To use the tensor as a numpy array, call :meth:`numpy`. To convert the tensor
    to a byte string for serialization, call :meth:`tobytes`.

    It is recommended to check the size of the tensor first before accessing the
    underlying data, because accessing the data may be expensive and incur IO
    overhead.

    Attributes:
        name: The name of the tensor.
        shape: The shape of the tensor.
        dtype: The data type of the elements of the tensor. It is an :class:`ir.DataType` enum.
        doc_string: Documentation string.
        raw: The raw data behind this tensor. It can be anything.
        size: The number of elements in the tensor.
        nbytes: The number of bytes in the tensor.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    
str | NonenameShapeProtocolshape_enums.DataTyper   
doc_stringr   rawMutableMapping[str, str]metadata_propsMutableMapping[str, Any]metar   intc                 C  r   r   r   r+   r   r   r   size|      zTensorProtocol.sizec                 C  r   r   r   r+   r   r   r   nbytes   r=   zTensorProtocol.nbytesr   c                 C  r%   )z#Return the tensor as a numpy array.Nr   r+   r   r   r   numpy   r)   zTensorProtocol.numpyNc                 C  r%   )z=Return the tensor as a numpy array, compatible with np.array.Nr   r   r   r   r   r      r)   zTensorProtocol.__array__.r#   r$   c                C  r%   r&   r   r'   r   r   r   r(      r)   zTensorProtocol.__dlpack__c                 C  r%   r*   r   r+   r   r   r   r,      r)   z TensorProtocol.__dlpack_device__bytesc                 C  r%   )zYReturn the tensor as a byte string conformed to the ONNX specification, in little endian.Nr   r+   r   r   r   tobytes   r)   zTensorProtocol.tobytesr   r;   )r   r   r   r   r-   r.   )r   r@   )r   r   r    r!   __annotations__propertyr<   r>   r?   r   r(   r,   rA   r   r   r   r   r/   Y   s$   
 

r/   c                   @  s   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< d%ddZd&ddZd'ddZd(ddZ	d)d*d"d#Z	d$S )+ValueProtocola  Protocol for values.

    A value is a named entity that can be used to represent an input or output of a graph,
    a function, or a node. The information it stores generalizes over ``ValueInfoProto``
    in the ONNX specification.

    A :class:`Value` is always not owned or owned by exactly one node. When the value is not
    owned, it must be an input of a graph or a function. ``producer`` and ``index``
    are ``None``.

    When the value is owned by a node, it is an output of the node.
    The node that produces the value can be accessed with :meth:`producer`.
    The index of the output of the node that produces the value can be accessed with
    :meth:`index`.

    To find all the nodes that use this value as an input, call :meth:`uses`.

    To check if the value is an output of a graph, call :meth:`is_graph_output`.

    Attributes:
        name: The name of the value. A value is always named when it is part of a graph.
        shape: The shape of the value.
        type: The type of the value.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
        doc_string: Documentation string.
        const_value: The constant tensor is the value constant.
    strr1   zShapeProtocol | Noner3   zTypeProtocol | Nonetyper7   r8   r9   r:   r0   r5   zTensorProtocol | Noneconst_valuer   NodeProtocol | Nonec                 C  r%   )z"The node that produces this value.Nr   r+   r   r   r   producer   r)   zValueProtocol.producer
int | Nonec                 C  r%   )z=The index of the output of the node that produces this value.Nr   r+   r   r   r   index   r)   zValueProtocol.index$Collection[tuple[NodeProtocol, int]]c                 C  r%   )zUThe set of (node, input_index) with node being those that use this value as an input.Nr   r+   r   r   r   uses   r)   zValueProtocol.usesboolc                 C  r%   )z+Whether this value is an output of a graph.Nr   r+   r   r   r   is_graph_output   r)   zValueProtocol.is_graph_outputF	new_valueValueProtocol | Nonereplace_graph_outputsNonec                 C  r%   )zReplace all uses of this value with the given new value.

        Args:
            new_value: The new value to replace this value with.
            replace_graph_outputs: Whether to replace graph outputs that use this value.
        Nr   )r   rQ   rS   r   r   r   replace_all_uses_with   s   	z#ValueProtocol.replace_all_uses_withN)r   rI   )r   rK   )r   rM   )r   rO   )F)rQ   rR   rS   rO   r   rT   )
r   r   r    r!   rC   rJ   rL   rN   rP   rU   r   r   r   r   rE      s   
 



rE   c                   @  st   e Zd ZU dZded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< dddZdS )NodeProtocola1  Protocol for nodes.

    A node represents an invocation of an operation on the :class:`Value` s in
    the computational graph.

    A node can be optionally named. A name should typically be assigned when the
    node is added to a graph.

    :attr:`domain`, :attr:`op_type`, and :attr:`overload` together uniquely identify
    the operator, and are always strings. For ONNX operators, :attr:`domain` and :attr:`overload`
    are both empty strings.

    :attr:`inputs` and :attr:`outputs` are the input and output values of the node.

    :attr:`attributes` are the attributes of the node. The attributes are stored in an
    ordered dictionary to preserve the order of the attributes. This is a deviation from
    the current ONNX spec where attributes are unordered, but it is helpful for tools
    that rely on the order of the attributes, e.g. those converting to and from Python
    function keyword arguments.

    :attr:`version` is unique to the IR and is not specified in the ONNX spec. This
    allows the IR to represent a graph with mixed opset versions. Deserializers
    should decide how to reconcile the different versions within the graph. A typical
    graph will have a single version, declared in the :class:`Graph` object and
    the nodes will have ``None`` as the version.

    Attributes:
        domain: The domain of the operator. E.g. ``""`` for ONNX operators.
        op_type: The operator name.
        overload: The overload name when the node is invoking a function.
        inputs: Input values.
        outputs: Output values.
        attributes: The attributes of the operator.
        version: The version of the operator.
        doc_string: Documentation string.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    r0   r1   rF   domainop_typeoverloadSequence[ValueProtocol]inputsoutputsz@OrderedDict[str, AttributeProtocol | ReferenceAttributeProtocol]
attributesrK   versionr5   r7   r8   r9   r:   rL   r;   valuerR   r   rT   c                 C  r%   )zRSet the input at the given index to the given value, replacing the original value.Nr   r   rL   r_   r   r   r   replace_input_with  r)   zNodeProtocol.replace_input_withN)rL   r;   r_   rR   r   rT   )r   r   r    r!   rC   ra   r   r   r   r   rV      s   
 'rV   c                   @  s   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< d1ddZd2ddZd3ddZd3ddZd4d d!Z	d5d$d%Z
d4d&d'Zd6d*d+Zd6d,d-Zd7d.d/Zd0S )8GraphProtocola  Protocol for graphs.

    Graph represents a computation graph. In addition to the ONNX specification
    specified fields, it also contains a mapping of :attr:`opset_imports`. This
    allows different subgraphs to import different opsets. It is the responsibility
    of the deserializer to reconcile the different opsets.

    The nodes are not guaranteed to be topologically sorted. But the
    iteration order should be deterministic across different runs. It is the
    responsibility of the user to maintain a topological order of the nodes.

    Note that there is not a ``node`` attribute in the Graph. The Graph can be
    seen as a Sequence of nodes and should be used as such. For example, to obtain
    all nodes as a list, call ``list(graph)``.

    .. :note::
        ``quantization_annotation`` is deserialized into the Value's ``meta`` field
        under the ``quant_parameter_tensor_names`` key. Values that are stored
        under this key will be serialized as quantization annotations.

    Attributes:
        name: The name of the graph.
        inputs: The input values of the graph.
        outputs: The output values of the graph.
        initializers: The initializers in the graph.
        doc_string: Documentation string.
        opset_imports: Opsets imported by the graph.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    r0   r1   zMutableSequence[ValueProtocol]r[   r\   z"MutableMapping[str, ValueProtocol]initializersrF   r5   MutableMapping[str, int]opset_importsr7   r8   r9   r:   rL   r;   r   rV   c                 C  r   r   r   r   rL   r   r   r   __getitem__>  r   zGraphProtocol.__getitem__c                 C  r   r   r   r+   r   r   r   __len__?  r   zGraphProtocol.__len__Iterator[NodeProtocol]c                 C  r   r   r   r+   r   r   r   __iter__@  r   zGraphProtocol.__iter__c                 C  r   r   r   r+   r   r   r   __reversed__A  r   zGraphProtocol.__reversed__noderT   c                C  r%   )zAppend a node to the graph.Nr   r   rl   r   r   r   appendD  r)   zGraphProtocol.appendnodesIterable[NodeProtocol]c                C  r%   )z&Extend the graph with the given nodes.Nr   r   ro   r   r   r   extendH  r)   zGraphProtocol.extendc                C  r%   )zRemove a node from the graph.Nr   rm   r   r   r   removeL  r)   zGraphProtocol.remove	new_nodes%Iterator[NodeProtocol] | NodeProtocolc                C  r%   z&Insert new nodes after the given node.Nr   r   rl   rt   r   r   r   insert_afterP     zGraphProtocol.insert_afterc                C  r%   z'Insert new nodes before the given node.Nr   rw   r   r   r   insert_beforeV  ry   zGraphProtocol.insert_beforec                 C  r%   )z*Topologically sort the nodes in the graph.Nr   r+   r   r   r   sort\  r)   zGraphProtocol.sortNrL   r;   r   rV   rB   r   ri   rl   rV   r   rT   ro   rp   r   rT   rl   rV   rt   ru   r   rT   r   rT   )r   r   r    r!   rC   rg   rh   rj   rk   rn   rr   rs   rx   r{   r|   r   r   r   r   rb     s(   
 








rb   c                   @  sz   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< dddZd ddZd!ddZd!ddZdS )"GraphViewProtocola  Protocol for a read-only view on a graph.

    The GraphView is useful for analysis of a subgraph. It can be initialized
    with a subset of nodes from a :class:`Graph`. Creating GraphView does not
    change the ownership of the nodes, and so it is possible to create multiple
    GraphViews that contain the same nodes.

    Attributes:
        name: The name of the graph.
        inputs: The input values of the graph.
        outputs: The output values of the graph.
        initializers: The initializers in the graph.
        doc_string: Documentation string.
        opset_imports: Opsets imported by the graph.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    r0   r1   rZ   r[   r\   zMapping[str, ValueProtocol]rc   rF   r5   zMapping[str, int]re   r7   r8   r9   r:   rL   r;   r   rV   c                 C  r   r   r   rf   r   r   r   rg   ~  r   zGraphViewProtocol.__getitem__c                 C  r   r   r   r+   r   r   r   rh     r   zGraphViewProtocol.__len__ri   c                 C  r   r   r   r+   r   r   r   rj     r   zGraphViewProtocol.__iter__c                 C  r   r   r   r+   r   r   r   rk     r   zGraphViewProtocol.__reversed__Nr}   rB   r~   )	r   r   r    r!   rC   rg   rh   rj   rk   r   r   r   r   r   a  s   
 


r   c                   @  sj   e Zd ZU dZded< ded< ded< ded< ded	< d
ed< ded< ded< ded< ded< ded< dS )ModelProtocola  Protocol for models.

    A model is a container for a graph and metadata. It is the top-level object
    that represents an ONNX model.

    Attributes:
        graph: The graph of the model.
        ir_version: The version of the IR.
        producer_name: The name of the producer.
        producer_version: The version of the producer.
        domain: The domain of the model.
        model_version: The version of the model.
        doc_string: Documentation string.
        functions: The functions defined in the model.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    rb   graphr;   
ir_versionr0   producer_nameproducer_versionrW   rK   model_versionr5   z%MutableMapping[str, FunctionProtocol]	functionsrd   re   r7   r8   r9   r:   Nr   r   r    r!   rC   r   r   r   r   r     s   
 r   c                   @  s<   e Zd ZU dZded< ded< ded< ded	< dddZdS )AttributeProtocolzProtocol for ONNX attributes.

    Attributes:
        name: The name of the attribute.
        type: The type of the attribute.
        value: The value of the attribute.
        doc_string: Documentation string.
    rF   r1   _enums.AttributeTyperG   r   r_   r0   r5   r   Literal[False]c                 C  r   r   r   r+   r   r   r   is_ref  r   zAttributeProtocol.is_refN)r   r   r   r   r    r!   rC   r   r   r   r   r   r     s   
 	r   c                   @  s<   e Zd ZU dZded< ded< ded< ded< dddZdS )ReferenceAttributeProtocola`  Protocol for a reference attribute.

    A reference attribute can only appear inside the definition body of a function.

    Attributes:
        name: The name of the attribute.
        ref_attr_name: The name of the attribute definition this attribute refers to.
        type: The type of the attribute.
        doc_string: Documentation string.
    rF   r1   ref_attr_namer   rG   r0   r5   r   Literal[True]c                 C  r   r   r   r+   r   r   r   r     r   z!ReferenceAttributeProtocol.is_refN)r   r   r   r   r   r   r   r     s   
 r   c                   @  s&   e Zd ZU ded< ded< ded< dS )SparseTensorProtocolr/   valuesindicesSequence[int]dimsN)r   r   r    rC   r   r   r   r   r     s   
 r   c                   @  s   e Zd ZU dZded< dS )SymbolicDimProtocolzyValue of a single symbolic/dynamic dimension in a shape.

    Attributes:
        value: The value of the dimension.
    r0   r_   Nr   r   r   r   r   r     s   
 r   c                   @  s   e Zd ZU dZded< d*ddZd+d	d
Zejd,ddZ	ejd-ddZ	d.ddZ
d/ddZd0ddZd1dd Zd2d"d#Zd3d%d&Zd*d'd(Zd)S )4r2   zProtocol for ONNX shapes.

    A shape is a sequence of dimensions.

    Attributes:
        dims: The dimensions of the shape.
    z#Sequence[int | SymbolicDimProtocol]r   r   r;   c                 C  r   r   r   r+   r   r   r   rh     r   zShapeProtocol.__len__#Iterator[int | SymbolicDimProtocol]c                 C  r   r   r   r+   r   r   r   rj     r   zShapeProtocol.__iter__rL   int | SymbolicDimProtocolc                 C  r   r   r   rf   r   r   r   rg     r=   zShapeProtocol.__getitem__slice%tuple[int | SymbolicDimProtocol, ...]c                 C  r   r   r   rf   r   r   r   rg     r=   r_   &int | SymbolicDimProtocol | str | NonerT   c                 C  r   r   r   r`   r   r   r   __setitem__  r)   zShapeProtocol.__setitem__otherobjectrO   c                 C  r   r   r   )r   r   r   r   r   __eq__  r   zShapeProtocol.__eq__c                 C  r   r   r   r   r_   r   r   r   __ne__  r   zShapeProtocol.__ne__r0   c                 C  r   r   r   rf   r   r   r   get_denotation  r   zShapeProtocol.get_denotation
denotationc                 C  r   r   r   )r   rL   r   r   r   r   set_denotation  r   zShapeProtocol.set_denotationr   c                 C  r   r   r   r+   r   r   r   r?     r   zShapeProtocol.numpyc                 C  r   r   r   r+   r   r   r   rank  r   zShapeProtocol.rankNrB   )r   r   )rL   r;   r   r   )rL   r   r   r   )rL   r;   r_   r   r   rT   )r   r   r   rO   r_   r   r   rO   )rL   r;   r   r0   )rL   r;   r   r0   r   rT   )r   r   )r   r   r    r!   rC   rh   rj   typingrY   rg   r   r   r   r   r   r?   r   r   r   r   r   r2     s    
 







r2   c                   @  s4   e Zd ZU dZded< ded< ded< dddZdS )TypeProtocolai  Protocol for ONNX tensors, Sequence tensors, Optional tensors and Sparse tensors.

    These three types of tensors share the same attribute "elem_type" so they are
    merged in the same interface. Unlike the ONNX TensorProto, shapes are not included
    in the type and should be stored in the :class:`Value`.

    Attributes:
        denotation: An optional denotation can be used to denote the whole
            type with a standard semantic description as to what is
            stored inside.
            Refer to https://github.com/onnx/onnx/blob/main/docs/TypeDenotation.md#type-denotation-definition
            for pre-defined type denotations.
        elem_type: The type of its elements for nested types like Sequence[Optional] tensors.
            Or the DataType if the type is not nested.
        dtype: The data type of the tensor or the nested tensor.
    r0   r   zTypeProtocol | _enums.DataType	elem_typer4   r   r_   r   r   rO   c                C  r   r   r   r   r   r   r   r     r   zTypeProtocol.__eq__Nr   )r   r   r    r!   rC   r   r   r   r   r   r     s   
 r   c                   @  s"   e Zd ZU dZded< ded< dS )MapTypeProtocolzaProtocol for ONNX map types.

    TODO: This protocol is not yet implemented in the ONNX IR.
    ztyping.Literal[_enums.DataType.STRING, _enums.DataType.INT64, _enums.DataType.INT32, _enums.DataType.INT16, _enums.DataType.INT8, _enums.DataType.UINT64, _enums.DataType.UINT32, _enums.DataType.UINT16, _enums.DataType.UINT8]key_typer4   
value_typeNr   r   r   r   r   r     s   
 r   c                   @  s   e Zd ZU dZded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< d5ddZd6ddZd7ddZd7ddZd8d d!Z	d9d$d%Z
d:d(d)Zd9d*d+Zd;d.d/Zd;d0d1Zd<d2d3Zd4S )=FunctionProtocola  Protocol for ONNX functions.

    Like a graph, a function can have nodes that are not topologically sorted. It is
    the responsibility of the user to maintain a topological order of the nodes.

    Note that there is not a ``node`` attribute in the Function. The Function can be
    seen as a Sequence of nodes and should be used as such. For example, to obtain
    all nodes as a list, call ``list(function)``.

    Attributes:
        name: The function name.
        domain: The domain this function is defined in.
        overload: The overload name when the function is overloaded.
        inputs: The input values of the function.
        attributes: The attributes this function defines.
        outputs: The output values of the function.
        opset_imports: Opsets imported by the function.
        doc_string: Documentation string.
        metadata_props: Metadata that will be serialized to the ONNX file.
        meta: Metadata store for graph transform passes.
    rF   r1   rW   rY   rZ   r[   z#OrderedDict[str, AttributeProtocol]r]   r\   r5   rd   re   r7   r8   r9   r:   rL   r;   r   rV   c                 C  r   r   r   rf   r   r   r   rg   O  r   zFunctionProtocol.__getitem__c                 C  r   r   r   r+   r   r   r   rh   P  r   zFunctionProtocol.__len__ri   c                 C  r   r   r   r+   r   r   r   rj   Q  r   zFunctionProtocol.__iter__c                 C  r   r   r   r+   r   r   r   rk   R  r   zFunctionProtocol.__reversed__r   c                 C  r%   )z-Return the unique identifier of the function.Nr   r+   r   r   r   
identifierS  r)   zFunctionProtocol.identifierrl   rT   c                C  r%   )zAppend a node to the function.Nr   rm   r   r   r   rn   Y  r)   zFunctionProtocol.appendro   rp   c                C  r%   )z)Extend the function with the given nodes.Nr   rq   r   r   r   rr   ]  r)   zFunctionProtocol.extendc                C  r%   )z Remove a node from the function.Nr   rm   r   r   r   rs   a  r)   zFunctionProtocol.removert   ru   c                C  r%   rv   r   rw   r   r   r   rx   e  ry   zFunctionProtocol.insert_afterc                C  r%   rz   r   rw   r   r   r   r{   k  ry   zFunctionProtocol.insert_beforec                 C  r%   )z-Topologically sort the nodes in the function.Nr   r+   r   r   r   r|   q  r)   zFunctionProtocol.sortNr}   rB   r~   )r   r   r   r   r   r   )r   r   r    r!   rC   rg   rh   rj   rk   r   rn   rr   rs   rx   r{   r|   r   r   r   r   r   ,  s.   
 









r   )-r!   
__future__r   r   collectionsr   collections.abcr   r   r   r   r   r	   r
   r   r   r   onnx_irr   TYPE_CHECKINGr?   nptyping_extensionsr   tuplerF   r   rC   runtime_checkabler   r"   r/   rE   rV   rb   r   r   r   r   r   r   r2   r   r   r   r   r   r   r   <module>   sV   $	
=B9L"!
