
    
NgZ                        d dl Z d dlmZmZmZmZmZmZmZ d dl	Z
d dlmZmZmZmZmZmZ d dlmZmZ dee         dee         fdZ	 d0dee
j        e
j        e
j        f         d	eeeee         f                  de
j        fd
Zdededede
j        fdZ e
j        eddg          Z 	 	 	 d1dee
j        e
j        e
j        f         d	eeeee         f                  dedede
j        f
dZ!dededede
j        fdZ" e
j        e"ddg          Z#	 	 	 d2dee
j        e
j        e
j        f         d	eeeee         f                  dedede
j        f
dZ$dee
j        e
j        f         d	eeee         f         dede
j        fdZ%d3dede&de
j        fdZ'd0de
j        dee&         defdZ(dedee         fdZ)	 d4d ee         dee&         d!ee         defd"Z*d#edeeef         fd$Z+d0d%eeef         dee&         defd&Z,d'edee         fd(Z-	 d4d)ee         dee&         d!ee         defd*Z.deddfd+Z/	 d5d-ee         d!e
j0        d.ede
j        fd/Z1dS )6    N)AnyDictListOptionalSequenceTupleUnion)MapProtoOptionalProtoSequenceProtoTensorProtohelpersubbyte)load_external_data_for_tensoruses_external_datafareturnc                 Z      fdt          t                     dz            D             S )Nc                 Z    g | ]'}t          |d z           |d z  dz                      (S )      )complex).0ir   s     M/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/numpy_helper.py
<listcomp>z,combine_pairs_to_complex.<locals>.<listcomp>   s7    KKK!GBq1uIr!a%!)}--KKK    r   )rangelen)r   s   `r   combine_pairs_to_complexr       s/    KKKKuSWW\7J7JKKKKr   datadimsc                 2   d }|t          | j                  dk    r^ |t          j        | g                              t          j                                                t          j                  d         S  ||                     t          j                                                t          j                  S  ||                     t          j                                                |                              t          j                  S )a_  Converts ndarray of bf16 (as uint32) to f32 (as uint32).

    Args:
        data: A numpy array, empty dimensions are allowed if dims is
            None.
        dims: If specified, the function reshapes the results.

    Returns:
        A numpy array of float32 with the same dimension if dims is
        None, or reshaped to dims if specified
    c                     | dz  S )N    )xs    r   <lambda>z%bfloat16_to_float32.<locals>.<lambda>"   s
    a2g r   Nr   )	r   shapenparrayastypeint32viewfloat32reshape)r!   r"   shifts      r   bfloat16_to_float32r2      s     E|tz??a54&))00::;;@@LLQOOuT[[**++00<<<5RX&&''//55::2:FFFr   ivalfnuzc                    |st          d          | dk     s| dk    rt          |  d          |rd}| dk    rt          j        S nKd}| dk    rt          j        t          j                   S | dk    rt          j        t          j                  S | d	z  d
z	  }| dz  }| dz  }|dz  }|dk    rO|dk    rHd|z
  }|dz  dk    r|d
z  }|dz  }|dz  }|dz  dk    r|d
z  }|dz  }|dz  }||d
z  dz  z  }||dz  z  }n||dz  z  }|d|z
  z  }||dz  z  }t          j        |                              t          j                  }|S )Nzfn=False is not implemented.r      z is not a float8.            x            r            )NotImplementedError
ValueErrorr*   nanr/   uint32r.   	r3   r4   r5   exponent_biasexpomantsignresfs	            r   _float8e4m3_to_float32_scalarrN   *   s    B!"@AAAaxx4#::D333444	 	&4<<6M  3;;:rvg&&&3;;:bf%%%4KAD$;D$;D
"*Cqyy!88-'DczQ
	czQ
	D3J2%%C42:Ctrz}$$trz
	#BJ''AHr   )excludedTFc                 z    |st          d          t          | ||          }||S |                    |          S )a  Converts ndarray of float8, e4m3 (as uint32) to f32 (as uint32).

    See :ref:`onnx-detail-float8` for technical details.

    Args:
        data: A numpy array, empty dimensions are allowed if dims is None.
        dims: If specified, the function reshapes the results.
        fn: No infinite values.
        uz: No negative zero.

    Returns:
        A numpy array of float32 with the same dimension if dims is None,
        or reshaped to dims if specified.
    z4float32_to_float8e4m3 not implemented with fn=False.r4   r5   )rC   _float8e4m3_to_float32r0   r!   r"   r4   r5   rL   s        r   float8e4m3_to_float32rT   X   sS    (  
!B
 
 	
 !"
4
4
4C|
;;tr   c                    |r)|r'| dk    rt          j        t           j                  S d}n|s|s| dv rt          j        t           j                   S | dv rt          j        t           j                  S | dk    rt          j        t           j                   S | dk    rt          j        t           j                  S d}nt	          d          | dz  d	z	  }| d
z  }| dz  }|dz  }|dk    r7|dk    r0d|z
  }|d	z  dk    r|dz  }|dz  }|dz  }||dz  dz  z  }||dz  z  }n||dz  z  }|d|z
  z  }||dz  z  }t          j        |                              t           j                  }|S )Nr9   r%   >         r7   >   }   ~   r;      |      z%fn and uz must be both False or True.r   r=   r>   r   r;   r      rA   r@   )r*   r/   rE   infrC   rF   r.   rG   s	            r   _float8e5m2_to_float32_scalarr_   v   s   	 Kb K4<<:bf%%% K K?"":rvg&&&?"":bf%%%3;;:rvg&&&3;;:bf%%%!"IJJJ4KAD$;D$;D
"*Cqyy!88-'DczQ
	D3J2%%C42:Ctrz}$$trz
	#BJ''AHr   c                 X    t          | ||          }||S |                    |          S )a  Converts ndarray of float8, e5m2 (as uint32) to f32 (as uint32).

    See :ref:`onnx-detail-float8` for technical details.

    Args:
        data: A numpy array, empty dimensions are allowed if dims is None.
        dims: If specified, the function reshapes the results.
        fn: No infinite values.
        uz: No negative zero.

    Returns:
        A numpy array of float32 with the same dimension if dims is None,
        or reshaped to dims if specified
    rQ   )_float8e5m2_to_float32r0   rS   s        r   float8e5m2_to_float32rb      s4    ( !"
4
4
4C|
;;tr   signedc                    fd}t          j        |dd          } ||                                           \  }}t          j        |j        |j        z   ft           j                  }||ddd<   ||ddd<   |j        t          j        |          dz   k    r|                                dd         }|                    |          }|S )a  Converts ndarray of int4 (as packed uint8) to f32
    See :ref:`onnx-detail-int4` for technical details.

    Args:
        data: A numpy array, empty dimensions are allowed if dims is
            None.
        dims: The dimensions are used to reshape the unpacked buffer
        signed: Whether the 4 bit integer is signed or unsigned

    Returns:
        A numpy array of float32 reshaped to dims.
    c                 .    t          j        |           S N)r   unpack_single_4bitx2)r'   rc   s    r   r(   zunpack_int4.<locals>.<lambda>   s    G8FCC r   r   r   dtyper   N)r*   
frompyfuncravelemptysizer/   prodr0   )r!   r"   rc   single_funcfuncres_highres_lowrL   s     `     r   unpack_int4rt      s    " DCCCK=a++DTZZ\\**Hg
(HMGL02"*
E
E
ECC1IC1I 	BGDMMA%%%iikk#2#
++d

CJr    tensorbase_dirc                 t   |                      d          rt          d          | j        t          j        k    rt          d          | j        }t          j        |          }t          j        t          j        |                    }t          j	        |          }| j
        }| j        t          j        k    rVt          | |          }d |D             }t          j        |                              |                              |          S t#          |           rt%          | |           |                      d          rC| j        }	t(          j        dk    r:t          j        |	|                                                                          }	|t          j        k    r0t          j        |	t          j                  }
