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                       s$   e Zd Zdd„ Z‡ fdd„Z‡  ZS )ÚQLinearWherec                 C   s   dS )NT© )Úselfr   r   úL/tmp/pip-unpacked-wheel-socb9apf/onnxruntime/quantization/operators/where.pyÚshould_quantize	   s    zQLinearWhere.should_quantizec              	      sb  | j }|jdkst‚| jjs2| j j|g7  _d S | j |jd ¡\}}}}}| j |ddg¡\}}}}	|rt|d kr~t	ƒ  
¡ S |jd t }
|jrœ|jd nd}t|jd |
||tjƒ}|| jj|jd < i }|jD ]}| t|ƒ¡ qÔt|d< |jd |d |d |d |d |d |d ||g	}tjjd||
g|f|Ž}| j j|	7  _| j j|g7  _d S )	NÚWherer   r   r   Z_quantÚ Údomainr   )ÚnodeÚop_typeÚAssertionErrorÚ	quantizerÚforce_quantize_no_input_checkZ	new_nodesZ_get_quantization_paramsÚoutputZquantize_activationÚsuperÚquantizer   Únamer   r   ZInputZquantized_value_mapÚ	attributeÚupdater   r   ÚinputÚonnxÚhelperZ	make_node)r   r   Z
data_foundZoutput_scale_nameZoutput_zp_nameÚ_Zq_input_namesZzero_point_namesZscale_namesZnodesZqlinear_outputZqlinear_output_nameZq_outputÚkwargsr   Zqlwhere_inputsZqlwhere_node©Ú	__class__r   r   r      sl    úû
û
÷   ÿÿzQLinearWhere.quantize)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r#   r   r      s   r   c                   @   s   e Zd Zdd„ ZdS )ÚQDQWherec                 C   sÊ   | j }|jdkst‚| jjr„| j |jd ¡s@| j |jd ¡ | j |jd ¡sd| j |jd ¡ | jsÆ|j	D ]}| j |¡ qpnB| j |jd ¡rÆ| j |jd ¡rÆ| jsÆ|j	D ]}| j |¡ q´d S )Nr   r   r   )
r   r   r   r   r   Zis_tensor_quantizedr   Zquantize_activation_tensorZdisable_qdq_for_node_outputr   )r   r   r   r   r   r   r   F   s$    
ÿþý
zQDQWhere.quantizeN)r%   r&   r'   r   r   r   r   r   r)   E   s   r)   )r   Zquant_utilsr   r   r   r   r   Zbase_operatorr	   Zqdq_base_operatorr
   r   r)   r   r   r   r   Ú<module>   s
   =