o
    q::jK                     @  s   U d dl mZ d dlZd dlZd dl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Zd dlZd dlmZmZ d dlmZmZmZ d dlmZmZmZmZmZmZmZmZm Z m!Z!m"Z" ersd dlm#Z#m$Z$m%Z% d dl&m'Z' e(e)e*e*e*f e(e)e*e*e*e*f B Z+e,e+ Z-e,e(e)e)f  Z.g d	Z/d
e0d< e1e(e)e*f e*f Z2dddZ3e3e/Z4	ddddZ5				ddd&d'Z6dd*d+Z7				ddd6d7Z8dd8d9Z9					dddAdBZ:ddEdFZ;ddGdHZ<ddNdOZ=ddQdRZ>	dddZd[Z?dd_d`Z@ddedfZAddkdlZBddqdrZCddudvZD		ddd{d|ZE	ddd}dddZFdddZGdddZHdddZI	ddddZJ		dd ddZK	ddddZL		dd ddZMdddZNdddZOdddZP	ddddZQdddZR		ddddZS	ddddZTdddZUd	ddZVd
ddZWdddZX	ddddZYeZdddddZ[dddÄZ\ddd̈́Z]dddфZ^dddӄZ_dddՄZ`eaddddׄZbeaddddۄZcdddބZddd ejef D Zgde0d< dddZhdS (      )annotationsN)TYPE_CHECKINGAnyTypeVar)_mappingdefs)MapProtoOptionalProtoSequenceProto)AttributeProtoFunctionProto
GraphProto
ModelProto	NodeProtoOperatorSetIdProtoTensorProtoTensorShapeProtoTrainingInfoProto	TypeProtoValueInfoProto)CallableKeysViewSequence)RepeatedCompositeFieldContainer) )z1.0      r   )z1.1r      r   )z1.1.2r      r   )z1.2r      r   )z1.3r      r   )z1.4.1   	   r   )z1.5.0r   
   r   )z1.6.0r         )z1.7.0r      r$   r   )z1.8.0r      r$   r   )z1.8.1r   r&   r$   r   )z1.9.0r      r$   r   )z1.10.0r      r$   r   )z1.10.1r   r(   r$   r   )z1.10.2r   r(   r$   r   )z1.11.0r      r   r   )z1.12.0r      r   r   )z1.13.0r      r   r   )z1.13.1r   r+   r   r   )z1.14.0r!      r   r   )z1.14.1r!   r,   r   r   )z1.15.0r!      r    r   )z1.16.0r"      r   r   )z1.16.1r"   r.   r   r   )z1.16.2r"   r.   r   r   )z1.17.0r"      r   r   )z1.18.0r#      r   r   )z1.19.0r%      r   r   )z1.19.1r%   r1   r   r   )z1.20.0r&      r   r   )z1.20.1r&   r2   r   r   )z1.21.0r&      r   r   VersionTableTypeVERSION_TABLEtablereturnVersionMapTypec                 C  s   i }| D ]:}|d }t g d|dd ddD ]&}||vr=|||< |d dkr/||d	|d f< |d d
kr=||d|d f< qq|S )zOCreate a map from (opset-domain, opset-version) to ir-version from above table.r   )ai.onnxz
ai.onnx.mlai.onnx.trainingr$   NF)strictr   r9   zai.onnx.previewr:   zai.onnx.preview.training)zip)r6   resultrow
ir_versionpair rA   R/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnx/helper.py_create_op_set_id_version_mapT   s"   

rC   FopsetidlistSequence[OperatorSetIdProto]ignore_unknownboolintc                   s2   d d fdd| rt fd	d
| D S  S )a=  Given list of opset ids, determine minimum IR version required.

    Args:
        opsetidlist: A sequence of OperatorSetIdProto.
        ignore_unknown: If True, ignore unknown domain and return default minimum
            version for that domain.

    Returns:
        The minimum IR version required (integer)
    r   domain
str | NoneversionrH   r7   c                   s,   | pd|f}|t v rt | S r S td)Nr9   zUnsupported opset-version.)OP_SET_ID_VERSION_MAP
ValueError)rI   rK   key)default_min_versionrF   rA   rB   find_miny   s   z)find_min_ir_version_for.<locals>.find_minc                 3  s    | ]
} |j |jV  qd S N)rI   rK   .0x)rP   rA   rB   	<genexpr>   s    z*find_min_ir_version_for.<locals>.<genexpr>N)rI   rJ   rK   rH   r7   rH   )max)rD   rF   rA   )rO   rP   rF   rB   find_min_ir_version_forj   s
   rW   op_typestrinputsSequence[str]outputsnamerJ   
doc_stringrI   overloadkwargsr   r   c           	      K  sz   t  }| |_|j| |j| |r||_|r||_|dur#||_|dur*||_|r;|j	dd t
| D  |S )a  Construct a NodeProto.

    Args:
        op_type (string): The name of the operator to construct
        inputs (list of string): list of input names
        outputs (list of string): list of output names
        name (string, default None): optional unique identifier for NodeProto
        doc_string (string, default None): optional documentation string for NodeProto
        domain (string, default None): optional domain for NodeProto.
            If it's None, we will just use default domain (which is empty)
        overload (string, default None): optional field, used to
            resolve calls to model-local functions
        **kwargs (dict): the attributes of the node.  The acceptable values
            are documented in :func:`make_attribute`.

    Returns:
        NodeProto
    Nc                 s  s&    | ]\}}|d urt ||V  qd S rQ   )make_attribute)rS   rN   valuerA   rA   rB   rU      s    zmake_node.<locals>.<genexpr>)r   rX   inputextendoutputr]   r^   rI   r_   	attributesorteditems)	rX   rZ   r\   r]   r^   rI   r_   r`   noderA   rA   rB   	make_node   s"   