t7          |
|          S |t          j        k    r0t          j        |	t          j                  }
t=          |
|          S |t          j        k    r2t          j        |	t          j                  }
t=          |
|d	          S |t          j         k    r0t          j        |	t          j                  }
tC          |
|          S |t          j"        k    r3t          j        |	t          j                  }
tC          |
|dd
          S |t          j#        k    r2t          j        |	t          j$                  }
tK          |
|d          S |t          j&        k    r2t          j        |	t          j                  }
tK          |
|d          S t          j        |	|                              |          S |t          j'        k    rUt          j        | j(        t          j)                                      |          *                    t          j+                  S |t          j        k    r5t          j        | j(        t          j,                  }
t7          |
|          S |t          j        k    r5t          j        | j(        t          j,                  }
t=          |
|          S |t          j        k    r7t          j        | j(        t          j,                  }
t=          |
|d	          S |t          j         k    r5t          j        | j(        t          j,                  }
tC          |
|          S |t          j"        k    r8t          j        | j(        t          j,                  }
tC          |
|dd
          S |t          j#        k    r-t          j        | j(        |          }
tK          |
|d          S |t          j&        k    r-t          j        | j(        |          }
tK          |
|d          S t          | |          }
|t          j-        t          j.        fv rt_          |
          }
t          j        |
|                              |                              |          S )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.c                 8    g | ]}|                     d           S )utf-8)decode)r   ss     r   r   zto_array.<locals>.<listcomp>   s$    666Aahhw666r   raw_databigrh   T)r5   rQ   F)rc   )0HasFieldrD   	data_typer   	UNDEFINED	TypeErrorr   tensor_dtype_to_np_dtype$tensor_dtype_to_storage_tensor_dtypetensor_dtype_to_fieldr"   STRINGgetattrr*   asarrayr,   r0   r   r   r~   sys	byteorder
frombufferbyteswaptobytesBFLOAT16int16r2   FLOAT8E4M3FNint8rT   FLOAT8E4M3FNUZ
FLOAT8E5M2rb   FLOAT8E5M2FNUZUINT4uint8rt   INT4FLOAT16
int32_datauint16r.   float16r-   	COMPLEX64
COMPLEX128r    )rv   rw   tensor_dtypenp_dtypestorage_np_dtypestorage_fieldr"   utf8_stringsssr~   r!   s              r   to_arrayr      sV    y!! GEFFF;000NOOO#L.|<<H63LAA  0>>M;D;---v}5566666z"~~$$X..66t<<< &!! 8%fh777z"" $E?=E!!}XX>>>GGIIQQSSH ;///=:::D&tT222;333=999D(t444;555=999D(t====;111=999D(t444;555=999D(tFFFF;,,,=:::DtT%8888;+++=999DtT$7777}XX666>>tDDD {***Jv(	:::WT]]T"*	
 {+++z&+28<<<"4...{///z&+28<<<$T4000{111z&+28<<<$T4D9999{---z&+28<<<$T4000{111z&+28<<<$T4DTBBBB{(((z&+3CDDD4e4444{'''z&+3CDDD4d33336=))D-{/EFFF'--:d"2333::8DDLLTRRRr   arrnamec           	         t          | t          j        t          j        f          st	          dt          |                      t                      }|j                            | j	                   |r||_
        | j        t          k    rit          j        | j                  |_        |                                 }|D ]1}t          |t"                    r.|j                            |                    d                     Ft          |t          j                  rx|D ]t}t          |t"                    r.|j                            |                    d                     Et          |t*                    r|j                            |           ut          |t*                    r|j                            |           	t-          dt#          t          |                              |S 	 t          j        | j                  }n*# t.          $ r}t1          d| j                  |d}~ww xY w||_        |                                 |_        t6          j        dk    rt;          |           |S )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 r{   zMUnrecognized object in the object array, expect a string, or array of bytes: z$Numpy data type not understood yet: Nr   )
