o
    Vôj¥Z  ã                   @   s  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d„Z	dAd	ee
je
je
jf d
eeeee f  de
jfdd„Zdededede
jfdd„Ze
jeddgd�Z 			dBd	ee
je
je
jf d
eeeee f  dedede
jf
dd„Z!dededede
jfdd„Z"e
je"ddgd�Z#			dCd	ee
je
je
jf d
eeeee f  dedede
jf
dd„Z$d	ee
je
jf d
eeee f dede
jfdd„Z%dDded e&de
jfd!d"„Z'dAd#e
jd$ee& defd%d&„Z(d'edee fd(d)„Z)	dEd*ee d$ee& d+ee defd,d-„Z*d.edeeef fd/d0„Z+dAd1eeef d$ee& defd2d3„Z,d4edee fd5d6„Z-	dEd7ee d$ee& d+ee defd8d9„Z.deddfd:d;„Z/	<dFd=ee d+e
j0d>ede
jfd?d@„Z1dS )Gé    N)ÚAnyÚDictÚListÚOptionalÚSequenceÚTupleÚUnion)ÚMapProtoÚOptionalProtoÚSequenceProtoÚTensorProtoÚhelperÚsubbyte)Úload_external_data_for_tensorÚuses_external_dataÚfaÚreturnc                    s   ‡ fdd„t tˆ ƒd ƒD ƒS )Nc                    s*   g | ]}t ˆ |d   ˆ |d  d  ƒ‘qS )é   é   )Úcomplex)Ú.0Úi©r   © úd/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/onnx/numpy_helper.pyÚ
<listcomp>   s   * z,combine_pairs_to_complex.<locals>.<listcomp>r   )ÚrangeÚlenr   r   r   r   Úcombine_pairs_to_complex   s   r   ÚdataÚdimsc                 C   sx   dd„ }|du r-t | jƒdkr!|t | 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                 S   s   | d> S )Né   r   ©Úxr   r   r   Ú<lambda>"   s    z%bfloat16_to_float32.<locals>.<lambda>Nr   )	r   ÚshapeÚnpÚarrayÚastypeÚint32ÚviewÚfloat32Úreshape)r   r    Úshiftr   r   r   Úbfloat16_to_float32   s   $r.   ÚivalÚfnÚuzc           	      C   sL  |st dƒ‚| dk s| dkrt| › d�ƒ‚|r!d}| dkr tjS nd}| dkr.t tj ¡S | dkr8t tj¡S | d	@ d
? }| d@ }| d@ }|d> }|dkr‰|dkrˆd| }|d@ dkrh|d
M }|dK }|d8 }|d@ dkrz|d
M }|dK }|d8 }||d
@ d> O }||d> O }n||d> O }|d| 7 }||d> O }t |¡ tj¡}|S )Nzfn=False is not implemented.r   éÿ   z is not a float8.é   é€   é   é   éx   é   é   é   r   é   é   é   )ÚNotImplementedErrorÚ
ValueErrorr&   Únanr+   Úuint32r*   ©	r/   r0   r1   Zexponent_biasÚexpoÚmantÚsignÚresÚfr   r   r   Ú_float8e4m3_to_float32_scalar*   sJ   ÿ€rH   )ÚexcludedTFc                 C   s0   |st dƒ‚t| ||d�}|du r|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.©r0   r1   N)r>   Ú_float8e4m3_to_float32r,   ©r   r    r0   r1   rF   r   r   r   Úfloat8e4m3_to_float32X   s   ÿ
rM   c           	      C   sD  |r|r| dkrt  t j¡S d}n5|sB|sB| dv r t  t j ¡S | dv r*t  t j¡S | dkr5t  t j ¡S | dkr?t  t j¡S d}ntdƒ‚| d@ d	? }| d
@ }| d@ }|d> }|dkr…|dkr„d| }|d	@ dkrv|dM }|dK }|d8 }||d@ d> O }||d> O }n||d> O }|d| 7 }||d> O }t  |¡ t j¡}|S )Nr4   r!   >   éý   éþ   r2   >   é}   é~   r6   éü   é|   é   z%fn and uz must be both False or True.r   r8   r9   r   r6   r   é   r<   r;   )r&   r+   r@   Úinfr>   rA   r*   rB   r   r   r   Ú_float8e5m2_to_float32_scalarv   sD   €rW   c                 C   s$   t | ||d�}|du r|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
    rJ   N)Ú_float8e5m2_to_float32r,   rL   r   r   r   Úfloat8e5m2_to_float32¢   s   
