
    /jŴ                    f   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rd 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         z  Z-e.e-         Z/e.e*e+e+f                  Z0g d	Z1d
e2d<   e3e*e+e,f         e,f         Z4ddZ5 e5e1          Z6	 dddZ7	 	 	 	 ddd$Z8dd'Z9	 	 	 	 ddd2Z:dd3Z;	 	 	 	 	 ddd;Z<dd>Z=dd?Z>ddEZ?ddGZ@ddKZAddLZB	 dddTZCddXZDdd]ZEddbZFddgZGddjZH	 	 dddoZI	 dddqZJddsZKddvZLddxZM	 ddd~ZN	 	 dddZO	 dddZP	 	 dddZQddZRddZSddZT	 dddZUddZV	 	 dddZW	 dddZXddZYddZZddZ[ddZ\	 dddZ] ej^        d          ddd            Z_ddZ`ddZaddZbddZcddZd eje        d          dd            Zf eje        d          dd            ZgddZhd eji        j                                D             Zkde2d<   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&   r2   r   r   )z1.20.0r'      r   r   )z1.20.1r'   r3   r   r   )z1.21.0r'      r   r   VersionTableTypeVERSION_TABLEtablereturnVersionMapTypec                ,    i d
fd	}| D ]} ||  S )zOCreate a map from (opset-domain, opset-version) to ir-version from above table.release_versionstr
ir_versionintargsr   r8   Nonec                |    ~ t          g d|d          D ]$}|vr||<   |d         dk    r|d|d         f<   %d S )N)ai.onnxz
ai.onnx.mlai.onnx.trainingF)strictr   rC   zai.onnx.preview.trainingr   )zip)r;   r=   r?   pairresults       P/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/onnx/helper.pyprocessz._create_op_set_id_version_map.<locals>.processY   sz    9994
 
 
 	M 	MD 6!!)t7000BLF5tAw>?	M 	M    )r;   r<   r=   r>   r?   r   r8   r@    )r7   rI   rowrG   s      @rH   _create_op_set_id_version_maprM   U   sO    FM M M M M M   MrJ   FopsetidlistSequence[OperatorSetIdProto]ignore_unknownboolr>   c                X    dd	fd| rt          f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>   r8   c                b    | pd|f}|t           v rt           |         S rS t          d          )NrB   zUnsupported opset-version.)OP_SET_ID_VERSION_MAP
