U
    “±ËhÈ  ã                   @   sP   d dl mZmZmZ ddlmZ ddlmZ G dd„ deƒZG dd„ deƒZ	d	S )
é   )ÚTENSOR_NAME_QUANT_SUFFIXÚQuantizedValueÚQuantizedValueTypeé   )ÚQuantOperatorBase)ÚQDQOperatorBasec                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚDirect8BitOpc                    s   t ƒ  ||¡ d S ©N©ÚsuperÚ__init__©ÚselfZonnx_quantizerZ	onnx_node©Ú	__class__© úP/tmp/pip-unpacked-wheel-socb9apf/onnxruntime/quantization/operators/direct_q8.pyr   	   s    zDirect8BitOp.__init__c                    s`  | j }| jjs¢| j |jd ¡}|d kr>| j j|g7  _d S t|jd |jd t |j	|j
|jƒ}|| jj|jd < |j|jd< |j|jd< | j j|g7  _nº| j |jd ¡sÂtƒ  ¡  d S | j |dg¡\}}}}|d krìtƒ  ¡ S t|jd |jd t |d |d tjƒ}|| jj|jd < |d |jd< |j|jd< | |¡ | j j|7  _d S ©Né    )ÚnodeÚ	quantizerÚforce_quantize_no_input_checkZfind_quantized_valueÚinputZ	new_nodesr   Úoutputr   Z
scale_nameZzp_nameZ
value_typeZquantized_value_mapZq_nameZis_valid_quantize_weightr   ÚquantizeZquantize_activationr   ZInputÚappend)r   r   Zquantized_input_valueZquantized_output_valueZquantized_input_namesZzero_point_namesZscale_namesZnodesr   r   r   r      sP    û
û
û
zDirect8BitOp.quantize©Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r   r   r      s   r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQDQDirect8BitOpc                    s   t ƒ  ||¡ d S r	   r
   r   r   r   r   r   E   s    zQDQDirect8BitOp.__init__c                 C   s~   | j jrB| j  | jjd ¡ | jsz| j  | jjd | jjd ¡ n8| j  | jjd ¡rz| jsz| j  | jjd | jjd ¡ d S r   )r   r   Zquantize_activation_tensorr   r   Zdisable_qdq_for_node_outputr   Zis_tensor_quantized)r   r   r   r   r   H   s     zQDQDirect8BitOp.quantizer   r   r   r   r   r!   D   s   r!   N)
Zquant_utilsr   r   r   Zbase_operatorr   Zqdq_base_operatorr   r   r!   r   r   r   r   Ú<module>   s   <