
    /;ji                    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   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   r7   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   rB   zai.onnx.preview.trainingr   )zip)r:   r<   r>   pairresults       E/root/voice-cloning/.venv/lib/python3.11/site-packages/onnx/helper.pyprocessz._create_op_set_id_version_map.<locals>.processX   sz    9994
 
 
 	M 	MD 6!!)t7000BLF5tAw>?	M 	M    )r:   r;   r<   r=   r>   r   r7   r?    )r6   rH   rowrF   s      @rG   _create_op_set_id_version_maprL   T   sO    FM M M M M M   MrI   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=   r7   c                b    | pd|f}|t           v rt           |         S rS t          d          )NrA   zUnsupported opset-version.)OP_SET_ID_VERSION_MAP
ValueError)rR   rT   keydefault_min_versionrO   s      rG   find_minz)find_min_ir_version_for.<locals>.find_miny   sF    "G,'''(-- 	'&&5666rI   c              3  D   K   | ]} |j         |j                  V  d S N)rR   rT   ).0xrZ   s     rG   	<genexpr>z*find_min_ir_version_for.<locals>.<genexpr>   s3      FFQ88AHai00FFFFFFrI   )rR   rS   rT   r=   r7   r=   )max)rM   rO   rY   rZ   s    `@@rG   find_min_ir_version_forra   j   sd     7 7 7 7 7 7 7  GFFFF+FFFFFFrI   op_typer;   inputsSequence[str]outputsnamerS   
doc_stringrR   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]   rX   values      rG   r_   zmake_node.<locals>.<genexpr>   sB       
 
U  3&&    
 
rI   )r   rb   inputextendoutputrf   rg   rR   rh   	attributesorteditems)	rb   rc   re   rf   rg   rR   rh   ri   nodes	            rG   	make_noderu      s    8 ;;DDLJfKw 	 %$  
 
 
$V\\^^44
 
 
 	
 	
 	

 KrI   rT   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   rR   rT   )rR   rT   operatorsetids      rG   make_operatorsetidrz      s%     '((M!M#MrI   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   rt   ro   rf   rn   rp   r~   r   r   rg   )	r{   rf   rc   re   r~   rg   r   r   graphs	            rG   
make_graphr      s    0 !
LLE	JeEJ	Kv	L   	[)))	##$6777	J''' &%LrI   c                >    t                      }| |_        ||_        |S rw   rx   )rR   rT   opsetids      rG   make_opsetidr      s"     !""GGNGONrI   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   rR   rf   rn   ro   rp   rt   opset_importrq   attribute_protorg   rh   r   )rR   r   rc   re   r{   r   r   r   rg   rh   r   fs               rG   make_functionr     s     