ValueError)rS   rU   keydefault_min_versionrP   s      rH   find_minz)find_min_ir_version_for.<locals>.find_minz   sF    "G,'''(-- 	'&&5666rJ   c              3  D   K   | ]} |j         |j                  V  d S N)rS   rU   ).0xr[   s     rH   	<genexpr>z*find_min_ir_version_for.<locals>.<genexpr>   s3      FFQ88AHai00FFFFFFrJ   )rS   rT   rU   r>   r8   r>   )max)rN   rP   rZ   r[   s    `@@rH   find_min_ir_version_forrb   k   sd     7 7 7 7 7 7 7  GFFFF+FFFFFFrJ   op_typer<   inputsSequence[str]outputsnamerT   
doc_stringrS   overloadkwargsr   r   c                j   t                      }| |_        |j                            |           |j                            |           |r||_        |r||_        |||_        |||_        |rC|j	                            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              3  @   K   | ]\  }}|t          ||          V  d S r]   )make_attribute)r^   rY   values      rH   r`   zmake_node.<locals>.<genexpr>   sB       
 
U  3&&    
 
rJ   )r   rc   inputextendoutputrg   rh   rS   ri   	attributesorteditems)	rc   rd   rf   rg   rh   rS   ri   rj   nodes	            rH   	make_noderv      s    8 ;;DDLJfKw 	 %$  
 
 
$V\\^^44
 
 
 	
 	
 	

 KrJ   rU   r   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   rS   rU   )rS   rU   operatorsetids      rH   make_operatorsetidr{      s%     '((M!M#MrJ   nodesSequence[NodeProto]Sequence[ValueInfoProto]initializerSequence[TensorProto] | None
value_infoSequence[ValueInfoProto] | Nonesparse_initializer'Sequence[onnx.SparseTensorProto] | Noner   c                   |g }|g }|g }t                      }|j                            |            ||_        |j                            |           |j                            |           |j                            |           |j                            |           |j                            |           |r||_	        |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
    )
r   ru   rp   rg   ro   rq   r   r   r   rh   )	r|   rg   rd   rf   r   rh   r   r   graphs	            rH   
make_graphr      s    0 !
LLE	JeEJ	Kv	L   	[)))	##$6777	J''' &%LrJ   c                >    t                      }| |_        ||_        |S rx   ry   )rS   rU   opsetids      rH   make_opsetidr      s"     !""GGNGONrJ   fnameopset_imports
attributesSequence[str] | Noneattribute_protosSequence[AttributeProto] | Noner   c                   |g }|g }|
g }
t                      }| |_        ||_        |j                            |           |j                            |           |j                            |           |j                            |           |j                            |           |j	                            |           |r||_
        |	|	|_        |j                            |
           |S r]   )r   rS   rg   ro   rp   rq   ru   opset_importrr   attribute_protorh   ri   r   )rS   r   rd   rf   r|   r   r   r   rh   ri   r   fs               rH   make_functionr     s     

AAHAFGNN6HOOGFMM%N-(((Kz"""-... "!
L
###HrJ   r   r   c                   t                      }t          j        |_        |j                            |            |                    dd          }||j                            |           n1|j        	                                }t          j                    |_        |                    dd          }||j                            |           |                                D ]\  }}t          |||           |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   rp   addr   onnx_opset_versionrU   r   rt   setattr)r   rj   modelr   impr   kvs           rH   
make_modelr   (  s     LLE E	K9?: :M  !!-0000  $$&&-//06

;0M0MIy)))  1q!LrJ   c                |    d}||vr*d}|                     |g           }t          |          ||<   t          | fi |S )Nr=   r   )getrb   r   )r   rj   ir_version_fieldopset_imports_fieldimportss        rH   make_model_gen_versionr   M  sV    #v%%-**0"55#:7#C#C e&&v&&&rJ   protoRModelProto | GraphProto | FunctionProto | NodeProto | TensorProto | ValueInfoProto
dict_valuedict[str, str]r@   c                    | j         d d = |                                D ],\  }}| j                                         }||_        ||_        -d S r]   )metadata_propsrt   r   rY   rn   )r   r   r   r   entrys        rH   set_metadata_propsr   V  s`     	QQQ  ""  1$((**	 rJ   r   c                &    t          | |           d S r]   )r   )r   r   s     rH   set_model_propsr   h  s    uj)))))rJ   array
np.ndarraynpt.NDArray[np.uint8]c                @   |                                                      t          j                                                  }| j        }|dz  dk    }|r|                    |dz   gd           |dz  }|dddxx         dz  cc<   |ddd         |ddd         z  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_sizeds       rH   _pack_4bitx2r   l  s     ##BH--2244J:DqAI 64!8*u555$Jqt!taddjA...rJ   c                   |                                                      t          j                                                  }| j        }|dz  }|r|                    |d|z
  z   gd           |dz  }|dddxx         dz  cc<   |dddxx         dz  cc<   |dddxx         dz  cc<   |d	dd         |ddd         z  |ddd         z  |ddd         z  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_lens       rH   _pack_2bitx4r   y  s    ##BH--2244J:DQhG B41w;/05AAA$Jqt!tqt!tqt!taddjA..ADqD1AAJqtRStDTTTrJ   	data_typedimsSequence[int]vals*Sequence[int | float] | bytes | np.ndarrayrawr   c                j
   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 rd}n|j        }|t          j        |          z  }t          j        |          }t#          |t$          j                  r|t           j	        t           j        t           j
        hv r t(          j                            |          }n9|t           j        t           j        hv rt(          j                            |          }t(          j                            |          }n8t#          |t2                    r|}n t          dt5          |           d          t7          |          |k    r#t9          d| dt7          |           d          ||_        |S |r
J d	            |t           j        k    r\t%          j        |                                          }t7          |          d
k    r" t%          j         tB                    |          }n|t           j"        t           j#        t           j$        t           j%        hv rEt(          j        &                    t%          j'        |          |                                          }n|t           j(        k    rGt(          j        )                    t%          j'        |          dd                                          }n(t%          j'        ||                                          }|t           j*        k    r!|+                    t$          j,                  }nT|t           j-        k    r!|+                    t$          j.                  }n#|t           j/        t           j0        hv r |+                    t$          j1                  }n|t           j"        t           j#        t           j$        t           j%        t           j(        hv r |+                    t$          j2                  }n|t           j        t           j	        t           j
        hv rt-          |          }nY|t           j        t           j        hv rt/          |          }n/|t           j3        k    r|4                    t$          j2                  }tk          |          }	tm          ||	                              |           |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   rg   r   rp   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lenrX   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)
rg   r   r   r   r   tensornp_dtypeexpected_size_bytesr   fields
             rH   make_tensorr    s   0 ]]F FFK