rY   Úsignedc                    s’   ‡ fdd„}t  |dd¡}||  ¡ ƒ\}}t j|j|j ft jd�}||ddd…< ||ddd…< |jt  |¡d krB| ¡ 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                    s   t  | ˆ ¡S ©N)r   Úunpack_single_4bitx2r"   ©rZ   r   r   r$   Í   s    zunpack_int4.<locals>.<lambda>r   r   ©Údtyper   Néÿÿÿÿ)r&   Ú
frompyfuncÚravelÚemptyÚsizer+   Úprodr,   )r   r    rZ   Úsingle_funcÚfuncZres_highZres_lowrF   r   r]   r   Úunpack_int4¼   s   
rh   Ú ÚtensorÚbase_dirc                 C   s„  |   d¡r	tdƒ‚| jtjkrtdƒ‚| j}t |¡}t t |¡¡}t 	|¡}| j
}| jtjkrHt| |ƒ}dd„ |D ƒ}t |¡ |¡ |¡S t| ƒrQt| |ƒ |   d¡rú| j}	tjdkritj|	|d� ¡  ¡ }	|tjkr{tj|	tjd�}
t|
|ƒS |tjkr�tj|	tjd�}
t|
|ƒS |tjkr¡tj|	tjd�}
t|
|d	d
�S |tj kr³tj|	tjd�}
t!|
|ƒS |tj"krÈtj|	tjd�}
t!|
|d	d	d�S |tj#krÜtj|	tj$d�}
t%|
|dd�S |tj&krðtj|	tjd�}
t%|
|d	d�S tj|	|d� |¡S |tj'k�rtj| j(tj)d� |¡ *tj+¡S |tjk�r$tj| j(tj,d�}
t|
|ƒS |tjk�r8tj| j(tj,d�}
t|
|ƒS |tjk�rNtj| j(tj,d�}
t|
|d	d
�S |tj k�rbtj| j(tj,d�}
t!|
|ƒS |tj"k�rytj| j(tj,d�}
t!|
|d	d	d�S |tj#k�rŽtj| j(|d�}
t%|
|dd�S |tj&k�r£tj| j(|d�}
t%|
|d	d�S t| |ƒ}
|tj-tj.fv �rµt/|
ƒ}
tj|
|d� |¡ |¡S )zêConverts 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                 S   s   g | ]}|  d ¡‘qS )úutf-8)Údecode)r   Úsr   r   r   r   ö   s    zto_array.<locals>.<listcomp>Úraw_dataÚbigr^   T)r1   rJ   Fr]   )0ÚHasFieldr?   Ú	data_typer   Ú	UNDEFINEDÚ	TypeErrorr   Útensor_dtype_to_np_dtypeÚ$tensor_dtype_to_storage_tensor_dtypeÚtensor_dtype_to_fieldr    ÚSTRINGÚgetattrr&   Úasarrayr(   r,   r   r   rp   ÚsysÚ	byteorderÚ
frombufferÚbyteswapÚtobytesÚBFLOAT16Úint16r.   ÚFLOAT8E4M3FNÚint8rM   ÚFLOAT8E4M3FNUZÚ
FLOAT8E5M2rY   ÚFLOAT8E5M2FNUZÚUINT4Úuint8rh   ÚINT4ÚFLOAT16Ú
int32_dataÚuint16r*   Úfloat16r)   Ú	COMPLEX64Ú
COMPLEX128r   )rj   rk   Útensor_dtypeÚnp_dtypeZstorage_np_dtypeZstorage_fieldr    Zutf8_stringsÚssrp   r   r   r   r   Úto_arrayÝ   s’   