rj   rK   r   c                 C     t  }| |_||_|S zConstruct an OperatorSetIdProto.

    Args:
        domain (string): The domain of the operator set id
        version (integer): Version of operator set id
    Returns:
        OperatorSetIdProto
    r   rI   rK   )rI   rK   operatorsetidrA   rA   rB   make_operatorsetid   s   ro   nodesSequence[NodeProto]Sequence[ValueInfoProto]initializerSequence[TensorProto] | None
value_infoSequence[ValueInfoProto] | Nonesparse_initializer'Sequence[onnx.SparseTensorProto] | Noner   c           	      C  s   |du rg }|du rg }|du rg }t  }|j|  ||_|j| |j| |j| |j| |j| |rA||_	|S )a  Construct a GraphProto

    Args:
        nodes: list of NodeProto
        name (string): graph name
        inputs: list of ValueInfoProto
        outputs: list of ValueInfoProto
        initializer: list of TensorProto
        doc_string (string): graph documentation
        value_info: list of ValueInfoProto
        sparse_initializer: list of onnx.SparseTensorProto
    Returns:
        GraphProto
    N)
r   ri   rd   r]   rc   re   rs   rw   ru   r^   )	rp   r]   rZ   r\   rs   r^   ru   rw   graphrA   rA   rB   
make_graph   s"   rz   c                 C  rk   rl   rm   )rI   rK   opsetidrA   rA   rB   make_opsetid   s   	r|   fnameopset_imports
attributesSequence[str] | Noneattribute_protosSequence[AttributeProto] | Noner   c                 C  s   |d u rg }|d u rg }|
d u rg }
t  }| |_||_|j| |j| |j| |j| |j| |j	| |rD||_
|	d urK|	|_|j|
 |S rQ   )r   rI   r]   rc   rd   re   ri   opset_importrf   attribute_protor^   r_   ru   )rI   r}   rZ   r\   rp   r~   r   r   r^   r_   ru   frA   rA   rB   make_function  s*   r   ry   r   c                 K  s   t  }tj|_|j|  |dd}|dur|j| n
|j	 }t
 |_|dd}|dur8|j| | D ]
\}}t||| q<|S )zConstruct a ModelProto

    Args:
        graph (GraphProto): *make_graph* returns
        **kwargs: any attribute to add to the returned instance
    Returns:
        ModelProto
    r~   N	functions)r   onnx