KtK&&&3&DEEE'	22H
  *K,<k>TUUU"%;,k.>???"&"*"3ty."i(;<<dBJ'' 	 !&  
 (55d;;{0+2BCCC(55d;;(>>tDDHHe$$ 	HHQDJJQQQ   x==/// DH[  D  Dmpqymzmz  D  D  D   #=====7K&&&x~~%%''t99>>*2<	**400D	 ""	 
 
  ..rz$/?/?JJRRTT	k,	,	, ..Jtt / 
 

')) 	 z$h///7799K***yy$$	k+	+	+yy$$	{+[-@A	A	Ayy##	 "" 
 
 yy""	{(+*:K<RS	S	SD!!	{(+*:;	;	;D!!	k&	&	&{{28$$!),,EFE!!$'''MrJ   valuesindicesonnx.SparseTensorProtoc                    t          j                    }|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   rp   )r  r  r   sparses       rH   make_sparse_tensorr	    sY     #%%F
M6"""
NG$$$
KtMrJ   	elem_typeSequenceProto.DataTypeSequence[Any]r   c                   t                      }| |_        ||_        |t           j        k    r|S d}|t           j        k    r|j        }no|t           j        k    r|j        }nW|t           j        k    r|j	        }n?|t           j
        k    r|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   rg   r
  	UNDEFINEDTENSORtensor_valuesSPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesMAP
map_valuesr
   OPTIONALoptional_valuesr   rp   )rg   r
  r  sequencerr   s        rH   make_sequencer    s     HHM"HM+++8<IM(((*			m1	1	11			m,	,	,,			m'	'	''			m,	,	,,		RSSSVOrJ   key_typekeys	list[Any]r	   c                   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 r|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UINT64rg   r  r   string_keysrp   r  r  r   )rg   r  r  r  	map_protovalid_key_int_typess         rH   make_mapr*  0  s     

I	 IN!I;%%%$$T****	(	(	(d###f%%%rJ   OptionalProto.DataTypern   &google.protobuf.message.Message | Noner
   c                   t                      }| |_        ||_        |t           j        k    r|S d}|t           j        k    r|j        }no|t           j        k    r|j        }nW|t           j        k    r|j	        }n?|t           j
        k    r|j        }n'|t           j        k    r|j        }nt          d          |J |                    |           |S )z0Make an Optional with specified value arguments.Nz8The element type in the input optional is not supported.)r
   rg   r
  r  r  tensor_valuer  sparse_tensor_valuer  sequence_valuer  	map_valuer  optional_valuer   r   )rg   r
  rn   optionalrr   s        rH   make_optionalr4  O  s     HHM"HM+++8<IM((()			m1	1	10			m,	,	,+			m'	'	'&			m,	,	,+		RSSSuOrJ   str | bytesr   c                Z    t          | t                    r|                     d          S | S )z2Coerce a string (or bytes) value into UTF-8 bytes.utf-8)r   r<   encode)rn   s    rH   r   r   n  s+    % %||G$$$LrJ   rY   	attr_type
int | Noner   c                
   t                      }| |_        |r||_        t          |t          j                  r't          |          |_        t           j        |_	        nt          |t          j
                  r't          |          |_        t           j        |_	        nSt          |t          t          f          r't!          |          |_        t           j        |_	        nt          |t&                    r-|j                            |           t           j        |_	        nt          |t.          j                  r-|j                            |           t           j        |_	        nt          |t6                    r-|j                            |           t           j        |_	        nEt          |t<                    r-|j                            |           t           j         |_	        nt          |tB          j"        j#                  rtI          |          }tK          |          dk    r|tM          d|  d          |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t6          t           j,        ft<          t           j-        ffD ]$\  }t]          fd|D                       r|} n%|tM          d          |t           j'        k    r-|j/        0                    |           t           j'        |_	        n|t           j(        k    r-|j1        0                    |           t           j(        |_	        nX|t           j)        k    r7|j2        0                    d |D                        t           j)        |_	        n|t           j*        k    r,|j3        0                    |           t           j*        |_	        n|t           j+        k    r,|j4        0                    |           t           j+        |_	        n|t           j,        k    r,|j5        0                    |           t           j,        |_	        n]|t           j-        k    r,|j6        0                    |           t           j-        |_	        n!to                      tq          d	| d
          |K|j	        |k    r@tq          dts          |j	                   d|j	         dts          |           d| d	          |S )z0Makes an AttributeProto based on the value type.r   NzCould not infer attribute `z` type from empty iteratorc                ,    h | ]}t          |          S rK   )r   r^   r   s     rH   	<setcomp>z!make_attribute.<locals>.<setcomp>  s    ,,,T!WW,,,rJ   c              3  8   K   | ]}t          |          V  d S r]   )
issubclass)r^   texp_ts     rH   r`   z!make_attribute.<locals>.<genexpr>  s-      ;;z!U++;;;;;;rJ   zRCould not infer the attribute type from the elements of the passed Iterable value.c              3  4   K   | ]}t          |          V  d S r]   )r   r=  s     rH   r`   z!make_attribute.<locals>.<genexpr>  s(      <<	!<<<<<<rJ   'z%' is not an accepted attribute value.zInferred attribute type 'z'(z") mismatched with specified type ')):r   rg   rh   r   numbersIntegralr>   iINTr   Realfloatr   FLOATr<   r   r   sr   r   rA  r   r  r   r  sparse_tensorr  r   gGRAPHr   tp
TYPE_PROTOcollectionsabcIterablelistr   rX   INTSFLOATSSTRINGSTENSORSSPARSE_TENSORSGRAPHSTYPE_PROTOSallintsrp   floatsstringstensorssparse_tensorsgraphstype_protosAssertionErrorr   _attr_type_to_str)rY   rn   rh   r9  attrtypesexp_enumrB  s          @rH   rm   rm   u  s    DDI %$ %)** HJU"&			E7<	(	( EJu"(			EC<	(	( BJ5!!")			E;	'	' >J")			E41	2	2 ;J##E***"0			E:	&	& 8J"(			E9	%	% 5J"-			E;?3	4	4 1JUu::??y0McMMM   ,,e,,,E!>#67~45u~56n45')FG^23N67$  x ;;;;U;;;;;  (IE   h   +++IU###&+DII.///Ku%%%&-DII.000L<<e<<<<<<&.DII.000L&&&&.DII.777&&u---&5DII.///Ku%%%&-DII.444##E***&2DII """HEHHHIIIi!7!7 b(9$)(D(D  b  b	  b  b  vG  HQ  vR  vR  b  b  V_  b  b  b
 
 	
 KrJ   AttributeProto.AttributeTypec                P    t                      }| |_        ||_        |r||_        |S )zeMake an AttributeProto holding a reference to the parent function's attribute of given name and type.)r   rg   r   rh   )rg   r9  rh   rh  s       rH   make_attribute_refrm    s1     DDIDI %$KrJ   rh  c                L   | 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    r| j        S | j        t          j        k    r| j        S | j        t          j        k    r| j        S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                  S | j        t          j        k    rt'          | j                   S | j        t          j!        k    rd S t          d|            )Nz)Cannot get value of reference attribute: zUnsupported ONNX attribute: )"ref_attr_namerX   r   r   rL  r   rI  rH  r   rM  r  rA  r  rN  rP  rO  rR  rQ  rX  rV  r`  rW  r_  rY  ra  rZ  rb  r[  rc  r\  rd  r]  re  r  )rh  s    rH   get_attribute_valuerp    s    MKTKKLLLyN(((vyN&&&vyN)))vyN)))vyN000!!yN(((vyN---wyN)))DK   yN'''DIyN***DL!!!yN***DL!!!yN111D'(((yN)))DK   yN...D$%%%yN,,,t
:D::
;
;;rJ   ru   	attr_namec                    fd| j         D             }t          |          dk    rt          d           t          |          dk     rt          d           t          |d                   S )Nc                *    g | ]}|j         k    |S rK   rg   )r^   r_   rq  s     rH   
<listcomp>z'get_node_attr_value.<locals>.<listcomp>  s%    AAAaQVy-@-@-@-@-@rJ   r   z'Node has multiple attributes with name z Node has no attribute with name r   )rr   r   rX   rp  )ru   rq  matchings    ` rH   get_node_attr_valuerw    s    AAAA4>AAAH
8}}qN9NNOOO
8}}qGIGGHHHx{+++rJ   r   c                0    t                      }| |_        |S r]   )r   rg   )rg   value_info_protos     rH   make_empty_tensor_value_inforz    s    %'' rJ   shape!Sequence[str | int | None] | Noneshape_denotationlist[str] | Noner   c                    t                      }|j        }| |_        |j        }||j                            g            |r/t          |          t          |          k    rt          d          t          |          D ]}\  }}|j        	                                }|nMt          |t                    r||_        n0t          |t                    r||_        nt          d| d          |r||         |_        ~|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{  dimrp   r   rX   	enumerater   r   r>   	dim_valuer<   	dim_param
denotation)	r
  r{  r}  
type_prototensor_type_prototensor_shape_protorH  dr  s	            rH   make_tensor_type_protor    s)    J"."+*0 	%%b))) 	$4 5 5U C CP   e$$ 	5 	5DAq$(,,..CyAs##  !As##  ! MaMMM     5!1!!4rJ    c                    t                      }| |_        |r||_        t          |||          }|j                            |           |S )z8Makes a ValueInfoProto based on the data type and shape.)r   rg   rh   r  r   r   )rg   r
  r{  rh   r}  ry  r  s          rH   make_tensor_value_infor  ?  s[     &''  1&0#.y%AQRR""#4555rJ   c                    t                      }|j        }| |_        |j        }||j                            g            |r/t          |          t          |          k    rt          d          t          |          D ]}\  }}|j        	                                }|nMt          |t                    r||_        n0t          |t                    r||_        nt          d| d          |r||         |_        ~|S )z@Makes a SparseTensor TypeProto based on the data type and shape.Nr  r  z. Needs to be of int or text.)r   sparse_tensor_typer
  r{  r  rp   r   rX   r  r   r   r>   r  r<   r  r  )	r
  r{  r}  r  sparse_tensor_type_protosparse_tensor_shape_protorH  r  r  s	            rH   make_sparse_tensor_type_protor  Q  s)    J)<)2& 8 > 	"%,,R000 	$4 5 5U C CP   e$$ 	5 	5DAq+/3355CyAs##  !As##  ! NaNNN     5!1!!4rJ   c                    t                      }| |_        |r||_        t          |||          }|j        j                            |j                   |S )zEMakes a SparseTensor ValueInfoProto based on the data type and shape.)r   rg   rh   r  r   r  r   )rg   r
  r{  rh   r}  ry  r  s          rH   make_sparse_tensor_value_infor  ~  sp     &''  1&0#<5*    ,55 3   rJ   inner_type_protoc                `    t                      }|j        j                            |            |S )zMakes a sequence TypeProto.)r   sequence_typer
  r   r  r  s     rH   make_sequence_type_protor    .     J&//0@AAArJ   c                `    t                      }|j        j                            |            |S )zMakes an optional TypeProto.)r   optional_typer
  r   r  s     rH   make_optional_type_protor    r  rJ   