ÿ














ý



r”   ÚarrÚnamec              
   C   sd  t | tjtjfƒstdt| ƒ› �ƒ‚tƒ }|j | j	¡ |r!||_
| jtkr�t | j¡|_|  ¡ }|D ]K}t |tƒrD|j | d¡¡ q3t |tjƒrj|D ]}t |tƒr]|j | d¡¡ qLt |tƒrh|j |¡ qLq3t |tƒrv|j |¡ q3tdtt|ƒƒƒ‚|S zt | j¡}W n tyž } z	td| j›�ƒ|‚d}~ww ||_|  ¡ |_tjdkr°t|ƒ |S )zÁConverts 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 rm   zMUnrecognized object in the object array, expect a string, or array of bytes: z$Numpy data type not understood yet: Nrq   )Ú
isinstancer&   ÚndarrayÚgenericru   Útyper   r    Úextendr%   r–   r_   Úobjectr   Únp_dtype_to_tensor_dtypers   ÚflattenÚstrÚstring_dataÚappendÚencodeÚbytesr>   ÚKeyErrorÚRuntimeErrorr€   rp   r|   r}   Úconvert_endian)r•   r–   rj   Z
flat_arrayÚero   r_   r   r   r   Ú
from_arrayO  sP   
ÿ




€ü

þ€ÿ

r¨   Úsequencec                 C   sv   | j }|tjkrdd„ | jD ƒS |tjkrdd„ | jD ƒS |tjkr*dd„ | jD ƒS |tjkr7dd„ | j	D ƒS t
dƒ‚)z“Converts a sequence def to a Python list.

    Args:
        sequence: a SequenceProto object.

    Returns:
        list: the converted list.
    c                 S   ó   g | ]}t |ƒ‘qS r   ©r”   ©r   Úvr   r   r   r   ›  ó    zto_list.<locals>.<listcomp>c                 S   rª   r   r«   r¬   r   r   r   r   �  r®   c                 S   rª   r   )Úto_listr¬   r   r   r   r   Ÿ  r®   c                 S   rª   r   )Úto_dictr¬   r   r   r   r   ¡  r®   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_valuesru   )r©   r±   r   r   r   r¯   �  s   	



r¯   Úlstr_   c           	         s  t ƒ }|r||_|r|}n#tˆ ƒdkr-ˆ d }t|tƒr t j}nt|tƒr)t j}nt j}nt j}||_	tˆ ƒdkrHt
‡ fdd„ˆ D ƒƒsHtdƒ‚|t jkr]ˆ D ]}|j t|ƒg¡ qO|S |t jkrrˆ D ]}|j t|ƒg¡ qd|S |t jkr‡ˆ D ]}|j t|ƒg¡ qy|S tdƒ‚)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   s"   � | ]}t |tˆ d  ƒƒV  qdS )r   N)r—   rš   )r   Úelem©rº   r   r   Ú	<genexpr>Ç  s   €  zfrom_list.<locals>.<genexpr>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±   Úallru   r³   r›   r¨   r·   Ú	from_listr¹   Ú	from_dict)	rº   r–   r_   r©   r±   Ú
first_elemrj   ÚseqÚmappingr   r¼   r   rÁ   ¥  sB   

"ÿ