IR_VERSIONr?   ry   CopyFrompopr   rd   addr   onnx_opset_versionrK   r   rh   setattr)ry   r`   modelr~   impr   kvrA   rA   rB   
make_model'  s    	

r   c                 K  s8   d}||vrd}| |g }t|||< t| fi |S )Nr?   r~   )getrW   r   )ry   r`   ir_version_fieldopset_imports_fieldimportsrA   rA   rB   make_model_gen_versionL  s   r   protoRModelProto | GraphProto | FunctionProto | NodeProto | TensorProto | ValueInfoProto
dict_valuedict[str, str]Nonec                 C  s8   | j d d = | D ]\}}| j  }||_||_q
d S rQ   )metadata_propsrh   r   rN   rb   )r   r   r   r   entryrA   rA   rB   set_metadata_propsU  s   
r   r   c                 C  s   t | | d S rQ   )r   )r   r   rA   rA   rB   set_model_propsg  s   r   	data_typedimsSequence[int]vals*Sequence[int | float] | bytes | np.ndarrayrawr   c                 C  s  t  }||_| |_|j| |t jkr|rtdt|}|r|t jt j	t j
hv r-d}n|t jt jhv r8d}n|j}|t|9 }t|}t|tjrs|t j	t jt j
hv r^tj|}n|t jt jhv rltj|}tj|}nt|tr{|}n
tdt| dt||krtd| dt| d||_|S |rJ d	|t jkrt| }t|d
krt t!|}n6|t j"t j#t j$t j%hv rtj&t'|| }n|t j(krtjj)t'|ddd }n	tj'||d }t|}	t||	krtdt| d|	 d|t j*kr|+tj,}na|t j-kr&|+tj.}nT|t j/t j0hv r6|+tj1}nD|t j"t j#t j$t j%t j(hv rL|+tj2}n.|t jt j	t j
hv r^tj|}n|t jt jhv rntj|}n|t j3krz|4tj2}t5|}
t6||
| |S )au  Make a TensorProto with specified arguments.  If raw is False, this
    function will choose the corresponding proto field to store the
    values based on data_type. If raw is True, use "raw_data" proto
    field to store the values, and values should be of type bytes in
    this case.

    Args:
        name: tensor name
        data_type: a value such as onnx.TensorProto.FLOAT
        dims: shape
        vals: values
        raw: if True, vals contains the serialized content of the tensor,
            otherwise, vals should be a list of values of the type defined by ``data_type``.

    Returns:
        TensorProto
    z*Can not use raw_data to store string type.g      ?g      ?z1Raw data must be bytes or numpy.ndarray, but got .z5Raw data size does not match tensor's size. Expected z bytes, but got z bytes.z'Bug: raw should be False at this point.r   Tup)saturate
round_mode)dtypezNumber of values (z-) does not match tensor dimensions requiring z
 elements.)7r   r   r]   r   rd   STRING	TypeErrortensor_dtype_to_np_dtypeUINT4INT4
FLOAT4E2M1UINT2INT2itemsizemathprodceil
isinstancenpndarrayr   numpy_helper_pack_4bitx2_pack_2bitx4tobytes_little_endianbytestypelenrM   raw_dataarrayflatten	vectorize	_to_bytesFLOAT8E4M3FNFLOAT8E4M3FNUZ
FLOAT8E5M2FLOAT8E5M2FNUZsaturate_castasarray
FLOAT8E8M0to_float8e8m0
COMPLEX128viewfloat64	COMPLEX64float32BFLOAT16FLOAT16uint16uint8BOOLastypetensor_dtype_to_fieldgetattr)r]   r   r   r   r   tensornp_dtypeexpected_size_bytesr   expected_elementsfieldrA   rA   rB   make_tensork  s   




r   valuesindicesonnx.SparseTensorProtoc                 C  s0   t  }|j|  |j| |j| |S )zConstruct a SparseTensorProto

    Args:
        values (TensorProto): the values
        indices (TensorProto): the indices
        dims: the shape

    Returns:
        SparseTensorProto
    )r   SparseTensorProtor   r   r   r   rd   )r   r   r   sparserA   rA   rB   make_sparse_tensor  s
   r   	elem_typeSequenceProto.DataTypeSequence[Any]r
   c                 C  s   t  }| |_||_|t jkr|S d}|t jkr|j}n(|t jkr$|j}n|t jkr-|j	}n|t j
kr6|j}n|tjkr?|j}ntd|| |S )z/Make a Sequence with specified value arguments.Nz8The element type in the input sequence is not supported.)r
   r]   r   	UNDEFINEDTENSORtensor_valuesSPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesMAP
map_valuesr	   OPTIONALoptional_valuesr   rd   )r]   r   r   sequencerf   rA   rA   rB   make_sequence  s&   






r   key_typekeys	list[Any]r   c                 C  sr   t  }tjtjtjtjtjtjtjtj	g}| |_
||_|tjkr'|j| n
||v r1|j| |j| |S )zMake a Map with specified key-value pair arguments.

    Criteria for conversion:
    - Keys and Values must have the same number of elements
    - Every key in keys must be of the same type
    - Every value in values must be of the same type
    )r   r   INT8INT16INT32INT64UINT8UINT16UINT32UINT64r]   r   r   string_keysrd   r   r   r   )r]   r   r   r   	map_protovalid_key_int_typesrA   rA   rB   make_map  s$   


