
     TiyT                       % S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
JrJrJr  SSKJrJrJr  SSKJr  \R&                  (       a
  SSKrSSKJr  \\\\4   rS	\S
'   \R8                   " S S\5      5       r\R8                   " S S\5      5       r\R8                   " S S\\\5      5       r\R8                   " S S\5      5       r \R8                   " S S\5      5       r!\R8                   " S S\5      5       r"\R8                   " S S\5      5       r#\R8                   " S S\5      5       r$\R8                   " S S\5      5       r%\R8                   " S S\5      5       r&\R8                   " S S \5      5       r'\R8                   " S! S"\5      5       r(\R8                   " S# S$\5      5       r)\R8                   " S% S&\5      5       r*\R8                   " S' S(\5      5       r+\R8                   " S) S*\5      5       r,g)+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                  "    \ rS rSrSrSS jrSrg)ArrayCompatible<   zProtocol 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
c                    g N selfdtypes     Q/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/_protocols.py	__array__ArrayCompatible.__array__D   s    3    r   Nr   r   return
np.ndarray)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r   r   <   s     7r   r   c                  4    \ rS rSrSrSS.S	S jjrS
S jrSrg)DLPackCompatibleG   zProtocol 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.
.streamc                   gzReturn PyCapsule.Nr   r   r,   s     r   
__dlpack__DLPackCompatible.__dlpack__P       r   c                    gzReturn the device.Nr   r   s    r   __dlpack_device__"DLPackCompatible.__dlpack_device__T   r2   r   r   Nr,   r   r    r   r    r   )r"   r#   r$   r%   r&   r0   r6   r'   r   r   r   r)   r)   G   s     +. r   r)   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   \SS j5       r\SS j5       rSS jr	SSS jjr
SS.SS jjrS S jrS!S jrSrg)"TensorProtocolY   a  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]metac                    g r   r   r5   s    r   sizeTensorProtocol.size|   s    r   c                    g r   r   r5   s    r   nbytesTensorProtocol.nbytes   s     r   c                    g)z#Return the tensor as a numpy array.Nr   r5   s    r   numpyTensorProtocol.numpy   r2   r   Nc                    g)z=Return the tensor as a numpy array, compatible with np.array.Nr   r   s     r   r   TensorProtocol.__array__   r2   r   .r+   c                   gr.   r   r/   s     r   r0   TensorProtocol.__dlpack__   r2   r   c                    gr4   r   r5   s    r   r6    TensorProtocol.__dlpack_device__   r2   r   c                    g)zYReturn the tensor as a byte string conformed to the ONNX specification, in little endian.Nr   r5   s    r   tobytesTensorProtocol.tobytes   r2   r   r   r    int)r    r!   r   r   r8   r9   )r    bytes)r"   r#   r$   r%   r&   __annotations__propertyrI   rL   rO   r   r0   r6   rX   r'   r   r   r   r;   r;   Y   sl    2 	H,,
""     +. r   r;   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   S\S'   SS jrSS jrSS jrSS jr	 S     SS jjr
Srg)ValueProtocol   a  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.
strr>   zShapeProtocol | Noner@   zTypeProtocol | NonetyperD   rE   rF   rG   r=   rB   zTensorProtocol | Noneconst_valuec                    g)z"The node that produces this value.Nr   r5   s    r   producerValueProtocol.producer   r2   r   c                    g)z=The index of the output of the node that produces this value.Nr   r5   s    r   indexValueProtocol.index   r2   r   c                    g)zUThe set of (node, input_index) with node being those that use this value as an input.Nr   r5   s    r   usesValueProtocol.uses   r2   r   c                    g)z+Whether this value is an output of a graph.Nr   r5   s    r   is_graph_outputValueProtocol.is_graph_output   r2   r   c                    g)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   	new_valuereplace_graph_outputss      r   replace_all_uses_with#ValueProtocol.replace_all_uses_with   s     	r   r   N)r    zNodeProtocol | None)r    
int | None)r    z$Collection[tuple[NodeProtocol, int]])r    bool)F)rr   ValueProtocol | Noners   rw   r    None)r"   r#   r$   r%   r&   r]   rf   ri   rl   ro   rt   r'   r   r   r   r`   r`      so    : I
,,
""&&
 NS	-	FJ			 	r   r`   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   S	\S'   S\S'   S\S'   S\S'   S\S'   S\S'   SS jrSrg)NodeProtocol   a  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.
r=   r>   rb   domainop_typeoverloadSequence[ValueProtocol]inputsoutputsz@OrderedDict[str, AttributeProtocol | ReferenceAttributeProtocol]
attributesrv   versionrB   rD   rE   rF   rG   c                    g)zRSet the input at the given index to the given value, replacing the original value.Nr   r   ri   values      r   replace_input_withNodeProtocol.replace_input_with  r2   r   r   N)ri   r[   r   rx   r    ry   )r"   r#   r$   r%   r&   r]   r   r'   r   r   r   r{   r{      sN    %N KLM##$$PP,,
""r   r{   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   S\S'   SS jrSS jrS S jrS S jr	S!S jr
S"S jrS!S jr      S#S jr      S#S jrS$S jrSrg)%GraphProtocoli  aC  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.
r=   r>   zMutableSequence[ValueProtocol]r   r   z"MutableMapping[str, ValueProtocol]initializersrb   rB   MutableMapping[str, int]opset_importsrD   rE   rF   rG   c                    g r   r   r   ri   s     r   __getitem__GraphProtocol.__getitem__>      sr   c                    g r   r   r5   s    r   __len__GraphProtocol.__len__?      cr   c                    g r   r   r5   s    r   __iter__GraphProtocol.__iter__@      #r   c                    g r   r   r5   s    r   __reversed__GraphProtocol.__reversed__A      cr   c                   g)zAppend a node to the graph.Nr   r   nodes     r   appendGraphProtocol.appendD  r2   r   c                   g)z&Extend the graph with the given nodes.Nr   r   nodess     r   extendGraphProtocol.extendH  r2   r   c                   g)zRemove a node from the graph.Nr   r   s     r   removeGraphProtocol.removeL  r2   r   c                   gz&Insert new nodes after the given node.Nr   r   r   	new_nodess      r   insert_afterGraphProtocol.insert_afterP       	r   c                   gz'Insert new nodes before the given node.Nr   r   s      r   insert_beforeGraphProtocol.insert_beforeV  r   r   c                    g)z*Topologically sort the nodes in the graph.Nr   r5   s    r   sortGraphProtocol.sort\  r2   r   r   Nri   r[   r    r{   rZ   r    zIterator[NodeProtocol]r   r{   r    ry   r   zIterable[NodeProtocol]r    ry   r   r{   r   z%Iterator[NodeProtocol] | NodeProtocolr    ry   r    ry   )r"   r#   r$   r%   r&   r]   r   r   r   r   r   r   r   r   r   r   r'   r   r   r   r   r     s    > **++44O++,,
"":!59 -R	 -R	r   r   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   S\S'   SS jrSS jrSS jrSS jr	Sr
g)GraphViewProtocolia  a  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.
r=   r>   r   r   r   zMapping[str, ValueProtocol]r   rb   rB   zMapping[str, int]r   rD   rE   rF   rG   c                    g r   r   r   s     r   r   GraphViewProtocol.__getitem__~  r   r   c                    g r   r   r5   s    r   r   GraphViewProtocol.__len__  r   r   c                    g r   r   r5   s    r   r   GraphViewProtocol.__iter__  r   r   c                    g r   r   r5   s    r   r   GraphViewProtocol.__reversed__  r   r   r   Nr   rZ   r   )r"   r#   r$   r%   r&   r]   r   r   r   r   r'   r   r   r   r   r   a  sD    $ ##$$--O$$,,
"":!59r   r   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   Srg)ModelProtocoli  a_  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.
r   graphr[   
ir_versionr=   producer_nameproducer_versionr}   rv   model_versionrB   z%MutableMapping[str, FunctionProtocol]	functionsr   r   rD   rE   rF   rG   r   Nr"   r#   r$   r%   r&   r]   r'   r   r   r   r   r     sJ    $ O  44++,,
""r   r   c                  L    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   SS jrSrg)AttributeProtocoli  zProtocol 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.
rb   r>   _enums.AttributeTyperc   r   r   r=   rB   c                    g r   r   r5   s    r   is_refAttributeProtocol.is_ref  s    r   r   N)r    zLiteral[False]r"   r#   r$   r%   r&   r]   r   r'   r   r   r   r   r     s"     I
J+r   r   c                  L    \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   SS
 jrSrg)ReferenceAttributeProtocoli  aD  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.
rb   r>   ref_attr_namer   rc   r=   rB   c                    g r   r   r5   s    r   r   !ReferenceAttributeProtocol.is_ref  s    sr   r   N)r    zLiteral[True]r   r   r   r   r   r     s#    	 I
*r   r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg)	SparseTensorProtocoli  r;   valuesindicesSequence[int]dimsr   N)r"   r#   r$   r%   r]   r'   r   r   r   r   r     s    
r   r   c                  $    \ rS rSr% SrS\S'   Srg)SymbolicDimProtocoli  zmValue of a single symbolic/dynamic dimension in a shape.

Attributes:
    value: The value of the dimension.
r=   r   r   Nr   r   r   r   r   r     s     r   r   c                      \ rS rSr% SrS\S'   SS jrSS jr\R                  SS j5       r
\R                  SS j5       r
      SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSrg)r?   i  ztProtocol for ONNX shapes.

A shape is a sequence of dimensions.

Attributes:
    dims: The dimensions of the shape.
z#Sequence[int | SymbolicDimProtocol]r   c                    g r   r   r5   s    r   r   ShapeProtocol.__len__  r   r   c                    g r   r   r5   s    r   r   ShapeProtocol.__iter__  s    sr   c                    g r   r   r   s     r   r   ShapeProtocol.__getitem__  s    DGr   c                    g r   r   r   s     r   r   r     s    RUr   c                    g r   r   r   s      r   __setitem__ShapeProtocol.__setitem__  s    r   c                    g r   r   )r   others     r   __eq__ShapeProtocol.__eq__      Sr   c                    g r   r   r   r   s     r   __ne__ShapeProtocol.__ne__  r   r   c                    g r   r   r   s     r   get_denotationShapeProtocol.get_denotation  s    r   c                    g r   r   )r   ri   
denotations      r   set_denotationShapeProtocol.set_denotation  s    #r   c                    g r   r   r5   s    r   rO   ShapeProtocol.numpy  s    cr   c                    g r   r   r5   s    r   rankShapeProtocol.rank  s    3r   r   NrZ   )r    z#Iterator[int | SymbolicDimProtocol])ri   r[   r    zint | SymbolicDimProtocol)ri   slicer    z%tuple[int | SymbolicDimProtocol, ...])ri   r[   r   z&int | SymbolicDimProtocol | str | Noner    ry   )r   objectr    rw   r   r	  r    rw   )ri   r[   r    r=   )ri   r[   r   r=   r    ry   )r    r   )r"   r#   r$   r%   r&   r]   r   r   typingr   r   r   r   r   r   r  rO   r  r'   r   r   r   r?   r?     sj     .-!B__G G__U U!G	 10;M)r   r?   c                  B    \ rS rSr% SrS\S'   S\S'   S\S'   SS	 jrS
rg)TypeProtocoli  a5  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.
r=   r   zTypeProtocol | _enums.DataType	elem_typerA   r   c                   g r   r   r   s     r   r   TypeProtocol.__eq__  s    r   r   Nr
  )r"   r#   r$   r%   r&   r]   r   r'   r   r   r   r  r    s    " --3r   r  c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	MapTypeProtocoli  zYProtocol 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_typerA   
value_typer   Nr   r   r   r   r  r    s    

 
  r   r  c                     \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   S\S'   S\S'   S\S'   S S jrS!S jrS"S jrS"S jr	S#S jr
S$S jrS%S jrS$S jr      S&S jr      S&S jrS'S jrSrg)(FunctionProtocoli,  a  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.
rb   r>   r}   r   r   r   z#OrderedDict[str, AttributeProtocol]r   r   rB   r   r   rD   rE   rF   rG   c                    g r   r   r   s     r   r   FunctionProtocol.__getitem__O  r   r   c                    g r   r   r5   s    r   r   FunctionProtocol.__len__P  r   r   c                    g r   r   r5   s    r   r   FunctionProtocol.__iter__Q  r   r   c                    g r   r   r5   s    r   r   FunctionProtocol.__reversed__R  r   r   c                    g)z-Return the unique identifier of the function.Nr   r5   s    r   
identifierFunctionProtocol.identifierS  r2   r   c                   g)zAppend a node to the function.Nr   r   s     r   r   FunctionProtocol.appendY  r2   r   c                   g)z)Extend the function with the given nodes.Nr   r   s     r   r   FunctionProtocol.extend]  r2   r   c                   g)z Remove a node from the function.Nr   r   s     r   r   FunctionProtocol.removea  r2   r   c                   gr   r   r   s      r   r   FunctionProtocol.insert_aftere  r   r   c                   gr   r   r   s      r   r   FunctionProtocol.insert_beforek  r   r   c                    g)z-Topologically sort the nodes in the function.Nr   r5   s    r   r   FunctionProtocol.sortq  r2   r   r   Nr   rZ   r   )r    r   r   r   r   r   )r"   r#   r$   r%   r&   r]   r   r   r   r   r   r   r   r   r   r   r   r'   r   r   r   r  r  ,  s    , IKM##33$$O++,,
"":!59 -R	 -R	r   r  )-r&   
__future__r   r  collectionsr   collections.abcr   r   r   r   r	   r
   r   r   r   r   onnx_irr   TYPE_CHECKINGrO   nptyping_extensionsr   tuplerb   r   r]   runtime_checkabler   r)   r;   r`   r{   r   r   r   r   r   r   r   r?   r  r  r  r   r   r   <module>r7     s{  8 #  #    	+ !&c3m 4 I 4 7h 7 7 x  " :_&6 : :z ?H ? ?D 68 6 6r IH I IX : : :D #H # #B , , ,$ + + +( 8   (   H  8 48 4 42  h    ( Fx F Fr   