õ	
øüÿrÁ   Ú	map_protoc                 C   s`   g }| j tjkrt| jƒ}nt| jƒ}t| jƒ}t|ƒt|ƒkr't	d| j
dƒ‚tt||ƒƒ}|S )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.)Úkey_typer   ry   r¿   Ústring_keysÚkeysr¯   Úvaluesr   Ú
IndexErrorr–   r¾   Úzip)rÆ   Úkey_listÚ
value_listÚ
dictionaryr   r   r   r°   Þ  s   	

ýr°   Údict_c                    sô   t ƒ }|r||_t| ƒ}t |d ¡‰ t ˆ ¡}tjtj	tj
tjtjtjtjtjg}t‡ fdd„|D ƒƒs9tdƒ‚t|  ¡ ƒ}t |d ¡‰t‡fdd„|D ƒƒsUtdƒ‚t|ƒ}||_|tjkrh|j |¡ n
||v rr|j |¡ |j |¡ |S )zÂConverts 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   ó   � | ]
}t  |¡ˆ kV  qd S r[   ©r&   Úresult_type)r   Úkey)Úraw_key_typer   r   r½     s
   € ÿ
ÿzfrom_dict.<locals>.<genexpr>zfThe key type in the input dictionary is not the same for all keys and therefore is not valid as a map.c                 3   rÑ   r[   rÒ   )r   Úval)Úraw_value_typer   r   r½   !  s   € 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À   ru   rÊ   rÁ   rÇ   ry   rÈ   r›   rÉ   ÚCopyFrom)rÐ   r–   rÆ   rÉ   rÇ   Úvalid_key_int_typesrÊ   Z	value_seqr   )rÕ   r×   r   rÂ   ø  sH   

øþÿÿÿ
rÂ   Úoptionalc                 C   s€   | 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r2t| jƒS |tjkr<t| jƒS tdƒ‚)zœConverts 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
   rt   r²   r”   Útensor_valuer´   Úsparse_tensor_valuer¶   r¯   Úsequence_valuer¸   r°   Ú	map_valueÚOPTIONALÚto_optionalÚoptional_valueru   )râ   r±   r   r   r   rè   2  s   	










rè   Úoptc                 C   sì   t ƒ }|r||_|rtt j ¡ ƒ}||vrt|› d�ƒ‚|}nt| tƒr(t j}nt| tƒr1t j	}n| du r9t j
}nt j}||_| durt|t jkrR|j t| ƒ¡ |S |t j	kra|j t| ƒ¡ |S |t jkrp|j t| ƒ¡ |S 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Ê   ru   r—   r¾   r¸   r¶   rt   r²   r±   rã   rà   r¨   rå   rÁ   ræ   rÂ   )rê   r–   r_   râ   Úvalid_dtypesr±   r   r   r   Úfrom_optionalK  s<   




÷
ùüÿrí   c                 C   s.   | j }t |¡}tj| j|d� ¡  ¡ | _dS )zmCall to convert endianess of raw data in tensor.

    Args:
        tensor: TensorProto to be converted.
    r^   N)rs   r   rv   r&   r~   rp   r   r€   )rj   r‘   r’   r   r   r   r¦     s   
ÿr¦   r   Úinput_shapeÚseedc              	   C   s’   t j |¡ |t jt jt jt jt jt jt j	t j
fv rBtt  |¡jt  t j	¡jƒ}tt  |¡jt  t j	¡jƒ}t jj||| d� |¡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.
    )rd   z' is not supported by create_random_int.)r&   Úrandomrï   r‰   r�   rA   Úuint64r„   r‚   r)   Úint64ÚminÚiinfoÚmaxÚrandintr(   ru   )rî   r_   rï   ÚendÚstartr   r   r   Úcreate_random_intŒ  s   ørù   r[   )NTF)NFF)ri   )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˜   r.   Úboolr+   rH   Ú	vectorizerK   rM   rW   rX   rY   rh   rŸ   r”   r¨   r¯   rÁ   r°   rÂ   rè   rí   r¦   r_   rù   r   r   r   r   Ú<module>   sÀ   $ þÿþ
ý)ÿüÿþýü
û'ÿüÿþýü
ûÿþý
ü!rAÿÿÿÿ
þ9$:ÿÿÿÿ
þ4ÿÿÿÿþ