o
    wi                     @  s  d dl mZ d dlZd dlZd dlmZ d dlZejr!d dl	m
Z
 dZdZd ZdZejdedd	d
 Zd/ddZejdeddd Zd0ddZejdeddd Zd1dd Zejdedd!d" Zd2d$d%Zejdedd&d' Zd3d)d*Zejdedd+d, Zd4d-d.ZdS )5    )annotationsN)Sequencei      zpDeprecated since 1.18. Scheduled to remove in 1.20. Consider using libraries like ml_dtypes for dtype conversion)categoryc                  O     t | i |S N_float32_to_4bit_unpackedargskwargs r   I/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/subbyte.pyfloat32_to_4bit_unpacked      r   xnp.ndarray | np.dtype | floatsignedboolreturn
np.ndarrayc                 C  s\   |rt jnt j}|rtnt}|rtnt}t| t jst 	| } t 
| ||}t ||S )a  Cast to 4bit via rounding and clipping (without packing).

    Args:
        x: element to be converted
        signed: boolean, whether to convert to signed int4.

    Returns:
        An ndarray with a single int4 element (sign-extended to int8/uint8)
    )npint8uint8INT4_MIN	UINT4_MININT4_MAX	UINT4_MAX
isinstancendarrayasarraycliprintastype)r   r   dtypeclip_low	clip_highclippedr   r   r   r
      s   
r
   c                  O  r   r   )_float32x2_to_4bitx2r   r   r   r   float32x2_to_4bitx23   r   r*   val_lownp.dtypeval_highc                 C  s$   t ||}t | |}|d> |d@ B S )af  Cast two elements to 4bit (via rounding and clipping) and pack
    to a single byte
    Args:
        val_low: element to be packed in the 4 LSB
        val_high: element to be packed in the 4 MSB
        signed: boolean, whether to convert to signed int4.

    Returns:
        An ndarray with a single int8/uint8 element, containing both int4 elements
       r   r	   )r+   r-   r   i8_highi8_lowr   r   r   r)   ;   s   

r)   c                  O  r   r   )_unpack_4bitx2r   r   r   r   unpack_4bitx2M   r   r2   npt.NDArray[np.uint8]dimsint | Sequence[int]c                 C  s   t j| jd gt jd}| t d@ }| t d@ }|t dL }||ddd< ||ddd< |jt |d krA| dd	 }||}|S )
a  Unpack an array of packed uint8 elements (4bitx2) into individual elements
    (still represented as uint8)

    Args:
        x: Input data
        dims: The shape of the output array.

    Returns:
        A array containing unpacked 4-bit elements (as int8/uint8)
       r%   r      r.   r   N   )r   emptysizer   prodravelreshape)r   r4   resx_lowx_highr   r   r   r1   U   s   
r1   c                  O  r   r   )_unpack_single_4bitx2r   r   r   r   unpack_single_4bitx2p   r   rD   tuple[np.ndarray, np.ndarray]c                 C  st   dd }	 t | tjst| } | d@ }| d? }|r||n|}|r&||n|}|r-tjntj}||||fS )Nc                 S  s   t | d? dk| | dB S )N   r   r8   )r   where)r   r   r   r   unpack_signed{   s   z,_unpack_single_4bitx2.<locals>.unpack_signedr   r.   )r   r   r    r!   r   r   r$   )r   r   rH   rA   rB   r%   r   r   r   rC   x   s   
rC   c                  O  r   r   _float32_to_float4e2m1_unpackedr   r   r   r   float32_to_float4e2m1_unpacked   r   rK   valuesc                 C  s   t t | ddt j}t | }t j| jt jd}d||dk|dk @ < d||dk|dk@ < d	||dk|d
k @ < d||d
k|dk@ < d||dk|dk @ < d||dk|dk@ < d||dk< ||O }d|t | < |S )zCast float32 to float4e2m1 (without packing).

    Args:
        values: element or array to be converted

    Returns:
        An ndarray with unpacked float4e2m1 elements (as uint8)
       r   r7   r9   g      ?g      ?r6   g      ?rF   g      ?r.   g      @   g      @   g      @r   )	r   rG   signbitr$   r   abszerosshapeisnan)rL   sign	magnituder@   r   r   r   rJ      s   	
rJ   c                  O  r   r   )_float32x2_to_float4e2m1x2r   r   r   r   float32x2_to_float4e2m1x2   r   rX   c                 C  s    t |}t | }|d> |d@ B S )a	  Cast two elements to float4e2m1 and pack to a single byte
    Args:
        val_low: element to be packed in the 4 LSB
        val_high: element to be packed in the 4 MSB

    Returns:
        An ndarray with uint8 elements, containing both float4e2m1 elements
    r.   r   rI   )r+   r-   r/   r0   r   r   r   rW      s   	rW   )r   r   r   r   r   r   )r+   r,   r-   r,   r   r   r   r   )r   r3   r4   r5   r   r3   )r   r   r   r   r   rE   )rL   r   r   r   )r+   r   r-   r   r   r   )
__future__r   typingnumpyr   numpy.typingnpttyping_extensionsTYPE_CHECKINGcollections.abcr   r   r   r   r   
deprecatedDeprecationWarningr   r
   r*   r)   r2   r1   rD   rC   rK   rJ   rX   rW   r   r   r   r   <module>   s^   










