
    hS                        S SK r S SKJrJrJrJrJrJrJr  S SK	r
S SKJrJrJrJrJr  S SKJrJr  S\\   S\\   4S jr S*S\\
R.                  \
R0                  \
R2                  4   S	\\\\\   4      S\
R2                  4S
 jjrS\S\S\S\
R8                  4S jr\
R<                  " \SS/S9r   S+S\\
R.                  \
R0                  \
R2                  4   S	\\\\\   4      S\S\S\
R2                  4
S jjr S\S\S\S\
R8                  4S jr!\
R<                  " \!SS/S9r"   S,S\\
R.                  \
R0                  \
R2                  4   S	\\\\\   4      S\S\S\
R2                  4
S jjr#S-S\S\$S\
R2                  4S jjr%S*S\
R2                  S\\$   S\4S jjr&S\S\\   4S jr' S.S\\   S\\$   S\\   S\4S jjr(S\S\\\4   4S jr)S*S \\\4   S\\$   S\4S! jjr*S"\S\\   4S# jr+ S.S$\\   S\\$   S\\   S\4S% jjr,S\SS4S& jr- S/S'\\   S\
R\                  S(\S\
R2                  4S) jjr/g)0    N)AnyDictListOptionalSequenceTupleUnion)MapProtoOptionalProtoSequenceProtoTensorProtohelper)load_external_data_for_tensoruses_external_datafareturnc           	          [        [        U 5      S-  5       Vs/ s H  n[        XS-     XS-  S-      5      PM     sn$ s  snf )N      )rangelencomplex)r   is     K/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/numpy_helper.pycombine_pairs_to_complexr      s@    7<SW\7JK7J!GB1uIra%!)}-7JKKKs   #A datadimsc                    S nUc  [        U R                  5      S:X  aZ  U" [        R                  " U /5      R	                  [        R
                  5      5      R                  [        R                  5      S   $ U" U R	                  [        R
                  5      5      R                  [        R                  5      $ U" U R	                  [        R
                  5      5      R                  U5      R                  [        R                  5      $ )a/  Converts ndarray of bf16 (as uint32) to f32 (as uint32).

:param data: a numpy array, empty dimensions are allowed if dims is None
:param dims: if specified, the function reshapes the results
:return: a numpy array of float32 with the same dimension if dims is None,
    or reshaped to dims if specifiedc                     U S-  $ )N    )xs    r   <lambda>%bfloat16_to_float32.<locals>.<lambda>   s    a2g    r   )	r   shapenparrayastypeint32viewfloat32reshape)r   r   shifts      r   bfloat16_to_float32r/      s     E|tzz?a4&)00:;@@LQOOT[[*+00<<RXX&'//5::2::FFr%   ivalfnuzc                    U(       d  [        S5      eU S:  d  U S:  a  [        U  S35      eU(       a  SnU S:X  a  [        R                  $ OWSnU S:X  a%  [        R                  " [        R                  * 5      $ U S:X  a$  [        R                  " [        R                  5      $ U S	-  S
-	  nU S-  nU S-  nUS-  nUS:X  aM  US:  aF  SU-
  nUS-  S:X  a  US
-  nUS-  nUS-  nUS-  S:X  a  US
-  nUS-  nUS-  nXuS
-  S-  -  nXtS-  -  nOXuS-  -  nUSU-
  -  nXtS-  -  n[        R
                  " U5      R                  [        R                  5      nU$ )Nzfn=False is not implemented.r      z is not a float8.            x            r            )NotImplementedError
ValueErrorr'   nanr,   uint32r+   	r0   r1   r2   exponent_biasexpomantsignresfs	            r   _float8e4m3_to_float32_scalarrK   %   s~   !"@AAax4#:D6!2344	4<66M  3;::rvvg&&3;::bff%%4KAD$;D$;D
"*Cqy!8-'DczQ
	czQ
	3J2%%C2:Crz}$$rz
		#BJJ'AHr%   )excludedc                 f    U(       d  [        S5      e[        XUS9nUc  U$ UR                  U5      $ )a  Converts ndarray of float8, e4m3 (as uint32) to f32 (as uint32).

:param data: a numpy array, empty dimensions are allowed if dims is None
:param dims: if specified, the function reshapes the results
:param fn: no infinite values
:param uz: no negative zero
:return: a numpy array of float32 with the same dimension if dims is None,
    or reshaped to dims if specified.

See :ref:`onnx-detail-float8` for technical details.
z4float32_to_float8e4m3 not implemented with fn=False.r1   r2   )r@   _float8e4m3_to_float32r-   r   r   r1   r2   rI   s        r   float8e4m3_to_float32rQ   S   s>    " !B
 	
 !
4C|
;;tr%   c                 >   U(       a4  U(       a-  U S:X  a$  [         R                  " [         R                  5      $ SnOU(       d  U(       d  U S;   a%  [         R                  " [         R                  * 5      $ U S;   a$  [         R                  " [         R                  5      $ U S:X  a%  [         R                  " [         R                  * 5      $ U S:X  a$  [         R                  " [         R                  5      $ SnO[	        S5      eU S-  S	-	  nU S
-  nU S-  nUS-  nUS:X  a5  US:  a.  SU-
  nUS	-  S:X  a  US-  nUS-  nUS-  nXuS-  S-  -  nXtS-  -  nOXuS-  -  nUSU-
  -  nXtS-  -  n[         R
                  " U5      R                  [         R                  5      nU$ )Nr6   r    >         r4   >   }   ~   r8      |      z%fn and uz must be both False or True.r   r:   r;   r   r8   r      r>   r=   )r'   r,   rB   infr@   rC   r+   rD   s	            r   _float8e5m2_to_float32_scalarr\   n   sw   	b4<::bff%%?"::rvvg&&?"::bff%%3;::rvvg&&3;::bff%%!"IJJ4KAD$;D$;D
"*Cqy!8-'DczQ
	3J2%%C2:Crz}$$rz
		#BJJ'AHr%   c                 B    [        XUS9nUc  U$ UR                  U5      $ )aq  Converts ndarray of float8, e5m2 (as uint32) to f32 (as uint32).

:param data: a numpy array, empty dimensions are allowed if dims is None
:param dims: if specified, the function reshapes the results
:param fn: no infinite values
:param uz: no negative zero
:return: a numpy array of float32 with the same dimension if dims is None,
    or reshaped to dims if specifiedrN   )_float8e5m2_to_float32r-   rP   s        r   float8e5m2_to_float32r_      s)     !
4C|
;;tr%   tensorbase_dirc                    U R                  S5      (       a  [        S5      eU R                  [        R                  :X  a  [        S5      eU R                  n[        R                  " U5      n[        R                  " [        R                  " U5      5      n[        R                  " U5      nU R                  nU R                  [        R                  :X  a_  [        X5      nU Vs/ s H  oR                  S5      PM     n	n[        R                  " U	5      R!                  U5      R#                  U5      $ [%        U 5      (       a  ['        X5        U R                  S5      (       Ga  [(        R*                  S:X  a  [-        U 5        U[        R.                  :X  a8  [        R0                  " U R2                  [        R4                  S9n
[7        X5      $ U[        R8                  :X  a8  [        R0                  " U R2                  [        R:                  S9n
[=        X5      $ U[        R>                  :X  a7  [        R0                  " U R2                  [        R:                  S9n
[=        XSS	9$ U[        R@                  :X  a8  [        R0                  " U R2                  [        R:                  S9n
[C        X5      $ U[        RD                  :X  a8  [        R0                  " U R2                  [        R:                  S9n
[C        XSSS
9$ [        R0                  " U R2                  US9R#                  U5      $ U[        RF                  :X  aY  [        R                  " U RH                  [        RJ                  S9R#                  U5      RM                  [        RN                  5      $ U[        R.                  :X  a8  [        R                  " U RH                  [        RP                  S9n
[7        X5      $ U[        R8                  :X  a8  [        R                  " U RH                  [        RP                  S9n
[=        X5      $ U[        R>                  :X  a7  [        R                  " U RH                  [        RP                  S9n
[=        XSS	9$ U[        R@                  :X  a8  [        R                  " U RH                  [        RP                  S9n
[C        X5      $ U[        RD                  :X  a8  [        R                  " U RH                  [        RP                  S9n
[C        XSSS
9$ [        X5      n
U[        RR                  [        RT                  4;   a  [W        U
5      n
[        R                  " XS9R!                  U5      R#                  U5      $ s  snf )zConverts a tensor def object to a numpy array.

Args:
    tensor: a TensorProto object.
    base_dir: if external tensor exists, base_dir can help to find the path to it

Returns:
    arr: the converted array.
segmentz*Currently not supporting loading segments.z4The element type in the input tensor is not defined.utf-8raw_databigdtypeT)r2   rN   ),HasFieldrA   	data_typer   	UNDEFINED	TypeErrorr   tensor_dtype_to_np_dtype$tensor_dtype_to_storage_tensor_dtypetensor_dtype_to_fieldr   STRINGgetattrdecoder'   asarrayr)   r-   r   r   sys	byteorderconvert_endianBFLOAT16
frombufferre   int16r/   FLOAT8E4M3FNint8rQ   FLOAT8E4M3FNUZ
FLOAT8E5M2r_   FLOAT8E5M2FNUZFLOAT16
int32_datauint16r+   float16r*   	COMPLEX64
COMPLEX128r   )r`   ra   tensor_dtypenp_dtypestorage_np_dtypestorage_fieldr   utf8_stringssssr   s              r   to_arrayr      s    y!!EFF;000NOO##L..|<H6633LA 00>M;;D;---v5)56Ahhw6zz"~$$X.66t<< &!!%f7z""==E!6" ;///==AD&t22;333==@D(44;555==@D(==;111==@D(44;555==@D(FF}}V__H=EEdKK {***JJv((		:WT]T"**	
 {+++zz&++288<"4..{///zz&++288<$T00{111zz&++288<$TD99{---zz&++288<$T00{111zz&++288<$TDTBB6)D--{/E/EFF'-::d3::8DLLTRRK 7s   &Varrnamec           	         [        U [        R                  [        R                  45      (       d  [	        S[        U 5       35      e[        5       nUR                  R                  U R                  5        U(       a  Xl
        U R                  [        :X  Gan  [        R                  " U R                  5      Ul        U R!                  5       nU GH0  n[        U["        5      (       a,  UR$                  R'                  UR)                  S5      5        ME  [        U[        R                  5      (       a~  U Hv  n[        U["        5      (       a,  UR$                  R'                  UR)                  S5      5        MD  [        U[*        5      (       d  M[  UR$                  R'                  U5        Mx     M  [        U[*        5      (       a  UR$                  R'                  U5        GM  [-        S[#        [        U5      5      5      e   U$  [        R                  " U R                  5      nXbl        U R3                  5       Ul        [6        R8                  S:X  a  [;        U5        U$ ! [.         a  n[1        SU R                  < 35      UeSnAff = f)zConverts a numpy array to a tensor def.

Args:
    arr: a numpy array.
    name: (optional) the name of the tensor.

Returns:
    TensorProto: the converted tensor def.
z2arr must be of type np.generic or np.ndarray, got rd   zMUnrecognized object in the object array, expect a string, or array of bytes: z$Numpy data type not understood yet: Nrf   )
isinstancer'   ndarraygenericrl   typer   r   extendr&   r   rh   objectr   np_dtype_to_tensor_dtyperj   flattenstrstring_dataappendencodebytesr@   KeyErrorRuntimeErrortobytesre   rt   ru   rv   )r   r   r`   
flat_arrayer   rh   s          r   
from_arrayr     s    cBJJ

344@cL
 	
 ]F
KKsyy!
yyF!::399E [[]
A!S!!""))!((7*;<Arzz**A!!S))**11!((72CD#Au--**11!4	 
 Au%%""))!,)cQL    X//		: kkmFO
}}vM  XA#))OPVWWXs    I# #
J-JJsequencec                 (   U R                   nU[        R                  :X  a&  U R                   Vs/ s H  n[	        U5      PM     sn$ U[        R
                  :X  a&  U R                   Vs/ s H  n[	        U5      PM     sn$ U[        R                  :X  a&  U R                   Vs/ s H  n[        U5      PM     sn$ U[        R                  :X  a&  U R                   Vs/ s H  n[        U5      PM     sn$ [        S5      es  snf s  snf s  snf s  snf )zConverts a sequence def to a Python list.

Args:
    sequence: a SequenceProto object.

Returns:
    list: the converted list.
z8The element type in the input sequence is not supported.)	elem_typer   TENSORtensor_valuesr   SPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesto_listMAP
map_valuesto_dictrl   )r   r   vs      r   r   r   P  s     ""IM(((%-%;%;<%;%;<<M///%-%B%BC%B%BCCM***$,$<$<=$<q
$<==M%%%$,$7$78$7q
$788
N
OO =C=8s   D )D#D
Dlstrh   c                 j  ^  [        5       nU(       a  Xl        U(       a  UnO[        T 5      S:  ab  T S   n[        U[        5      (       a  [         R
                  nOG[        U[        5      (       a  [         R                  nO![         R                  nO[         R                  nXCl	        [        T 5      S:  a%  [        U 4S jT  5       5      (       d  [        S5      eU[         R                  :X  a0  T  H(  nUR                  R                  [        U5      /5        M*     U$ U[         R                  :X  a0  T  H(  nUR                  R                  [!        U5      /5        M*     U$ U[         R
                  :X  a0  T  H(  nUR"                  R                  [%        U5      /5        M*     U$ [        S5      e)a=  Converts a list into a sequence def.

Args:
    lst: a Python list
    name: (optional) the name of the sequence.
    dtype: (optional) type of element in the input list, used for specifying
                      sequence values when converting an empty list.

Returns:
    SequenceProto: the converted sequence def.
r   c              3   T   >#    U  H  n[        U[        TS    5      5      v   M     g7f)r   N)r   r   ).0elemr   s     r   	<genexpr>from_list.<locals>.<genexpr>  s#     !QST*T4A<"@"@Ss   %(zqThe element type in the input list is not the same for all elements and therefore is not supported as a sequence.zZThe element type in the input list is not a tensor, sequence, or map and is not supported.)r   r   r   r   dictr   listr   r   r   allrl   r   r   r   r   	from_listr   	from_dict)	r   r   rh   r   r   
first_elemr`   seqmappings	   `        r   r   r   e  sv    H		SAV
j$''%))I
D))%..I%,,I "((	"C1c!QS!QQQM
 	

 M(((F"")):f+=*>?  O 
m,,	,C$$++Ys^,<=  O 
m''	'G&&	'(:';<  O	 5
 	
r%   	map_protoc                 L   / nU R                   [        R                  :X  a  [        U R                  5      nO[        U R
                  5      n[        U R                  5      n[        U5      [        U5      :w  a  [        SU R                  S5      e[        [        X5      5      nU$ )z|Converts a map def to a Python dictionary.

Args:
    map: a MapProto object.

Returns:
    dict: the converted dictionary.
z2Length of keys and values for MapProto (map name: z) are not the same.)key_typer   rp   r   string_keyskeysr   valuesr   
IndexErrorr   r   zip)r   key_list
value_list
dictionarys       r   r   r     s     H[///	--.	'))*J
8}J'@NN!
 	

 c(/0Jr%   dict_c                   ^^	 [        5       nU(       a  Xl        [        U 5      n[        R                  " US   5      m[
        R                  " T5      n[        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  /n[!        U4S jU 5       5      (       d  [#        S5      e[        U R%                  5       5      n[        R                  " US   5      m	[!        U	4S jU 5       5      (       d  [#        S5      e['        U5      nXBl        U[        R*                  :X  a  UR,                  R/                  U5        O XE;   a  UR0                  R/                  U5        UR$                  R3                  U5        U$ )zConverts a Python dictionary into a map def.

Args:
    dict: Python dictionary
    name: (optional) the name of the map.

Returns:
    MapProto: the converted map def.
r   c              3   V   >#    U  H  n[         R                  " U5      T:H  v   M      g 7fNr'   result_type)r   keyraw_key_types     r   r   from_dict.<locals>.<genexpr>  s&      
 NN3</   &)zfThe key type in the input dictionary is not the same for all keys and therefore is not valid as a map.c              3   V   >#    U  H  n[         R                  " U5      T:H  v   M      g 7fr   r   )r   valraw_value_types     r   r   r     s     Gr~~c"n4r   zjThe value type in the input dictionary is not the same for all values and therefore is not valid as a map.)r
   r   r   r'   r   r   r   r   INT8INT16INT32INT64UINT8UINT16UINT32UINT64r   rl   r   r   r   rp   r   r   r   CopyFrom)
r   r   r   r   r   valid_key_int_typesr   	value_seqr   r   s
           @@r   r   r     sp    
I;D>>$q'*L..|<H 		 	 

 	
 	

 @
 	

 %,,.!F^^F1I.NGGGGB
 	

 &!I!;%%%$$T*		(d#i(r%   optionalc                    U R                   nU[        R                  :X  a  gU[        R                  :X  a  [	        U R
                  5      $ U[        R                  :X  a  [	        U R                  5      $ U[        R                  :X  a  [        U R                  5      $ U[        R                  :X  a  [        U R                  5      $ U[        R                  :X  a  [        U R                   5      $ [#        S5      e)zConverts an optional def to a Python optional.

Args:
    optional: an OptionalProto object.

Returns:
    opt: the converted optional.
Nz8The element type in the input optional is not supported.)r   r   rk   r   r   tensor_valuer   sparse_tensor_valuer   r   sequence_valuer   r   	map_valueOPTIONALto_optionaloptional_valuerl   )r   r   s     r   r   r     s     ""IM+++M(((--..M///4455M***x..//M%%%x))**M***82233
N
OOr%   optc                     [        5       nU(       a  Xl        U(       a=  [        [         R                  R	                  5       5      nX$;  a  [        U S35      eUnOp[        U [        5      (       a  [         R                  nOJ[        U [        5      (       a  [         R                  nO$U c  [         R                  nO[         R                  nXSl        U b  U[         R                  :X  a&  UR                  R                  [        U 5      5        U$ U[         R                  :X  a&  UR                   R                  [#        U 5      5        U$ U[         R                  :X  a&  UR$                  R                  ['        U 5      5        U$ [        S5      eU$ )a  Converts an optional value into a Optional def.

Args:
    opt: a Python optional
    name: (optional) the name of the optional.
    dtype: (optional) type of element in the input, used for specifying
                      optional values when converting empty none. dtype must
                      be a valid OptionalProto.DataType value

Returns:
    optional: the converted optional def.
z( must be a valid OptionalProto.DataType.zUThe element type in the input is not a tensor, sequence, or map and is not supported.)r   r   r   DataTyper   rl   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   )r   r   rh   r   valid_dtypesr   s         r   from_optionalr     sI     HM2299;<$ug%MNOO		C		!%%		C		!**		!++	!((	"
,,,!!**:c?; O -000##,,Ys^< O -+++''	#7 O	 9  Or%   c                     U R                   n[        R                  " U5      n[        R                  " U R
                  US9R                  5       R                  5       U l        g)zu
Call to convert endianess of raw data in tensor.

Arguments:
    tensor (TensorProto): TensorProto to be converted.
rg   N)rj   r   rm   r'   rx   re   byteswapr   )r`   r   r   s      r   rv   rv   ?  sJ     ##L..|<H
fooX6??AIIK Or%   input_shapeseedc           	      
   [         R                  R                  U5        U[         R                  [         R                  [         R
                  [         R                  [         R                  [         R                  [         R                  [         R                  4;   a  [        [         R                  " U5      R                  [         R                  " [         R                  5      R                  5      n[        [         R                  " U5      R                  [         R                  " [         R                  5      R                  5      n[         R                  R                  XCU S9R                  U5      $ [!        U S35      e)a3  
Create random integer array for backend/test/case/node.

Args:
    input_shape: specify the shape for the returned integer array.
    dtype: specify the NumPy data type for the returned integer array.
    seed: (optional) the seed for np.random.

Returns:
    np.ndarray: the created random integer array.
)sizez' is not supported by create_random_int.)r'   randomr   uint8r   rC   uint64r{   ry   r*   int64miniinfomaxrandintr)   rl   )r   rh   r   endstarts        r   create_random_intr  M  s     IINN4

		
		
		



	 	 "((5/%%rxx'9'='=>BHHUO''"(();)?)?@yy  + >EEeLL5'!HIJJr%   r   )NTF)NFF) )NN)r   )0rt   typingr   r   r   r   r   r   r	   numpyr'   onnxr
   r   r   r   r   onnx.external_data_helperr   r   intr   r   ry   r*   r   r/   boolr,   rK   	vectorizerO   rQ   r\   r^   r_   r   r   r   r   r   r   r   r   r   rv   rh   r  r!   r%   r   <module>r
     sn    D D D  L L WL# L4= L 15G
"((BJJ.
/G
5hsm+,
-G ZZG$& & &4 &BJJ &R !T4L  15	
"((BJJ.
/
5hsm+,
- 	 		
 ZZ6$ $ $4 $BJJ $N !T4L  15	
"((BJJ.
/
5hsm+,
- 	 		
 ZZ(^S[ ^SC ^S ^SB>BJJ >hsm >{ >BPm PS	 P, HL6	c6"3-67?}66rx DcN 47T#s(^ 78C= 7H 7tP- PHSM P4 LP1	#1&sm1;CC=11h; 4  ;<KsK$&HHK47KZZKr%   