r  OptionalProto.DataTyperb   &google.protobuf.message.Message | Noner	   c                 C  s   t  }| |_||_|t jkr|S d}|t jkr|j}n(|t jkr$|j}n|t jkr-|j	}n|t j
kr6|j}n|t jkr?|j}ntd|dusIJ || |S )z0Make an Optional with specified value arguments.Nz8The element type in the input optional is not supported.)r	   r]   r   r   r   tensor_valuer   sparse_tensor_valuer   sequence_valuer   	map_valuer   optional_valuer   r   )r]   r   rb   optionalrf   rA   rA   rB   make_optional8  s(   






r  str | bytesr   c                 C  s   t | tr
| dS | S )z2Coerce a string (or bytes) value into UTF-8 bytes.utf-8)r   rY   encode)rb   rA   rA   rB   r   W  s   

r   rN   	attr_type
int | Noner   c              
     s&  t  }| |_|r||_t|tjrt||_t j|_	nSt|tj
r-t||_t j|_	nBt|ttfr?t||_t j|_	n0t|trP|j| t j|_	nt|tjrb|j| t j|_	nt|trr|j| t j|_	nt|tr|j| t j |_	nt|t!j"j#rgt$|}t%|dkr|du rt&d|  d|du rdd |D }tjt j'ftj
t j(fttft j)ftt j*ftjt j+ftt j,ftt j-ffD ]\ }t. fdd|D r|} nq|du rt&d	|t j'kr|j/0| t j'|_	nu|t j(kr|j10| t j(|_	nd|t j)kr!|j20d
d |D  t j)|_	nN|t j*kr2|j30| t j*|_	n=|t j+krC|j40| t j+|_	n,|t j,krT|j50| t j,|_	n|t j-kre|j60| t j-|_	n
t7t8d| d|dur|j	|krt8dt9|j	 d|j	 dt9| d| d	|S )z0Makes an AttributeProto based on the value type.r   NzCould not infer attribute `z` type from empty iteratorc                 S  s   h | ]}t |qS rA   )r   rS   r   rA   rA   rB   	<setcomp>  s    z!make_attribute.<locals>.<setcomp>c                 3  s    | ]}t | V  qd S rQ   )
issubclassrS   texp_trA   rB   rU     s    z!make_attribute.<locals>.<genexpr>zRCould not infer the attribute type from the elements of the passed Iterable value.c                 s  s    | ]}t |V  qd S rQ   )r   r  rA   rA   rB   rU     s    'z%' is not an accepted attribute value.zInferred attribute type 'z'(z") mismatched with specified type ')):r   r]   r^   r   numbersIntegralrH   iINTr   Realfloatr   FLOATrY   r   r   sr   r   r  r   r   r   r   sparse_tensorr   r   gGRAPHr   tp
TYPE_PROTOcollectionsabcIterablelistr   rM   INTSFLOATSSTRINGSTENSORSSPARSE_TENSORSGRAPHSTYPE_PROTOSallintsrd   floatsstringstensorssparse_tensorsgraphstype_protosAssertionErrorr   _attr_type_to_str)rN   rb   r^   r  attrtypesexp_enumrA   r  rB   ra   ^  s   











	







(ra   )ref_attr_nameAttributeProto.AttributeTyperD  c                C  s>   |du r| }|st dt }| |_||_||_|r||_|S )a  Make an AttributeProto holding a reference to the parent function's attribute.

    The returned attribute carries no value of its own; at instantiation time its
    value is supplied by the parent function's attribute named ``ref_attr_name``.
    When ``ref_attr_name`` is not provided, it defaults to ``name``. Reference
    attributes are only valid inside a function (sub-graph).

    Args:
        name: The name of this attribute as used inside the function body.
        attr_type: The type of the attribute.
        doc_string: Optional human-readable documentation for the attribute.
        ref_attr_name: The name of the parent function's attribute being referenced.
    Nzref_attr_name must be non-empty)rM   r   r]   r   rD  r^   )r]   r  r^   rD  rA  rA   rA   rB   make_attribute_ref  s   rF  rA  c                 C  sJ  | j r
