o
    soi                     @  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m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ryd 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  Z0g d	Z1d
e2d< e3e*e+e,f e,f Z4dddZ5e5e1Z6	ddddZ7				ddd&d'Z8dd*d+Z9				ddd6d7Z:dd8d9Z;					dddAdBZ<ddEdFZ=ddGdHZ>ddNdOZ?ddQdRZ@ddVdWZAddXdYZB	dddadbZCddfdgZDddldmZEddrdsZFddxdyZGdd|d}ZH		ddddZI	dd ddZJdddZKdddZLdddZM	ddddZN		ddddZO	ddddZP		ddddZQdddZRdddZSdddZT	d	d
ddZUdddZV		ddddZW	ddddZXdddZYdddZZdddZ[dddZ\	ddddZ]e^dád	dddńZ_dddȄZ`ddd҄ZadddքZbddd؄ZcdddڄZdeedddd܄ZfeeddddZgdddZhdd ejij D Zkde2d< dddZld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   VersionTableTypeVERSION_TABLEtablereturnVersionMapTypec                   s(   i  d fd	d
}| D ]}||  q S )zOCreate a map from (opset-domain, opset-version) to ir-version from above table.release_versionstr
ir_versionintargsr   r6   Nonec                   sJ   ~ t g d|ddD ]}| vr"| |< |d dkr"| d|d f< q
d S )N)ai.onnxz
ai.onnx.mlai.onnx.trainingF)strictr   r?   zai.onnx.preview.trainingr   )zip)r8   r:   r<   pairresult ?/home/ubuntu/.local/lib/python3.10/site-packages/onnx/helper.pyprocessX   s   

z._create_op_set_id_version_map.<locals>.processN)r8   r9   r:   r;   r<   r   r6   r=   rE   )r5   rG   rowrE   rC   rF   _create_op_set_id_version_mapT   s
   

rI   FopsetidlistSequence[OperatorSetIdProto]ignore_unknownboolr;   c                   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versionr;   r6   c                   s,   | pd|f}|t v rt | S r S td)Nr>   zUnsupported opset-version.)OP_SET_ID_VERSION_MAP
ValueError)rN   rP   key)default_min_versionrL   rE   rF   find_miny   s   z)find_min_ir_version_for.<locals>.find_minc                 3  s    | ]
} |j |jV  qd S N)rN   rP   .0x)rU   rE   rF   	<genexpr>   s    z*find_min_ir_version_for.<locals>.<genexpr>N)rN   rO   rP   r;   r6   r;   )max)rJ   rL   rE   )rT   rU   rL   rF   find_min_ir_version_forj   s
   r\   op_typer9   inputsSequence[str]outputsnamerO   
doc_stringrN   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 rV   )make_attribute)rX   rS   valuerE   rE   rF   rZ      s    zmake_node.<locals>.<genexpr>)r   r]   inputextendoutputra   rb   rN   rc   	attributesorteditems)	r]   r^   r`   ra   rb   rN   rc   rd   noderE   rE   rF   	make_node   s"   
rn   rP   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   rN   rP   )rN   rP   operatorsetidrE   rE   rF   make_operatorsetid   s   rs   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   rm   rh   ra   rg   ri   rw   r{   ry   rb   )	rt   ra   r^   r`   rw   rb   ry   r{   graphrE   rE   rF   
make_graph   s"   r~   c                 C  ro   rp   rq   )rN   rP   opsetidrE   rE   rF   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 rV   )r   rN   ra   rg   rh   ri   rm   opset_importrj   attribute_protorb   rc   ry   )rN   r   r^   r`   rt   r   r   r   rb   rc   ry   frE   rE   rF   make_function  s*   r   r}   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:   r}   CopyFrompopr   rh   addr   onnx_opset_versionrP   r   rl   setattr)r}   rd   modelr   impr   kvrE   rE   rF   
make_model'  s    	