value_typec                x    t                      }| |j        _        |j        j                            |           |S )zMakes a map TypeProto.)r   map_typer  r  r   )r  r  r  s      rH   make_map_type_protor    s8    
 J#+J "++J777rJ   r  c                v    t                      }| |_        |r||_        |j                            |           |S )z1Makes a ValueInfoProto with the given type_proto.)r   rg   rh   r   r   )rg   r  rh   ry  s       rH   make_value_infor    sG     &''  1&0#"":...rJ   rM  c                   t          | t                    r| }n<t          | t                    r|                     dd          }nt          |           }t	          |          dk     r|S |d d         dt	          |          dz
   dz   S )Nr7  ignore)errors@   z	...<+len=>)r   r<   r   decoder   )rM  	sanitizeds     rH   _sanitize_strr    s    !S 			Au		 HHWXH66		FF	
9~~SbS>@Y")<@@@@@rJ   elem_shape_denotationc                    t                      }| |_        |r||_        t          |||          }t	          |          }|j        j                            |j                   |S )zJMakes a Sequence[Tensors] ValueInfoProto based on the data type and shape.)r   rg   rh   r  r  r   r  r   )rg   r
  r{  rh   r  ry  r  sequence_type_protos           rH   make_tensor_sequence_value_infor    sp     &''  1&0#.y%AVWW23DEE'001D1RSSSrJ   	subgraphs"str | tuple[str, list[GraphProto]]c           
        g }|                     | j                   |                     d           d"d}d#d	}t          d
          }d$d}g }|                     d          r%|                      || j                             n|                     d          r%|                      || j                             nl|                     d          r6|                     t          t          | j                                       n!|                     d          r{t          | j
        j                  dk    r|                     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        rI|                     t)          t+          t-          t          | j                                                 n^| j        r|                     d           n@| j        r|                     d           t3          | j                  D ]?\  }	}