td|  | jtjkr| jS | jtjkr| jS | jtjkr%| j	S | jtj
kr.| jS | jtjkr7| jS | jtjkr@| jS | jtjkrI| jS | jtjkrTt| jS | jtjkr_t| jS | jtjkrjt| jS | jtjkrut| jS | jtjkrt| jS | jtjkrt| jS | jtjkrt| j S | jtj!krd S td|  )Nz)Cannot get value of reference attribute: zUnsupported ONNX attribute: )"rD  rM   r   r   r%  r   r"  r!  r   r&  r   r  r   r'  r)  r(  r+  r*  r1  r/  r9  r0  r8  r2  r:  r3  r;  r4  r<  r5  r=  r6  r>  r   )rA  rA   rA   rB   get_attribute_value  sB   






rG  ri   	attr_namec                   sT    fdd| j D }t|dkrtd  t|dk r$td  t|d S )Nc                   s   g | ]	}|j  kr|qS rA   r]   rR   rH  rA   rB   
<listcomp>  s    z'get_node_attr_value.<locals>.<listcomp>r   z'Node has multiple attributes with name z Node has no attribute with name r   )rf   r   rM   rG  )ri   rH  matchingrA   rJ  rB   get_node_attr_value  s   rM  r   c                 C  s   t  }| |_|S rQ   )r   r]   )r]   value_info_protorA   rA   rB   make_empty_tensor_value_info  s   rO  shape!Sequence[str | int | None] | Noneshape_denotationlist[str] | Noner   c           	      C     t  }|j}| |_|j}|durX|jg  |r$t|t|kr$tdt|D ]/\}}|j	 }|du r6nt
|tr?||_nt
|trH||_ntd| d|rW|| |_q(|S )z:Makes a Tensor TypeProto based on the data type and shape.N>Invalid shape_denotation. Must be of the same length as shape.Invalid item in shape: z. Needs to be of int or str.)r   tensor_typer   rP  dimrd   r   rM   	enumerater   r   rH   	dim_valuerY   	dim_param
denotation)	r   rP  rR  
type_prototensor_type_prototensor_shape_protor!  drX  rA   rA   rB   make_tensor_type_proto  2   