r   c                 K  s8   d}||vrd}| |g }t|||< t| fi |S )Nr:   r   )getr\   r   )r}   rd   ir_version_fieldopset_imports_fieldimportsrE   rE   rF   make_model_gen_versionL  s   r   protoRModelProto | GraphProto | FunctionProto | NodeProto | TensorProto | ValueInfoProto
dict_valuedict[str, str]r=   c                 C  s8   | j d d = | D ]\}}| j  }||_||_q
d S rV   )metadata_propsrl   r   rS   rf   )r   r   r   r   entryrE   rE   rF   set_metadata_propsU  s   
r   r   c                 C  s   t | | d S rV   )r   )r   r   rE   rE   rF   set_model_propsg  s   r   array
np.ndarraynpt.NDArray[np.uint8]c                 C  sx   |   tj }| j}|d dk}|r|j|d gdd |dM }|ddd  dK  < |ddd |ddd B S )	z[Convert a numpy array to flatten, packed int4/uint4. Elements must be in the correct range.r$   r   Frefcheckr(   Nr    r   ravelviewnpuint8copysizeresize)r   
array_flatr   	odd_sizedrE   rE   rF   _pack_4bitx2k  s   r   c                 C  s   |   tj }| j}|d }|r|j|d|  gdd |dM }|ddd  dK  < |ddd  dK  < |ddd  dK  < |d	dd |ddd B |ddd B |ddd B S )
z[Convert a numpy array to flatten, packed int2/uint2. Elements must be in the correct range.r    Fr   r   r   Nr$   r   r   r   )r   r   r   pad_lenrE   rE   rF   _pack_2bitx4x  s   8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 j*kr|+tj,}n]|t j-kr|+tj.}nP|t j/t j0hv r|+tj1}n@|t j"t j#t j$t j%t j(hv r2|+tj2}n*|t jt j	t j
hv rBt|}n|t jt jhv rPt|}n|t j3kr\|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)dtype)7r   r   ra   r   rh   STRING	TypeErrortensor_dtype_to_np_dtypeUINT4INT4
FLOAT4E2M1UINT2INT2itemsizemathprodceil
isinstancer   ndarrayr   numpy_helperr   r   tobytes_little_endianbytestypelenrR   raw_datar   flatten	vectorize	_to_bytesFLOAT8E4M3FNFLOAT8E4M3FNUZ
FLOAT8E5M2FLOAT8E5M2FNUZsaturate_castasarray
FLOAT8E8M0to_float8e8m0
COMPLEX128r   float64	COMPLEX64float32BFLOAT16FLOAT16uint16r   BOOLastypetensor_dtype_to_fieldgetattr)
ra   r   r   r   r   tensornp_dtypeexpected_size_bytesr   fieldrE   rE   rF   make_tensor  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   rh   )r   r   r   sparserE   rE   rF   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
   ra   r   	UNDEFINEDTENSORtensor_valuesSPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesMAP
map_valuesr	   OPTIONALoptional_valuesr   rh   )ra   r   r   sequencerj   rE   rE   rF   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UINT64ra   r  r   string_keysrh   r  r   r   )ra   r  r  r   	map_protovalid_key_int_typesrE   rE   rF   make_map/  s$   


r  OptionalProto.DataTyperf   &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	   ra   r   r   r   tensor_valuer   sparse_tensor_valuer   sequence_valuer  	map_valuer  optional_valuer   r   )ra   r   rf   optionalrj   rE   rE   rF   make_optionalN  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   r9   encode)rf   rE   rE   rF   r   m  s   

r   rS   	attr_type
int | Noner   c              
     s(  t  }| |_|r||_t|tjrt||_t j|_	nTt|tj
