o
    to™i½  ã                   @  s"  d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 ejejejejejejejejejejejejejhZejdejdejdejd	ejd
ejdejdejdiZ	d%d&dd„ZG dd„ de
ƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZ dS )'é    )ÚannotationsN)ÚTensorProto)Únp_dtype_to_tensor_dtypeÚtensor_dtype_to_np_dtype)ÚOpRun)r   éÿ   )i€ÿÿÿé   )r   iÿÿ  )i €ÿÿiÿ  )r   é   )iøÿÿÿé   )r   é   )éþÿÿÿé   Úvalueú
np.ndarrayÚshapeútuple[int, ...]ÚaxisÚintÚ
block_sizeú
int | NoneÚreturnc                 C  s  t | jƒdkr	| S t | jƒdkr| jdkr| d S |sUt | jƒdks$J ‚dgt |ƒ }z| j||< |  t|ƒ¡W S  tyT } ztd|› d| j› d|› dƒ|‚d}~ww |dkr]tdƒ‚tj| ||d	} || | j| kry| j	t
|| ƒ|d
} | j|kr‚tdƒ‚t || j¡|ksJ ‚| S )a¨  Reshape/Replicate scale/zero-point to be broadcastable to shape.

    Args:
        value: the array to be reshaped/replicated
        shape: the target shape
        axis: quantization axis, applicable for per-axis and blocked quantization
        block_size: size of quantization block, applicable only for blocked quantization

    Returns:
        value array after reshape/replicate according to quantization mode.
    r   r   zaxis is out of boundary, axis=z, value.shape=z, shape=Ú.Nz&block_size must be a positive integer.)Úrepeatsr   )Úindicesr   z’Invalid shapes for Blocked Quantization. Input 2 shape should identical to Input 1 shape, except for one dimension, in which blocking is performed)Úlenr   ÚsizeÚreshapeÚtupleÚ
IndexErrorÚ
ValueErrorÚnpÚrepeatÚtakeÚrangeÚbroadcast_shapes)r   r   r   r   ÚdimsÚe© r'   úY/home/ubuntu/.local/lib/python3.10/site-packages/onnx/reference/ops/op_quantize_linear.pyÚ_reshape_input.   sB   
ÿÿÿý€ÿ
ÿr)   c                   @  s$   e Zd Z						dddd„ZdS )Ú_CommonQuantizeLinearNr   TÚxr   Úy_scaleÚ
zero_pointúnp.ndarray | Noner   r   ÚsaturateÚboolr   r   Úoutput_dtypeúTensorProto.DataType | NoneÚ	precisionr   útuple[np.ndarray]c	                 C  sv  t ||j||ƒ}|}	|d ur%t|jƒ}
|r#||
kr#td|› d|
› ƒ‚|
}	|	p)tj}	|	tvr6td|	› dƒ‚|d urBt ||j||ƒnd}|rUt|ƒ}| 	|¡| 	|¡ }n|| }|	t
v rt |¡ 	tj¡}||7 }t|	ƒ}t
|	 }t ||d |d ¡ 	|¡fS |	tjtjtjtjhv r¢|rštjj|t|	ƒdfS | 	t|	ƒ¡fS |	tjkr³||7 }| 	t|	ƒ¡fS td|	› dƒ‚)Nz+Mismatched output data-types: output_dtype=z, zero_point type=zUnexpected type: output_dtype=z# is not a supported quantized type.r   r   )Údtype)r)   r   r   r5   r   r   ÚUINT8Ú_QUANT_TYPESr   ÚastypeÚ_QUANT_INTEGER_RANGESr    ÚrintÚint32ÚclipÚFLOAT8E4M3FNÚFLOAT8E4M3FNUZÚ
FLOAT8E5M2ÚFLOAT8E5M2FNUZÚonnxÚnumpy_helperÚsaturate_castÚ
FLOAT4E2M1)Úselfr+   r,   r-   r   r/   r   r1   r3   Útensor_typeÚzero_point_typeÚprecision_npÚxir5   Úquant_ranger'   r'   r(   Ú_runa   s\   
ÿ

ÿÿýüÿÿ

ÿz_CommonQuantizeLinear._run)Nr   TNNN)r+   r   r,   r   r-   r.   r   r   r/   r0   r   r   r1   r2   r3   r   r   r4   )Ú__name__Ú
__module__Ú__qualname__rK   r'   r'   r'   r(   r*   `   s    ÷r*   c                      s    e Zd Zdd‡ fdd„Z‡  ZS )	ÚQuantizeLinear_10Nr   r   r   c                   s*   t |jƒdkrtdƒ‚tƒ j||||dS )Nr   ú%Input 2 must be a vector or a number.)r   ©r   r   r   ÚsuperrK   )rE   r+   r,   r-   r   ©Ú	__class__r'   r(   rK   ©   s   zQuantizeLinear_10._run)Nr   )r   r   ©rL   rM   rN   rK   Ú__classcell__r'   r'   rS   r(   rO   ¨   ó    rO   c                      s    e Zd Zd
d‡ fdd	„Z‡  ZS )ÚQuantizeLinear_19Nr   Tr   r   r/   r0   c                   s,   t |jƒdkrtdƒ‚tƒ j|||||dS )Nr   rP   )r   r/   rQ   )rE   r+   r,   r-   r   r/   rS   r'   r(   rK   °   s   zQuantizeLinear_19._run)Nr   T)r   r   r/   r0   rU   r'   r'   rS   r(   rX   ¯   rW   rX   c                      s*   e Zd Zdddddœd‡ fdd„Z‡  ZS )ÚQuantizeLinear_21r   Tr   N©r   r/   r   r1   r   r   r/   r0   r   c                  s   t ƒ j|||||dœŽS )NrZ   ©rR   rK   )rE   r   r/   r   r1   ÚargsrS   r'   r(   rK   ·   s   	ûzQuantizeLinear_21._run©r   r   r/   r0   r   r   rU   r'   r'   rS   r(   rY   ¶   s    úrY   c                      ó,   e Zd Zddddddœd‡ fdd„Z‡  ZS )ÚQuantizeLinear_23r   Tr   N©r   r/   r   r1   r3   r   r   r/   r0   r   c                  ó   t ƒ j||||||dœŽS ©Nr`   r[   ©rE   r   r/   r   r1   r3   r\   rS   r'   r(   rK   Ê   ó   
úzQuantizeLinear_23._runr]   rU   r'   r'   rS   r(   r_   É   ó    ùr_   c                      r^   )ÚQuantizeLinear_25r   Tr   Nr`   r   r   r/   r0   r   c                  ra   rb   r[   rc   rS   r'   r(   rK   ß   rd   zQuantizeLinear_25._runr]   rU   r'   r'   rS   r(   rf   Þ   re   rf   )N)
r   r   r   r   r   r   r   r   r   r   )!Ú
__future__r   Únumpyr    rA   r   Úonnx.helperr   r   Úonnx.reference.op_runr   r6   ÚINT8ÚUINT16ÚINT16ÚINT2ÚUINT2ÚUINT4ÚINT4r=   r>   r?   r@   rD   r7   r9   r)   r*   rO   rX   rY   r_   rf   r'   r'   r'   r(   Ú<module>   sJ   óøü2H