o
    ¢ÄiJ  ã                   @   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                    ó   t ƒ  ||¡ d S ©N©ÚsuperÚ__init__©ÚselfZonnx_quantizerZ	onnx_node©Ú	__class__© úg/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/quantization/operators/activation.pyr   	   ó   zQLinearActivation.__init__c                    sj   | j }|jdks|jdksJ ‚|jd | jjvs| jjr!tƒ  ¡ S | jj|jd  }|| jj|jd < d S )NÚReluÚClipr   )	ÚnodeÚop_typeÚinputÚ	quantizerÚquantized_value_mapÚis_activation_symmetricr   ÚquantizeÚoutput)r   r   Zquantized_valuer   r   r   ÚQuantizeClipRelu   s   
z"QLinearActivation.QuantizeClipReluc                    s~  | j }|jdks|jdkr|  ¡  d S d}|jdko%|| jjv o%| jj| }|r*dnd }|r0dnd }| j |jd ||¡\}}}}	}	| j |dg¡\}
}}}|rT|
d u rYtƒ  	¡ S |jd t
 }d}|jrj|jd }i }|jD ]	}| t|ƒ¡ qot|d	< |
d |d |d ||g}tjjd
|j ||g|fi |¤Ž}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_namesÚ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                    r   r   r   r   r   r   r   r   c   r   zQDQRemovableActivation.__init__c                 C   sˆ   | j }| j |jd ¡sd S | jjs+| jjs+| j |jd |jd ¡r+| j | j ¡ n	| j 	|jd ¡ | j
sB| j 	|jd ¡ d S d S )Nr   )r   r   Zis_tensor_quantizedr   r   Zqdq_keep_removable_activationsZ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/   r0   r   r    r1   r   r   r   r   r2   b   s    r2   )r(   Zquant_utilsr   r   r   r   r   Zbase_operatorr	   Zqdq_base_operatorr
   r   r2   r   r   r   r   Ú<module>   s    Z