r-t||_t j|_	nCt|ttfr?t||_t j|_	n1t|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#rht$|}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'|_	nv|t j(kr|j10| t j(|_	ne|t j)kr!|j20d
d |D  t j)|_	nO|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 rE   )r   rX   r   rE   rE   rF   	<setcomp>  s    z!make_attribute.<locals>.<setcomp>c                 3  s    | ]}t | V  qd S rV   )
issubclassrX   texp_trE   rF   rZ     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 rV   )r   r$  rE   rE   rF   rZ     s    'z%' is not an accepted attribute value.zInferred attribute type 'z'(z") mismatched with specified type ')):r   ra   rb   r   numbersIntegralr;   iINTr   Realfloatr   FLOATr9   r   r   sr   r   r(  r   r   r   r   sparse_tensorr   r   gGRAPHr   tp
TYPE_PROTOcollectionsabcIterablelistr   rR   INTSFLOATSSTRINGSTENSORSSPARSE_TENSORSGRAPHSTYPE_PROTOSallintsrh   floatsstringstensorssparse_tensorsgraphstype_protosAssertionErrorr   _attr_type_to_str)rS   rf   rb   r"  attrtypesexp_enumrE   r)  rF   re   t  s   











	







(re   AttributeProto.AttributeTypec                 C  s    t  }| |_||_|r||_|S )zeMake an AttributeProto holding a reference to the parent function's attribute of given name and type.)r   ra   r   rb   )ra   r"  rb   rO  rE   rE   rF   make_attribute_ref  s   rS  rO  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: )"ref_attr_namerR   r   r   r3  r   r0  r/  r   r4  r   r(  r   r5  r7  r6  r9  r8  r?  r=  rG  r>  rF  r@  rH  rA  rI  rB  rJ  rC  rK  rD  rL  r   )rO  rE   rE   rF   get_attribute_value  sB   






rU  rm   	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 rE   ra   rW   rV  rE   rF   
<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   )rj   r   rR   rU  )rm   rV  matchingrE   rX  rF   get_node_attr_value  s   r[  r   c                 C  s   t  }| |_|S rV   )r   ra   )ra   value_info_protorE   rE   rF   make_empty_tensor_value_info  s   r]  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   r^  dimrh   r   rR   	enumerater   r   r;   	dim_valuer9   	dim_param
denotation)	r   r^  r`  
type_prototensor_type_prototensor_shape_protor/  drf  rE   rE   rF   make_tensor_type_proto  2   




ro   c                 C  s2   t  }| |_|r||_t|||}|j| |S )z8Makes a ValueInfoProto based on the data type and shape.)r   ra   rb   ro  r   r   )ra   r   r^  rb   r`  r\  rl  rE   rE   rF   make_tensor_value_info>  s   rr  c           	      C  rb  )z@Makes a SparseTensor TypeProto based on the data type and shape.Nrc  rd  z. Needs to be of int or text.)r   sparse_tensor_typer   r^  rf  rh   r   rR   rg  r   r   r;   rh  r9   ri  rj  )	r   r^  r`  rk  sparse_tensor_type_protosparse_tensor_shape_protor/  rn  rf  rE   rE   rF   make_sparse_tensor_type_protoP  rp  rv  c                 C  s6   t  }| |_|r||_t|||}|jj|j |S )zEMakes a SparseTensor ValueInfoProto based on the data type and shape.)r   ra   rb   rv  r   rs  r   )ra   r   r^  rb   r`  r\  rt  rE   rE   rF   make_sparse_tensor_value_info}  s   rw  inner_type_protoc                 C     t  }|jj|  |S )zMakes a sequence TypeProto.)r   sequence_typer   r   rx  rk  rE   rE   rF   make_sequence_type_proto     r|  c                 C  ry  )zMakes an optional TypeProto.)r   optional_typer   r   r{  rE   rE   rF   make_optional_type_proto  r}  r  
value_typec                 C  s    t  }| |j_|jj| |S )zMakes a map TypeProto.)r   map_typer  r  r   )r  r  rk  rE   rE   rF   make_map_type_proto  s   r  rk  c                 C  s&   t  }| |_|r||_|j| |S )z1Makes a ValueInfoProto with the given type_proto.)r   ra   rb   r   r   )ra   rk  rb   r\  rE   rE   rF   make_value_info  s   r  r4  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   r9   r   decoder   )r4  	sanitizedrE   rE   rF   _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   ra   rb   ro  r|  r   rz  r   )ra   r   r^  rb   r  r\  rl  sequence_type_protorE   rE   rF   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rP| tt| j n| drwt	| j
jdkrc| d nt| j
j}| dt| j
| d n| dr| d| jj d | | j n| dr| d| j d n| jr| ||| j n| jr| ||| j n| jr| tttt| j ny| jr| d np| jr| d t| jD ]\}	}
|	t	| jd krd nd!}| d|
 d|  q| d" n?| jr=| d t| jD ]\}	}|	t	| jd kr!d nd!}| d|j d|  q| d" || j n| d# |rLd$||fS d$|S )(N=r   r2  r6   r9   c                 S  s   | dS )Nz.15grE   )r   rE   rE   rF   	str_float  s   z&printable_attribute.<locals>.str_floatr/  r;   c                 S  s   t | S rV   )r9   )r/  rE   rE   rF   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  rE   rE   rF   str_list  s   z%printable_attribute.<locals>.str_listr4  r(  r   z<Tensor>z<Scalar Tensor r  r6  z<graph r8  z<Type Proto z[<Tensor>, ...]r  r   ,rq  r  z	<Unknown> )r   r2  r6   r9   )r/  r;   r6   r9   )r  r  r  r  r6   r9   )appendra   r   HasFieldr   r/  reprr  r4  r   r(  r   r   r   r   r6  r8  rG  rF  rH  r9   r=  r  rI  rL  rg  rK  rh   r  )rO  r  contentr  r  r  r  rK  r   r/  r8  commar6  rE   rE   rF   printable_attribute  sb   



	