ra   c                 C  s2   t  }| |_|r||_t|||}|j| |S )z8Makes a ValueInfoProto based on the data type and shape.)r   r]   r^   ra  r   r   )r]   r   rP  r^   rR  rN  r^  rA   rA   rB   make_tensor_value_info>  s   rd  c           	      C  rT  )z@Makes a SparseTensor TypeProto based on the data type and shape.NrU  rV  z. Needs to be of int or text.)r   sparse_tensor_typer   rP  rX  rd   r   rM   rY  r   r   rH   rZ  rY   r[  r\  )	r   rP  rR  r]  sparse_tensor_type_protosparse_tensor_shape_protor!  r`  rX  rA   rA   rB   make_sparse_tensor_type_protoP  rb  rh  c                 C  s6   t  }| |_|r||_t|||}|jj|j |S )zEMakes a SparseTensor ValueInfoProto based on the data type and shape.)r   r]   r^   rh  r   re  r   )r]   r   rP  r^   rR  rN  rf  rA   rA   rB   make_sparse_tensor_value_info}  s   ri  inner_type_protoc                 C     t  }|jj|  |S )zMakes a sequence TypeProto.)r   sequence_typer   r   rj  r]  rA   rA   rB   make_sequence_type_proto     rn  c                 C  rk  )zMakes an optional TypeProto.)r   optional_typer   r   rm  rA   rA   rB   make_optional_type_proto  ro  rq  
value_typec                 C  s    t  }| |j_|jj| |S )zMakes a map TypeProto.)r   map_typer   rr  r   )r   rr  r]  rA   rA   rB   make_map_type_proto  s   rt  r]  c                 C  s&   t  }| |_|r||_|j| |S )z1Makes a ValueInfoProto with the given type_proto.)r   r]   r^   r   r   )r]   r]  r^   rN  rA   rA   rB   make_value_info  s   ru  r&  c                 C  sb   t | tr| }nt | tr| jddd}nt| }t|dk r!|S |d d dt|d  d S )Nr  ignore)errors@   z	...<+len=>)r   rY   r   decoder   )r&  	sanitizedrA   rA   rB   _sanitize_str  s   

 r|  elem_shape_denotationc                 C  s>   t  }| |_|r||_t|||}t|}|jj|j |S )zJMakes a Sequence[Tensors] ValueInfoProto based on the data type and shape.)r   r]   r^   ra  rn  r   rl  r   )r]   r   rP  r^   r}  rN  r^  sequence_type_protorA   rA   rB   make_tensor_sequence_value_info  s   r  	subgraphs"str | tuple[str, list[GraphProto]]c                 C  s  g }| | j | d d(dd}d)d
d}td}d*dd}g }| dr1| || j n'| dr@| || j n| drQ| tt| j n| drxt	| j
jdkrd| d nt| j
j}| dt| j
| d n| dr| d| jj d | | j n| dr| d| j d n| dr| d n| jr| ||| j n| jr| ||| j n| jr| tttt| j n| jr| d nz| jr| d  nq| jr| d! t| jD ]\}	}
|	t	| jd" krd#nd$}| d|
 d|  q| d% n?| jrS| d! t| jD ]\}	}|	t	| jd" kr7d#nd$}| d|j d|  q'| d% || j n| d& |rbd'||fS d'|S )+N=r   r$  r7   rY   c                 S  s   | dS )Nz.15grA   )r   rA   rA   rB   	str_float  s   z&printable_attribute.<locals>.str_floatr!  rH   c                 S  s   t | S rQ   )rY   )r!  rA   rA   rB   str_int  s   z$printable_attribute.<locals>.str_int_Tstr_elemCallable[[_T], str]xsSequence[_T]c                 S  s   dd t| | d S )N[, ])joinmap)r  r  rA   rA   rB   str_list  s   z%printable_attribute.<locals>.str_listr&  r  r   z<Tensor>z<Scalar Tensor ry  r(  z<graph r*  z<Type Proto r'  z<Sparse Tensor>z[<Tensor>, ...]z[<Sparse Tensor>, ...]r  r   ,rc  r  z	<Unknown> )r   r$  r7   rY   )r!  rH   r7   rY   )r  r  r  r  r7   rY   )appendr]   r   HasFieldr   r!  reprr|  r&  r   r  r   r   r   r   r(  r*  r9  r8  r:  rY   r/  r  r;  r<  r>  rY  r=  rd   r  )rA  r  contentr  r  r  r  r=  r   r!  r*  commar(  rA   rA   rB   printable_attribute  sj   



	











r  rX  TensorShapeProto.Dimensionc                 C  s$   |  d}|d u rdS tt| |S )Nrb   ?)
WhichOneofrY   r   )rX  whichrA   rA   rB   printable_dim/  s   
r  r  c                 C  s   |  ddkr5tj| jj}| jdr3t| jjj	r/|t
ddtt| jjj	 7 }|S |d7 }|S |  dd u r>dS d|  d S )	Nrb   rW  rP  r  rT   , scalarrc  zUnknown type )r  r   DataTypeNamerW  r   r  r   rP  rX  rY   r  r  r  r  r&  rA   rA   rB   printable_type6  s   "r  r   c                 C  s,   d| j  }| jr| dt| j d}|S )N%r  r  )r]   r   r  )r   r&  rA   rA   rB   printable_value_infoD  s   r  c                 C  sh   d| j  d}|tj| j7 }| jd ur.t| jr*|tddt	t| j 7 }n|d7 }|d7 }|S )Nr  r  r  rT   r  r  )
r]   r   r  r  r   r   r   rY   r  r  r  rA   rA   rB   printable_tensor_protoK  s   

 r  prefixc                 C  sD  g }t | jr|ddd | jD  |d g }g }| jD ]=}|rHt||}t|d ts9tdt d|	|d  ||d  q!t|}t|t
sYtd	t
 d|| q!dt|}	dd
d | jD }
| jr|| j d|	 d|
 d n|| j d|
 d |r|d| |fS |d| S )Nr  c                 S     g | ]}d | qS r  rA   rS   r]   rA   rA   rB   rK  \      z"printable_node.<locals>.<listcomp>r  r   z1printed_attr_subgraphs[1] must be an instance of r   r   zprinted must be an instance of c                 S  r  r  rA   r  rA   rA   rB   rK  p  r  r  z](r  (r  )r   re   r  r  rf   r  r   r/  r   rd   rY   rg   rc   rX   )ri   r  r  r  r=  printed_attrsrA  printed_attr_subgraphsprintedprinted_attributesprinted_inputsrA   rA   rB   printable_nodeW  s6   





 r  zEDeprecated since 1.19. Consider using onnx.printer.to_text() instead.c                   sn  g }|d }d| j g}dd | jD }t| jr|d g }g }| jD ]}|j |vr3|t| q$|t| q$|rW||d|  g }|D ]}	||d |	  qK|d |r|d ||d|  g }|D ]}	||d |	  qq|d t|t|k rd	d | jD   fd
d| jD }
|d ||d|  g }|
D ]}	||d |	  q|d |d ||d|  g }| jD ]&}t||dd}t	|d t
stdt
 d||d  ||d  qdg}t| jr|ddd | jD  ||d|  ||d  |D ]}|dt|  q%d|S )a  Display a GraphProto as a string.

    .. deprecated:: 1.19
        Consider using :func:`onnx.printer.to_text` instead.

    Args:
        graph (GraphProto): the graph to display
        prefix (string): prefix of every line

    Returns:
        string
    z  ry   c                 S     h | ]}|j qS rA   rI  r  rA   rA   rB   r        z"printable_graph.<locals>.<setcomp>r  r  r  z,optional inputs with matching initializers (c                 S  r  rA   rI  rS   r!  rA   rA   rB   r    r  c                   s   g | ]}|j  vrt|qS rA   )r]   r  r  graph_inputsrA   rB   rK    s
    
z#printable_graph.<locals>.<listcomp>zinitializers ({T)r  r   z-contents_subgraphs[1] must be an instance of r   r   r7   r  c                 S  s   g | ]}d |j  qS r  rI  )rS   outrA   rA   rB   rK    s    }
)r]   rs   r   rc   r  r  r  ri   r  r   r/  r   rd   re   printable_graph)ry   r  r  indentheaderinitializersin_strsin_with_init_strsinpline	init_strsr=  ri   contents_subgraphstailr(  rA   r  rB   r  z  sj   













r  google.protobuf.message.Messagec                 C  s   t | tjjjstdtjjj d| jjD ]6}|jdkr%| 	|j q|j
|jkrM|j|jkr?t| |jD ]}t| q7q| |jrMtt| |j qdS )z:Empties `doc_string` field on any nested protobuf messageszproto must be an instance of r   r^   N)r   googleprotobufmessageMessager   
DESCRIPTORfieldsr]   
ClearFieldr   TYPE_MESSAGElabelLABEL_REPEATEDr   strip_doc_stringr  )r   
descriptorrT   rA   rA   rB   r    s    

r  	algorithmalgorithm_bindingsAssignmentBindingTypeinitializationGraphProto | Noneinitialization_bindingsAssignmentBindingType | Noner   c                 C  sr   t  }|j|  |D ]\}}|j }||_||_q|r#|j| |r7|D ]\}}|j }||_||_q'|S rQ   )	r   r  r   update_bindingr   rN   rb   r  initialization_binding)r  r  r  r  training_infor   r   bindingrA   rA   rB   make_training_info  s   

r  tensor_dtypenp.dtypec                 C     t j|  jS )zConvert a TensorProto's data_type to corresponding numpy dtype. It can be used while making tensor.

    Args:
        tensor_dtype: TensorProto's data_type

    Returns:
        numpy's data_type
    )r   TENSOR_TYPE_MAPr   r  rA   rA   rB   r        	r   c                 C  r  )zConvert a TensorProto's data_type to corresponding data_type for storage.

    Args:
        tensor_dtype: TensorProto's data_type

    Returns:
        data_type for storage
    )r   r  storage_dtyper  rA   rA   rB   $tensor_dtype_to_storage_tensor_dtype  r  r  c                 C  r  )zGet the name of given TensorProto's data_type.

    Args:
        tensor_dtype: TensorProto's data_type

    Returns:
        the name of data_type
    )r   r  r]   r  rA   rA   rB   tensor_dtype_to_string  r  r  c                 C  sZ   t tjdt tjdt tjdt tjdt tjdt tjdt tjdi}|t	j
|  j S )zConvert a TensorProto's data_type to corresponding field name for storage. It can be used while making tensors.

    Args:
        tensor_dtype: TensorProto's data_type

    Returns:
        field name
    
float_data
int32_data
int64_datadouble_datauint64_datastring_data)rH   r   r%  r   r   DOUBLEr  r  r   r   r  r  )r  storage_tensor_type_to_fieldrA   rA   rB   r     s   






	
r   r   TensorProto.DataTypec                 C  sP   dd t j D }| |v rtd||  S t| tjr tj	S t
d| d)zConvert a numpy's dtype to corresponding tensor type. It can be used while converting numpy arrays to tensors.

    Args:
        np_dtype: numpy's data_type

    Returns:
        TensorsProto's data_type
    c                 S  s   i | ]\}}|j |qS rA   )r   )rS   r   r   rA   rA   rB   
<dictcomp>=  s    z,np_dtype_to_tensor_dtype.<locals>.<dictcomp>r  zUnable to convert type z into TensorProto element type.)r   r  rh   typingcastr   
issubdtypestr_r   r   rM   )r   _np_dtype_to_tensor_dtyperA   rA   rB   np_dtype_to_tensor_dtype3  s   

r  KeysView[int]c                   C  s
   t j S )zcGet all tensor types from TensorProto.

    Returns:
        all tensor types from TensorProto
    )r   r  r   rA   rA   rA   rB   get_all_tensor_dtypesJ  s   
r  c                 C  s   i | ]\}}||qS rA   rA   )rS   r   r   rA   rA   rB   r  S  s    
r  zdict[int, str]_ATTRIBUTE_TYPE_TO_STRc                 C  s$   | t j v rt|  S t j d S )zConvert AttributeProto type to string.

    Args:
        attr_type: AttributeProto type.

    Returns:
        String representing the supplied attr_type.
    r   )r   AttributeTyper   r  r   )r  rA   rA   rB   r@  X  s   	r@  )r6   r4   r7   r8   )F)rD   rE   rF   rG   r7   rH   )NNNN)rX   rY   rZ   r[   r\   r[   r]   rJ   r^   rJ   rI   rJ   r_   rJ   r`   r   r7   r   )rI   rY   rK   rH   r7   r   )rp   rq   r]   rY   rZ   rr   r\   rr   rs   rt   r^   rJ   ru   rv   rw   rx   r7   r   )NNNNN)rI   rY   r}   rY   rZ   r[   r\   r[   rp   rq   r~   rE   r   r   r   r   r^   rJ   r_   rJ   ru   rv   r7   r   )ry   r   r`   r   r7   r   )r   r   r   r   r7   r   )r   r   r   r   r7   r   )r]   rY   r   rH   r   r   r   r   r   rG   r7   r   )r   r   r   r   r   r   r7   r   )r]   rY   r   r   r   r   r7   r
   )
r]   rY   r   rH   r   r   r   r
   r7   r   )r]   rY   r   r  rb   r	  r7   r	   )rb   r  r7   r   )NN)
rN   rY   rb   r   r^   rJ   r  r  r7   r   rQ   )
r]   rY   r  rE  r^   rJ   rD  rJ   r7   r   )rA  r   r7   r   )ri   r   rH  rY   r7   r   )r]   rY   r7   r   )r   rH   rP  rQ  rR  rS  r7   r   )rc  N)r]   rY   r   rH   rP  rQ  r^   rY   rR  rS  r7   r   )rj  r   r7   r   )r   rH   rr  r   r7   r   )rc  )r]   rY   r]  r   r^   rY   r7   r   )r&  r  r7   rY   )r]   rY   r   rH   rP  rQ  r^   rY   r}  rS  r7   r   )rA  r   r  rG   r7   r  )rX  r  r7   rY   )r  r   r7   rY   )r   r   r7   rY   )r  r   r7   rY   )rc  F)ri   r   r  rY   r  rG   r7   r  )ry   r   r  rY   r7   rY   )r   r  r7   r   )
r  r   r  r  r  r  r  r  r7   r   )r  rH   r7   r  )r  rH   r7   rH   )r  rH   r7   rY   )r   r  r7   r  )r7   r  )r  rH   r7   rY   )i
__future__r   collections.abcr,  	functoolsr   r  r  r   r   r   google.protobuf.messager  numpyr   typing_extensionsr   r   r   onnx.onnx_data_pbr   r	   r
   onnx.onnx_pbr   r   r   r   r   r   r   r   r   r   r   r   r   r   #google.protobuf.internal.containersr   tuplerY   rH   VersionRowTyper/  r4   r  r5   __annotations__dictr8   rC   rL   rW   rj   ro   rz   r|   r   r   r   r   r   r   r   r   r  r  r   ra   rF  rG  rM  rO  ra  rd  rh  ri  rn  rq  rt  ru  r|  r  r  r  r  r  r  r  
deprecatedr  r  r  r   r  r  	lru_cacher   r  r  r  rh   r  r@  rA   rA   rA   rB   <module>   s   4"$
 
1
+
$
%
	
	
{




a
"
$
		11		P#P	