U
    yh                     @  s   d dl mZ d dlZd dlmZmZ d dlZd dlm  m	Z
 d dlmZ d dlmZmZmZ d dlmZmZmZ ddgZd	d
ddZG dd deZdS )    )annotationsN)ListSet)PerChannelMinMaxObserver)QuantizationAnnotationQuantizationSpec	Quantizer)OperatorConfigOperatorPatternTypeQuantizationConfigget_embedding_operators_configEmbeddingQuantizerr	   returnc                  C  sZ   t tjtjdtjddd} td d | d }tjjgg}|	t
jg t||d}t|S )Nr   g      0?)eps)ZdtypeZqschemeZch_axisZobserver_or_fake_quant_ctr)config	operators)r   torchZuint8Z per_channel_affine_float_qparamsr   Z	with_argsr   nnZ	EmbeddingappendF	embeddingr	   copydeepcopy)Zweight_quantization_specquantization_configopsZsupported_config_and_operators r   e/var/www/html/venv/lib/python3.8/site-packages/torch/ao/quantization/quantizer/embedding_quantizer.pyr      s    
 c                      s   e Zd Z fddZeddddZeddd	d
dZdddddZdddddZdddddZ	eddddZ
  ZS )r   c                   s   t    d S N)super__init__)self	__class__r   r   r    ,   s    zEmbeddingQuantizer.__init__zList[QuantizationConfig]r   c                 C  s   dd |   D }t|S )Nc                 S  s   h | ]\}}|qS r   r   ).0spec_r   r   r   	<setcomp>1   s    zHEmbeddingQuantizer.get_supported_quantization_configs.<locals>.<setcomp>)get_supported_operatorslist)clsZ
op_configsr   r   r   "get_supported_quantization_configs/   s    z5EmbeddingQuantizer.get_supported_quantization_configsr   zList[OperatorPatternType])r   r   c                 C  s&   |   D ]\}}||kr|  S qg S r   )r(   )r*   r   r   r   r   r   r   .get_supported_operator_for_quantization_config6   s    
zAEmbeddingQuantizer.get_supported_operator_for_quantization_configztorch.fx.GraphModule)modelr   c                 C  s   |  |j |S )z!just handling global spec for now)_annotate_embedding_opsgraphr!   r-   r   r   r   annotateD   s    zEmbeddingQuantizer.annotateztorch.fx.GraphNone)r/   r   c                 C  sd   t  }|jD ]R}|jdkr|jtjjjjkr|j	j
d kr@tdt|jd |j	j
id|jd< qd S )NZcall_functionz<Embedding config must have a valid weight quantization spec.r   )Zinput_qspec_mapZquantization_annotation)r   Znodesoptargetr   r   Zatenr   defaultr   weight
ValueErrorr   argsmeta)r!   r/   Zembedding_confignoder   r   r   r.   I   s    
 z*EmbeddingQuantizer._annotate_embedding_opsc                 C  s   d S r   r   r0   r   r   r   validate\   s    zEmbeddingQuantizer.validatezList[OperatorConfig]c                 C  s   t  gS r   )r   )r*   r   r   r   r(   _   s    z*EmbeddingQuantizer.get_supported_operators)__name__
__module____qualname__r    classmethodr+   r,   r1   r.   r;   r(   __classcell__r   r   r"   r   r   +   s   )
__future__r   r   typingr   r   r   Ztorch.nn.functionalr   Z
functionalr   Ztorch.ao.quantization.observerr   Z)torch.ao.quantization.quantizer.quantizerr   r   r   Z7torch.ao.quantization.quantizer.xnnpack_quantizer_utilsr	   r
   r   __all__r   r   r   r   r   r   <module>   s   