r  rf  TensorShapeProto.Dimensionc                 C  s$   |  d}|d u rdS tt| |S )Nrf   ?)
WhichOneofr9   r   )rf  whichrE   rE   rF   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 )	Nrf   re  r^  r  rY   , scalarrq  zUnknown type )r  r   DataTypeNamere  r   r  r   r^  rf  r9   r  r  r  r(  r4  rE   rE   rF   printable_type2  s   "r  r   c                 C  s,   d| j  }| jr| dt| j d}|S )N%r  r  )ra   r   r  )r   r4  rE   rE   rF   printable_value_info@  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  rY   r  r  )
ra   r   r  r  r   r   r   r9   r  r  r  rE   rE   rF   printable_tensor_protoG  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  rE   rX   ra   rE   rE   rF   rY  X      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  rE   r  rE   rE   rF   rY  l  r  r  z](r,  (r  )r   ri   r  r  rj   r  r   r=  r   rh   r9   rk   rg   r]   )rm   r  r  r  rK  printed_attrsrO  printed_attr_subgraphsprintedprinted_attributesprinted_inputsrE   rE   rF   printable_nodeS  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  r}   c                 S     h | ]}|j qS rE   rW  r'  rE   rE   rF   r%        z"printable_graph.<locals>.<setcomp>r  r  r,  z,optional inputs with matching initializers (c                 S  r  rE   rW  rX   r/  rE   rE   rF   r%    r  c                   s   g | ]}|j  vrt|qS rE   )ra   r  r  graph_inputsrE   rF   rY    s
    
