
    SinS                       S SK Jr  S SKrS SKJrJr  S SKrS SKrS SK	Jr
  S SKrS SKJr  \(       a  S SKJr    S       SS jjr      SS jrSS jr      SS	 jrSS
 jrSSS jjrSS jrSSS jjrSS jr S        S!S jjrS"S jrSS#S jjrS$S jr S        S%S jjr S&       S'S jjrS(S jrg))    )annotationsN)TYPE_CHECKINGAny)helper)Sequencec                @   [         R                  " U [         R                  S9nUR                  [         R                  5      nUS-	  S-  nUR                  [         R                  5      nUS:H  n[         R                  " U[         R                  S9nSXv'   U) nUS:X  a  US-  S:  R                  [         R                  5      n	US-  S:  R                  [         R                  5      n
US-  S:  R                  [         R                  5      nUS:  R                  [         R                  5      nU	S	:H  U
S	:H  US	:H  -  US	:H  -  -  n[         R                  " U5      nS	XU-  '   U(       a  US
:H  U-  U-  nSX'   X^-  nO]US:X  aB  US-  S:  nUU-  nU(       a  US
:H  U-  nSX'   X]R                  [         R                  5      -  nOUS:X  a  O[        SU 35      eUR                  [         R                  5      nXX   Xx'   UR                  [        R                  5      $ )aR  Convert float32 NumPy array to float8e8m0 representation. If the input
is not a float32 array, it will be cast to one first.

Args:
    x: Input array to convert.
    saturate: Whether to saturate at max/min float8e8m0 value.
    round_mode: "nearest", "up", or "down".

Returns:
    np.ndarray: Array of ml_dtypes.float8_e8m0fnu values.
dtype      nearesti  @ r   i    i       upiO FdownzUnsupported rounding mode: )npasarrayfloat32viewuint32astypeuint16
zeros_likeuint8
ValueError	ml_dtypesfloat8_e8m0fnu)xsaturate
round_modex_f32f_bitsexponentspecial_maskoutputnormal_maskgrslsbround_up	incrementmax_maskhas_fractions                    P/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx/numpy_helper.pyto_float8e8m0r0      s     JJq

+EZZ		"F "$H
		H
 t#L]]82884FF  -KYx1$,,RXX6x1$,,RXX6x1$,,RXX6!|##BHH-FQ162cQh?@MM(+	,-	[() D(H4{BH"#I	t	)Q.+- D(H4H!&HOOBII..	v	 6zlCDD rxx(H"/F;;y//00    c                   [         R                  " U R                  S-  /U R                  S9nU [         R                  " S5      -  nU [         R                  " S5      -  nU[         R                  " S5      -  nX2SSS2'   XBSSS2'   UR                  [         R
                  " U5      S-   :X  a  USS	 nUR                  US
S9  U$ )zConvert a packed uint4 array to unpacked uint4 array represented as uint8.

Args:
    data: A numpy array.
    dims: The dimensions are used to reshape the unpacked buffer.

Returns:
    A numpy array of int8/uint8 reshaped to dims.
   r	            r   Nr   Frefcheck)r   emptysizer
   r   prodresize)datadimsresult	array_low
array_highs        r/   _unpack_4bitrC   _   s     XXtyy1}oTZZ8Frxx~%I&J288A;J14a4L14a4L{{bggdma''
MM$M'Mr1   c                "   U R                  5       R                  [        R                  5      R	                  5       nU R
                  nUS-  S:H  nU(       a  UR                  US-   /SS9  US-  nUSSS2==   S-  ss'   USSS2   USSS2   -  $ )	z[Convert a numpy array to flatten, packed int4/uint4. Elements must be in the correct range.r3   r   Fr8   r4   Nr6   r   ravelr   r   r   copyr;   r=   )array
array_flatr;   	odd_sizeds       r/   _pack_4bitx2rK   x   s     ##BHH-224J::DqAI4!8*u5$Jqt!taddjA...r1   c                d   [         R                  " U R                  S-  /U R                  S9nU S-  USSS2'   U S-	  S-  USSS2'   U S-	  S-  USSS2'   U S-	  S-  USSS2'   UR                  [         R                  " U5      :  a  US[         R                  " U5       nUR                  US	S
9  U$ )zConvert a packed uint2 array to unpacked uint2 array represented as uint8.

Args:
    data: A numpy array.
    dims: The dimensions are used to reshape the unpacked buffer.

Returns:
    A numpy array of int8/uint8 reshaped to dims.
r6   r	      r   Nr3   r      Fr8   )r   r:   r;   r
   r<   r=   )r>   r?   r@   s      r/   _unpack_2bitrO      s     XXtyy1}oTZZ8F$;F14a4LAI%F14a4LAI%F14a4LAI%F14a4L{{RWWT]""''$-(
MM$M'Mr1   c                   U R                  5       R                  [        R                  5      R	                  5       nU R
                  nUS-  nU(       a  UR                  USU-
  -   /SS9  US-  nUSSS2==   S-  ss'   USSS2==   S-  ss'   USSS2==   S-  ss'   US	SS2   USSS2   -  USSS2   -  USSS2   -  $ )
z[Convert a numpy array to flatten, packed int2/uint2. Elements must be in the correct range.r6   Fr8   rM   r   Nr3   rN   r   rE   )rH   rI   r;   pad_lens       r/   _pack_2bitx4rR      s     ##BHH-224J::DQhG41w;/05A$Jqt!tqt!tqt!taddjA..ADqD1AAJqtRStDTTTr1   c                   U R                  S5      (       a  [        S5      eU R                  [        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                  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      $ [        R&                  R)                  U 5      (       a  [        R&                  R+                  X5        U R                  S5      (       Gav  U R,                  n
[.        R0                  S:X  a0  [        R2                  " XS9R5                  5       R7                  5       n
U[        R                  R8                  [        R                  R:                  [        R                  R<                  1;   a=  [        R2                  " U
[        R>                  S9n[A        X5      RC                  U5      $ U[        R                  RD                  [        R                  RF                  1;   a=  [        R2                  " U
[        R>                  S9n[I        X5      RC                  U5      $ [        R2                  " XS9R%                  U5      $ U[        R                  RJ                  [        R                  RL                  [        R                  RN                  [        R                  RP                  1;   a  [        RR                  " U RT                  [        RV                  S9RC                  [        RX                  5      R#                  [        RZ                  5      R%                  U5      RC                  U5      $ U[        R                  R\                  [        R                  R^                  [        R                  R`                  [        R                  Rb                  [        R                  Rd                  [        R                  Rf                  1;   a  [        RR                  " U RT                  [        RV                  S9RC                  [        RX                  5      R#                  [        R>                  5      RC                  U5      R%                  U5      $ U[        R                  R:                  [        R                  R8                  [        R                  R<                  1;   a  [        RR                  " U RT                  [        RV                  S9RC                  [        RX                  5      R#                  [        R>                  5      n[A        X5      RC                  U5      $ U[        R                  RD                  [        R                  RF                  1;   a  [        RR                  " U RT                  [        RV                  S9RC                  [        RX                  5      R#                  [        R>                  5      n[I        X5      RC                  U5      $ [        X5      nU[        R                  Rh                  [        R                  Rj                  4;   a0  [        RR                  " XS9RC                  US9R%                  U5      $ [        R                   " XS9R#                  U5      R%                  U5      $ s  snf )a  Converts a tensor def object to a numpy array.

This function uses ml_dtypes if the dtype is not a native numpy dtype.

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.z2The element type in the input tensor is UNDEFINED.utf-8raw_databigr	   )6HasFieldr   	data_typeonnxTensorProto	UNDEFINED	TypeErrorr   tensor_dtype_to_np_dtype$tensor_dtype_to_storage_tensor_dtypetensor_dtype_to_fieldr?   STRINGgetattrdecoder   r   r   reshapeexternal_data_helperuses_external_dataload_external_data_for_tensorrV   sys	byteorder
frombufferbyteswaptobytesINT4UINT4
FLOAT4E2M1r   rC   r   UINT2INT2rO   BFLOAT16FLOAT16INT16UINT16rH   
int32_dataint32r   r   FLOAT8E4M3FNFLOAT8E4M3FNUZ
FLOAT8E5M2FLOAT8E5M2FNUZ
FLOAT8E8M0BOOL	COMPLEX64
COMPLEX128)tensorbase_dirtensor_dtypenp_dtypestorage_np_dtypestorage_fieldr?   utf8_stringsr)   ssrV   r>   s               r/   to_arrayr      s#    y!!EFF4++555LMM##L..|<H6633LA 00>M;;D4++222v5)56Ahhw6zz"~$$X.66t<<   33F;;!!??Qz""??==E!}}X>GGIQQSH!!""''
 

 ==:D+00::""!!
 
 ==:D+00::}}X6>>tDD!!  	  HHV&&bhh7T"))_VBIIWT]T(^	
 %%''##''##  HHV&&bhh7T"))_VBHHT(^WT]	
 ##  HHV&&bhh7<<RYYGNNrxxX 	 D',,X66 
 HHV&&bhh7<<RYYGNNrxxX 	 D',,X666)D((22D4D4D4O4OPPxx5:::JRRSWXX::d3::8DLLTRRi 7s   :]c                   U R                   R                  S:X  d.  [        R                  S:X  aD  U R                   R                  S:X  a*  U R                  U R                   R	                  S5      5      n U R                  5       $ )zConverts an array into bytes in little endian byte order.

Args:
    array: a numpy array.

Returns:
    bytes: Byte representation of passed array in little endian byte order.

.. versionadded:: 1.20
>rW   =<)r
   ri   rh   r   newbyteorderrl   )rH   s    r/   tobytes_little_endianr     s^     {{#5;;#8#8C#? U[[55c:;==?r1   c          	     `   [         R                  " 5       nUR                  R                  U R                  5        U(       a  Xl        U R                  [        :X  d4  [        R                  " U R                  [        R                  5      (       a  [         R                  R                  Ul        U R                  5       nU H  n[        U[        5      (       a,  UR                   R#                  UR%                  S5      5        MD  [        U[&        5      (       a  UR                   R#                  U5        Mv  [)        S[        [+        U5      5      5      e   U$ [,        R.                  " U R                  5      nU[         R                  R0                  [         R                  R2                  [         R                  R4                  1;   a  [7        U 5      n U[         R                  R8                  [         R                  R:                  1;   a  [=        U 5      n [?        U 5      Ul         XRl        U$ )zConverts an array into a TensorProto including

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

Returns:
    TensorProto: the converted tensor def.
rU   zMUnrecognized object in the object array, expect a string, or array of bytes: )!rZ   r[   r?   extendshapenamer
   objectr   
issubdtypestr_ra   rY   flatten
isinstancestrstring_dataappendencodebytesNotImplementedErrortyper   np_dtype_to_tensor_dtyperm   rn   ro   rK   rp   rq   rR   r   rV   )rH   r   r   
flat_arrayer
   s         r/   
from_arrayr   2  s    F
KKu{{#{{fekk277 C C++22 ]]_
A!S!!""))!((7*;<Au%%""))!,)cQL   ++EKK8E##  U# 
 U#+E2FOMr1   c                x   U R                   nU[        R                  R                  :X  a&  U R                   Vs/ s H  n[        U5      PM     sn$ U[        R                  R                  :X  a&  U R                   Vs/ s H  n[        U5      PM     sn$ U[        R                  R                  :X  a&  U R                   Vs/ s H  n[        U5      PM     sn$ U[        R                  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_typerZ   SequenceProtoTENSORtensor_valuesr   SPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesto_listMAP
map_valuesto_dictr]   )sequencer   vs      r/   r   r   m  s     ""ID&&---%-%;%;<%;%;<<D&&444%-%B%BC%B%BCCD&&///$,$<$<=$<q
$<==D&&***$,$7$78$7q
$788
N
OO =C=8s   D(=D-D2D7c                4  ^  [         R                  " 5       nU(       a  Xl        U(       a  UnO[        T 5      S:  a  T S   n[	        U[
        5      (       a  [         R                  R                  nOe[	        U[        5      (       a  [         R                  R                  nO5[         R                  R                  nO[         R                  R                  nXCl
        [        T 5      S:  a%  [        U 4S jT  5       5      (       d  [        S5      eU[         R                  R                  :X  aD  T  H<  nUR                  R                  [        [         R"                  " U5      5      /5        M>     U$ U[         R                  R                  :X  a0  T  H(  nUR$                  R                  ['        U5      /5        M*     U$ U[         R                  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elemlsts     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.)rZ   r   r   lenr   dictr   listr   r   r   allr]   r   r   r   r   r   r   	from_listr   	from_dict)	r   r   r
   r   r   
first_elemr   seqmappings	   `        r/   r   r     s    !!#H		SAV
j$''**..I
D))**33I**11I &&--	"C1c!QS!QQQM
 	

 D&&---F"")):bjj6H+I*JK  O 
d((11	1C$$++Ys^,<=  O 
d((,,	,G&&	'(:';<  O	 5
 	
r1   c                Z   / nU R                   [        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SS95      $ )z|Converts a map def to a Python dictionary.

Args:
    map_proto: a MapProto object.

Returns:
    The converted dictionary.
z2Length of keys and values for MapProto (map name: z) are not the same.F)strict)key_typerZ   r[   ra   r   string_keyskeysr   valuesr   
IndexErrorr   r   zip)	map_protokey_list
value_lists      r/   r   r     s     HT--444	--.	'))*J
8}J'@NN!
 	

 H788r1   c                Z  ^^	 [         R                  " 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                  [         R                  R                  [         R                  R                  [         R                  R                  [         R                  R                   1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                  R,                  :X  a  UR.                  R1                  U5        O XE;   a  UR2                  R1                  U5        UR&                  R5                  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     Dts#|3t   &)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.)rZ   MapProtor   r   r   r   r   r   r[   INT8rt   INT32INT64UINT8ru   UINT32UINT64r   r]   r   r   r   ra   r   r   r   CopyFrom)
dict_r   r   r   r   valid_key_int_typesr   	value_seqr   r   s
           @@r/   r   r     s    I;D>>$q'*L..|<H 		 DtDDD@
 	

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

 &!I!4##***$$T*		(d#i(r1   c                l   U R                   nU[        R                  R                  :X  a  gU[        R                  R                  :X  a  [        U R                  5      $ U[        R                  R                  :X  a  [        U R                  5      $ U[        R                  R                  :X  a  [        U R                  5      $ U[        R                  R                  :X  a  [        U R                  5      $ U[        R                  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   rZ   OptionalProtor\   r   r   tensor_valuer   sparse_tensor_valuer   r   sequence_valuer   r   	map_valueOPTIONALto_optionaloptional_valuer]   )optionalr   s     r/   r   r   	  s     ""ID&&000D&&-----..D&&4444455D&&///x..//D&&***x))**D&&///82233
N
OOr1   c                   [         R                  " 5       nU(       a  Xl        Ub=  U[         R                  R                  R	                  5       ;  a  [        U S35      eUnO[        U [        5      (       a  [         R                  R                  nOh[        U [        5      (       a  [         R                  R                  nO8U c  [         R                  R                  nO[         R                  R                  nXCl        U b  U[         R                  R                  :X  a&  UR                  R                  [!        U 5      5        U$ U[         R                  R                  :X  a&  UR"                  R                  [%        U 5      5        U$ U[         R                  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.)rZ   r   r   DataTyper   r]   r   r   r   r   r   r\   r   r   r   r   r   r   r   r   r   )optr   r
   r   r   s        r/   from_optionalr   "  s{     !!#H**33::<<ug%MNOO		C		&&**		C		&&//		&&00	&&--	"
**111!!**:c?; O $,,555##,,Ys^< O $,,000''	#7 O	 9  Or1   c           	     
   [         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)a  Create random integer array for backend/test/case/node.

Args:
    input_shape: The shape for the returned integer array.
    dtype: The NumPy data type for the returned integer array.
    seed: The seed for np.random.

Returns:
    np.ndarray: Random integer array.
)r;   z' is not supported by create_random_int.)r   randomseedr   r   r   uint64int8int16rw   int64miniinfomaxrandintr   r]   )input_shaper
   r   endstarts        r/   create_random_intr   U  s     IINN4

		
		
		



	 	 "((5/%%rxx'9'='=>BHHUO''"(();)?)?@yy  + >EEeLL
ugDE
FFr1   c                   [         R                  " U[         R                  5      (       dB  U[        R                  [        R
                  [        R                  [        R                  4;   a-  [        R                  " U5      n[         R                  " U 5      n O[        R                  " U5      n[         R                  " XR                  UR                  5      R                  U5      $ )zSaturate cast for numeric types.

This function ensures that values outside the representable range
of the target dtype are clamped to the maximum or minimum representable
value of that dtype.
)r   r   integerr   int4uint4int2uint2r   roundfinfoclipr   r   r   )r   r
   infos      r/   saturate_castr  t  s     
}}UBJJ''5	5 , u%HHQKu%771hh)0077r1   )Tr   )r   
np.ndarrayr   boolr    r   returnr  )r>   npt.NDArray[np.uint8]r?   zSequence[int]r  r  )rH   r  r  r  ) )r   onnx.TensorProtor   r   r  r  )rH   r  r  r   r   )r   
str | NonerH   r  r  r  )r   onnx.SequenceProtor  	list[Any])NN)r   r  r   r  r
   
int | Noner  r  )r   onnx.MapProtor  dict[Any, Any])r   r  r   r  r  r  )r   onnx.OptionalProtor  
Any | None)r   r  r   r  r
   r  r  r  )r   )r   z
tuple[int]r
   np.dtyper   intr  r  )r   r  r
   r  r  r  ) 
__future__r   rh   typingr   r   r   numpyr   numpy.typingnptonnx.external_data_helperrZ   r   collections.abcr   r0   rC   rK   rO   rR   r   r   r   r   r   r   r   r   r   r   r   r1   r/   <module>r$     sY   # 
 %      (
 H1H1H1 H1 	H1V
'42
/
'40UoSd(8vP, BF6	6$64>66r922jP4 CG0	0%05?00h ;<GG$,G47GG>8r1   