isinstancer*   ndarraygenericr   typer   r"   extendr)   r   ri   objectr   np_dtype_to_tensor_dtyper   flattenstrstring_dataappendencodebytesrC   KeyErrorRuntimeErrorr   r~   r   r   convert_endian)r   r   rv   
flat_arrayer}   ri   s          r   
from_arrayr   O  s_    cBJ
344 
LcLL
 
 	
 ]]F
Ksy!!! 
yF!:39EE [[]]
 	 	A!S!! "))!((7*;*;<<<<Arz**  5 5A!!S)) 5*11!((72C2CDDDD#Au-- 5*11!444	5
 Au%% "))!,,,,)cQLL   X/	:: X X XO#)OOPPVWWXFkkmmFO
}vMs   7H 
H8H33H8sequencec                 6   | j         }|t          j        k    rd | j        D             S |t          j        k    rd | j        D             S |t          j        k    rd | j        D             S |t          j        k    rd | j	        D             S t          d          )zConverts a sequence def to a Python list.

    Args:
        sequence: a SequenceProto object.

    Returns:
        list: the converted list.
    c                 ,    g | ]}t          |          S r&   r   r   vs     r   r   zto_list.<locals>.<listcomp>  s    <<<<<<r   c                 ,    g | ]}t          |          S r&   r   r   s     r   r   zto_list.<locals>.<listcomp>  s    CCCCCCr   c                 ,    g | ]}t          |          S r&   )to_listr   s     r   r   zto_list.<locals>.<listcomp>  s    ===q