AAHAFGNN6HOOGFMM%N-(((Kz"""-... "!
L
###HrI   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   ro   addr   onnx_opset_versionrT   r   rs   setattr)r   ri   modelr   impr   kvs           rG   
make_modelr   '  s     LLE E	K9?: :M  !!-0000  $$&&-//06

;0M0MIy)))  1q!LrI   c                |    d}||vr*d}|                     |g           }t          |          ||<   t          | fi |S )Nr<   r   )getra   r   )r   ri   ir_version_fieldopset_imports_fieldimportss        rG   make_model_gen_versionr   L  sV    #v%%-**0"55#:7#C#C e&&v&&&rI   protoRModelProto | GraphProto | FunctionProto | NodeProto | TensorProto | ValueInfoProto
dict_valuedict[str, str]r?   c                    | j         d d = |                                D ],\  }}| j                                         }||_        ||_        -d S r\   )metadata_propsrs   r   rX   rm   )r   r   r   r   entrys        rG   set_metadata_propsr   U  s`     	QQQ  ""  1$((**	 rI   r   c                &    t          | |           d S r\   )r   )r   r   s     rG   set_model_propsr   g  s    uj)))))rI   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       rG   _pack_4bitx2r   k  s     ##BH--2244J:DqAI 64!8*u555$Jqt!taddjA...rI   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       rG   _pack_2bitx4r   x  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TrI   	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   rf   r   ro   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lenrW   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)
rf   r   r   r   r   tensornp_dtypeexpected_size_bytesr   fields
             rG   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!!$'''MrI   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   ro   )r  r  r   sparses       rG   make_sparse_tensorr    sY     #%%F
M6"""
NG$$$
KtMrI   	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   rf   r	  	UNDEFINEDTENSORtensor_valuesSPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesMAP
map_valuesr
   OPTIONALoptional_valuesr   ro   )rf   r	  r  sequencerq   s        rG   make_sequencer    s     HHM"HM+++8<IM(((*			m1	1	11			m,	,	,,			m'	'	''			m,	,	,,		RSSSVOrI   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UINT64rf   r  r   string_keysro   r  r  r   )rf   r  r  r  	map_protovalid_key_int_typess         rG   make_mapr)  /  s     

I	 IN!I;%%%$$T****	(	(	(d###f%%%rI   OptionalProto.DataTyperm   &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
   rf   r	  r  r  tensor_valuer  sparse_tensor_valuer  sequence_valuer  	map_valuer  optional_valuer   r   )rf   r	  rm   optionalrq   s        rG   make_optionalr3  N  s     HHM"HM+++8<IM((()			m1	1	10			m,	,	,+			m'	'	'&			m,	,	,+		RSSSuOrI   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)rm   s    rG   r   r   m  s+    % %||G$$$LrI   rX   	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 rJ   )r   r]   r   s     rG   	<setcomp>z!make_attribute.<locals>.<setcomp>  s    ,,,T!WW,,,rI   c              3  8   K   | ]}t          |          V  d S r\   )
issubclass)r]   texp_ts     rG   r_   z!make_attribute.<locals>.<genexpr>  s-      ;;z!U++;;;;;;rI   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     rG   r_   z!make_attribute.<locals>.<genexpr>  s(      <<	!<<<<<<rI   'z%' is not an accepted attribute value.zInferred attribute type 'z'(z") mismatched with specified type ')):r   rf   rg   r   numbersIntegralr=   iINTr   Realfloatr   FLOATr;   r   r   sr   r   r@  r   r  r   r  sparse_tensorr  r   gGRAPHr   tp
TYPE_PROTOcollectionsabcIterablelistr   rW   INTSFLOATSSTRINGSTENSORSSPARSE_TENSORSGRAPHSTYPE_PROTOSallintsro   floatsstringstensorssparse_tensorsgraphstype_protosAssertionErrorr   _attr_type_to_str)rX   rm   rg   r8  attrtypesexp_enumrA  s          @rG   rl   rl   t  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
 
 	
 KrI   AttributeProto.AttributeTypec                P    t                      }| |_        ||_        |r||_        |S )zeMake an AttributeProto holding a reference to the parent function's attribute of given name and type.)r   rf   r   rg   )rf   r8  rg   rg  s       rG   make_attribute_refrl    s1     DDIDI %$KrI   rg  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_namerW   r   r   rK  r   rH  rG  r   rL  r  r@  r  rM  rO  rN  rQ  rP  rW  rU  r_  rV  r^  rX  r`  rY  ra  rZ  rb  r[  rc  r\  rd  r  )rg  s    rG   get_attribute_valuero    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::
;
;;rI   rt   	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 rJ   rf   )r]   r^   rp  s     rG   
<listcomp>z'get_node_attr_value.<locals>.<listcomp>  s%    AAAaQVy-@-@-@-@-@rI   r   z'Node has multiple attributes with name z Node has no attribute with name r   )rq   r   rW   ro  )rt   rp  matchings    ` rG   get_node_attr_valuerv    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{+++rI   r   c                0    t                      }| |_        |S r\   )r   rf   )rf   value_info_protos     rG   make_empty_tensor_value_infory    s    %'' rI   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	  rz  dimro   r   rW   	enumerater   r   r=   	dim_valuer;   	dim_param
denotation)	r	  rz  r|  
type_prototensor_type_prototensor_shape_protorG  dr  s	            rG   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rI    c                    t                      }| |_        |r||_        t          |||          }|j                            |           |S )z8Makes a ValueInfoProto based on the data type and shape.)r   rf   rg   r  r   r   )rf   r	  rz  rg   r|  rx  r  s          rG   make_tensor_value_infor  >  s[     &''  1&0#.y%AQRR""#4555rI   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	  rz  r  ro   r   rW   r  r   r   r=   r  r;   r  r  )	r	  rz  r|  r  sparse_tensor_type_protosparse_tensor_shape_protorG  r  r  s	            rG   make_sparse_tensor_type_protor  P  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rI   c                    t                      }| |_        |r||_        t          |||          }|j        j                            |j                   |S )zEMakes a SparseTensor ValueInfoProto based on the data type and shape.)r   rf   rg   r  r   r  r   )rf   r	  rz  rg   r|  rx  r  s          rG   make_sparse_tensor_value_infor  }  sp     &''  1&0#<5*    ,55 3   rI   inner_type_protoc                `    t                      }|j        j                            |            |S )zMakes a sequence TypeProto.)r   sequence_typer	  r   r  r  s     rG   make_sequence_type_protor    .     J&//0@AAArI   c                `    t                      }|j        j                            |            |S )zMakes an optional TypeProto.)r   optional_typer	  r   r  s     rG   make_optional_type_protor    r  rI   
value_typec                x    t                      }| |j        _        |j        j                            |           |S )zMakes a map TypeProto.)r   map_typer  r  r   )r  r  r  s      rG   make_map_type_protor    s8    
 J#+J "++J777rI   r  c                v    t                      }| |_        |r||_        |j                            |           |S )z1Makes a ValueInfoProto with the given type_proto.)r   rf   rg   r   r   )rf   r  rg   rx  s       rG   make_value_infor    sG     &''  1&0#"":...rI   rL  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 )Nr6  ignore)errors@   z	...<+len=>)r   r;   r   decoder   )rL  	sanitizeds     rG   _sanitize_strr    s    !S 			Au		 HHWXH66		FF	
