o
    to™i  ã                   @  sž   d dl mZ d dlZd dlmZ d dlmZmZ d dl	m
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dS )é    )ÚannotationsN)ÚTensorProto)Únp_dtype_to_tensor_dtypeÚtensor_dtype_to_np_dtype)ÚOpRunÚ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   é   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!   ú[/home/ubuntu/.local/lib/python3.10/site-packages/onnx/reference/ops/op_dequantize_linear.pyÚ_reshape_input   sB   
ÿÿÿý€ÿ
ÿr#   c                   @  s    e Zd Z				dddd„ZdS )Ú_CommonDequantizeLinearNr   r   Úxr   Úx_scaleÚx_zero_pointúnp.ndarray | Noner   r   r   Úoutput_dtyper   c                 C  s  t |jƒ}|tjtjtjtjhv }|d ur?|s?|tjkr?t |jƒ}	||	kr0td|› d|	› dƒ‚| 	t
j¡t||j||ƒ }
n)|rb|d urb| 	t
j¡}| ¡ }| ¡ }||ks^|t
 d¡krbtdƒ‚| 	t
j¡}
|
t||j||ƒ }| 	|r|t|ƒ¡fS |j¡fS )NzType mismatch z != z in DequantizeLinear.r   z=x_zero_point is not null but should be zero for float8 types.)r   Údtyper   ÚFLOAT8E4M3FNÚFLOAT8E4M3FNUZÚ
FLOAT8E5M2ÚFLOAT8E5M2FNUZÚ
FLOAT4E2M1r   Úastyper   Úfloat32r#   r	   Úuint8ÚminÚmaxr   )Úselfr%   r&   r'   r   r   r)   Úx_typeÚfp8_typeÚ	zero_typeÚdxÚu_x_zero_pointÚumiÚumaÚyr!   r!   r"   Ú_run@   sJ   
	üÿ

ÿ
ÿÿÿÿÿýÿz_CommonDequantizeLinear._run)Nr   r   N)r%   r   r&   r   r'   r(   r   r   r   r   r)   r   )Ú__name__Ú
__module__Ú__qualname__r>   r!   r!   r!   r"   r$   ?   s    ùr$   c                      s    e Zd Zdd‡ fdd„Z‡  ZS )	ÚDequantizeLinear_19Nr   r   r   c                   s(   t |jƒdkrtdƒ‚tƒ  ||||¡S )Nr   z%Input 2 must be a vector or a number.)r   r	   r   Úsuperr>   )r5   r%   r&   r'   r   ©Ú	__class__r!   r"   r>   s   s   zDequantizeLinear_19._run)Nr   )r   r   ©r?   r@   rA   r>   Ú__classcell__r!   r!   rD   r"   rB   r   s    rB   c                      s&   e Zd Zdddœd	‡ fdd„Z‡  ZS )
ÚDequantizeLinear_21r   r   ©r   r   r   r   r   c                  s   t ƒ j|||dœŽS )NrI   ©rC   r>   )r5   r   r   ÚargsrD   r!   r"   r>   z   s   zDequantizeLinear_21._run©r   r   r   r   rF   r!   r!   rD   r"   rH   y   s    rH   c                      ó(   e Zd Zddddœd
‡ fdd	„Z‡  ZS )ÚDequantizeLinear_23r   r   N©r   r   r)   r   r   r   c                  ó   t ƒ j||||dœŽS ©NrO   rJ   ©r5   r   r   r)   rK   rD   r!   r"   r>   €   ó   ÿzDequantizeLinear_23._runrL   rF   r!   r!   rD   r"   rN      ó     rN   c                      rM   )ÚDequantizeLinear_25r   r   NrO   r   r   r   c                  rP   rQ   rJ   rR   rD   r!   r"   r>   ˆ   rS   zDequantizeLinear_25._runrL   rF   r!   r!   rD   r"   rU   ‡   rT   rU   )N)
r   r   r	   r
   r   r   r   r   r   r   )Ú
__future__r   Únumpyr   Úonnxr   Úonnx.helperr   r   Úonnx.reference.op_runr   r#   r$   rB   rH   rN   rU   r!   r!   r!   r"   Ú<module>   s   ü23