===r   c                 ,    g | ]}t          |          S r&   )to_dictr   s     r   r   zto_list.<locals>.<listcomp>  s    888q

888r   z8The element type in the input sequence is not supported.)	elem_typer   TENSORtensor_valuesSPARSE_TENSORsparse_tensor_valuesSEQUENCEsequence_valuesMAP
map_valuesr   )r   r   s     r   r   r     s     "IM(((<<X%;<<<<M///CCX%BCCCCM***==H$<====M%%%88H$78888
N
O
OOr   lstri   c                 J    t                      }|r||_        |r|}nxt                     dk    rY d         }t          |t                    rt           j        }n;t          |t                    rt           j        }nt           j        }nt           j        }||_	        t                     dk    r*t           fd D                       st          d          |t           j        k    r. D ]*}|j                            t          |          g           +n|t           j        k    r. D ]*}|j                            t!          |          g           +nM|t           j        k    r. D ]*}|j                            t%          |          g           +nt          d          |S )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   ^   K   | ]'}t          |t          d                              V  (dS )r   N)r   r   )r   elemr   s     r   	<genexpr>zfrom_list.<locals>.<genexpr>  s7      !Q!QT*T4A<<"@"@!Q!Q!Q!Q!Q!Qr   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   allr   r   r   r   r   	from_listr   	from_dict)	r   r   ri   r   r   
first_elemrv   seqmappings	   `        r   r   r     s    H  )			SAV
j$'' 	-%)II
D)) 	-%.II%,II "(	"HC1c!Q!Q!Q!QS!Q!Q!QQQM
 
 	

 M((( 	@ 	@F")):f+=+=*>????	@	m,	,	, 	> 	>C$++Ys^^,<====	>	m'	'	' 	= 	=G&&	'(:(:';<<<<	= 5
 
 	
 Or   	map_protoc                 T   g }| j         t          j        k    rt          | j                  }nt          | j                  }t          | j                  }t          |          t          |          k    rt          d| j
        d          t          t          ||                    }|S )zConverts 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.)key_typer   r   r   string_keyskeysr   valuesr   
IndexErrorr   r   zip)r   key_list
value_list
dictionarys       r   r   r     s     H[///	-..	'')**J
8}}J''@N!
 
 	

 c(J//00Jr   dict_c                 v  	 t                      }|r||_        t          |           }t          j        |d                   t          j                  }t          j        t          j	        t          j
        t          j        t          j        t          j        t          j        t          j        g}t!          fd|D                       st#          d          t          |                                           }t          j        |d                   	t!          	fd|D                       st#          d          t'          |          }||_        |t          j        k    r|j                            |           n||v r|j                            |           |j                            |           |S )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   H   K   | ]}t          j        |          k    V  d S rf   r*   result_type)r   keyraw_key_types     r   r   zfrom_dict.<locals>.<genexpr>  sF       
 
 N3</
 
 
 
 
 
r   zfThe key type in the input dictionary is not the same for all keys and therefore is not valid as a map.c              3   H   K   | ]}t          j        |          k    V  d S rf   r   )r   valraw_value_types     r   r   zfrom_dict.<locals>.<genexpr>!  s2      GGr~c""n4GGGGGGr   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   r   r   r   r   r   r   r   r   CopyFrom)
r   r   r   r   r   valid_key_int_typesr   	value_seqr   r   s
           @@r   r   r     s    

I 	;;D>$q'**L.|<<H 		 	 
 
 
 

 
 
 	
 	
	
 @
 
 	

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

 &!!I!I;%%%$$T****	(	(	(d###i(((r   optionalc                    | j         }|t          j        k    rdS |t          j        k    rt	          | j                  S |t          j        k    rt	          | j                  S |t          j        k    rt          | j
                  S |t          j        k    rt          | j                  S |t          j        k    rt          | j                  S t#          d          )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   r   r   r   tensor_valuer   sparse_tensor_valuer   r   sequence_valuer   r   	map_valueOPTIONALto_optionaloptional_valuer   )r   r   s     r   r  r  2  s     "IM+++tM(((-...M///4555M***x.///M%%%x)***M***82333
N
O
OOr   optc                    t                      }|r||_        |rDt          t           j                                                  }||vrt          | d          |}n_t          | t                    rt           j        }n=t          | t                    rt           j	        }n| t           j
        }nt           j        }||_        | |t           j        k    r(|j                            t          |                      n|t           j	        k    r(|j                            t#          |                      nG|t           j        k    r(|j                            t'          |                      nt          d          |S )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.NzUThe element type in the input is not a tensor, sequence, or map and is not supported.)r   r   r   DataTyper   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r   )r  r   ri   r   valid_dtypesr   s         r   from_optionalr
  K  sm     H  )M299;;<<$$uNNNOOO			C		 )!%			C		 )!*			!+		!(	"H
,,,!**:c??;;;;-000#,,Ys^^<<<<-+++''	#77779   Or   c                     | j         }t          j        |          }t          j        | j        |                                                                          | _        dS )zmCall to convert endianess of raw data in tensor.

    Args:
        tensor: TensorProto to be converted.
    rh   N)r   r   r   r*   r   r~   r   r   )rv   r   r   s      r   r   r     sS     #L.|<<H
foX666??AAIIKK OOOr   r   input_shapeseedc           	         t           j                            |           |t           j        t           j        t           j        t           j        t           j        t           j        t           j	        t           j
        fv rt          t          j        |          j        t          j        t           j	                  j                  }t          t          j        |          j        t          j        t           j	                  j                  }t           j                            |||                               |          S t!          | d          )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.
    )rn   z' is not supported by create_random_int.)r*   randomr  r   r   rF   uint64r   r   r-   int64miniinfomaxrandintr,   r   )r  ri   r  endstarts        r   create_random_intr    s     INN4

	
	
	



	 	 	 "(5//%rx'9'9'=>>BHUOO'"(););)?@@y  + >>EEeLLL5IIIJJJr   rf   )NTF)NFF)ru   )NN)r   )2r   typingr   r   r   r   r   r   r	   numpyr*   onnxr
   r   r   r   r   r   onnx.external_data_helperr   r   intr   r    r   r-   r   r2   boolr/   rN   	vectorizerR   rT   r_   ra   rb   rt   r   r   r   r   r   r   r   r  r
  r   ri   r  r&   r   r   <module>r      sr   


 D D D D D D D D D D D D D D D D D D     U U U U U U U U U U U U U U U U W W W W W W W WL# L4= L L L L 15G G
"(BJ.
/G
5hsm+,
-G ZG G G G.& & &4 &BJ & & & &R &!T4L    15	 
"(BJ.
/
5hsm+,
- 	 		
 Z   <$ $ $4 $BJ $ $ $ $N &!T4L    15	 
"(BJ.
/
5hsm+,
- 	 		
 Z   4
"*$
%
Xc]"
#  Z	   BoS oS[ oSC oS oS oS oS oSd> >BJ >hsm >{ > > > >BPm PS	 P P P P, HL6 6	c6"3-67?}66 6 6 6rx DcN    47 7T#s(^ 78C= 7H 7 7 7 7tP- PHSM P P P P4 LP1 1	#1&sm1;CC=11 1 1 1h
; 
4 
 
 
 
 ;<K KsK$&HK47KZK K K K K Kr   