U
    “±Ëhú  ã                   @   s`   d dl Z ddlmZmZ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 )é    Né   )ÚTENSOR_NAME_QUANT_SUFFIXÚQuantizedValueÚQuantizedValueTypeÚattribute_to_kwargÚ	ms_domainé   )ÚQuantOperatorBase)ÚQDQOperatorBasec                       s4   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚQLinearActivationc                    s   t ƒ  ||¡ d S ©N©ÚsuperÚ__init__©ÚselfZonnx_quantizerZ	onnx_node©Ú	__class__© úQ/tmp/pip-unpacked-wheel-socb9apf/onnxruntime/quantization/operators/activation.pyr   	   s    zQLinearActivation.__init__c                    sj   | j }|jdks|jdkst‚|jd | jjks8| jjrBtƒ  ¡ S | jj|jd  }|| jj|j	d < d S )NÚReluÚClipr   )
ÚnodeÚop_typeÚAssertionErrorÚinputÚ	quantizerÚquantized_value_mapÚis_activation_symmetricr   ÚquantizeÚoutput)r   r   Zquantized_valuer   r   r   ÚQuantizeClipRelu   s    
z"QLinearActivation.QuantizeClipReluc                    sz  | j }|jdks|jdkr&|  ¡  d S d}|jdkoJ|| jjkoJ| jj| }|rTdnd }|r`dnd }| j |jd ||¡\}}}}	}	| j |dg¡\}
}}}|r¨|
d kr²tƒ  	¡ S |jd t
 }d}|jrÔ|jd }i }|jD ]}| t|ƒ¡ qÞt|d	< |
d |d |d ||g}tjjd
|j ||g|f|Ž}t|jd |||tjƒ}|| jj|jd < | |¡ | j j|7  _d S )Nr   r   zextra.Sigmoid.nnapiZSigmoidg      p?r   Ú Z_quantÚdomainZQLinear)r   r   r!   r   Zextra_optionsZ_get_quantization_paramsr    Zquantize_activationr   r   r   ÚnameÚ	attributeÚupdater   r   ÚonnxÚhelperZ	make_noder   r   ZInputr   ÚappendZ	new_nodes)r   r   Znnapi_sigmoid_optionZsigmoid_nnapi_modeZ	use_scaleZuse_zeropointZ
data_foundZoutput_scale_nameZoutput_zp_nameÚ_Zquantized_input_namesZzero_point_namesZscale_namesZnodesZqlinear_activation_outputZqlinear_activation_nameÚkwargsr%   Zqlinear_activation_inputsZqlinear_activation_nodeZq_outputr   r   r   r      sv    

ÿ
ý
úû


ûüû	û
zQLinearActivation.quantize)Ú__name__Ú
__module__Ú__qualname__r   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 )ÚQDQRemovableActivationc                    s   t ƒ  ||¡ d S r   r   r   r   r   r   r   c   s    zQDQRemovableActivation.__init__c                 C   s|   | j }| j |jd ¡sd S | jjsN| j |jd |jd ¡rN| j | j ¡ n| j |jd ¡ | j	sx| j |jd ¡ d S )Nr   )
r   r   Zis_tensor_quantizedr   r   Ztry_replacing_upstream_outputr    Úremove_nodeZquantize_activation_tensorZdisable_qdq_for_node_output)r   r   r   r   r   r   f   s     ÿzQDQRemovableActivation.quantize)r,   r-   r.   r   r   r/   r   r   r   r   r0   b   s   r0   )r'   Zquant_utilsr   r   r   r   r   Zbase_operatorr	   Zqdq_base_operatorr
   r   r0   r   r   r   r   Ú<module>   s
   Z