9~~SbS>@Y")<@@@@@rI   elem_shape_denotationc                    t                      }| |_        |r||_        t          |||          }t	          |          }|j        j                            |j                   |S )zJMakes a Sequence[Tensors] ValueInfoProto based on the data type and shape.)r   rf   rg   r  r  r   r  r   )rf   r	  rz  rg   r  rx  r  sequence_type_protos           rG   make_tensor_sequence_value_infor    sp     &''  1&0#.y%AVWW23DEE'001D1RSSSrI   	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   rJ  r7   r;   c                
    | dS )Nz.15grJ   )r   s    rG   	str_floatz&printable_attribute.<locals>.str_float  s     {{rI   rG  r=   c                     t          |           S r\   )r;   )rG  s    rG   str_intz$printable_attribute.<locals>.str_int  s    1vvrI   _Tstr_elemCallable[[_T], str]xsSequence[_T]c                T    dd                     t          | |                    z   dz   S )N[, ])joinmap)r  r  s     rG   str_listz%printable_attribute.<locals>.str_list  s(    TYYs8R00111C77rI   rL  r@  r   z<Tensor>z<Scalar Tensor r  rN  z<graph rP  z<Type Proto z[<Tensor>, ...]r  r   ,r  r  z	<Unknown> )r   rJ  r7   r;   )rG  r=   r7   r;   )r  r  r  r  r7   r;   )appendrf   r   HasFieldr   rG  reprr  rL  r   r@  r   r   r   r   rN  rP  r_  r^  r`  r;   rU  r  ra  rd  r  rc  ro   r  )rg  r  contentr  r  r  r  rc  r   rG  rP  commarN  s                rG   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rI   r  TensorShapeProto.Dimensionc                n    |                      d          }|dS t          t          | |                    S )Nrm   ?)
WhichOneofr;   r   )r  whichs     rG   printable_dimr  +  s5    NN7##E}swsE""###rI   r@  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 )	Nrm   r  rz  r  r^   , scalarr  zUnknown type )r  r   DataTypeNamer  r	  r  r   rz  r  r;   r  r  r  r@  rL  s     rG   printable_typer  2  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2rI   r   c                \    d| j          }| j        r| dt          | j                   d}|S )N%r  r  )rf   r   r  )r   rL  s     rG   printable_value_infor  @  s>    AFAv -,,>!&)),,,HrI   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  )
rf   r   r  r  r   r   r   r;   r  r  r  s     rG   printable_tensor_protor  G  s    AFA		"	"1;	/	//Avqv;; 	TCHHSaf%5%5666777AAOAHAHrI   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  rJ   r]   rf   s     rG   rt  z"printable_node.<locals>.<listcomp>X  s    !E!E!E*d**!E!E!ErI   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  rJ   r  s     rG   rt  z"printable_node.<locals>.<listcomp>l  s    BBBt
D

BBBrI   r  z](rD  (r  )r   rp   r  r  rq   r  r   rU  r   ro   r;   rr   rn   rb   )rt   r  r  r  rc  printed_attrsrg  printed_attr_subgraphsprintedprinted_attributesprinted_inputss              rG   printable_noder  S  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%%%%rI   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 rJ   rs  )r]   r@  s     rG   r=  z"printable_graph.<locals>.<setcomp>  s    666qAF666rI   r  r  rD  z,optional inputs with matching initializers (c                    h | ]	}|j         
S rJ   rs  )r]   rG  s     rG   r=  z"printable_graph.<locals>.<setcomp>  s    888qAF888rI   c                @    g | ]}|j         vt          |          S rJ   )rf   r  )r]   rG  graph_inputss     rG   rt  z#printable_graph.<locals>.<listcomp>  s8       6-- 'q))---rI   zinitializers ({T)r  r   z-contents_subgraphs[1] must be an instance of r   r   r7   r  c                "    g | ]}d |j          S r  rs  )r]   outs     rG   rt  z#printable_graph.<locals>.<listcomp>  s     FFF#~38~~FFFrI   }
)rf   r~   r   rn   r  r  r  rt   r  r   rU  r   ro   rp   printable_graph)r   r  r  indentheaderinitializersin_strsin_with_init_strsinpline	init_strsrc  rt   contents_subgraphstailrN  r  s                   @rG   r  r  v  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rI   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   rg   N)r   googleprotobufmessageMessager   
DESCRIPTORfieldsrf   
ClearFieldr   TYPE_MESSAGElabelLABEL_REPEATEDr   strip_doc_stringr  )r   
descriptorr^   s      rG   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rI   	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   rX   rm   r  initialization_binding)r  r  r  r  training_infor   r   bindings           rG   make_training_infor    s     &''M$$Y///"  1.2244 >$--n=== + 	 	DAq#:>>@@GGKGMMrI   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    rG   r   r     s     #L1::rI   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    rG   $tensor_dtype_to_storage_tensor_dtyper"    s     #L1??rI   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  rf   r  s    rG   tensor_dtype_to_stringr$    s     #L166rI   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   rK  r   r!  DOUBLEr$  r%  r   r   r  r!  )r  storage_tensor_type_to_fields     rG   r   r     s     	KKKKKKK$  ( .< rI   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 rJ   )r   )r]   r   r   s      rG   
<dictcomp>z,np_dtype_to_tensor_dtype.<locals>.<dictcomp>9  s-     ! ! !!Q
A! ! !rI   r.  zUnable to convert type z into TensorProto element type.)r   r  rs   typingcastr   
issubdtypestr_r   r   rW   )r   _np_dtype_to_tensor_dtypes     rG   np_dtype_to_tensor_dtyper7  /  s    ! !"*":"@"@"B"B! ! ! ,,,{13LX3VWWW	}Xrw'' "!!
M(MMM  rI   KeysView[int]c                 >    t           j                                        S )zcGet all tensor types from TensorProto.

    Returns:
        all tensor types from TensorProto
    )r   r  r  rJ   rI   rG   get_all_tensor_dtypesr:  F  s     #((***rI   c                    i | ]\  }}||	S rJ   rJ   )r]   r   r   s      rG   r1  r1  O  s.     * * *1 q* * *rI   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  )r8  s    rG   rf  rf  U  sC     N0779999%i00',,..q11rI   )r6   r4   r7   r8   )F)rM   rN   rO   rP   r7   r=   )NNNN)rb   r;   rc   rd   re   rd   rf   rS   rg   rS   rR   rS   rh   rS   ri   r   r7   r   )rR   r;   rT   r=   r7   r   )r{   r|   rf   r;   rc   r}   re   r}   r~   r   rg   rS   r   r   r   r   r7   r   )NNNNN)rR   r;   r   r;   rc   rd   re   rd   r{   r|   r   rN   r   r   r   r   rg   rS   rh   rS   r   r   r7   r   )r   r   ri   r   r7   r   )r   r   r   r   r7   r?   )r   r   r   r   r7   r?   )r   r   r7   r   )rf   r;   r   r=   r   r   r   r   r   rP   r7   r   )r  r   r  r   r   r   r7   r  )rf   r;   r	  r
  r  r  r7   r   )
rf   r;   r  r=   r  r  r  r   r7   r	   )rf   r;   r	  r*  rm   r+  r7   r
   )rm   r4  r7   r   )NN)
rX   r;   rm   r   rg   rS   r8  r9  r7   r   r\   )rf   r;   r8  rj  rg   rS   r7   r   )rg  r   r7   r   )rt   r   rp  r;   r7   r   )rf   r;   r7   r   )r	  r=   rz  r{  r|  r}  r7   r   )r  N)rf   r;   r	  r=   rz  r{  rg   r;   r|  r}  r7   r   )r  r   r7   r   )r  r=   r  r   r7   r   )r  )rf   r;   r  r   rg   r;   r7   r   )rL  r4  r7   r;   )rf   r;   r	  r=   rz  r{  rg   r;   r  r}  r7   r   )rg  r   r  rP   r7   r  )r  r  r7   r;   )r@  r   r7   r;   )r   r   r7   r;   )r@  r   r7   r;   )r  F)rt   r   r  r;   r  rP   r7   r  )r   r   r  r;   r7   r;   )r   r   r7   r?   )
r  r   r  r  r  r  r  r  r7   r   )r  r=   r7   r  )r  r=   r7   r=   )r  r=   r7   r;   )r   r  r7   r.  )r7   r8  )r8  r=   r7   r;   )m
__future__r   collections.abcrR  	functoolsr   rE  r2  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=   VersionRowTyperU  r4   r  r5   __annotations__dictr8   rL   rV   ra   ru   rz   r   r   r   r   r   r   r   r   r   r  r  r  r)  r3  r   rl   rl  ro  rv  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  
deprecatedr  r  r  r   r"  r$  	lru_cacher   r7  r:  r>  rs   r<  rf  rJ   rI   rG   <module>rP     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_- !# !# !# ! ! ! !F 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 2rI   