|	t          | j                  dz
  k    rdnd}|                     d|
 d|            @|                     d           n| j        r|                     d           t3          | j                  D ]D\  }	}|	t          | j                  dz
  k    rdnd}|                     d|j         d|            E|                     d           |                    | j                   n|                     d            |rd!                    |          |fS d!                    |          S )%N=r   rK  r8   r<   c                
    | dS )Nz.15grK   )r   s    rH   	str_floatz&printable_attribute.<locals>.str_float  s     {{rJ   rH  r>   c                     t          |           S r]   )r<   )rH  s    rH   str_intz$printable_attribute.<locals>.str_int  s    1vvrJ   _Tstr_elemCallable[[_T], str]xsSequence[_T]c                T    dd                     t          | |                    z   dz   S )N[, ])joinmap)r  r  s     rH   str_listz%printable_attribute.<locals>.str_list  s(    TYYs8R00111C77rJ   rM  rA  r   z<Tensor>z<Scalar Tensor r  rO  z<graph rQ  z<Type Proto z[<Tensor>, ...]r  r   ,r  r  z	<Unknown> )r   rK  r8   r<   )rH  r>   r8   r<   )r  r  r  r  r8   r<   )appendrg   r   HasFieldr   rH  reprr  rM  r   rA  r   r   r   r   rO  rQ  r`  r_  ra  r<   rV  r  rb  re  r  rd  rp   r  )rh  r  contentr  r  r  r  rd  r  rH  rQ  commarO  s                rH   printable_attributer    sd    GNN49NN3       
B8 8 8 8 F}}S *$yy(())))	s		 ($wwtv''''	s		 &$tM$&11223333	s		 #$tv{aNN:&&&& *$&*:;;ENNFWTVU-C-CFFFGGGG	s		 $////000df	t		 $0dg0001111	 $xx	4;778888	 $xx334444	 $s4M4< @ @AABBCCCC	 $())))		 $st/00 	8 	8EArD$4 5 5 999CCrENN6"66u667777s	 $sdk** 	7 	7DAqDK 0 01 444CC"ENN5QV55e556666sdk""""{### )xx  &((88GrJ   r  TensorShapeProto.Dimensionc                n    |                      d          }|dS t          t          | |                    S )Nrn   ?)
WhichOneofr<   r   )r  whichs     rH   printable_dimr  ,  s5    NN7##E}swsE""###rJ   rA  c           
        |                      d          dk    rt          j                            | j        j                  }| j                            d          rnt          | j        j        j	                  rK|t          dd                    t          t          | j        j        j	                            z             z  }n|dz  }|S |                      d          dS d|                      d           S )	Nrn   r  r{  r  r_   , scalarr  zUnknown type )r  r   DataTypeNamer  r
  r  r   r{  r  r<   r  r  r  rA  rM  s     rH   printable_typer  3  s    ||G--%**1=+BCC=!!'** 	 1=&*++  S]AM<O<S)T)T U UUVVVZ||G$r21<<00222rJ   r   c                \    d| j          }| j        r| dt          | j                   d}|S )N%r  r  )rg   r   r  )r   rM  s     rH   printable_value_infor  A  s>    AFAv -,,>!&)),,,HrJ   c           
     6   d| j          d}|t          j                            | j                  z  }| j        Zt          | j                  rA|t          dd                    t          t          | j                            z             z  }n|dz  }|dz  }|S )Nr  r  r  r_   r  r  )
rg   r   r  r  r   r   r   r<   r  r  r  s     rH   printable_tensor_protor  H  s    AFA		"	"1;	/	//Avqv;; 	TCHHSaf%5%5666777AAOAHAHrJ   prefixc                   g }t          | j                  rL|                    d                    d | j        D                                  |                    d           g }g }| j        D ]}|rzt          ||          }t          |d         t                    st          dt           d          |	                    |d                    |                    |d                    ~t          |          }t          |t                    st          dt           d          |                    |           d                    t          |                    }	d                    d	 | j        D                       }
| j        r$|                    | j         d
|	 d|
 d           n |                    | j         d|
 d           |r|d                    |          z   |fS |d                    |          z   S )Nr  c                    g | ]}d | S r  rK   r^   rg   s     rH   ru  z"printable_node.<locals>.<listcomp>Y  s    !E!E!E*d**!E!E!ErJ   r  r   z1printed_attr_subgraphs[1] must be an instance of r   r   zprinted must be an instance of c                    g | ]}d | S r  rK   r  s     rH   ru  z"printable_node.<locals>.<listcomp>m  s    BBBt
D

BBBrJ   r  z](rE  (r  )r   rq   r  r  rr   r  r   rV  r   rp   r<   rs   ro   rc   )ru   r  r  r  rd  printed_attrsrh  printed_attr_subgraphsprintedprinted_attributesprinted_inputss              rH   printable_noder  T  s&    G
4; tyy!E!E!E!E!EFFGGGs!FM * * 	*%8y%I%I"4Q7>> OOOO   MM03444  !7!:;;;;)$//Ggs++ J H# H H HIII  ))))6-#8#899YYBBtzBBBCCN~ <$,PP);PP~PPPQQQQ$,:::::;;; 2)))611CHHW%%%%rJ   zEDeprecated since 1.19. Consider using onnx.printer.to_text() instead.c                   g }|dz   }d| j         g}d | j        D             }t          | j                  r	|                    d           g }g }| j        D ]P}|j         |vr#|                    t          |                     .|                    t          |                     Q|rM|                    |d                    |          z              g }|D ]}	|                    |dz   |	z              |                    d           |rw|                    d           |                    |d                    |          z              g }|D ]}	|                    |dz   |	z              |                    d           t          |          t          |          k     rd | j        D             fd	| j        D             }
|                    d
           |                    |d                    |          z              g }|
D ]}	|                    |dz   |	z              |                    d           |                    d           |                    |d                    |          z              g }| j        D ]}}t          ||d          }t          |d         t                    st          dt           d          |                    |d                    |                    |d                    ~dg}t          | j                  r7|                    d                    d | j        D                                  |                    |d                    |          z              |                    |dz              |D ]'}|                    dt          |          z              (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                    h | ]	}|j         
S rK   rt  )r^   rA  s     rH   r>  z"printable_graph.<locals>.<setcomp>  s    666qAF666rJ   r  r  rE  z,optional inputs with matching initializers (c                    h | ]	}|j         
S rK   rt  )r^   rH  s     rH   r>  z"printable_graph.<locals>.<setcomp>  s    888qAF888rJ   c                @    g | ]}|j         vt          |          S rK   )rg   r  )r^   rH  graph_inputss     rH   ru  z#printable_graph.<locals>.<listcomp>  s8       6-- 'q))---rJ   zinitializers ({T)r  r   z-contents_subgraphs[1] must be an instance of r   r   r8   r  c                "    g | ]}d |j          S r  rt  )r^   outs     rH   ru  z#printable_graph.<locals>.<listcomp>  s     FFF#~38~~FFFrJ   }
)rg   r   r   ro   r  r  r  ru   r  r   rV  r   rp   rq   printable_graph)r   r  r  indentheaderinitializersin_strsin_with_init_strsinpline	init_strsrd  ru   contents_subgraphstailrO  r  s                   @rH   r  r  w  s     Gd]Fuz"F66E$5666L
5; &c"$; 	D 	DCx|++3C889999!(()=c)B)BCCCC 	5NN6CHHV$4$44555F 5 5v}t34444c 	MMHIIINN6CHHV$4$44555F) 5 5v}t34444MM#  !!C$5$55588EK888L   *  I
 MM*+++NN6CHHV$4$44555F! 5 5v}t34444MM#
MM#NN6CHHV,,,---!F
 - -+D&DIII,Q/66 	USDSSSTTT)!,---(+,,,,:D
5< IDIIFFFFFGGHHHNN6CHHTNN*+++NN6C<    2 2toa000111199WrJ   google.protobuf.message.Messagec                   t          | t          j        j        j                  s't          dt          j        j        j         d          | j        j        D ]}|j        dk    r| 	                    |j                   (|j
        |j        k    rt|j        |j        k    r(t          | |j                  D ]}t          |           p|                     |j                  r"t          t          | |j                             dS )z:Empties `doc_string` field on any nested protobuf messageszproto must be an instance of r   rh   N)r   googleprotobufmessageMessager   
DESCRIPTORfieldsrg   
ClearFieldr   TYPE_MESSAGElabelLABEL_REPEATEDr   strip_doc_stringr  )r   
descriptorr_   s      rH   r  r    s   eV_4<== 
NFO,C,KNNN
 
 	
 &- B B
?l**Z_----_
 777:#<<< 
88 ( (A$Q''''(
00 B 
!@!@AAAB BrJ   	algorithmalgorithm_bindingsAssignmentBindingTypeinitializationGraphProto | Noneinitialization_bindingsAssignmentBindingType | Noner   c                N   t                      }|j                            |            |D ],\  }}|j                                        }||_        ||_        -|r|j                            |           |r/|D ],\  }}|j                                        }||_        ||_        -|S r]   )	r   r  r   update_bindingr   rY   rn   r  initialization_binding)r  r  r  r  training_infor   r   bindings           rH   make_training_infor    s     &''M$$Y///"  1.2244 >$--n=== + 	 	DAq#:>>@@GGKGMMrJ   tensor_dtypenp.dtypec                0    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  s    rH   r   r     s     #L1::rJ   c                0    t           j        |          j        S )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   s    rH   $tensor_dtype_to_storage_tensor_dtyper#     s     #L1??rJ   c                0    t           j        |          j        S )zGet the name of given TensorProto's data_type.

    Args:
        tensor_dtype: TensorProto's data_type

    Returns:
        the name of data_type
    )r   r  rg   r   s    rH   tensor_dtype_to_stringr%    s     #L166rJ   c                   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   rL  r!  r"  DOUBLEr%  r&  r   r   r  r"  )r  storage_tensor_type_to_fields     rH   r   r     s     	KKKKKKK$  ( .< rJ   r   TensorProto.DataTypec                   d t           j                                        D             }| |v rt          j        d||                    S t          j        | 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                $    i | ]\  }}|j         |S rK   )r   )r^   r   r   s      rH   
<dictcomp>z,np_dtype_to_tensor_dtype.<locals>.<dictcomp>:  s-     ! ! !!Q
A! ! !rJ   r/  zUnable to convert type z into TensorProto element type.)r   r  rt   typingcastr   
issubdtypestr_r   r   rX   )r   _np_dtype_to_tensor_dtypes     rH   np_dtype_to_tensor_dtyper8  0  s    ! !"*":"@"@"B"B! ! ! ,,,{13LX3VWWW	}Xrw'' "!!
M(MMM  rJ   KeysView[int]c                 >    t           j                                        S )zcGet all tensor types from TensorProto.

    Returns:
        all tensor types from TensorProto
    )r   r  r  rK   rJ   rH   get_all_tensor_dtypesr;  G  s     #((***rJ   c                    i | ]\  }}||	S rK   rK   )r^   r   r   s      rH   r2  r2  P  s.     * * *1 q* * *rJ   zdict[int, str]_ATTRIBUTE_TYPE_TO_STRc                    | 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  )r9  s    rH   rg  rg  V  sC     N0779999%i00',,..q11rJ   )r7   r5   r8   r9   )F)rN   rO   rP   rQ   r8   r>   )NNNN)rc   r<   rd   re   rf   re   rg   rT   rh   rT   rS   rT   ri   rT   rj   r   r8   r   )rS   r<   rU   r>   r8   r   )r|   r}   rg   r<   rd   r~   rf   r~   r   r   rh   rT   r   r   r   r   r8   r   )NNNNN)rS   r<   r   r<   rd   re   rf   re   r|   r}   r   rO   r   r   r   r   rh   rT   ri   rT   r   r   r8   r   )r   r   rj   r   r8   r   )r   r   r   r   r8   r@   )r   r   r   r   r8   r@   )r   r   r8   r   )rg   r<   r   r>   r   r   r   r   r   rQ   r8   r   )r  r   r  r   r   r   r8   r  )rg   r<   r
  r  r  r  r8   r   )
rg   r<   r  r>   r  r  r  r   r8   r	   )rg   r<   r
  r+  rn   r,  r8   r
   )rn   r5  r8   r   )NN)
rY   r<   rn   r   rh   rT   r9  r:  r8   r   r]   )rg   r<   r9  rk  rh   rT   r8   r   )rh  r   r8   r   )ru   r   rq  r<   r8   r   )rg   r<   r8   r   )r
  r>   r{  r|  r}  r~  r8   r   )r  N)rg   r<   r
  r>   r{  r|  rh   r<   r}  r~  r8   r   )r  r   r8   r   )r  r>   r  r   r8   r   )r  )rg   r<   r  r   rh   r<   r8   r   )rM  r5  r8   r<   )rg   r<   r
  r>   r{  r|  rh   r<   r  r~  r8   r   )rh  r   r  rQ   r8   r  )r  r  r8   r<   )rA  r   r8   r<   )r   r   r8   r<   )rA  r   r8   r<   )r  F)ru   r   r  r<   r  rQ   r8   r  )r   r   r  r<   r8   r<   )r   r  r8   r@   )
r  r   r  r  r  r  r  r  r8   r   )r  r>   r8   r  )r  r>   r8   r>   )r  r>   r8   r<   )r   r  r8   r/  )r8   r9  )r9  r>   r8   r<   )m
__future__r   collections.abcrS  	functoolsr   rF  r3  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   tupler<   r>   VersionRowTyperV  r5   r  r6   __annotations__dictr9   rM   rW   rb   rv   r{   r   r   r   r   r   r   r   r   r   r  r	  r  r*  r4  r   rm   rm  rp  rw  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  
deprecatedr  r  r  r   r#  r%  	lru_cacher   r8  r;  r?  rt   r=  rg  rK   rJ   rH   <module>rQ     s   # " " " " " "            . . . . . . . . . .                            D D D D D D D D D D                           T<<<<<<<<<<SSSSSSsCc)*U3S#s3J-KK' U38_- "# "# "# " " " "H eCHos*+   & 65mDD  GL    @ !. . . . .b   . 15!26BF( ( ( ( (V   , (,8<!26! ! ! ! !H       J' ' ' '   $* * * *
/ 
/ 
/ 
/U U U U( r r r r rj   (   >   >   >    " 	[ [ [ [ [~ RV	 	 	 	 	!< !< !< !<H, , , ,    *.* * * * *b )-    * *.* * * * *b )-    ,              	A 	A 	A 	A  .2    * -2I I I I IX$ $ $ $3 3 3 3   	 	 	 	 :? &  &  &  &  &F K M M M M M`B B B B"   2	; 	; 	; 	;	@ 	@ 	@ 	@	7 	7 	7 	7 T   . T   ,+ + + +* *,2244* * *     2 2 2 2 2 2rJ   