z#printable_graph.<locals>.<listcomp>zinitializers ({T)r  r   z-contents_subgraphs[1] must be an instance of r   r   r6   r  c                 S  s   g | ]}d |j  qS r  rW  )rX   outrE   rE   rF   rY    s    }
)ra   rw   r   rg   r  r  r  rm   r  r   r=  r   rh   ri   printable_graph)r}   r  r  indentheaderinitializersin_strsin_with_init_strsinpline	init_strsrK  rm   contents_subgraphstailr6  rE   r  rF   r  v  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   rb   N)r   googleprotobufmessageMessager   
DESCRIPTORfieldsra   
ClearFieldr   TYPE_MESSAGElabelLABEL_REPEATEDr   strip_doc_stringr  )r   
descriptorrY   rE   rE   rF   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 rV   )	r   r  r   update_bindingr   rS   rf   r  initialization_binding)r  r  r  r  training_infor   r   bindingrE   rE   rF   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  rE   rE   rF   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  rE   rE   rF   $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  ra   r  rE   rE   rF   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)r;   r   r3  r  r  DOUBLEr  r  r   r   r  r  )r  storage_tensor_type_to_fieldrE   rE   rF   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 rE   )r   )rX   r   r   rE   rE   rF   
<dictcomp>9  s    z,np_dtype_to_tensor_dtype.<locals>.<dictcomp>r  zUnable to convert type z into TensorProto element type.)r   r  rl   typingcastr   
issubdtypestr_r   r   rR   )r   _np_dtype_to_tensor_dtyperE   rE   rF   np_dtype_to_tensor_dtype/  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  rE   rE   rE   rF   get_all_tensor_dtypesF  s   
r  c                 C  s   i | ]\}}||qS rE   rE   )rX   r   r   rE   rE   rF   r  O  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"  rE   rE   rF   rN  U  s   	rN  )r5   r3   r6   r7   )F)rJ   rK   rL   rM   r6   r;   )NNNN)r]   r9   r^   r_   r`   r_   ra   rO   rb   rO   rN   rO   rc   rO   rd   r   r6   r   )rN   r9   rP   r;   r6   r   )rt   ru   ra   r9   r^   rv   r`   rv   rw   rx   rb   rO   ry   rz   r{   r|   r6   r   )NNNNN)rN   r9   r   r9   r^   r_   r`   r_   rt   ru   r   rK   r   r   r   r   rb   rO   rc   rO   ry   rz   r6   r   )r}   r   rd   r   r6   r   )r   r   r   r   r6   r=   )r   r   r   r   r6   r=   )r   r   r6   r   )ra   r9   r   r;   r   r   r   r   r   rM   r6   r   )r   r   r   r   r   r   r6   r   )ra   r9   r   r   r   r   r6   r
   )
ra   r9   r  r;   r  r	  r   r
   r6   r   )ra   r9   r   r  rf   r  r6   r	   )rf   r  r6   r   )NN)
rS   r9   rf   r   rb   rO   r"  r#  r6   r   rV   )ra   r9   r"  rR  rb   rO   r6   r   )rO  r   r6   r   )rm   r   rV  r9   r6   r   )ra   r9   r6   r   )r   r;   r^  r_  r`  ra  r6   r   )rq  N)ra   r9   r   r;   r^  r_  rb   r9   r`  ra  r6   r   )rx  r   r6   r   )r  r;   r  r   r6   r   )rq  )ra   r9   rk  r   rb   r9   r6   r   )r4  r  r6   r9   )ra   r9   r   r;   r^  r_  rb   r9   r  ra  r6   r   )rO  r   r  rM   r6   r  )rf  r  r6   r9   )r(  r   r6   r9   )r   r   r6   r9   )r(  r   r6   r9   )rq  F)rm   r   r  r9   r  rM   r6   r  )r}   r   r  r9   r6   r9   )r   r  r6   r=   )
r  r   r  r  r  r  r  r  r6   r   )r  r;   r6   r  )r  r;   r6   r;   )r  r;   r6   r9   )r   r  r6   r  )r6   r  )r"  r;   r6   r9   )m
__future__r   collections.abcr:  	functoolsr   r-  r  r   r   r   google.protobuf.messager  numpyr   numpy.typingnpt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   tupler9   r;   VersionRowTyper=  r3   r  r4   __annotations__dictr7   rI   rQ   r\   rn   rs   r~   r   r   r   r   r   r   r   r   r   r   r  r  r  r   re   rS  rU  r[  r]  ro  rr  rv  rw  r|  r  r  r  r  r  r  r  r  r  r  r  
deprecatedr  r  r  r   r  r  	lru_cacher   r  r  r  rl   r  rN  rE   rE   rE   rF   <module>   s   4"#
 
1
+
$
%
	



u




_$		11		L#P	